Browse Source

Added Object3D.computeBoundingBox() and THREE.BoundingBoxHelper

WestLangley 12 years ago
parent
commit
e70cfb488e
3 changed files with 85 additions and 0 deletions
  1. 55 0
      src/core/Object3D.js
  2. 29 0
      src/extras/helpers/BoundingBoxHelper.js
  3. 1 0
      utils/build/includes/extras.json

+ 55 - 0
src/core/Object3D.js

@@ -41,6 +41,8 @@ THREE.Object3D = function () {
 
 	this.frustumCulled = true;
 
+	this.boundingBox = null;
+
 	this.userData = {};
 
 };
@@ -442,6 +444,53 @@ THREE.Object3D.prototype = {
 
 	},
 
+	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();
@@ -475,6 +524,12 @@ THREE.Object3D.prototype = {
 
 		object.frustumCulled = this.frustumCulled;
 
+		if ( this.boundingBox instanceof THREE.Box3 ) {
+
+			object.boundingBox = this.boundingBox.clone();
+
+		}
+
 		object.userData = JSON.parse( JSON.stringify( this.userData ) );
 
 		for ( var i = 0; i < this.children.length; i ++ ) {

+ 29 - 0
src/extras/helpers/BoundingBoxHelper.js

@@ -0,0 +1,29 @@
+/**
+ * @author WestLangley / http://github.com/WestLangley
+ */
+
+// a helper to show the world-axis-aligned bounding box for an object
+
+THREE.BoundingBoxHelper = function ( object, hex ) {
+
+	var color = hex || 0x888888;
+
+	this.object = object;
+
+	this.box = new THREE.Box3();
+
+	THREE.Mesh.call( this, new THREE.CubeGeometry( 1, 1, 1 ), new THREE.MeshBasicMaterial( { color: color, wireframe: true } ) );
+
+};
+
+THREE.BoundingBoxHelper.prototype = Object.create( THREE.Mesh.prototype );
+
+THREE.BoundingBoxHelper.prototype.update = function () {
+
+	this.object.computeBoundingBox();
+
+	this.object.boundingBox.size( this.scale );
+
+	this.object.boundingBox.center( this.position );
+
+};

+ 1 - 0
utils/build/includes/extras.json

@@ -46,6 +46,7 @@
 	"src/extras/helpers/AxisHelper.js",
 	"src/extras/helpers/ArrowHelper.js",
 	"src/extras/helpers/BoxHelper.js",
+	"src/extras/helpers/BoundingBoxHelper.js",
 	"src/extras/helpers/CameraHelper.js",
 	"src/extras/helpers/DirectionalLightHelper.js",
 	"src/extras/helpers/FaceNormalsHelper.js",