当灯光连接到相机时,Three.js DirectionalLightHelper

时间:2015-10-05 13:37:22

标签: 3d camera three.js light

当我的定向灯连接到相机时,我正在尝试使用DirectionalLightHelper。我正在将灯光附加到相机上,以便在使用TrackballControls时模拟场景旋转。

var directionalLight = new THREE.DirectionalLight( 0xffffff, 1.5 ); 
directionalLight.position.set( 100, 100, 0 ); 

camera.add( directionalLight );

directionalLightHelper = new THREE.DirectionalLightHelper(directionalLight, 50); 
camera.add( directionalLightHelper); 

然而,这并没有给我预期的结果。如果我使用TrackballControls稍微移动相机,我最终会“找到”由帮助者渲染的光线平面和目标线,但它们不在正确的位置。我希望飞机的中心位于面向光源的0,0,0,并且该线将从100,100,0到0,0,0。我在这里误解了什么,或者在使用辅助灯和相机附带灯的时候还需要做些什么?

3 个答案:

答案 0 :(得分:2)

只需将灯光助手添加到场景,而不是添加到相机。

答案 1 :(得分:0)

确保灯光遵循相机坐标,而不是世界光线。

答案 2 :(得分:0)

将directionalLight连接到相机时,我也遇到了这个问题。而不是将我的directionalLightHelper附加到相机,我不得不将它附加到场景。

scene.add(directionalLightHelper); 

然后我必须将以下内容添加到我的渲染循环中,以便在光线改变位置时正确更新辅助对象。

directionalLight.updateMatrixWorld();
directionalLightHelper.position.setFromMatrixPosition(directionalLight.matrixWorld);
directionalLightHelper.updateMatrix();
directionalLightHelper.update();

编辑:

破碎的助手https://codepen.io/briantjacobs/pen/dRNJpw的代码 工作助手https://codepen.io/briantjacobs/pen/VWProE

的代码