Three.js Shadow不会在场景的一部分上投射

时间:2018-06-02 19:17:42

标签: three.js

我正在这个场景中,我在我的计划中投射了一个球体阴影,在第一张图片中都很好。

Picture1

当我将球体放在另一个地方时,阴影并未在计划中完全投射,就像在这张照片中一样。

Picture2

这是代码:

//Create a DirectionalLight and turn on shadows for the light
var light = new THREE.DirectionalLight( 0xffffff, 0.7, 1 );
light.position.set( 1, 1,1);            
light.castShadow = true;            
light.shadowDarkness = 0.8;
scene.add( light );

//Set up shadow properties for the light
light.shadow.mapSize.width = 512;  
light.shadow.mapSize.height = 512;
light.shadow.camera.near = 0.0;   
light.shadow.camera.far = 5;     


var sphereGeometry = new THREE.SphereBufferGeometry(1, 32, 32 );
var sphereMaterial = new THREE.MeshStandardMaterial( { color: 0xff0000 } );
var sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
sphere.translateY(1);
sphere.translateZ(-5);
sphere.translateX(-2);
sphere.castShadow = true; 
sphere.receiveShadow = false;
scene.add( sphere );

我收到了计划中的影子:

var objectLoader = new THREE.ObjectLoader();
objectLoader.load("stone/rush_tex01.json", function ( object ) {    
    object.traverse( function( node ) { if ( node instanceof THREE.Mesh ) { node.receiveShadow = true; } } );
    scene.add( object );
} );

那应该是什么?

1 个答案:

答案 0 :(得分:1)

尝试增加定向光影摄像机的平截头体。代码可能如下所示:

var d = 5;
directionalLight.castShadow = true;
directionalLight.shadow.camera.left = - d;
directionalLight.shadow.camera.right = d;
directionalLight.shadow.camera.top = d;
directionalLight.shadow.camera.bottom = - d;
directionalLight.shadow.camera.near = 1;
directionalLight.shadow.camera.far = 20;

以下示例显示了整个代码。您可能需要调整d的值,以便它适用于您的特定用例。

https://threejs.org/examples/#webaudio_timing