You want to use Godot’s
OpenSimplexNoise to generate noise, noise textures, and other effects.
Noise, or more specifically gradient noise is a method for generating more natural-looking “random” patterns. To do this, Godot provides the OpenSimplexNoise class.
OpenSimplexNoise works by generating a 3D “cloud” of points. Each point has a value ranging from
1. Below are two examples of noise generated by
OpenSimplexNoise. In the images, each pixel’s white value is mapped to the noise value at that point.
The parameters used for configuring OpenSimplexNoise:
seed- The seed used to generate the random values. Default value:
period- Lower values result in higher frequency noise (i.e. more changes in value across the same distance). Default value:
octaves- The number of noise layers that contribute to the result. Each successive layer affects the result less (based on the
persistence). Default value:
lacunarity- The difference in period between octaves. Default value:
persistence- The amount that each octave contributes to the noise. Default value:
Here’s the easiest way to experiment with the effects of the different parameters. Add a
Sprite and in its Texture property, choose “New NoiseTexture”. In that
NoiseTexture for Noise choose “New OpenSimplexNoise”.
You’ll see a black-and-white texture that’s generated based on the noise values. Adjust the values and observe the effect on the image.
Using noise values
Once you have your noise configured, there are a few ways you can access the values.
get_noise_3d(x, y, z)
get_noise_4d(x, y, z, w)
In each case, the result is a value in the range
You may have noticed there’s no
get_noise_1d() method. If you need one-dimensional noise, use
get_noise_2d() and keep one of the values constant.
For example, we can use
get_noise_3d() to generate a 2d image while varying the third dimension over time to get an animated effect:
In this image the color is based on the noise value. Since we don’t want negative values for color, we use
var value = get_noise_3d(x, y, z) * 0.5 + 0.5
to modify the results to the range
[0, 1]. Then, this value is applied to color using the following formula:
Color(1.0-value/2.0, 0.5-value/2.0, value/4.0)
Noise can be used for a wide range of applications:
- Procedural terrain generation
- Visual effects (water, fog, fire, etc)