Worley Noise in C

Today I made Worley noise in C! I was watching this video and when it got to Worley noise I was like, “Wait, isn’t that a Voronoi texture?” I looked it up and it basically is. I’ve been using Voronoi textures in Blender for years and I was surprised to see something I thought was complicated turn out to be simple.
Obviously I had to make some for myself, so I extracted the SDL2 code from my 3D renderer and started implementing it. Worley noise is straight forward: position some points randomly on the screen and for each pixel in the screen, color it brighter if it’s close to a point and darker if it’s further away. I got it working, but I wanted it to move. I added a Z component for each point and included it in the distance calculation. That was all it took to make the texture 3D. It’s almost like a ton of textures stacked on top of each other. To select a single texture or slice of the 3D space, I can use the same Z value for all the pixels on the screen. To make the texture move, I change the Z value. Then I wanted to make a looping animation. I could just ping pong the Z slice back and forth between 0 and the maximum Z, but it’s easy to tell where the 3D space ends. My second idea was to duplicate the scene three times along the Z axis and start Z at 1/3 of the maximum Z and end at 2/3. Not super efficient, but it worked. I also added what I’m calling weights to each point, which are multipliers of the distance. The weights gives the points some size variety:

Later I realized that I don’t have to use the same z value for all pixels on the screen. Here’s what it looks like if I increase the Z value for pixels further down the screen, essentially skewing the view slice:

Here’s the project if you’d like to play with it:

That’s all I have for today!

Actually not! While I was cleaning the code for this post, I changed the color percentage code and this happened:

Also available as a GIF, but be warned, flashing colors and it’s 22MB: view

I love graphics programming.

Leave a comment

Your email address will not be published. Required fields are marked *


Your data is processed in accordance with my privacy policy. Comments are manually approved and may take a while to appear.