gpt4 book ai didi

javascript - Light mesh with specific light 三个js

转载 作者:行者123 更新时间:2023-11-29 18:44:36 25 4
gpt4 key购买 nike

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

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

目前,我使用 meshBasicMaterial 执行此操作,但这会阻止 pipe 看起来像 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);
};

关于javascript - Light mesh with specific light 三个js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54807618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com