Three.js - 将两种材质放在同一个物体上

时间:2016-01-02 16:59:30

标签: three.js shader material

我想知道如何将两种材料放在同一个物体上...... 我有一个用于颜色的着色器材质,但我也希望有一个基础朗伯阴影。

以下是代码:

var material1 = new THREE.ShaderMaterial( {
    vertexShader: document.getElementById( 'vertexShader' ).textContent,
    fragmentShader: document.getElementById( 'fragmentShader' ).textContent
} );

var material2 = new THREE.MeshLambertMaterial({
    color: 0xffffff,
});

var materials = [material1, material2];

var group = new THREE.Object3D();   
var plane = new THREE.Mesh(geometry, materials);

你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

Here是一个小提琴,展示如何将lambert chuck合并到你自己的着色器中 这是你想要的吗? 它在顶点着色器中计算与THREE.MeshLambertMaterial相同。

var myShader = {
    uniforms : THREE.UniformsUtils.merge([
    THREE.UniformsLib['lights'],
    {
        color : {type:"c", value:new THREE.Color(0x0000ff)}
    }
  ]),
  vertexShader : [
    THREE.ShaderChunk[ "common" ],
    'varying vec3 vLightFront;',
    THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
    'void main() {',
    '  vec3 transformedNormal = normalMatrix * normal;',
    '  vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );',
    THREE.ShaderChunk[ "lights_lambert_vertex" ],
    '  gl_Position = projectionMatrix * mvPosition;',
    '}'
  ].join('\n'),
  fragmentShader : [
    'uniform vec3 color;',
    'varying vec3 vLightFront;',
    'void main() {',
    '  vec3 outgoingLight = color.rgb + vLightFront;',
    '  gl_FragColor = vec4(outgoingLight, 1.0);',
    '}',
  ].join('\n'),
};

    material = new THREE.ShaderMaterial({
      uniforms : myShader.uniforms,
      vertexShader : myShader.vertexShader,
      fragmentShader : myShader.fragmentShader,
      lights: true
    });