BEER/Malt Wiki

Search…

Malt (backend)

BEER (frontend)

Tips

Code Snippets

External Links

Powered By GitBook

2D Perlin Noise

What is Perlin noise?

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.

Suzanne with 2D Perlin noise as a colour.

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 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 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.

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.

Simple Implementation

Following Wikipedia's example implementation, this requires three functions.

1.

RandomGradient. This function returns a random number.

2.

DotGridGradient. This returns the dot product of the distance and gradient vectors.

3.

The Perlin function itself, which calculates the noise at the x and y coordindates.

Here is a simple implementation for use in Malt.

1

#include "Pipelines/NPR_Pipeline.glsl"

2

3

//Perlin noise functions adapted from https://en.wikipedia.org/wiki/Perlin_noise#Implementation by Riya.

4

5

float interpolate (float a, float b, float t)

6

{

7

return a + (t * (b - a));

8

}

9

10

vec2 random_gradient(int ix, int iy)

11

{

12

//change the first number to make it seem to churn/flow

13

float random = 2000 * sin(ix * 21942 + iy * 171324 + 8912) * cos(ix * 23157 * iy * 217832 + 9758);

14

vec2 returner = vec2(cos(random), sin(random));

15

return returner;

16

}

17

18

float dot_grid_gradient(int ix, int iy, float x, float y)

19

{

20

vec2 gradient = random_gradient(ix, iy);

21

22

float x_distance = x - ix;

23

float y_distance = y - iy;

24

return ((x_distance*gradient.x) + (y_distance*gradient.y));

25

}

26

27

float perlin_2d(float x, float y, float scale)

28

{

29

30

x = x*scale;

31

y = y*scale;

32

int XLeft = int(x);

33

int XRight = XLeft+1;

34

35

int YBottom = int(y);

36

int YTop = YBottom+1;

37

38

float SmoothX = x - XLeft;

39

float SmoothY = y - YBottom;

40

41

float n0 = dot_grid_gradient(XLeft, YBottom, x, y);

42

float n1 = dot_grid_gradient(XRight,YBottom,x,y);

43

44

float interpolated_x = interpolate(n0,n1, SmoothX);

45

46

n0 = dot_grid_gradient(XLeft,YTop,x,y);

47

n1 = dot_grid_gradient(XRight,YTop,x,y);

48

49

float interpolated_y = interpolate(n0,n1,SmoothX);

50

51

return interpolate(interpolated_x,interpolated_y,SmoothY);

52

}

Copied!

Actual Use

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.

1

#define CUSTOM_VERTEX_DISPLACEMENT

2

#include "Pipelines/NPR_Pipeline.glsl"

Copied!

Add the following variables, for better control.

1

uniform float noise_scale = 50;

2

uniform float displacement_strength = 1;

Copied!

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.

1

vec3 VERTEX_DISPLACEMENT_SHADER(Surface S)

2

{

3

vec3 displacement = vec3(0,0,0);

4

vec2 screen = screen_uv();

5

float perlin = perlin_2d(screen.x,screen.y,noise_scale);

6

displacement = vec3(perlin,perlin,0)*displacement_strength;

7

return displacement;

8

}

Copied!

Add this for the colour and shading.

1

void COMMON_PIXEL_SHADER(Surface S, inout PixelOutput PO)

2

{

3

vec3 diffuse = vec3(0,1,0) * get_diffuse_half();

4

PO.color.rgb = diffuse;

5

}

Copied!

The result should look like this.

Suzanne with Perlin noise used as displacement on the screen's X and Y.

Conclusion

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.

Last modified 9mo ago

Copy link