ThreeJS中的阴影距离问题



我目前正在为我的场景使用directionnal ligth。一个小角色可以移动。所有阴影都有效。但如果我开始对化身做得更远,阴影就不再对它起作用了。我认为它可能来自光线的角度,但我无法改变它。我可以在阴影/非阴影区域周围画一个正方形。

这是我的密码。巨大的数字在这里让我测试

const dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
dirLight.color.setHSL( 0.1, 1, 0.95 );
dirLight.position.set( - 1, 1.75, 1 );
dirLight.position.multiplyScalar( 30 );
dirLight.distance = 1200;
dirLight.focus = 1200;
dirLight.angle = Math.PI / 1;
scene.add( dirLight );
dirLight.castShadow = true;
dirLight.shadow.mapSize.width = 2048;
dirLight.shadow.mapSize.height = 2048;
const d = 100;
dirLight.shadow.camera.left = - d;
dirLight.shadow.camera.right = d;
dirLight.shadow.camera.top = d;
dirLight.shadow.camera.bottom = - d;
dirLight.shadow.camera.far = 35000;
dirLight.shadow.bias = - 0;
const dirLightHelper = new THREE.DirectionalLightHelper( dirLight, 10 );
scene.add( dirLightHelper );

这是接收阴影的地面代码

const groundGeo = new THREE.PlaneBufferGeometry( 10000, 10000 );
const groundMat = new THREE.MeshLambertMaterial( { color: 0xffffff } );
groundMat.color.setHSL( 0.095, 1, 0.75 );
const ground = new THREE.Mesh( groundGeo, groundMat );
ground.position.y = - 33;
ground.rotation.x = - Math.PI / 2;
ground.receiveShadow = true;
scene.add( ground );

我在flamingo中重用了ThreeJS示例提供的大部分代码https://threejs.org/examples/?q=light#webgl_lights_hemisphere

这里还有一个imgur的链接,我只是放了一些照片来看看阴影https://i.stack.imgur.com/jWHra.jpg

感谢

如果你的阴影在有限的区域内工作,那么你的角色一定是走到了阴影相机的截头体之外。

若要创建方向阴影,请创建正交摄影机,并使用.left, .right, .top, .bottom指定其尺寸,在这种情况下,摄影机在每个方向上都有100个。当你的角色超过这200个单位时,它就在阴影摄影机之外,不再接收阴影。

您应该在光影中添加一个CameraHelper,以便查看其视锥体内的空间,如DirectionalLightShadow代码示例中所示

const helper = new THREE.CameraHelper( dirLight.shadow.camera );
scene.add( helper );

也许你可以让影子摄像机跟踪角色,这样他就会一直在里面。

最新更新