Javascript WebGL定向照明-照明立方体(照明工作不正常)
我已经重写了这段代码几次了,但是同样的灯光问题出现了。。。我将这段代码与几个月前我写的代码进行比较,我写的代码也做了同样的事情(照亮一个立方体),我似乎没有遗漏任何东西 立方体的正面和背面都很好,但左右两边的动作都很奇怪,顶部和底部也是如此。。。看起来法线有问题,但它们没问题。。。检查并重写了几次,只是为了确定 例如: 着色器:Javascript WebGL定向照明-照明立方体(照明工作不正常),javascript,Javascript,我已经重写了这段代码几次了,但是同样的灯光问题出现了。。。我将这段代码与几个月前我写的代码进行比较,我写的代码也做了同样的事情(照亮一个立方体),我似乎没有遗漏任何东西 立方体的正面和背面都很好,但左右两边的动作都很奇怪,顶部和底部也是如此。。。看起来法线有问题,但它们没问题。。。检查并重写了几次,只是为了确定 例如: 着色器: <script id="vShader" type="x-shader/x-vertex"> attribute vec3 aVertexP
<script id="vShader" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;
attribute vec3 aVertexNormal;
uniform mat4 uPMatrix;
uniform mat4 uMVMatrix;
uniform mat3 uNMatrix;
uniform vec3 uAmbientLightColor;
uniform vec3 uDirectionalLightColor;
uniform vec3 uLightDirection;
uniform bool uUseLighting;
varying vec2 vTextureCoord;
varying vec3 vLightWeighting;
void main(void){
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
vTextureCoord = aTextureCoord;
if(!uUseLighting){
vLightWeighting = vec3(1.0, 1.0, 1.0);
}else{
vec3 transformedNormal = aVertexNormal * uNMatrix;
float directionalLightWeighting = max(dot(uLightDirection, transformedNormal), 0.0);
vLightWeighting = uAmbientLightColor + uDirectionalLightColor * directionalLightWeighting;
}
}
</script>
<script id="fShader" type="x-shader/x-fragment">
precision mediump float;
varying vec2 vTextureCoord;
varying vec3 vLightWeighting;
uniform sampler2D uSampler;
void main(void){
vec4 textureColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
gl_FragColor = vec4(textureColor.rgb * vLightWeighting, textureColor.a);
}
</script>
照明代码是:initShaders()中的引用,initBuffers()中的法线和顶点缓冲区,在drawScene()中设置统一变量,还在setMatrixUniforms()中创建normalMatrix
如果有人花时间看一看,告诉我出了什么问题,我会很感激的。好的。。生气了,再次重写代码,然后用一个工作示例中的代码一点一点地替换代码。。。伙计们告诉我!问题在于顶点着色器,请告诉我它们之间的区别
vec3 transformedNormal = uNMatrix * aVertexNormal;
及
。。。第二个会产生错误,但没有意义。。。我乘法的顺序有什么关系。请继续阅读。你可以比较两个例子,比如说“5-3=2”和“5-2=2”。如果你想得到想要的结果,顺序很重要!
vec3 transformedNormal = uNMatrix * aVertexNormal;
vec3 transformedNormal = aVertexNormal * uNMatrix;