Light mesh with specific light 三个js

Light mesh with specific light three js

我有一个包含几个对象的场景,在这些情况下,我希望球体受到粉红色和蓝色灯光的影响。 但我也有一个管几何结构,它应该只受白光影响,而不受粉红色和蓝色光的影响。

有关问题的演示,请参见下图: 现在发生了什么: 但我希望管子不是粉红色和蓝色而是灰色:

目前我用 meshBasicMaterial 来做这件事,但这会阻止管子看起来是 3D 的。

那么我如何创建这个带有 MeshLambertMaterial 且不受粉色和蓝色灯光影响的灰色管?

请参阅下面的代码,了解我现在如何生成场景:

# Generating of spheres:
for (var i = 0; i < 5; i++) {
        var SphereGeometry = new THREE.SphereGeometry(2, 20, 20);
        var SphereMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
        var Sphere = new THREE.Mesh(SphereGeometry, SphereMaterial);
        Sphere.position.set(i * 7 -14, 0 + Math.random() * 5 - 2.5, 2);
        spheresArray.push(Sphere);
        scene.add(Sphere);
    }

#Generation of tubes:
for (var i = 0; i < CurvesArray.length; i++) {
        var geometry = new THREE.TubeGeometry( CurvesArray[i], 20, 0.5, 8, false );
        var material = new THREE.MeshBasicMaterial( { color: 0xe3e3e3 } );
        var mesh = new THREE.Mesh( geometry, material );
        scene.add( mesh );
    }

#and these are my lights:
var spotLight = new THREE.SpotLight( 0xff00cc );
    spotLight.position.set( -10, 3, 30, 100 );
    lightsArray.push(spotLight);
    scene.add(spotLight);

    var spotLight2 = new THREE.SpotLight( 0xaa99cc );
    spotLight2.position.set( 5, 15, 10, 100 );
    lightsArray.push(spotLight2);
    scene.add(spotLight2);

    var spotLight3 = new THREE.SpotLight( 0x0022bb );
    spotLight3.position.set( 2, -15, 10, 100 );
    lightsArray.push(spotLight3);
    scene.add(spotLight3);

    var light = new THREE.AmbientLight( 0x5f5f5f );
    scene.add( light );

如果需要任何其他信息或有任何不清楚的地方,请告诉我,以便我澄清!

使用图层:https://threejs.org/docs/#api/en/core/Object3D.layers

示例:https://jsfiddle.net/mmalex/1xr356zf/

  1. 灯光只会影响共享同一层的对象。
  2. 相机将看到属于相机所属图层的对象。

分配对象和灯光层:

for (var i = 0; i < 5; i++) {
    var SphereGeometry = new THREE.SphereGeometry(2, 20, 20);
    var SphereMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
    var Sphere = new THREE.Mesh(SphereGeometry, SphereMaterial);
    Sphere.position.set(i * 7 -14, 0 + Math.random() * 5 - 2.5, 2);

    // set layers to Spheres
    Sphere.layers.set(i%3); // <<=== check here!
    spheresArray.push(Sphere);
    scene.add(Sphere);
}

为灯光指定图层:

var spotLight = new THREE.SpotLight( 0xff00cc );
spotLight.position.set( -10, 3, 30, 100 );
spotLight.layers.set(0); // << === layer 0 is by default
lightsArray.push(spotLight);
scene.add(spotLight);

var spotLight2 = new THREE.SpotLight( 0xaa99cc );
spotLight2.position.set( 5, 15, 10, 100 );
spotLight2.layers.set(1);  // << === layer 1 set to spotLight2
lightsArray.push(spotLight2);
scene.add(spotLight2);

var spotLight3 = new THREE.SpotLight( 0x0022bb );
spotLight3.position.set( 2, -15, 10, 100 );
spotLight3.layers.set(2);  // << === layer 2 set to spotLight3
lightsArray.push(spotLight3);
scene.add(spotLight3);

并一层层渲染:

let animate = function() {
  requestAnimationFrame(animate);

  controls.update();

  renderer.autoClear = true;
  camera.layers.set(0); // << == switch camera between layers
  renderer.render(scene, camera);

  renderer.autoClear = false; // don't remove previous layer results

  camera.layers.set(1); // << == switch camera between layers
  renderer.render(scene, camera);

  camera.layers.set(2); // << == switch camera between layers
  renderer.render(scene, camera);
};