Browse Source

Merge pull request #18755 from gkjohnson/csm-remove-frustumboundingbox

CSM: Replace FrustumBoundingBox with Box3
Mr.doob 5 years ago
parent
commit
a6dbe0aac9
2 changed files with 15 additions and 92 deletions
  1. 15 9
      examples/jsm/csm/CSM.js
  2. 0 83
      examples/jsm/csm/FrustumBoundingBox.js

+ 15 - 9
examples/jsm/csm/CSM.js

@@ -13,17 +13,18 @@ import {
 	BufferGeometry,
 	BufferAttribute,
 	Line,
-	Matrix4
+	Matrix4,
+	Box3
 } from '../../../build/three.module.js';
 import Frustum from './Frustum.js';
-import FrustumBoundingBox from './FrustumBoundingBox.js';
 import Shader from './Shader.js';
 
 const _cameraToLightMatrix = new Matrix4();
 const _lightSpaceFrustum = new Frustum();
 const _frustum = new Frustum();
 const _center = new Vector3();
-const _bbox = new FrustumBoundingBox();
+const _size = new Vector3();
+const _bbox = new Box3();
 const _uniformArray = [];
 const _logArray = [];
 
@@ -163,20 +164,25 @@ export default class CSM {
 		for ( let i = 0; i < this.frustums.length; i ++ ) {
 
 			const light = this.lights[ i ];
+			light.shadow.camera.updateMatrixWorld( true );
 			_cameraToLightMatrix.multiplyMatrices( light.shadow.camera.matrixWorldInverse, cameraMatrix );
 			this.frustums[ i ].toSpace( _cameraToLightMatrix, _lightSpaceFrustum );
 
-			light.shadow.camera.updateMatrixWorld( true );
+			_bbox.makeEmpty();
+			for ( let j = 0; j < 4; j ++ ) {
 
-			_bbox.fromFrustum( _lightSpaceFrustum );
-			_bbox.getSize();
-			_bbox.getCenter( this.lightMargin );
+				_bbox.expandByPoint( _lightSpaceFrustum.vertices.near[ j ] );
+				_bbox.expandByPoint( _lightSpaceFrustum.vertices.far[ j ] );
+
+			}
 
-			const squaredBBWidth = Math.max( _bbox.size.x, _bbox.size.y );
+			_bbox.getSize( _size );
+			_bbox.getCenter( _center );
+			_center.z = _bbox.max.z + this.lightMargin;
 
-			_center.copy( _bbox.center );
 			_center.applyMatrix4( light.shadow.camera.matrixWorld );
 
+			const squaredBBWidth = Math.max( _size.x, _size.y );
 			light.shadow.camera.left = - squaredBBWidth / 2;
 			light.shadow.camera.right = squaredBBWidth / 2;
 			light.shadow.camera.top = squaredBBWidth / 2;

+ 0 - 83
examples/jsm/csm/FrustumBoundingBox.js

@@ -1,83 +0,0 @@
-/**
- * @author vHawk / https://github.com/vHawk/
- */
-
-export default class FrustumBoundingBox {
-
-	constructor() {
-
-		this.min = {
-			x: 0,
-			y: 0,
-			z: 0
-		};
-		this.max = {
-			x: 0,
-			y: 0,
-			z: 0
-		};
-
-	}
-
-	fromFrustum( frustum ) {
-
-		const vertices = [];
-
-		for ( let i = 0; i < 4; i ++ ) {
-
-			vertices.push( frustum.vertices.near[ i ] );
-			vertices.push( frustum.vertices.far[ i ] );
-
-		}
-
-		this.min = {
-			x: vertices[ 0 ].x,
-			y: vertices[ 0 ].y,
-			z: vertices[ 0 ].z
-		};
-		this.max = {
-			x: vertices[ 0 ].x,
-			y: vertices[ 0 ].y,
-			z: vertices[ 0 ].z
-		};
-
-		for ( let i = 1; i < 8; i ++ ) {
-
-			this.min.x = Math.min( this.min.x, vertices[ i ].x );
-			this.min.y = Math.min( this.min.y, vertices[ i ].y );
-			this.min.z = Math.min( this.min.z, vertices[ i ].z );
-			this.max.x = Math.max( this.max.x, vertices[ i ].x );
-			this.max.y = Math.max( this.max.y, vertices[ i ].y );
-			this.max.z = Math.max( this.max.z, vertices[ i ].z );
-
-		}
-
-		return this;
-
-	}
-
-	getSize() {
-
-		this.size = {
-			x: this.max.x - this.min.x,
-			y: this.max.y - this.min.y,
-			z: this.max.z - this.min.z
-		};
-
-		return this.size;
-
-	}
-
-	getCenter( margin ) {
-
-		this.center = {
-			x: ( this.max.x + this.min.x ) / 2,
-			y: ( this.max.y + this.min.y ) / 2,
-			z: this.max.z + margin
-		};
-
-		return this.center;
-
-	}
-
-}