如何在三个js中的自定义着色器上使用高光贴图创建高光?

时间:2013-07-31 14:49:16

标签: three.js webgl shader specular

我正在使用Three.JS在WebGL中创建一个地球,到目前为止,我有一个日夜纹理和大气的地球。现在为了完成它,我想在我的地球上添加一个仅在水面上显示的镜面。现在,我已经有了一个镜面反射贴图,但我似乎无法将它拼凑在一起。

由于地球/夜晚/大气层的原因,我在自定义着色器中执行所有操作,并且我读到我还必须在此等式中包含镜面反射计算,因为Three.JS无法堆叠多个着色器。

这是我的着色器,我只是停留在镜面部分:

<script id="earthFragmentShader" type="x-shader/x-fragment">
    uniform sampler2D dayTexture;
    uniform sampler2D nightTexture;
    uniform sampler2D specularTexture;
    uniform vec3 sunDirection;
    varying vec3 vNormal;
    varying vec2 vUv;
    void main() {
        // Textures for day and night:
        vec3 dayColor       = texture2D( dayTexture, vUv ).xyz;
        vec3 nightColor     = texture2D( nightTexture, vUv ).xyz;

        // compute cosine sun to normal so -1 is away from sun and +1 is toward sun.
        float cosineAngleSunToNormal = dot(normalize(vNormal), sunDirection);

        // sharpen the edge beween the transition
        cosineAngleSunToNormal = clamp( cosineAngleSunToNormal * 3.0, -1.0, 1.0);

        // convert to 0 to 1 for mixing
        float mixAmount = cosineAngleSunToNormal * 0.5 + 0.5;

        // Atmosphere:
        float intensity = 1.09 - dot( vNormal, vec3( 0.0,0.0, 1.0 ) );
        vec3 atmosphere = vec3( 1,1,1) * pow( intensity, 2.0 );

        // Select day or night texture based on mixAmount.
        vec3 color = mix( nightColor, dayColor + atmosphere, mixAmount );

        // Specular:
        vec3 specularAmount = texture2D( specularTexture, vUv ).xyz;
        vec3 specularColor  = vec3(1,1,1) * specularAmount;

        color = mix(color, specularColor, mixAmount);

        // Set the color
        gl_FragColor = vec4(color, 1.0);
    }
</script>

到目前为止,它只是将镜面反射贴图添加为地球上的纹理,但正如我所提到的,我想使用此BW图像来显示某种镜面反射。任何人都可以指出我正确的方向吗?

修改

好吧,我使用以下代码实现了整个映射:

// Specular:
vec3 specularAmount = texture2D( specularTexture, vUv ).xyz;
vec3 specularColor  = vec3(1,1,1);

float c = 0.35;
float p = 3.0;

float mixAmountSpecular = pow(c * dot(normalize(vNormal), sunDirection), p) * specularAmount.z;

// Select day or night texture based on mixAmount.
vec3 color = mix(dayColor, specularColor, mixAmountSpecular);
color = mix( nightColor, color + atmosphere, mixAmount );

现在唯一剩下的就是如何让镜面光斑更小......对此有什么看法?

编辑2:

好的,我终于通过调整一些参数来实现它:

float c = 0.035;
float p = 30.0;
float mixAmountSpecular = pow(c * dot(normalize(vNormal), specularDirection), p) * (specularAmount.z * 0.5);

作为参考,我的sunDirection是-2,0.8,1,我的specularDirection是-15,10,22.5。

1 个答案:

答案 0 :(得分:0)

我的2次编辑总结了最终使用的着色器:

<script id="earthFragmentShader" type="x-shader/x-fragment">
    uniform sampler2D dayTexture;
    uniform sampler2D nightTexture;
    uniform sampler2D specularTexture;
    uniform vec3 sunDirection;
    uniform vec3 specularDirection;
    varying vec3 vNormal;
    varying vec2 vUv;
    void main() {
        // Textures for day and night:
        vec3 dayColor       = texture2D( dayTexture, vUv ).xyz;
        vec3 nightColor     = texture2D( nightTexture, vUv ).xyz;

        // compute cosine sun to normal so -1 is away from sun and +1 is toward sun.
        float cosineAngleSunToNormal = dot(normalize(vNormal), sunDirection);

        // sharpen the edge beween the transition
        cosineAngleSunToNormal = clamp( cosineAngleSunToNormal * 3.0, -1.0, 1.0);

        // convert to 0 to 1 for mixing
        float mixAmount = cosineAngleSunToNormal * 0.5 + 0.5;

        // Atmosphere:
        float intensity = 1.09 - dot( vNormal, vec3( 0.0,0.0, 1.0 ) );
        vec3 atmosphere = vec3( 1,1,1) * pow( intensity, 2.0 );

        // Specular:
        vec3 specularAmount = texture2D( specularTexture, vUv ).xyz;
        vec3 specularColor  = vec3(1,1,1);

        // Play with these parameters to adjust the specular:
        float c = 0.035;    // Size, I guess...
        float p = 30.0;     // Blur
        float mixAmountSpecular = pow(c * dot(normalize(vNormal), specularDirection), p) * (specularAmount.z * 0.5);

        // Select day or night texture based on mixAmount.
        vec3 color = mix(dayColor, specularColor, mixAmountSpecular);
        color = mix( nightColor, color + atmosphere, mixAmount );

        // Set the color
        gl_FragColor = vec4(color, 1.0);
    }
</script>