更新渲染循环中阴影相机的位置

时间:2018-03-12 16:06:31

标签: three.js camera shadow

我试图将定向光影相机的位置设置为与渲染循环中的主透视相机相同:

sunlight = new THREE.DirectionalLight(0xffffff, 1);
sunlight.position.x = camera.position.x ;
sunlight.position.y = 300;
sunlight.position.z = camera.position.z ;from top
sunlight.castShadow = true;

sunlight.shadow.mapSize.width = 2048 ;
sunlight.shadow.mapSize.height = 2048 ;
sunlight.shadow.bias = -0.0027;

sunlight.shadow.camera.left = -75;
sunlight.shadow.camera.right = 75;
sunlight.shadow.camera.top = 75;
sunlight.shadow.camera.bottom = -75;
sunlight.shadow.camera.position = camera.position;

sunlight.shadow.camera.far = sunlight.position.distanceTo(scene.position) + 20;
sunlight.shadow.camera.near = sunlight.position.distanceTo(scene.position) - (camera.position.y * 2);

sunlight.shadow.camera.updateProjectionMatrix();


function render() {
    renderer.render(scene, camera);

    sunlight.shadow.camera.position = camera.position;
    sunlight.shadow.camera.updateProjectionMatrix();
}

然而,影子相机根本不动。是否可以在运行时更新阴影摄像机的位置?

1 个答案:

答案 0 :(得分:0)

shadow.camera更像是阳光的内部组件。

移动阳光使其跟随相机的方式更像是这样:

sunlight.position.copy( camera.position );
sunlight.target.copy( camera.position );
sunlight.position.y += 20;

这应该使阳光照射在相机上方,指向下方,距离为20个单位。

另外你的shadow.camera.near / far设置看起来有点奇怪。

我只想将它们设置为

light.shadow.camera.near = 0.5;
light.shadow.camera.far = 50.0;

..或类似的东西。