Elm on fire! Shaders in elm []

Elm on fire

Shaders have long been on the list of possible subject to study for Jappie. The potential of both creating beautiful art as well as doing parallel processing seem incredible valuable capabilities to have. This post comments on the effort of porting a JavaScript WebGL fire to an elm implementation. Elm was chosen as target language because it is opinionated, easy and type safe. In this post we explore how to get started in elm with shaders, and then move on trying to port the fire project, finally performance is increased as much as possible.

In the beginning there was nothing.

There are some example shader setups for elm. The `crate’ was copied over resulting into having a fully 3d crate! This is not exactly the output desired, a crate is not a fire (obviously), but now there is a skeleton for the elm architecture and some example shaders to play with.

A crate in gl

From here on there are two possible paths to continue, one can try and completely understand what the shaders do and how they work, or one can just copy over the shader code from the JavaScript project and see if we can make that work. Although initial work was started on the former approach, the latter approach won out because the topic of ‘shader’ is just too large. There is a lot of math involved. Although this is an exercise of exploration and learning, trying to understand it all is a massive scope creep.

Unbreak rendering

After copying over the shader logic from the fire project, everything broke. This was not surprising as the crate project was 3d, whereas the fire project was 2d. Luckily Elm has strongly typed input for the shaders. Therefore solving these mismatches was relatively easy. We could just follow the compile errors. After that, the example program was essentially gutted, only the basic architecture and API calls were left in tact. Elm forces this architecture upon us, there is no choice in this. The result of this effort is shown below.

Something in gl

It does not look like much of anything, however, this is counted as progress. Not having a blank screen is good. The next thing to do was fixing the colors. This happened by porting the hue code, there was no elm implementation for this particular kind of Hue representation. Because a white background and the hue produces light blue, we added a black background which mixes into an orange. Now we had the right color, however transparency was also broken. Transparency was quite interesting because my initial fix involved changing the shader. However the the right (API) option was eventually found that solved this issue. With all of this in place we get a single circle with the right color!

Single circle!

Baby steps. Graphics take time.

Random spheres

This is not impressive at all. However, in life one may find that arity changes everything. A single dot on it’s own is just a single dot, but if we randomly place it all over the screen we get something nice to look at (live here):

Recent stuff

Tags