Skip to content

Commit

Permalink
Merge pull request mrdoob#18723 from gkjohnson/csm-cache-objects
Browse files Browse the repository at this point in the history
CSM Example: Use cache objects in initCascades
  • Loading branch information
mrdoob authored Feb 25, 2020
2 parents afc703b + dfe8dc9 commit 1d11f34
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 44 deletions.
14 changes: 4 additions & 10 deletions examples/jsm/csm/CSM.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export default class CSM {
this.lightFar = data.lightFar || 2000;
this.lightMargin = data.lightMargin || 200;
this.customSplitsCallback = data.customSplitsCallback;
this.mainFrustum = new Frustum();
this.frustums = [];

this.lights = [];
this.materials = [];
Expand Down Expand Up @@ -74,16 +76,8 @@ export default class CSM {

const camera = this.camera;
camera.updateProjectionMatrix();
this.mainFrustum = new Frustum( {

maxFar: this.maxFar,
projectionMatrix: camera.projectionMatrix

} );

this.mainFrustum.getViewSpaceVertices();

this.frustums = this.mainFrustum.split( this.breaks );
this.mainFrustum.setFromProjectionMatrix( camera.projectionMatrix, this.maxFar );
this.mainFrustum.split( this.breaks, this.frustums );

}

Expand Down
84 changes: 50 additions & 34 deletions examples/jsm/csm/Frustum.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,54 @@ export default class Frustum {

data = data || {};

this.projectionMatrix = data.projectionMatrix;
this.maxFar = data.maxFar || 10000;

this.vertices = {
near: [],
far: []
near: [
new Vector3(),
new Vector3(),
new Vector3(),
new Vector3()
],
far: [
new Vector3(),
new Vector3(),
new Vector3(),
new Vector3()
]
};

if ( data.projectionMatrix !== undefined ) {

this.setFromProjectionMatrix( data.projectionMatrix, data.maxFar || 10000 );

}

}

getViewSpaceVertices() {
setFromProjectionMatrix( projectionMatrix, maxFar ) {

const maxFar = this.maxFar;
const projectionMatrix = this.projectionMatrix;
const isOrthographic = projectionMatrix.elements[ 2 * 4 + 3 ] === 0;

inverseProjectionMatrix.getInverse( this.projectionMatrix );
inverseProjectionMatrix.getInverse( projectionMatrix );

// 3 --- 0 vertices.near/far order
// | |
// 2 --- 1
// clip space spans from [-1, 1]

this.vertices.near.push(
new Vector3( 1, 1, - 1 ),
new Vector3( 1, - 1, - 1 ),
new Vector3( - 1, - 1, - 1 ),
new Vector3( - 1, 1, - 1 )
);
this.vertices.near[ 0 ].set( 1, 1, - 1 );
this.vertices.near[ 1 ].set( 1, - 1, - 1 );
this.vertices.near[ 2 ].set( - 1, - 1, - 1 );
this.vertices.near[ 3 ].set( - 1, 1, - 1 );
this.vertices.near.forEach( function( v ) {

v.applyMatrix4( inverseProjectionMatrix );

} );

this.vertices.far.push(
new Vector3( 1, 1, 1 ),
new Vector3( 1, - 1, 1 ),
new Vector3( - 1, - 1, 1 ),
new Vector3( - 1, 1, 1 )
)
this.vertices.far[ 0 ].set( 1, 1, 1 );
this.vertices.far[ 1 ].set( 1, - 1, 1 );
this.vertices.far[ 2 ].set( - 1, - 1, 1 );
this.vertices.far[ 3 ].set( - 1, 1, 1 );
this.vertices.far.forEach( function( v ) {

v.applyMatrix4( inverseProjectionMatrix );
Expand All @@ -74,48 +81,57 @@ export default class Frustum {

}

split( breaks ) {
split( breaks, target ) {

const result = [];
while ( breaks.length > target.length ) {

target.push( new Frustum() );

}
target.length = breaks.length;

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

const cascade = new Frustum();
const cascade = target[ i ];

if ( i === 0 ) {

cascade.vertices.near = this.vertices.near;
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.push( new Vector3().lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i - 1 ] ) );
cascade.vertices.near[ j ].lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i - 1 ] );

}

}

if ( i === breaks - 1 ) {

cascade.vertices.far = this.vertices.far;
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.push( new Vector3().lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i ] ) );
cascade.vertices.far[ j ].lerpVectors( this.vertices.near[ j ], this.vertices.far[ j ], breaks[ i ] );

}

}

result.push( cascade );

}

return result;

}

toSpace( cameraMatrix ) {
Expand All @@ -127,11 +143,11 @@ export default class Frustum {

point.set( this.vertices.near[ i ].x, this.vertices.near[ i ].y, this.vertices.near[ i ].z );
point.applyMatrix4( cameraMatrix );
result.vertices.near.push( point.clone() );
result.vertices.near[ i ] = point.clone();

point.set( this.vertices.far[ i ].x, this.vertices.far[ i ].y, this.vertices.far[ i ].z );
point.applyMatrix4( cameraMatrix );
result.vertices.far.push( point.clone() );
result.vertices.far[ i ] = point.clone();

}

Expand Down

0 comments on commit 1d11f34

Please sign in to comment.