提交 e70cfb48 编写于 作者: W WestLangley

Added Object3D.computeBoundingBox() and THREE.BoundingBoxHelper

上级 c22da229
......@@ -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 ++ ) {
......
/**
* @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 );
};
......@@ -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",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册