定制材料仅适用于方向灯

时间:2015-04-23 19:07:27

标签: three.js

基本组件来自ShaderLib ['lambert'],
但是我没有在片段着色器中找到Lambert的光照计算 此外,在当前着色器中出现错误: three.js:22433 WebGL:INVALID_VALUE:uniform3fv:no array

 this.uniforms= {
                    "directionalLightDirection" : { type: "fv", value: [] },
                    "directionalLightColor" : { type: "fv", value: [] },
                    "opacity":{type:'f', value:1},
                    "map" : { type: "t", value: params.map }
            };

this.vertexShader=[
            varying vec2 vUv;',
            "varying vec3 vLightFront;",

            THREE.ShaderChunk[ 'logdepthbuf_pars_vertex'],


            "#if MAX_DIR_LIGHTS > 0",

            "   uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
            "   uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",

            "#endif",

            'void main()',
            '{',

                'vUv = uv;',
                'vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );',
                'gl_Position = projectionMatrix * mvPosition;',
                THREE.ShaderChunk[ 'logdepthbuf_vertex'],



            "#ifdef USE_SKINNING",
            "   vec3 objectNormal = skinnedNormal.xyz;", 
            "#elif defined( USE_MORPHNORMALS )",
            "   vec3 objectNormal = morphedNormal;",
            "#else",
            "   vec3 objectNormal = normal;", 
            "#endif",

            "#ifdef FLIP_SIDED", 
            "   objectNormal = -objectNormal;", 
            "#endif",

            "vec3 transformedNormal = normalMatrix * objectNormal;",


    "#if MAX_DIR_LIGHTS > 0",

        "transformedNormal = normalize( transformedNormal );",

        "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",

        "   vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
        "   vec3 dirVector = normalize( lDirection.xyz );",

        "   float dotProduct = dot( transformedNormal, dirVector );",
        "   vec3 directionalLightWeighting = vec3( max( dotProduct, 0.0 ) );",

        "   #ifdef DOUBLE_SIDED",

        "       vec3 directionalLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",

        "       #ifdef WRAP_AROUND",

        "           vec3 directionalLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",

        "       #endif",

        "   #endif",

        "   #ifdef WRAP_AROUND",

        "       vec3 directionalLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
        "       directionalLightWeighting = mix( directionalLightWeighting, directionalLightWeightingHalf, wrapRGB );",

        "       #ifdef DOUBLE_SIDED",

        "           directionalLightWeightingBack = mix( directionalLightWeightingBack, directionalLightWeightingHalfBack, wrapRGB );",

        "       #endif",

        "   #endif",

        "   vLightFront += directionalLightColor[ i ] * directionalLightWeighting;",

        "   #ifdef DOUBLE_SIDED",

        "       vLightBack += directionalLightColor[ i ] * directionalLightWeightingBack;",

        "   #endif",

        "}",
        "   #endif",    *
            '}'].join('\n');


this.fragmentShader=[
            THREE.ShaderChunk[ 'logdepthbuf_pars_fragment'],
            'varying vec2 vUv;',
            'uniform float opacity;',
            'uniform sampler2D map;',   



            'void main()',
            '{',

                'gl_FragColor = vec4( 0.0,0.0,0.0, opacity );',
                'gl_FragColor+=texture2D(map,vUv);',
                THREE.ShaderChunk[ 'logdepthbuf_fragment'],

            '}'
].join('\n');

我在顶点着色器中遗漏了什么?什么除了需要指定?

1 个答案:

答案 0 :(得分:0)

lights: true添加到参数材料,如下所示:

new THREE.ShaderMaterial({ fragmentShader: fragmentShader, vertexShader: vertexShader, uniforms: uniforms, side: THREE.DoubleSide, lights: true // <-- })