2D Perlin Noise
Last updated
Last updated
Perlin noise is a type of procedural noise developed by Ken Perlin. It's commonly used to create organic-looking surfaces, or displacement/bump to make surfaces appear more natural and varied. Common uses include fire, clouds, water and terrain. In NPR, this can be used to introduce imperfections into elements such as shading to reduce the appearance of being CGI.
From Wikipedia's page on Perlin noise:
Perlin noise is most commonly implemented as a two-, three- or four-dimensional function, but can be defined for any number of dimensions. An implementation typically involves three steps: defining a grid of random gradient vectors, computing the dot product between the gradient vectors and their offsets, and interpolation between these values.
Define an n-dimensional grid where each grid intersection has associated with it a fixed random n-dimensional unit-length gradient vector, except in the one dimensional case where the gradients are random scalars between -1 and 1.
For each corner, we take the dot product between its gradient vector and the offset vector to the candidate point. This dot product will be zero if the candidate point is exactly at the grid corner.
Following Wikipedia's example implementation, this requires three functions.
RandomGradient. This function returns a random number.
DotGridGradient. This returns the dot product of the distance and gradient vectors.
The Perlin function itself, which calculates the noise at the x and y coordindates.
Here is a simple implementation for use in Malt.
An example of what this could be used for is to displace the mesh on screen to take away its perfect CGI edge. This code will be referencing from the basic_displacement example.
"#define CUSTOM_VERTEX_DISPLACEMENT" must be before the pipeline.
Add the following variables, for better control.
Displacement strength defaults to 1, but should be at a low value for this. Noise_scale can be anything, but lower values will make more subtle changes. Uniform allows them to be viewed in the Material Settings. Then, add this function. It uses the screen's UV and applies the Perlin to it. By doing it this way, it can emulate imperfection in a 2d drawing that has no real depth, no Z axis to be distorted on.
Add this for the colour and shading.
The result should look like this.
Perlin noise is a useful function. There are many ways to take advantage of it; the use here is only a very basic way, with 2D Perlin. There are many uses, and variations on Perlin noise to look into. They're well worth consideration for their many applications in NPR.
For working out the value of any candidate point, first find the unique grid cell in which the point lies. Then the2n{\displaystyle 2^{n}} corners of that cell, and their associated gradient vectors, are identified. Next, for each corner, an offset vector is calculated, being the displacement vector from the candidate point to that corner.
For a point in a two-dimensional grid, this will require the computation of 4 offset vectors and dot products, while in three dimensions it will require 8 offset vectors and 8 dot products. In general, the algorithm has anO(2n){\displaystyle O(2^{n})} complexity scaling.