Phong ShaderMaterial参数/设置定义

时间:2012-12-05 05:40:53

标签: three.js

我目前正在使用THREE.ShaderMaterial自定义基础phong材料,并使用大多数三个js的基本块和一个自定义片段着色器块重建材质。我遇到的问题是在三个.js的许多部分使用#define's并尝试找到设置它们的正确方法。

在实际程序中,它就像这样

// Clone the uniforms
var uniforms = THREE.UniformsUtils.clone(shader['uniforms']);

// Set uniform values
uniforms["map"].value = texture;
uniforms["diffuse"].value = new THREE.Color( 0xff0000 );
uniforms["envMap"].value = envMapt;
uniforms["reflectivity"].value = 0.7;

// Create material using shader
var material = new THREE.ShaderMaterial( {
  vertexShader: shader['vertexShader'],
  fragmentShader: shader['fragmentShader'],
  uniforms: uniforms,
  lights: true,
  //map: true, // These don't  seem to do anything
  //envMap: true // These don't  seem to do anything
} );

使用这样构建的自定义着色器

    fragmentShader: [

        "#define USE_MAP",
        //"#define USE_ENVMAP",
        "uniform vec3 diffuse;",
        "uniform float opacity;",

.......
        "void main() {",

            THREE.ShaderChunk[ "alphatest_fragment" ],
            THREE.ShaderChunk[ "specularmap_fragment" ],
......

            // NDJ - Using custom frag shader
            //THREE.ShaderChunk[ "lights_phong_fragment" ],
            CustomShaderChunk[ "lights_phong_fragment" ],
......

            THREE.ShaderChunk[ "fog_fragment" ],

        "}"

    ].join("\n")

通过在着色器的开头手动添加所需的#defines,我可以让它做我想要的。然而,这似乎不是设置它的正确方法,它不是很灵活。

this类似的东西,但我只需要基本定义。我已经尝试通过api和示例来查找如何设置这些但似乎无法使其工作。

1 个答案:

答案 0 :(得分:2)

完全按照你的描述去做。这是一个示例:

ph = new THREE.MeshPhongMaterial( { ambient: 0x000000,
                    color: 0x0020ff,
                    specular: 0x2040ff,
                    shininess: 30,
                    map: theMap,
                    side: THREE.DoubleSide } );
ph.defines = {waldo_waldo_three: '(dx+3)', wonke: 7};

如果您要谨慎,请小心覆盖任何现有的定义'对象