【问题标题】:Calculating Per-Vertex Normals from 3D Noise从 3D 噪声计算每个顶点法线
【发布时间】:2013-05-20 23:20:34
【问题描述】:

我目前在 GLSL 中实现了一个 3D 噪声函数,用于置换球体的顶点以提供地形。我目前正在使用几何着色器来简单地计算每个面的法线(我也有镶嵌,因此我在这里这样做而不是顶点着色器)。现在我想计算每个顶点的法线。

现在我看到了一些关于在置换平面网格时从噪声中计算法线的帖子,但似乎无法让它为我自己工作。 下面是我的镶嵌评估着色器中的一个 sn-p,它计算新顶点的位置。 (这很好用)。

// 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 的值并更改它用于抵消的方式,这会导致不同程度的“错误”。

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

【问题讨论】:

    标签: opengl glsl noise perlin-noise normals


    【解决方案1】:

    您添加用于构造切线的向量(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))。

    【讨论】:

    • 非常感谢!这个方法很好用!现在只是平滑度的一些小问题,但这似乎是由于尚未为我的噪声函数优化 theta 的最佳值。
    猜你喜欢
    • 2011-09-24
    • 2013-09-02
    • 1970-01-01
    • 2013-02-15
    • 2015-05-25
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 2021-06-02
    相关资源
    最近更新 更多