2016-01-20 158 views
0

我今天开始使用THREE.js并设法渲染一个带有太空船和一些框的场景。太空船应该在箱子上投下阴影,但阴影仅在我使用OrbitControls时显示。如果我用我自己的相机,一切都很好,但不是影子。我如何设置我的PerspectiveCamera以显示阴影?THREE.js阴影只能使用OrbitControls显示

使用OrbitControls: shadow showing

不使用OrbitControls: enter image description here

我是一个初学者,不能让我的jsfiddle代码工作,但代码是here

var spotLight = new THREE.SpotLight(0xffffff); 
 
spotLight.position.copy(ship.position); 
 
spotLight.position.z += 5; 
 
spotLight.shadowDarkness = 0.7; 
 
spotLight.target = ship; 
 
spotLight.castShadow = true; 
 

 
spotLight.shadowMapWidth = 1024; 
 
spotLight.shadowMapHeight = 1024; 
 
spotLight.shadowCameraNear = 1; 
 
spotLight.shadowCameraFar = 20; 
 
spotLight.intensity = 0.2; 
 
spotLight.shadowCameraFov = 30; 
 
spotLight.shadowCameraNear = true; 
 

 

 
scene.add(spotLight); 
 

 

 
if (useOrbitControls) { 
 

 

 
    controls = new THREE.OrbitControls(camera); 
 
    controls.center.clone(ship.position); 
 
    controls.addEventListener('change', render); 
 
}

+0

根据您的世界的大小,shadowCameraFar参数值可能太低。 – gaitat

回答

0

我终于解决了。由于OBJLoader的异步特性,第一次调用animate()是在3D模型完全加载之前完成的。通过将第一个animate()调用移到OBJLoader回调中,所有事情突然按预期工作。