ComputeTangents in rev。 72

时间:2015-10-12 13:12:35

标签: javascript three.js glsl webgl

在我最初的问题thread中,我询问了为什么我的灯光没有显示并根据我提供的信息得到答案,但我发现我并没有提出正确的问题。我觉得我现在知道自己的问题。最新版本似乎不像原始脚本使用该函数那样支持切线,因为它似乎会自动删除.computeTangent命令,我认为这并不重要,但我只是注意到了我的着色器在很大程度上依赖于此来将光线映射到纹理,因为它围绕我的对象旋转。

这就是我的着色器脚本的样子,有没有办法更新它,所以它适用于最新版本。

<script id="vertexShader" type="x-shader/x-vertex">
attribute vec4 tangent;
uniform vec2 uvScale;
uniform vec3 lightPosition;

varying vec2 vUv;
varying mat3 tbn;
varying vec3 vLightVector;

void main() {
    vUv = uvScale * uv;

    /** Create tangent-binormal-normal matrix used to transform
        coordinates from object space to tangent space */
    vec3 vNormal = normalize(normalMatrix * normal);
    vec3 vTangent = normalize( normalMatrix * tangent.xyz );
    vec3 vBinormal = normalize(cross( vNormal, vTangent ) * tangent.w);
    tbn = mat3(vTangent, vBinormal, vNormal);

    /** Calculate the vertex-to-light vector */
    vec4 lightVector = viewMatrix * vec4(lightPosition, 1.0);
    vec4 modelViewPosition = modelViewMatrix * vec4(position, 1.0);
    vLightVector = normalize(lightVector.xyz - modelViewPosition.xyz);

    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
</script>

<!-- GLSL fragment shader for the moon -->
<script id="fragmentShader" type="x-shader/x-fragment">
uniform sampler2D textureMap;
uniform sampler2D normalMap;

varying vec2 vUv;
varying mat3 tbn;
varying vec3 vLightVector;

void main() {
    /** Transform texture coordinate of normal map to a range (-1, 1) */
    vec3 normalCoordinate = texture2D(normalMap, vUv).xyz * 2.0 - 1.0;

    /** Transform the normal vector in the RGB channels to tangent space */
    vec3 normal = normalize(tbn * normalCoordinate.rgb);

    /** Lighting intensity is calculated as dot of normal vector and 
        the vertex-to-light vector */
    float intensity = max(0.07, dot(normal, vLightVector));
    vec4 lighting = vec4(intensity, intensity, intensity, 1.0);

    /** Final color is calculated with the lighting applied */
    gl_FragColor = texture2D(textureMap, vUv) * lighting;
}

0 个答案:

没有答案