3

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

在实际程序中是这样的

// 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,我可以让它做我想做的事。然而,这似乎不是设置它的正确方法,而且它不是很灵活。

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

4

1 回答 1

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};

如果您要谨慎,请注意覆盖任何现有的“定义”对象

于 2012-12-06T00:58:09.467 回答