SimplexNoise 重新缩放 fbm 内的梯度

SimplexNoise rescaling the Gradient inside fbm

我需要如何修改我的 fbm 循环中的梯度向量?

问题是我无法使用此代码正确计算法线... 他们指向的方向稍微偏离了正确的方向,这会导致奇怪的照明。

这里法向量的计算:

    float3 grad;
    float noise = getNoiseValue(TriPos, grad);
    float3 nTriPos = normalize(TriPos);
    grad = grad / (noise);
    float3 h = grad - dot(grad, nTriPos) * nTriPos;
    float3 n = nTriPos - (h);

编辑: 我稍微改变了正常计算。并删除了频率。 现在看起来还可以。有没有办法插入频率?

    float getNoiseValue(float3 TriPos, out float3 gradient)
    {    
        float strength = 0.45f;
        float octaves = 15;
        float Persistence = 0.53333f;
        float BaseRoughness = 0.71f;
        float Roughness = 1.81f;
        float MinNoiseValue = 1.4f;

        float noiseValue = 0.f;
        float frequency = BaseRoughness;
        float amplitude = 1.f;
        TriPos = TriPos / 40000.f;
        for (int i = 0; i < octaves; i++)
        {
            float v = (sdnoise3(TriPos, gradient) + 1.f) * 0.5f;
            gradient += gradient * amplitude;
            noiseValue += v * amplitude;
            frequency *= Roughness;
            amplitude *= Persistence;
        }
        noiseValue = max(0.f, noiseValue - MinNoiseValue);
        return noiseValue;
    }

更新:

所以现在一切正常,但是当我将频率添加到循环中时,我再次得到奇怪的照明...

    for (int i = 0; i < octaves; i++)
    {
        float v = (sdnoise3(normalize(TriPos) * frequency, gradient) + 1.f) * 0.5f;
        gradient = gradient + (gradient * amplitude * frequency);
        noiseValue = noiseValue + (v * amplitude);
        frequency *= Roughness;
        amplitude *= Persistence;
    }

谁能告诉我这里出了什么问题?

我发现了问题。一些逻辑错误...

    float3 grad;
    for (int i = 0; i < octaves; i++)
    {
        float v = (sdnoise3(TriPos * frequency, gradient) + 1.f) * 0.5f;
        grad += (gradient * amplitude);
        noiseValue += (v * amplitude);
        frequency = frequency * Roughness;
        amplitude = amplitude * Persistence;
    }

I used the gradient variable as out parameter of sdnoise3 function so it got overriden every iteration.