Webgl中的点光源问题

时间:2017-05-07 01:31:03

标签: glsl webgl shader lighting

我正试图在太阳纹理的地球纹理球体上进行点光照  球体,但光线没有出现在正确的位置,并与相机反向旋转。奇怪的是,当我将场景上下颠倒时,点光源完美地工作。我不确定我做错了什么,但我认为这可能是我的常规问题。

Right side up

Upside down

顶点着色器:

<!-- 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>

Fragment Shader:

<!-- 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>

任何见解都将受到赞赏。

0 个答案:

没有答案