【发布时间】:2020-01-15 05:15:58
【问题描述】:
我正在尝试让定向光随玩家移动。我这样做是为了提高阴影质量,以便阴影相机的尺寸可以更小。当玩家移动时,阴影会稍微移动。看起来它跟玩家的距离不够近。
有没有更好的方法来做到这一点?还是我忘了更新一些东西?任何建议表示赞赏:)
在播放器类中,我有一个对象,我将相机连接到该对象并将该对象用作灯光的目标。然后在 player.update 函数中,我将灯光位置设置为对象位置的偏移量。
class Player{
constructor(){
this.object = new THREE.Object3D();
scene.add( this.object );
this.object.add( camera );
this.directionalLightOffset = new THREE.Vector3(-5000, 2, 5);
this.directionalLight = new THREE.DirectionalLight( 0xffffff, 0.85 );
scene.add( this.directionalLight);
this.directionalLight.position.copy( this.directionalLightOffset );
this.directionalLight.castShadow = true;
this.directionalLight.shadow.mapSize.width = 750;
this.directionalLight.shadow.mapSize.height = 750;
this.directionalLight.shadow.camera.left = -2000;
this.directionalLight.shadow.camera.right = 2000;
this.directionalLight.shadow.camera.top = -2000;
this.directionalLight.shadow.camera.bottom = 2000;
this.directionalLight.shadow.camera.near = 0.5;
this.directionalLight.shadow.camera.far = 90000;
this.directionalLight.target = this.object;
}
update(){
this.directionalLight.position.copy( this.object.position );
this.directionalLight.add( this.directionalLightOffset );
}
}
【问题讨论】:
-
哦..忘了提...您还需要移动 light.target 点!否则光的方向会随着你的移动而改变..你需要给light.position和light.target添加相同的偏移量!
-
是的!但不是 ;p 目标是 player.object (每帧移动)。但我不应该使用您在下面发布的解决方案这样做。
标签: javascript three.js