Browse Source

Merge pull request #10296 from Mugen87/dev

BufferGeometry: Better Compatibility to InterleavedBufferAttribute
Mr.doob 8 years ago
parent
commit
bc3ebe9ed1
3 changed files with 67 additions and 19 deletions
  1. 26 10
      docs/api/math/Box3.html
  2. 10 9
      src/core/BufferGeometry.js
  3. 31 0
      src/math/Box3.js

+ 26 - 10
docs/api/math/Box3.html

@@ -93,6 +93,22 @@
 		Determines whether or not this box intersects *plane*.
 		</div>
 
+		<h3>[method:Box3 setFromArray]( [page:Array array] ) [page:Box3 this]</h3>
+		<div>
+		array -- An array of position data that the resulting box will envelop.
+		</div>
+		<div>
+		Sets the upper and lower bounds of this box to include all of the data in *array*.
+		</div>
+
+		<h3>[method:Box3 setFromBufferAttribute]( [page:BufferAttribute attribute] ) [page:Box3 this]</h3>
+		<div>
+		buffer -- A buffer attribute of position data that the resulting box will envelop.
+		</div>
+		<div>
+		Sets the upper and lower bounds of this box to include all of the data in *attribute*.
+		</div>
+
 		<h3>[method:Box3 setFromPoints]( [page:Array points] ) [page:Box3 this]</h3>
 		<div>
 		points -- Set of points that the resulting box will envelop.
@@ -101,6 +117,16 @@
 		Sets the upper and lower bounds of this box to include all of the points in *points*.
 		</div>
 
+		<h3>[method:Box3 setFromCenterAndSize]( [page:Vector3 center], [page:Vector3 size] ) [page:Box3 this]</h3>
+		<div>
+		center -- Desired center position of the box. <br>
+		size -- Desired x, y and z dimensions of the box.
+		</div>
+		<div>
+		Centers this box on *center* and sets this box's width, height and depth to the values specified <br>
+		in *size*.
+		</div>
+
 		<h3>[method:Box3 setFromObject]( [page:Object3D object] ) [page:Box3 this]</h3>
 		<div>
 		object -- [page:Object3D] to compute the bounding box for.
@@ -262,16 +288,6 @@
 		If the point lies inside of this box, the distance will be 0.
 		</div>
 
-		<h3>[method:Box3 setFromCenterAndSize]( [page:Vector3 center], [page:Vector3 size] ) [page:Box3 this]</h3>
-		<div>
-		center -- Desired center position of the box. <br>
-		size -- Desired x, y and z dimensions of the box.
-		</div>
-		<div>
-		Centers this box on *center* and sets this box's width, height and depth to the values specified <br>
-		in *size*.
-		</div>
-
 		<h2>Source</h2>
 
 		[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]

+ 10 - 9
src/core/BufferGeometry.js

@@ -579,11 +579,11 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, {
 
 		}
 
-		var positions = this.attributes.position.array;
+		var position = this.attributes.position;
 
-		if ( positions !== undefined ) {
+		if ( position !== undefined ) {
 
-			this.boundingBox.setFromArray( positions );
+			this.boundingBox.setFromBufferAttribute( position );
 
 		} else {
 
@@ -612,14 +612,13 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, {
 
 			}
 
-			var positions = this.attributes.position;
+			var position = this.attributes.position;
 
-			if ( positions ) {
+			if ( position ) {
 
-				var array = positions.array;
 				var center = this.boundingSphere.center;
 
-				box.setFromArray( array );
+				box.setFromBufferAttribute( position );
 				box.getCenter( center );
 
 				// hoping to find a boundingSphere with a radius smaller than the
@@ -627,9 +626,11 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, {
 
 				var maxRadiusSq = 0;
 
-				for ( var i = 0, il = array.length; i < il; i += 3 ) {
+				for ( var i = 0, il = position.count; i < il; i ++ ) {
 
-					vector.fromArray( array, i );
+					vector.x = position.getX( i );
+					vector.y = position.getY( i );
+					vector.z = position.getZ( i );
 					maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
 
 				}

+ 31 - 0
src/math/Box3.js

@@ -59,6 +59,37 @@ Box3.prototype = {
 
 	},
 
+	setFromBufferAttribute: function ( attribute ) {
+
+		var minX = + Infinity;
+		var minY = + Infinity;
+		var minZ = + Infinity;
+
+		var maxX = - Infinity;
+		var maxY = - Infinity;
+		var maxZ = - Infinity;
+
+		for ( var i = 0, l = attribute.count; i < l; i ++ ) {
+
+			var x = attribute.getX( i );
+			var y = attribute.getY( i );
+			var z = attribute.getZ( i );
+
+			if ( x < minX ) minX = x;
+			if ( y < minY ) minY = y;
+			if ( z < minZ ) minZ = z;
+
+			if ( x > maxX ) maxX = x;
+			if ( y > maxY ) maxY = y;
+			if ( z > maxZ ) maxZ = z;
+
+		}
+
+		this.min.set( minX, minY, minZ );
+		this.max.set( maxX, maxY, maxZ );
+
+	},
+
 	setFromPoints: function ( points ) {
 
 		this.makeEmpty();