Space-coherent noise
There are a number of overloaded ofNoise()
functions that depend on two, three, or four parameters: ofNoise( x, y )
, ofNoise( x, y, z
), and ofNoise( x, y, z, t )
. They have behavior like ofNoise( t )
but use several input parameters. Their coordinates may be scaled as 2D or 3D space coordinates of some point and may even include time. So such functions give a way for generating 2D, 3D, and 4D fields with coherently changing values that are static or evolving with time. For example, the ofNoise( x, y )
function can be used for drawing a smooth random texture in the following way:
float scaleX = 0.007; //1.0 / scaleX is coherence in x float scaleY = 0.008; //1.0 / scaleY is coherence in y float posX0 = 593.2; float posY0 = 43.7; for (int y=0; y<500; y++) { for (int x=0; x<500; x++) { float value = ofNoise( x*scaleX+posX0, y*scaleY+posY0 ); ofSetColor( value*255, value*255, value*255 ); ofRect( x, y, 1, 1 ); } }
Note
This is example 13-PerlinNoise/03-PerlinTexture
.
On running the code, you will see the following texture:
Now add the third parameter in the calling function ofNoise()
, which increases with time, and you will obtain a texture that slowly evolves with time.
float value = ofNoise( x*scaleX+posX0, y*scaleY+posY0, time*0.1 + 445.6 );
This code is simple and great for demonstrating the idea. But it works extremely slowly because we render each texture's pixel as a separate rectangle with size 1 × 1. To obtain real-time performance, prepare the texture colors in the ofImage image
object and draw it in one step using image.draw()
(see the Creating images section in Chapter 4, Images and Textures).
Tip
The fastest way of generating textures is by using shaders; see the A liquify distortion example section in Chapter 8, Using Shaders. Though the example is about texture distortion with Perlin noise, it is simple to change it for generating textures. Using shaders is especially useful while generating really big textures in real time.
A similar technique can be used for generating values for evolving height maps of 3D surfaces (see The oscillating plane example section in Chapter 7, Drawing in 3D).