3
\$\begingroup\$

I am trying to implement this formula to generate bump but I am facing some issue. The result doesn't look the same it's much darker.

enter image description here

Here is my result (without same parameters) but it is much darker and I don't get why.

enter image description here

And here is the associated code.

// randx, randy and frequencies are array with some random values for each sin wave.
for (int x = 0; x < _width; ++x)
{
    for (int y = 0; y < _height; ++y)
    {
        float color = 0.0f;
        for (int i = 0; i < _iterations; ++i)
        {
            val += Mathf.Sin(Mathf.Sqrt(Mathf.Pow(x - randx[i], 2.0f) + Mathf.Pow(y - randy[i], 2.0f)) * 1.0f / (2.08f + 5.0f * frequencies[i]));
        }
        color /= (float)_iterations;
    }
}

Any idea why I am getting this result ?
Thanks a lot !

EDIT: Thanks to @DMGregory it works by doing this.

float tmp = Mathf.Sin(Mathf.Sqrt(Mathf.Pow(x - randx[i], 2.0f) + Mathf.Pow(y - randy[i], 2.0f)) * 1.0f / (2.08f + 5.0f * frequencies[i]));
tmp = tmp * 0.5f + 0.5f;
val += tmp;
\$\endgroup\$
1

1 Answer 1

5
\$\begingroup\$

I would guess that they've scaled and shifted their outputs into the range from 0f to 1f (0-255) for the purpose of that greyscale height visualisation.

They probably omitted that adjustment from the formula because it's not critical to the shape they're describing with the normal map.

Try multiplying the result by something in the neighbourhood of 0.5f and adding 0.5f.

(I say "in the neighbourhood" because the probability that this formula returns 1 or -1 is vanishingly low — most values are clustered close to zero, so using a coefficient somewhere above 0.5 but lower than 1 may help make the variability more visible in a greyscale output. Just watch for oversaturation of the range, which can produce ridge artifacts if you generate normals/lighting after clamping to the 0-1 range)

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .