0
我正在嘗試在太陽紋理 球體的地球紋理球體上進行點照明,但光線並未出現在正確的光點上並與相機反向旋轉。奇怪的是,當我將場景顛倒時,點光源完美地工作。我不知道我在做什麼錯,但我認爲這可能是我的法線問題。Webgl中的點照明問題
頂點着色器:
<!-- Earth Vertex Shader -->
<script id="VertexShader1" type="x-shader/x-vertex">
precision mediump float;
attribute vec3 vPos; //vertex position
attribute vec3 normals;
varying vec3 texCoords;
varying vec3 vLightWeighting;
uniform mat4 uVMatrix;
uniform mat4 uNormalMatrix;
uniform mat4 uMVMatrix;//modelviewmatrix
uniform mat4 uPMatrix;//projectionmatrix
void main(void) {
vec4 mvPosition = uMVMatrix * vec4(vPos, 1.0);
vec4 lPos = vec4(-20.0,0.0,-20.0,1.0)*uMVMatrix;
gl_Position = uPMatrix * mvPosition;
vec3 lightDirection = normalize(lPos.xyz - mvPosition.xyz);
vec3 transformedNormal = normalize(vec3(uNormalMatrix * vec4(normals, 0.0)));
float directionalLightWeighting = max(dot(transformedNormal, lightDirection), 0.0);
vLightWeighting = vec3(0.6, 0.6, 0.6) + vec3(1.0, 1.0, 1.0) * directionalLightWeighting;
texCoords = normals;
}
</script>
片段着色器:
<!-- Earth Fragment Shader -->
<script id="FragmentShader1" type="x-shader/x-fragment">
#ifdef GL_OES_standard_derivatives
#extension GL_OES_standard_derivatives : enable
#endif
precision mediump float;
varying vec3 texCoords;
varying vec3 vLightWeighting;
uniform sampler2D uSampler;
void main(void){
vec2 textureCoords = vec2(atan(texCoords.z, texCoords.x)/(2.0 * 3.14159) + 0.5, asin(texCoords.y)/3.14159 + 0.5);
vec4 textureColor = texture2D(uSampler, textureCoords);
gl_FragColor = vec4(textureColor.rgb * vLightWeighting, textureColor.a);
}
</script>
任何瞭解,將不勝感激。
我認爲你應該做'uMVMatrix * vec4(-20.0,0.0,-20.0,1.0);'而不是' vec4(-20.0,0.0,-20.0,1.0)* uMVMatrix;' – SurvivalMachine
這實際上會改變什麼嗎? – user2998333
哇,修正它。非常感謝 – user2998333