# Derivatives usage in FBM core

So I learned now about derivatives and fbm, but can not figure out the effect of this code example (credits https://www.iquilezles.org/www/articles/morenoise/morenoise.htm, bottom of page):

const mat2 m = mat2(0.8,-0.6,0.6,0.8);

float terrain( in vec2 p )
{
float a = 0.0;
float b = 1.0;
vec2  d = vec2(0.0);
for( int i=0; i<15; i++ )
{
vec3 n=noised(p);
d +=n.yz;
a +=b*n.x/(1.0+dot(d,d));
b *=0.5;
p=m*p*2.0;
}
return a;
}


What effect does (1.0+dot(d,d))? As the slope is more / it flattens the fbm sum?

You can just play with the code to get a feel for how this term affects the output.

Here I've made a Shadertoy using a piece of Inigo Quilez's code with two extra parameters added to the terrain function:

float terrainH( in vec2 x, int o, float e)
{
vec2  p = x*0.003/SC;
float a = 0.0;
float b = 1.0;
vec2  d = vec2(0.0);

for( int i=0; i<o; i++ )
{
vec3 n = noised(p);
d += n.yz;
a += b*n.x/(1.0+e*dot(d,d));
b *= 0.5;
p = m2*p*2.0;
}

return SC*a;
}

• o controls how many octaves of noise we sum.

You can click & drag left and right to vary this parameter, from one octave at the left, to 7 at the right.

• e controls how much we apply the pseudo-erosion factor.

You can click & drag up and down to vary this parameter. At the bottom, when e is zero, we apply no erosion, just vanilla fbm. At the top, when e is 1, it's the same as Inigo's original.

Taking a look at a cross section through the resulting heightmap, here's what it looks like as we play with it:

You can see that this pseudo-erosion term makes the terrain sink down wherever there's a steep slope - as though water and landslides running down the incline carved away some of its material, leaving little mounds in the valleys.

It also changes the character of the curves. The interpolation between grid points gives the base noise a symmetric sigmoid shape, and both the terrain under the curve and the empty sky above keep a similar-looking profile as we add octaves (you could flip the image over to exchange land and sky and the terrain would look roughly the same). But as we erode, the high parts of the terrain fall faster because they lose more to division even with the same denominator, leading to sharper peaks and steeper cliffs, slumping down into more gradual valleys, breaking the symmetry between land & sky.

It's most noticeable in the first octave, since its amplitude dominates the rest, but it applies fractally at all scales.

• Thank you so much – Janis Taranda Nov 11 '19 at 7:10
• I kind of run into a problem with this, with negatives noise values (<0). The /(1.0+e*dot(d,d)) actually in this case make it go UP. I have to things in mind. 1) Mapping -1,1 to 0,1 2) Add +1 to noise, divide by /(1.0+e*dot(d,d)) and then -1. What are your thoughts on this? – Janis Taranda Nov 11 '19 at 14:32
• If you check Inigo Quilez's example, you can see he's using a noise function that returns only positive values. – DMGregory Nov 11 '19 at 14:39