我正在 webgl 中开发一个简单的 phong 着色器,我想我已经接近了,但还是有问题。Dead give away:如果我有一个广告牌并让它滚动(所以它像轮子一样旋转),那么被点亮的广告牌部分也会随之旋转 :(。这让我感到困惑,因为这似乎是模型的问题矩阵,但是变换使所有位置和旋转正确,只是照明错误。与视图矩阵同上,我可以四处移动并自由查看,一切都位于正确的位置,只是照明错误。
这是我的着色器(减去空间的定义,为了清晰起见,模型矩阵中的照明移动到 GPU 中){如果您更喜欢在 github 中阅读:https ://github.com/nickgeorge/quantum/blob/master/index .html#L41 }
<script id="fragment-shader" type="x-shader/x-fragment">
void main(void) {
vec3 lightWeighting;
if (!uUseLighting) {
lightWeighting = vec3(1.0, 1.0, 1.0);
} else {
vec3 lightDirection = normalize(vLightPosition.xyz - vPosition.xyz);
float directionalLightWeighting = max(0.0, dot(
normalize(vTransformedNormal),
lightDirection));
lightWeighting = uAmbientColor + uPointLightingColor * directionalLightWeighting;
}
vec4 fragmentColor;
if (uUseTexture) {
fragmentColor = texture2D(uSampler,
vec2(vTextureCoord.s, vTextureCoord.t));
} else {
fragmentColor = uColor;
}
gl_FragColor = vec4(fragmentColor.rgb * lightWeighting, fragmentColor.a);
}
</script>
<script id="vertex-shader" type="x-shader/x-vertex">
void main(void) {
vPosition = uModelMatrix * vec4(aVertexPosition, 1.0);
// TODO: Move back to CPU
vLightPosition = uModelMatrix * vec4(uPointLightingLocation, 1.0);
gl_Position = uPerspectiveMatrix * uViewMatrix * vPosition;
vTextureCoord = aTextureCoord;
vTransformedNormal = normalize(uNormalMatrix * aVertexNormal);
}
</script>
非常感谢,如果还有其他有用的补充,请告诉我。