Noise. Random Noise. Perlin Noise. With Jupyter Notebook.

Noise.

Random Noise. Perlin Noise.

Introduction

This is an article about content generation through algorithms.
It sounds fancy, but the idea is a simple one – to generate textures or materials automatically, from numbers. Without real drawing in “Paint” or in another PC program. With other words, if we have the following binary np.array in python:

we would be able to generate the following image with it in python easily:
Ok, it is quite visible, that somehow we have plotted the 0 as a black square and the 1 as white squire.
Is this useful somehow? Probably, if you feel like building online chess boards and you can find someone to pay you for that. Anyway, the idea is to make it a bit more complicated from there, as between 1 and 0 we have enough values, and we will be able to generate something more interesting, based on the numbers there.

So, if the numbers are a bit random, we will get some random noise, that will look really not following any pattern, like this:

The idea of this article is to provide a tool, to build noise, that looks like it is not-random (but it is random!):

This, non-random looking noise that we will be building is called “Perlin Noise” and is an extremely powerful algorithm that is used often in procedural content generation. It is especially useful for games and other visual media such as movies.[1] Named after Ken Perlin, this technique earned him an “Oscar” for its originality. In game development, Perlin Noise can be used for any sort of wave-like, undulating material or texture. For example, it could be used for procedural terrain (Minecraft-like terrain can be created with Perlin Noise, for example), fire effects, water, and clouds.[1] Generally, Perlin Noise has a more organic appearance because it produces a naturally ordered (“smooth”) sequence of pseudo-random numbers [2].

Research Questions:

A good research question can be defined as How to build Perlin Noise in python?
A subquestion can be How to distinguish random noise from Perlin Noise?

A few lines about random noise (with examples)

Before going into Perlin Noise, it will be useful to get a few examples of non-Perlin Noise. Let’s plot a 6x6 non-random np.array(), just to see how it works:


Well, the picture above looks like strange 6×6 chess, with some squares in white and some in grey or being black completely. As discussed in the introduction, this is due to the fact that all the numbers between 0 and 1 can be plotted in the grey colors schema between black and white.

Let’s continue with random, non-Perlin noises. First we will plot some 8x8 random noises and then we will plot 10000x10000.


Well, it is obvious that the noises are random. But are these Perlin Noises? In general, the answer is simple no, but proving so might require a bit of an effort. The easiest way to prove it, is to open the noise plot in paint and to cut something:
If we can paste it anywhere in the picture without having the picture “distroyed” and without seeing where exactly did we paste it, then it is not a Perlin noise:

Perlin noise methodology

In this part of the article, an explanation for the “Perlin noise” is to be written.
And then the code. At the end the nice random pictures, that are nice, because they look non-random.

  1. Create an empty noise array
  2. Generate random gradient vectors
  3. Iterate over each pixel in the noise array and
    • Calculate the grid cell coordinates for the current pixel
    • Calculate the position within the cell as fractional offsets
    • Calculate the dot products between gradients and offsets
    • Interpolate the dot products using smoothstep function
    • Store the interpolated value in the noise array
  4. Normalize the noise values within the range of 0 to 1
Now, let’s run the code, with the provided functions.

Looks ok-ish, but we may try all the supported_cmap values, built-in matplotlib. In the supported_cmap list, add the commented values to the list, to see all of the possible cases. It might take up to 1 minute to generate all, that is why I have commented most of these.