修改顶点位置后重新计算片段法线

时间:2017-12-26 12:46:49

标签: three.js glsl webgl shader

以下笔是我的原始代码

https://codepen.io/johnhckuo/pen/RxrXxX

我使用柏林噪音改变了地形的高度,现在我想在地形上放置一些定向光。

然而,地形的法线似乎保持不变。 那么如何重新计算每个片段的法线呢?

这是我的顶点着色器代码:

  vNormal = normal;
  uVu = uv;
  modelPosition = position;
  vec2 st = uVu.xy * frequency;
  vec3 pos = vec3(st.x, st.y, 100.0);
  vec3 newPosition = position + normal * noise(pos) * depth;
  gl_Position = projectionMatrix *
                modelViewMatrix *
                vec4(newPosition, 1.0);

这是片段着色器的代码:

  vec2 st = uVu * frequency;
  vec3 pos = vec3(st.x, st.y, 100.0);
  vec3 sunColor = vec3(0.298, 0.662, 0.450);
  vec3 color = = diffuseLight(vNormal, 5.0, sunColor);    
  gl_FragColor = vec4( color, 1.0);

此外,使用CPU生成perlin噪音的效率低于使用GPU吗?

0 个答案:

没有答案
相关问题