ThreeJS中的阴影距离问题

问题描述

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

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

                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,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 );

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

这里还有一个指向 imgur链接,我只是放了一些图片来查看阴影 https://imgur.com/a/MGrc5cw

谢谢

解决方法

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

要创建定向阴影,您需要创建一个正交相机并使用 if (isset($_POST['submit'])) //do submit actions if (isset($_POST['delete'])) //do delete actions if (isset($_POST['export'])) //do export actions 指定其尺寸,在这种情况下,您的相机在每个方向上都有 100 个。当您的角色超出这 200 个单位时,它就在阴影摄像机之外,并且不再接收阴影。

您应该为光影添加一个 .left,.right,.top,.bottom 以让您看到其视锥体中的空间,如in the DirectionalLightShadow code sample

CameraHelper

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...