Browse Source

Merging with @WestLangley.

Mr.doob 12 years ago
parent
commit
c2f2a65c23
1 changed files with 63 additions and 11 deletions
  1. 63 11
      src/core/Object3D.js

+ 63 - 11
src/core/Object3D.js

@@ -7,7 +7,7 @@
 
 THREE.Object3D = function () {
 
-	this.id = THREE.Math.generateUUID();
+	this.id = THREE.Object3DIdCount ++;
 
 	this.name = '';
 
@@ -41,6 +41,8 @@ THREE.Object3D = function () {
 
 	this.frustumCulled = true;
 
+	this.boundingBox = null;
+
 	this.userData = {};
 
 };
@@ -442,10 +444,56 @@ THREE.Object3D.prototype = {
 
 	},
 
-	clone: function ( object, recursive ) {
+	computeBoundingBox: function() {
+
+		// computes the world-axis-aligned bounding box of object (including its children),
+		// accounting for both the object's and childrens' world transforms
+
+		var v1 = new THREE.Vector3();
+
+		return function() {
+
+			if ( this.boundingBox === null ) {
+
+				this.boundingBox = new THREE.Box3();
+
+			}
+
+			this.boundingBox.makeEmpty();
+
+			var scope = this.boundingBox;
+
+			this.updateMatrixWorld( true );
+
+			this.traverse( function ( node ) {
+
+				if ( node.geometry !== undefined && node.geometry.vertices !== undefined ) {
+
+					var vertices = node.geometry.vertices;
+
+					for ( var i = 0, il = vertices.length; i < il; i++ ) {
+
+						v1.copy( vertices[ i ] );
+
+						v1.applyMatrix4( node.matrixWorld );
+
+						scope.expandByPoint( v1 );
+
+					}
+
+				}
+
+			} );
+
+			return this.boundingBox;
+
+		};
+
+	}(),
+
+	clone: function ( object ) {
 
 		if ( object === undefined ) object = new THREE.Object3D();
-		if ( recursive === undefined ) recursive = true;
 
 		object.name = this.name;
 
@@ -476,16 +524,18 @@ THREE.Object3D.prototype = {
 
 		object.frustumCulled = this.frustumCulled;
 
-		object.userData = JSON.parse( JSON.stringify( this.userData ) );
+		if ( this.boundingBox instanceof THREE.Box3 ) {
 
-		if ( recursive ) {
-		
-			for ( var i = 0; i < this.children.length; i ++ ) {
+			object.boundingBox = this.boundingBox.clone();
 
-				var child = this.children[ i ];
-				object.add( child.clone() );
+		}
 
-			}
+		object.userData = JSON.parse( JSON.stringify( this.userData ) );
+
+		for ( var i = 0; i < this.children.length; i ++ ) {
+
+			var child = this.children[ i ];
+			object.add( child.clone() );
 
 		}
 
@@ -495,4 +545,6 @@ THREE.Object3D.prototype = {
 
 };
 
-THREE.Object3D.defaultEulerOrder = 'XYZ';
+THREE.Object3D.defaultEulerOrder = 'XYZ',
+
+THREE.Object3DIdCount = 0;