// # P.xy store the position for which we want to calculate the normals
// # height() here is a function that return the height at a point in the terrain
// read neightbor heights using an arbitrary small offset
vec3 off = vec3(1.0, 1.0, 0.0);
float hL = height(P.xy - off.xz);
float hR = height(P.xy + off.xz);
float hD = height(P.xy - off.zy);
float hU = height(P.xy + off.zy);
// deduce terrain normal
N.x = hL - hR;
N.y = hD - hU;
N.z = 2.0;
N = normalize(N);
1条答案
按热度按时间eh57zj3b1#
你实际上可以不用叉积,通过使用“有限差分法”(或者至少我认为它是这样叫的)来计算。
实际上,它的速度足够快,我用它来计算顶点着色器中的法线。