我目前正在自定义基础 phong 材质,并使用THREE.ShaderMaterial
三个 js 的大部分基础块和自定义片段着色器块重建材料。我遇到的问题是#define's
three.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 和示例来查找如何设置这些,但似乎无法让它工作。