Browse Source

Nicer API for Geometry.merge()

Mr.doob 11 years ago
parent
commit
b6e22e226c
2 changed files with 23 additions and 17 deletions
  1. 9 14
      src/core/Geometry.js
  2. 14 3
      src/extras/GeometryUtils.js

+ 9 - 14
src/core/Geometry.js

@@ -455,26 +455,21 @@ THREE.Geometry.prototype = {
 
 	},
 
-	merge: function ( object2 /* mesh | geometry */, materialIndexOffset ) {
+	merge: function ( geometry, matrix, materialIndexOffset ) {
 
-		var matrix, normalMatrix,
+		var normalMatrix,
 		vertexOffset = this.vertices.length,
 		uvPosition = this.faceVertexUvs[ 0 ].length,
-		geometry2 = object2 instanceof THREE.Mesh ? object2.geometry : object2,
 		vertices1 = this.vertices,
-		vertices2 = geometry2.vertices,
+		vertices2 = geometry.vertices,
 		faces1 = this.faces,
-		faces2 = geometry2.faces,
+		faces2 = geometry.faces,
 		uvs1 = this.faceVertexUvs[ 0 ],
-		uvs2 = geometry2.faceVertexUvs[ 0 ];
+		uvs2 = geometry.faceVertexUvs[ 0 ];
 
 		if ( materialIndexOffset === undefined ) materialIndexOffset = 0;
 
-		if ( object2 instanceof THREE.Mesh ) {
-
-			object2.matrixAutoUpdate && object2.updateMatrix();
-
-			matrix = object2.matrix;
+		if ( matrix !== undefined ) {
 
 			normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
 
@@ -488,7 +483,7 @@ THREE.Geometry.prototype = {
 
 			var vertexCopy = vertex.clone();
 
-			if ( matrix ) vertexCopy.applyMatrix4( matrix );
+			if ( matrix !== undefined ) vertexCopy.applyMatrix4( matrix );
 
 			vertices1.push( vertexCopy );
 
@@ -505,7 +500,7 @@ THREE.Geometry.prototype = {
 			faceCopy = new THREE.Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
 			faceCopy.normal.copy( face.normal );
 
-			if ( normalMatrix ) {
+			if ( normalMatrix !== undefined ) {
 
 				faceCopy.normal.applyMatrix3( normalMatrix ).normalize();
 
@@ -515,7 +510,7 @@ THREE.Geometry.prototype = {
 
 				normal = faceVertexNormals[ j ].clone();
 
-				if ( normalMatrix ) {
+				if ( normalMatrix !== undefined ) {
 
 					normal.applyMatrix3( normalMatrix ).normalize();
 

+ 14 - 3
src/extras/GeometryUtils.js

@@ -7,11 +7,22 @@ THREE.GeometryUtils = {
 
 	// Merge two geometries or geometry and geometry from object (using object's transform)
 
-	merge: function ( geometry1, object2 /* mesh | geometry */, materialIndexOffset ) {
+	merge: function ( geometry1, geometry2, materialIndexOffset ) {
 
-		console.warn( 'DEPRECATED: GeometryUtils\'s .merge() has been moved to Geometry. Use geometry.merge( geometry2 ) instead.' );
+		console.warn( 'DEPRECATED: GeometryUtils\'s .merge() has been moved to Geometry. Use geometry.merge( geometry2, matrix, materialIndexOffset ) instead.' );
 
-		geometry1.merge( object2, materialIndexOffset );
+		var matrix;
+
+		if ( geometry2 instanceof THREE.Mesh ) {
+
+			geometry2.matrixAutoUpdate && geometry2.updateMatrix();
+
+			matrix = geometry2.matrix;
+			geometry2 = geometry2.geometry;
+
+		}
+
+		geometry1.merge( geometry2, matrix, materialIndexOffset );
 
 	},