StrandedKitty/three-csm

Can't understand cascade bounds, shouldn't it be Fit to Cascade?

Closed this issue · 2 comments

Hello!
Thanks for implementing CSM, it's very useful! I may be wrong, but it seems that the cascade boundaries are wrong (or CSMHelper is drawing them incorrectly?), from CSM.ts and CSMFrustum.ts the cascade boundaries are Fit to Cascade, but CSMHelper is drawing them as Fit to Scene.

fit-to-scene-vs-fit-to-cascade

current-bounds

CSMFrustum.ts split function (Fit to Cascade):
`

public split( breaks: number[], target: CSMFrustum[] ) {

	while ( breaks.length > target.length ) {

		target.push( new CSMFrustum() );

	}

	target.length = breaks.length;

	for ( let i = 0; i < breaks.length; i ++ ) {

		const cascade = target[ i ];

		if ( i === 0 ) {

			for ( let j = 0; j < 4; j ++ ) {

				cascade.vertices.near[ j ].copy( this.vertices.near[ j ] );

			}

		} else {

			for ( let j = 0; j < 4; j ++ ) {

				cascade.vertices.near[ j ].lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i - 1 ] );

			}

		}

		if ( i === breaks.length - 1 ) {

			for ( let j = 0; j < 4; j ++ ) {

				cascade.vertices.far[ j ].copy( this.vertices.far[ j ] );

			}

		} else {

			for ( let j = 0; j < 4; j ++ ) {

				cascade.vertices.far[ j ].lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i ] );

			}

		}

	}

}

`

True, ideally you want bounding box for each cascade to be as small as possible. These boundaries are then used to create orthographic cameras (1 per cascade) that are used to render shadows.

But we also want to make sure that shadow pixels don't flicker when we move or rotate the camera. If we naively make cascade bounding boxes as tight as possible then we end up with different box sizes depending on camera rotation. So what we do is we find the size of each box in the worst-case scenario and always use this value. This allows to keep bounding box sizes contant and fix the flickering problem.

Orthographic frustums are updated here: https://github.com/StrandedKitty/three-csm/blob/master/src/CSM.ts#L185

Thank you very much!