2017-05-07 18 views
0

태양 질감의 구체 구면에 점 질감 점을 찍으려고하는데 빛이 정확한 지점에 나타나지 않고 카메라와 반대로 회전합니다. 흥미로운 것은 내가 거꾸로 된 장면을 회전시킬 때 점 조명이 완벽하게 작동한다는 것입니다. 내가 뭘 잘못하고 있는지 모르겠지만, 내 법선에 문제가있는 것 같아.Webgl의 점 조명 문제

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> 

조각 쉐이더 : 모든 통찰력을 감상 할 수있다

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

을 uMVMatrix * vec4 (-20.0,0.0, -20.0,1.0)'대신' vec4 (-20.0,0.0, -20.0,1.0) * uMVMatrix; ' – SurvivalMachine

+0

실제로 변경 될까요? – user2998333

+0

와우는 그것을 고쳤습니다. 정말 고맙습니다 – user2998333

답변

0

다른 위치에서 행렬을 곱하면됩니다 : matrix * vector 빛 위치는 다르게 곱 해집니다 : vector * matrix.

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

와 : 같은 규칙을 사용하려면,이 라인 교체 당신이 '어떻게해야한다고 생각

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