从3D噪声计算每顶点法线

时间:2013-05-25 14:30:32

标签: opengl glsl noise perlin-noise normals

我目前在GLSL中实现了3D噪声功能,用于移动球体的顶点以提供地形。我目前正在使用几何着色器来简单地计算每面法线(我也有曲面细分,因此我在这里做这个而不是顶点着色器)。现在我想计算每顶点法线。

现在我已经看过一些关于在移动平面网格时计算噪声法线的帖子,但似乎无法让它为我自己工作。 下面是我的曲面细分评估着色器的片段,它计算新顶点的位置。 (这很好)。

// Get the position of the newly created vert and push onto sphere's surface.
tePosition = normalize(point0 + point1 + point2) * Radius;

// Get the noise val at given location. (Using fractional brownian motion)
float noiseVal = fBM(tePosition, Octaves); 

// Push out vertex by noise amount, adjust for amplitude desired.
tePosition = tePosition + normalize(tePosition) * noiseVal * Amplitude; 

tePosition然后进入几何着色器,其中三个用于计算三角形的曲面法线。 我将如何使用它来计算所述顶点的法线?

我曾试图做"邻居"通过重新采样来自tePosition的两个小偏移处的噪声的方法(我将它们推回到球体上,然后用噪声值替换它们)。然后使用这两个新位置,我从tePosition获取向量到每个位置,并使用交叉积来获得正常。然而,这导致许多区域为黑色(表明法线向后)并且法线面向外的部分在球体周围看起来非常均匀(在光的相对侧照射)。 这是执行上述操作的代码:

// theta used for small offset
float theta = 0.000001; 
// Calculate two new position on the sphere.
vec3 tangent = tePosition + vec3(theta, 0.0, 0.0);
tangent = normalize(tangent) * Radius;
vec3 bitangent = tePosition + vec3(0.0, theta, 0.0);
bitangent = normalize(bitangent) * Radius; 

// Displace new positions by noise, then calculate vector from tePosition
float tanNoise = fBM(tangent, Octaves) * Amplitude;
tangent += normalize(tangent) * tanNoise;
tangent = tangent - tePosition;

float bitanNoise = fBM(bitangent, Octaves) * Amplitude;
bitangent += normalize(bitangent) * bitanNoise;
bitangent = bitangent - tePosition;

vec3 norm = normalize(cross(normalize(tangent), normalize(bitangent)));

我尝试改变theta值并更改其用于偏移的方式,这会导致不同程度的错误"。

有没有人对如何正确计算法线有任何想法?

1 个答案:

答案 0 :(得分:2)

您为构造切线(theta, 0.0, 0.0)(0.0, theta, 0.0)而添加的矢量与球体不相切。要获得切线和切线,您应该使用叉积:

// pos x (1,0,0) could be 0, so add pos x (0,1,0).
vec3 vecTangent = normalize(cross(tePosition, vec3(1.0, 0.0, 0.0))
  + cross(tePosition, vec3(0.0, 1.0, 0.0)));
// vecTangent is orthonormal to tePosition, compute bitangent
// (rotate tangent 90° around tePosition)
vec3 vecBitangent = normalize(cross(vecTangent, tePosition));

vec3 ptTangentSample = noisy(tePosition + theta * normalize(vecTangent));
vec3 ptBitangentSample = noisy(tePosition + theta * normalize(vecBitangent));

vec3 vecNorm = normalize(
  cross(ptTangentSample - tePosition, ptBitangentSample - tePosition));

作为旁注,我建议不要对向量使用相同的变量(方向+长度,数学(x,y,z,0))和点(坐标系中的位置,数学上(x,y,z) ,1))。

相关问题