Difficulty in understanding noise turbulence code from processing book creating organic textures

float power=6; //TURBULENCE POWER
  float d=8; //TURBULENCE DENSITY
  noStroke();
  for (int y=0; y<height; y++) {
    for (int x=0; x<width; x++) {
      float total=0.0;
      for (float i=d; i>=1; i=i/2.0) {
        total+=noise(x/d, y/d)*d;
        //println("total"+total);
      }
      float turbulence=128.0*total/d;
      ////println("turbulence"+turbulence);
      float base=(x*0.2)+(y*0.12);
      //println("x"+x);
      //println("y"+y);
      //println("base"+base);
      float offset=base+(power*turbulence/256.0);
      //println("offset"+offset);
      float gray=abs(sin(offset))*256.0;
      stroke(gray);
      point(x, y);
    }
  }

This code is from processing book from section Math4:Random Pg. 132. I was playing around with this code but certain variables I find hard to understand.
Like why total is accumulated till the amount of d. What is the role of turbulence & base? I can’t seem to get my head around this. Skipping some shows changes in textures but I can’t understand intuitively how it works as to why some variables were created in the first place.
Elaborate explanation would help. Thanks:)

1 Like

I think it’s a typo. d doesn’t change in that for loop, so it’s basically calculating the same noise value 4 times. I think the intention was to calculate noise at different scales. The variable i contains 8.0, 4.0, 2.0, 1.0. So I think it should say total += noise(x/i, y/i)*i.

The Processing noise() function implements internally different levels of detail (see the noiseDetail() function). But the aesthetic is different.

An unrelated interesting thing is that this code uses point(), which I think is slower than set(), which is slower than accessing the pixels[] array to set pixel colors directly. But point() is more intuitive.

In case you are curious you can see how noise() is implemented here:


That code can be a bit tricky to understand because it uses both << and *= for multiplication and several constants that need to be looked up elsewhere.

1 Like

@hamoid Thanks! I somewhat understood the total noise accumulation as its similar to the perlin noise octave interpolation concept.

Changing the scale by which it accumulates (i=i/2) causes changes in the overall smoothness of the stripes.
For i=i/1.5, the accumulation of noise is more causing deviation in the overall shape. Likewise if i=i/3, the accumulation of noise is less causing smooth change in the stripes.
As for d , it imparts noise by giving tiny variations to the overall shape of stripes causing granular type look.
For high value d, noise is less causing lesser tiny variations. For low value d, noise is more causing more tiny variations.

1 Like