three.js 光线从相机直射到物体

three.js light from camera straight to object

在我的三个 js 设置中,我对定向光进行了以下设置:

private aLight: THREE.DirectionalLight;

this.aLight = new THREE.DirectionalLight(0xffffff, 1.0);
this.aLight.position.set(-5, 5, 5);

this.aScene.add(this.aLight);

为了让光线跟随我的相机并始终照亮我的网格,我在我的渲染函数中设置了以下内容: 私有 onRender() {

this.aLight.position.copy(this.aCamera.getWorldPosition());

window.requestAnimationFrame(_ => this.onRender());
this.aRenderer.render(this.aScene, this.aCamera);

现在,对象总是被照亮:

但是如果我放大面向相机的表面是黑暗的:

我想让我的光线始终从我的相机指向物体。我做错了什么?

如果您希望光源与相机重合,最简单的解决方案是使用点光源并将其添加为相机的子光源。您可以使用这样的模式:

camera.position.set( 10, 10, 10 );
scene.add( camera ); // required in this case since the camera will have a child

// ambient
scene.add( new THREE.AmbientLight( 0xffffff, 0.1 ) ); // optional

// light
var light = new THREE.PointLight( 0xffffff, 1 );
camera.add( light );

如果您使用定向光,请记住定向光有一个 target 属性,一个 Object3D。因此,如果放大,您可能需要更改 target.position。如上所述,使用点光源更容易。

three.js r.86