带有特定灯光的js轻型网格物体js

时间:2019-02-21 12:55:27

标签: javascript three.js 3d light

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

请参见下图以演示问题: 现在会发生什么: enter image description here 但我希望管子不是粉色和蓝色而是灰色: enter image description here

当前,我使用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 );

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

1 个答案:

答案 0 :(得分:3)

使用图层: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);
};