2017-05-07 6 views
0

J'essaie de faire un éclairage ponctuel sur une sphère texturée en terre à partir d'une sphère texturée par le soleil, mais la lumière n'apparaît pas au bon endroit et tourne inversement avec l'appareil photo. La chose curieuse est que lorsque je tourne la scène à l'envers, l'éclairage des points fonctionne parfaitement. Je ne suis pas sûr de ce que je fais mal, mais je pense que cela pourrait être un problème avec mes normales.Problème d'éclairage ponctuel dans Webgl

Right side up

Upside down

Vertex Shader:

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

Toute idée serait appréciée.

+0

Je pense que vous devriez faire 'uMVMatrix * vec4 (-20.0,0.0, -20.0,1.0),' au lieu de ' vec4 (-20.0,0.0, -20.0,1.0) * uMVMatrix; ' – SurvivalMachine

+0

Cela changera-t-il quelque chose? – user2998333

+0

Wow qui l'a réparé. Merci beaucoup – user2998333

Répondre

0

Comme je l'ai suggéré dans les commentaires, vous multipliez les matrices comme ceci dans d'autres endroits: matrix * vector mais votre position de lumière est multipliée différemment: vector * matrix. Pour utiliser la même convention, remplacer cette ligne:

vec4(-20.0,0.0,-20.0,1.0) * uMVMatrix; 

avec:

uMVMatrix * vec4(-20.0,0.0,-20.0,1.0);