Ethan Hermsey
Ethan Hermsey

Reputation: 940

Wandering shadows when DirectionalLight is attached to player object

I'm trying to get a directionalLight to move with the player. I do this to improve the shadow quality, so that the shadow camera's dimensions can be smaller. When the player moves the shadows wander a bit. It looks like it is not following the player close enough.

I have the following problem illustrated in this VERY POOR quality gif; enter image description here

Is there a better way of doing this? Or did I forget to update something? Any advice is appreciated :)

In the player class I have an object where I attach the camera to and use that object as the target for the light. Then in the player.update function I set the light position with an offset to the object's position.

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

Upvotes: 0

Views: 97

Answers (1)

manthrax
manthrax

Reputation: 5036

Try only moving the camera in increments of your shadowmap pixel size or similar. Even just delaying how often you move the camera can minimize the shimmer.. so put a radius around the player and only reset the camera position when the player has wandered out of that radius.

Upvotes: 2

Related Questions