Browse Source

Merge branch 'master' of https://github.com/zz85/three.js into dev

Mr.doob 12 years ago
parent
commit
c7b0e65120
1 changed files with 73 additions and 4 deletions
  1. 73 4
      examples/js/modifiers/SubdivisionModifier.js

+ 73 - 4
examples/js/modifiers/SubdivisionModifier.js

@@ -26,6 +26,68 @@
  *		selective subdivision
  *		selective subdivision
  */
  */
 
 
+THREE.Face4Stub = function ( a, b, c, d, normal, color, materialIndex ) {
+
+	this.a = a;
+	this.b = b;
+	this.c = c;
+	this.d = d;
+
+	this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
+	this.vertexNormals = normal instanceof Array ? normal : [ ];
+
+	this.color = color instanceof THREE.Color ? color : new THREE.Color();
+	this.vertexColors = color instanceof Array ? color : [];
+
+	this.vertexTangents = [];
+
+	this.materialIndex = materialIndex !== undefined ? materialIndex : 0;
+
+	this.centroid = new THREE.Vector3();
+
+};
+
+
+THREE.GeometryUtils.convertFace4s = function(geometry) {
+
+	// return geometry;
+
+	var faces = geometry.faces;
+	var faceVertexUvs = geometry.faceVertexUvs[0];
+
+	var newfaces = [];
+	var newfaceVertexUvs = [];
+
+	var f, fl, face, uv;
+
+	for (f=0, fl=faces.length; f < fl; f++) {
+
+		face = faces[f];
+		uv = faceVertexUvs[f];
+
+		if ( face instanceof THREE.Face3 ) {
+			
+			newfaces.push(face);
+			if (uv) newfaceVertexUvs.push(uv);
+
+		} else {
+
+			newfaces.push( new THREE.Face3( face.a, face.b, face.c, null, face.color, face.materialIndex) );
+			newfaces.push( new THREE.Face3( face.d, face.a, face.c, null, face.color, face.materialIndex) );
+
+
+			if (uv) newfaceVertexUvs.push([uv[0], uv[1], uv[2]]);
+			if (uv) newfaceVertexUvs.push([uv[3], uv[0], uv[2]]);
+
+		}
+
+	}
+
+	geometry.faces = newfaces;
+	geometry.faceVertexUvs = [newfaceVertexUvs];
+
+}
+
 
 
 THREE.SubdivisionModifier = function ( subdivisions ) {
 THREE.SubdivisionModifier = function ( subdivisions ) {
 
 
@@ -47,6 +109,13 @@ THREE.SubdivisionModifier.prototype.modify = function ( geometry ) {
 		this.smooth( geometry );
 		this.smooth( geometry );
 	}
 	}
 
 
+	THREE.GeometryUtils.convertFace4s( geometry );
+	delete geometry.__tmpVertices;
+	geometry.computeCentroids();
+	geometry.computeFaceNormals();
+	geometry.computeVertexNormals();
+
+
 };
 };
 
 
 /// REFACTORING THIS OUT
 /// REFACTORING THIS OUT
@@ -98,7 +167,7 @@ THREE.GeometryUtils.computeEdgeFaces = function ( geometry ) {
 			hash = orderedKey( face.c, face.a );
 			hash = orderedKey( face.c, face.a );
 			mapEdgeHash( hash, i );
 			mapEdgeHash( hash, i );
 
 
-		} else if ( face instanceof THREE.Face4 ) {
+		} else if ( face instanceof THREE.Face4Stub ) {
 
 
 			hash = orderedKey( face.a, face.b );
 			hash = orderedKey( face.a, face.b );
 			mapEdgeHash( hash, i );
 			mapEdgeHash( hash, i );
@@ -176,7 +245,7 @@ THREE.SubdivisionModifier.prototype.smooth = function ( oldGeometry ) {
 
 
 		// TODO move vertex selection over here!
 		// TODO move vertex selection over here!
 
 
-		var newFace = new THREE.Face4( a, b, c, d, null, oldFace.color, oldFace.materialIndex );
+		var newFace = new THREE.Face4Stub( a, b, c, d, null, oldFace.color, oldFace.materialIndex );
 
 
 		if (scope.useOldVertexColors) {
 		if (scope.useOldVertexColors) {
 
 
@@ -345,7 +414,7 @@ THREE.SubdivisionModifier.prototype.smooth = function ( oldGeometry ) {
 			avgUv.x /= 3;
 			avgUv.x /= 3;
 			avgUv.y /= 3;
 			avgUv.y /= 3;
 
 
-		} else if ( face instanceof THREE.Face4 ) {
+		} else if ( face instanceof THREE.Face4Stub ) {
 
 
 			avgUv.x = getUV( face.a, i ).x + getUV( face.b, i ).x + getUV( face.c, i ).x + getUV( face.d, i ).x;
 			avgUv.x = getUV( face.a, i ).x + getUV( face.b, i ).x + getUV( face.c, i ).x + getUV( face.d, i ).x;
 			avgUv.y = getUV( face.a, i ).y + getUV( face.b, i ).y + getUV( face.c, i ).y + getUV( face.d, i ).y;
 			avgUv.y = getUV( face.a, i ).y + getUV( face.b, i ).y + getUV( face.c, i ).y + getUV( face.d, i ).y;
@@ -540,7 +609,7 @@ THREE.SubdivisionModifier.prototype.smooth = function ( oldGeometry ) {
 			f4( currentVerticeIndex, edgePoints[hashBC], face.c, edgePoints[hashCA], face, bca123, i );
 			f4( currentVerticeIndex, edgePoints[hashBC], face.c, edgePoints[hashCA], face, bca123, i );
 			f4( currentVerticeIndex, edgePoints[hashCA], face.a, edgePoints[hashAB], face, cab123, i );
 			f4( currentVerticeIndex, edgePoints[hashCA], face.a, edgePoints[hashAB], face, cab123, i );
 
 
-		} else if ( face instanceof THREE.Face4 ) {
+		} else if ( face instanceof THREE.Face4Stub ) {
 
 
 			// create 4 face4s
 			// create 4 face4s