This is another post in the ongoing series in which I try to learn 2D vector graphics using Julia. It doesn’t contain any revelations or new material, and you should visit the following sites if you’re looking for a good introduction to the subject of noise in graphics:
I’m using Julia version 1.0 if you want to play along; you can find the source files and notebooks on the github. If you do, you’ll need the packages Luxor, Colors, and ColorSchemes. I used Literate.jl to produce the Markdown and Jupyter notebook versions.
Random versus Noise
Luxor provides a function called noise(). This can accept a single floating-point number as input, and it returns a value between 0.0 and 1.0.
It will be easier to draw some graphs. Here’s a quick throwaway function to draw a simple graph.
To test this out, graph 200 random integers:
To start with, let’s graph the output of the noise() function for the first 200 integers:
It looks very random. But let’s look at 200 values between 0 and 10:
There’s some randomness, but it’s smoother, and looks more natural.
Zoom and enhance, between 0 and 5:
You can see that the left half of the 0 to 10 graph has been stretched.
Between 0 and 1:
One more for luck:
The more often you sample the noise space (ie the shorter the gaps between the set of values passed to noise(), the closer together the output values will be.
So the noise() function provides gently changing undulations rather than the unpredictable jumps of randomness. Here’s a slowly changing color pattern in the LCHab color space, using a set of noisy values to choose the hue. We’ll change the length of each line as well just for fun:
You can use noisy values to specify other changing parameters. For example, let’s place some pebbles at random, and control their size using a noisy distribution, to give the illusion of a naturally changing distribution.
Detail and persistence
The noise() function has two optional keyword arguments that let you tweak the knobs of the noise generator.
The first is detail, an integer. Increasing it from the default value of 1 upwards will add finer detail to the basic noise. The second is persistence, a floating-point value between 0 and 1 (or more).
‘detail` is graphed here with values from 1 to 12. As the level increases, you can see that the same overall noise contours are gradually modulated with finer variations.
You can see the original noisy curve (in red) behind each more detailed graph. The noise generator is doubling the frequency but halving the amplitude every time you go one level higher. Noise, like music, can have octaves of higher frequencies mixed with lower fundamental frequency. The detail keyword is adding one or more octaves of noise.
The persistence argument defaults to zero. The value controls the amplitude of each successive octave of noise, with higher values of persistence producing higher levels of finer detail, as the values persist for longer.
Here, the detail is kept at 4, and the persistence varies from 0 upwards. As the persistence increases, the effects accumulate, until the original curve is barely visible.
There are many uses for noisy input, such as generating varying shapes that don’t have that undesirable ‘too random’ quality.
Here’s a more questionable idea, using noise to control the setting of a line of text.
I then used the readpng() and placeimage() functions to add a background image (the original Tenniel illustration), with the following result:
Why noise?
The first use of computer graphics in movies is generally considered to be Tron (1981).
Tron lies at the very beginning of the history of CGI in the movies, and the technology available to the artists, mathematicians, and programmers making Tron was amazingly underpowered compared with the computing power that we have today on our wrists, let alone on our phones.
Ken Perlin was a mathematician and programmer who worked on Tron, and (I think after the film was released) he realised that there was room for using mathematical techniques for realistic-looking surfaces and textures, such as terrain.
Ken’s noise, or Perlin Noise as it became known, was quickly adodpted as the best way to generate naturalistic surfaces.
I think the reason why natural scenes appear to us as variable but not completely random is due to the (possibly hidden) larger scale processes that make smaller and more visible details clump together, and appear to work together and change gradually. For example, clouds, mountains, and pebble beaches have large scale structure controlled by unseen forces like heat, pressure, and gravity. We mostly see the objects that are subject to these forces, rather than the forces themselves.
Moving into 2D
So far the noise we’ve been producing has been one-dimensional, although we’ve been using 2D graphics to draw it.
The noise() function can accept two floating-point numbers as input. These effectively define a rectangular grid of varying noise values: the x and y inputs produce a third value which requires representation.
A simple way of doing this is to draw a table and vary the color of each square, giving a type of heat map.
Alternatively, we can create a 3D surface and use the noise values for the height at each point. Normally this would require a visit to Julia’s colorful and generally awesome Swedish-nightclub-themed Package manager, Pkg:
to download some of the cool plotting packages available, not least Simon Danisch’s impressive Makie.jl.
But, just to be contrary, I decided to whip up a simple isometric projection:
(This image reminds me of the famous Joy Division LP cover and T-shirt image, which features plots of the first ever pulsar discovered by Jocelyn Bell Burnell and Antony Hewish in 1967. This then became the basis of many entertaining blog posts, such as this one.)
A more conventional surface rendering is also possible:
What, more dimensions?
So far we’ve been generating 2D noise. The noise() function can also accept three floating-point numbers as input. This produces noise values in 3D space, where each 3D point can have a noise value between 0 and 1. Rendering these point clouds is definitely a job for something other than a simple 2D graphics system. But, while we’re here, let’s have a go:
The noise values nearer 1 look like hot plasma, whereas values nearer 0 are almost translucent. It suggests what you might expect to see from a real volume visualization tool.
Journey to Algorithmia
The final images in this post combine 2D noise and 1D noise; 2D noise for the sky, and 1D noise to create the contours.
There’s a seednoise() function. This takes an array of 512 integers between 1 and 12, and is broadly the equivalent of the Random.seed!() function in Julia. This is useful when you want the noise to vary from image to image.
I generated a few hundred of these (there are over 300 colorschemes that can be selected at random) and, scrolling through them quickly, I found that sometimes the results were good, sometimes they weren’t. Randomness—and noise—can be hard to predict.