尝试在WebGL中实现点光源时,我注意到当我放大时我的光线角度会发生变化。
我的顶点着色器:
uniform mat3 u_rotationMatrix;
uniform mat4 u_transformMatrix;
attribute vec3 a_position;
attribute vec3 a_normal;
varying vec3 v_normal;
varying vec3 v_viewCoords;
void main() {
vec4 tcoords = u_transformMatrix * vec4(a_position, 1.0);
v_viewCoords = tcoords.xyz;
v_normal = u_rotationMatrix * a_normal;
gl_Position = tcoords;
}
这对我来说是一种不必要的影响。我怎么能避免这种情况?我做错了什么?
答案 0 :(得分:2)
你没有计算a directional light。你正在计算一个点光源。定向光根据定义没有位置,因此light[i].position
对定向光没有意义。
好的,所以你正在计算点光源。 A point light wants the surface normal dotted with the direction from the surface to the light。但这似乎并不是你在计算的东西。要使曲面朝向光方向,您需要从模型/世界矩阵中分离出投影和视图矩阵。看着你的着色器
vec4 tcoords = u_transformMatrix * vec4(a_position, 1.0);
v_viewCoords = tcoords.xyz;
v_normal = u_rotationMatrix * a_normal;
gl_Position = tcoords;
看来u_transformMatrix
包含您的视图和投影矩阵。我希望着色器看起来更像这样
vec4 worldPosition = u_worldMatrix * vec4(a_position, 1.0);
v_viewCoords = worldPosition.xyz;
v_normal = u_rotationMatrix * a_normal;
gl_Position = u_viewProjection * worldPosition;
u_worldMatrix
除了视图和投影矩阵之外的所有内容的结果,u_viewProjection
是projection * view
矩阵
放大时更改的原因是视图矩阵包含缩放。因此,表面位置计算会根据放置视图/摄像机的位置而变化。通过从计算中删除该问题应该消失。