我需要如何修改fbm循环中的梯度向量?
问题是,我无法用这个代码正确计算Normals。。。它们的指向有点偏离了正确的方向,这导致了奇怪的照明。
这里的法向量计算:
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);
编辑:我稍微更改了一下Normal计算。并删除了频率。现在看起来还可以。有没有办法插入频率?
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.