소스 검색

Face3ized JSONLoader and BinaryLoader.

Mr.doob 12 년 전
부모
커밋
5900232fdb
2개의 변경된 파일177개의 추가작업 그리고 64개의 파일을 삭제
  1. 19 8
      examples/js/loaders/BinaryLoader.js
  2. 158 56
      src/loaders/JSONLoader.js

+ 19 - 8
examples/js/loaders/BinaryLoader.js

@@ -694,7 +694,8 @@ THREE.BinaryLoader.prototype.createBinModel = function ( data, callback, texture
 
 	function f4 ( scope, a, b, c, d, mi ) {
 
-		scope.faces.push( new THREE.Face4( a, b, c, d, null, null, mi ) );
+		scope.faces.push( new THREE.Face3( a, b, d, null, null, mi ) );
+		scope.faces.push( new THREE.Face3( b, c, d, null, null, mi ) );
 
 	};
 
@@ -739,13 +740,17 @@ THREE.BinaryLoader.prototype.createBinModel = function ( data, callback, texture
 			ndy = normals[ nd*3 + 1 ],
 			ndz = normals[ nd*3 + 2 ];
 
-		scope.faces.push( new THREE.Face4( a, b, c, d,
-						  [new THREE.Vector3( nax, nay, naz ),
-						   new THREE.Vector3( nbx, nby, nbz ),
-						   new THREE.Vector3( ncx, ncy, ncz ),
-						   new THREE.Vector3( ndx, ndy, ndz )],
-						  null,
-						  mi ) );
+		scope.faces.push( new THREE.Face3( a, b, d, [
+			new THREE.Vector3( nax, nay, naz ),
+			new THREE.Vector3( nbx, nby, nbz ),
+			new THREE.Vector3( ndx, ndy, ndz )
+		], null, mi ) );
+
+		scope.faces.push( new THREE.Face3( b, c, d, [
+			new THREE.Vector3( nbx, nby, nbz ),
+			new THREE.Vector3( ncx, ncy, ncz ),
+			new THREE.Vector3( ndx, ndy, ndz )
+		], null, mi ) );
 
 	};
 
@@ -763,10 +768,16 @@ THREE.BinaryLoader.prototype.createBinModel = function ( data, callback, texture
 
 		where.push( [
 			new THREE.Vector2( u1, v1 ),
+			new THREE.Vector2( u2, v2 ),
+			new THREE.Vector2( u4, v4 )
+		] );
+
+		where.push( [
 			new THREE.Vector2( u2, v2 ),
 			new THREE.Vector2( u3, v3 ),
 			new THREE.Vector2( u4, v4 )
 		] );
+
 	};
 
 	Model.prototype = Object.create( THREE.Geometry.prototype );

+ 158 - 56
src/loaders/JSONLoader.js

@@ -119,7 +119,7 @@ THREE.JSONLoader.prototype.parse = function ( json, texturePath ) {
 
 		var i, j, fi,
 
-		offset, zLength, nVertices,
+		offset, zLength,
 
 		colorIndex, normalIndex, uvIndex, materialIndex,
 
@@ -130,9 +130,9 @@ THREE.JSONLoader.prototype.parse = function ( json, texturePath ) {
 		hasFaceNormal, hasFaceVertexNormal,
 		hasFaceColor, hasFaceVertexColor,
 
-		vertex, face, color, normal,
+		vertex, face, faceA, faceB, color, hex, normal,
 
-		uvLayer, uvs, u, v,
+		uvLayer, uv, u, v,
 
 		faces = json.faces,
 		vertices = json.vertices,
@@ -194,114 +194,216 @@ THREE.JSONLoader.prototype.parse = function ( json, texturePath ) {
 
 			if ( isQuad ) {
 
-				face = new THREE.Face4();
+				faceA = new THREE.Face3();
+				faceA.a = faces[ offset ];
+				faceA.b = faces[ offset + 1 ];
+				faceA.c = faces[ offset + 3 ];
 
-				face.a = faces[ offset ++ ];
-				face.b = faces[ offset ++ ];
-				face.c = faces[ offset ++ ];
-				face.d = faces[ offset ++ ];
+				faceB = new THREE.Face3();
+				faceB.a = faces[ offset + 1 ];
+				faceB.b = faces[ offset + 2 ];
+				faceB.c = faces[ offset + 3 ];
 
-				nVertices = 4;
+				offset += 4;
 
-			} else {
+				if ( hasMaterial ) {
 
-				face = new THREE.Face3();
+					materialIndex = faces[ offset ++ ];
+					faceA.materialIndex = materialIndex;
+					faceB.materialIndex = materialIndex;
 
-				face.a = faces[ offset ++ ];
-				face.b = faces[ offset ++ ];
-				face.c = faces[ offset ++ ];
+				}
 
-				nVertices = 3;
+				// to get face <=> uv index correspondence
 
-			}
+				fi = geometry.faces.length;
 
-			if ( hasMaterial ) {
+				if ( hasFaceVertexUv ) {
 
-				materialIndex = faces[ offset ++ ];
-				face.materialIndex = materialIndex;
+					for ( i = 0; i < nUvLayers; i++ ) {
 
-			}
+						uvLayer = json.uvs[ i ];
+
+						geometry.faceVertexUvs[ i ][ fi ] = [];
+						geometry.faceVertexUvs[ i ][ fi + 1 ] = []
+
+						for ( j = 0; j < 4; j ++ ) {
+
+							uvIndex = faces[ offset ++ ];
+
+							u = uvLayer[ uvIndex * 2 ];
+							v = uvLayer[ uvIndex * 2 + 1 ];
+
+							uv = new THREE.Vector2( u, v );
+
+							if ( j !== 2 ) geometry.faceVertexUvs[ i ][ fi ].push( uv );
+							if ( j !== 0 ) geometry.faceVertexUvs[ i ][ fi + 1 ].push( uv );
+
+						}
+
+					}
+
+				}
 
-			// to get face <=> uv index correspondence
+				if ( hasFaceNormal ) {
 
-			fi = geometry.faces.length;
+					normalIndex = faces[ offset ++ ] * 3;
 
-			if ( hasFaceVertexUv ) {
+					faceA.normal.set(
+						normals[ normalIndex ++ ],
+						normals[ normalIndex ++ ],
+						normals[ normalIndex ]
+					);
 
-				for ( i = 0; i < nUvLayers; i++ ) {
+					faceB.normal.copy( faceA.normal );
 
-					uvLayer = json.uvs[ i ];
+				}
+
+				if ( hasFaceVertexNormal ) {
 
-					uvs = [];
+					for ( i = 0; i < 4; i++ ) {
 
-					for ( j = 0; j < nVertices; j ++ ) {
+						normalIndex = faces[ offset ++ ] * 3;
 
-						uvIndex = faces[ offset ++ ];
+						normal = new THREE.Vector3(
+							normals[ normalIndex ++ ],
+							normals[ normalIndex ++ ],
+							normals[ normalIndex ]
+						);
 
-						u = uvLayer[ uvIndex * 2 ];
-						v = uvLayer[ uvIndex * 2 + 1 ];
 
-						uvs[ j ] = new THREE.Vector2( u, v );
+						if ( i !== 2 ) faceA.vertexNormals.push( normal );
+						if ( i !== 0 ) faceB.vertexNormals.push( normal );
 
 					}
 
-					geometry.faceVertexUvs[ i ][ fi ] = uvs;
+				}
+
+
+				if ( hasFaceColor ) {
+
+					colorIndex = faces[ offset ++ ];
+					hex = colors[ colorIndex ];
+
+					faceA.color.setHex( hex );
+					faceB.color.setHex( hex );
 
 				}
 
-			}
 
-			if ( hasFaceNormal ) {
+				if ( hasFaceVertexColor ) {
 
-				normalIndex = faces[ offset ++ ] * 3;
+					for ( i = 0; i < 4; i++ ) {
 
-				face.normal.set(
-					normals[ normalIndex ++ ],
-					normals[ normalIndex ++ ],
-					normals[ normalIndex ]
-				);
+						colorIndex = faces[ offset ++ ];
+						hex = colors[ colorIndex ];
 
-			}
+						if ( i !== 2 ) faceA.vertexColors.push( new THREE.Color( hex ) );
+						if ( i !== 0 ) faceB.vertexColors.push( new THREE.Color( hex ) );
+
+					}
+
+				}
+
+				geometry.faces.push( faceA );
+				geometry.faces.push( faceB );
 
-			if ( hasFaceVertexNormal ) {
+			} else {
+
+				face = new THREE.Face3();
+				face.a = faces[ offset ++ ];
+				face.b = faces[ offset ++ ];
+				face.c = faces[ offset ++ ];
+
+				if ( hasMaterial ) {
+
+					materialIndex = faces[ offset ++ ];
+					face.materialIndex = materialIndex;
+
+				}
+
+				// to get face <=> uv index correspondence
+
+				fi = geometry.faces.length;
+
+				if ( hasFaceVertexUv ) {
+
+					for ( i = 0; i < nUvLayers; i++ ) {
+
+						uvLayer = json.uvs[ i ];
+
+						geometry.faceVertexUvs[ i ][ fi ] = [];
+
+						for ( j = 0; j < 3; j ++ ) {
+
+							uvIndex = faces[ offset ++ ];
+
+							u = uvLayer[ uvIndex * 2 ];
+							v = uvLayer[ uvIndex * 2 + 1 ];
+
+							uv = new THREE.Vector2( u, v );
+
+							geometry.faceVertexUvs[ i ][ fi ].push( uv );
 
-				for ( i = 0; i < nVertices; i++ ) {
+						}
+
+					}
+
+				}
+
+				if ( hasFaceNormal ) {
 
 					normalIndex = faces[ offset ++ ] * 3;
 
-					normal = new THREE.Vector3(
+					face.normal.set(
 						normals[ normalIndex ++ ],
 						normals[ normalIndex ++ ],
 						normals[ normalIndex ]
 					);
 
-					face.vertexNormals.push( normal );
-
 				}
 
-			}
+				if ( hasFaceVertexNormal ) {
 
+					for ( i = 0; i < 3; i++ ) {
 
-			if ( hasFaceColor ) {
+						normalIndex = faces[ offset ++ ] * 3;
 
-				colorIndex = faces[ offset ++ ];
-				face.color.setHex( colors[ colorIndex ] );
+						normal = new THREE.Vector3(
+							normals[ normalIndex ++ ],
+							normals[ normalIndex ++ ],
+							normals[ normalIndex ]
+						);
 
-			}
+						face.vertexNormals.push( normal );
 
+					}
+
+				}
 
-			if ( hasFaceVertexColor ) {
 
-				for ( i = 0; i < nVertices; i++ ) {
+				if ( hasFaceColor ) {
 
 					colorIndex = faces[ offset ++ ];
-					face.vertexColors.push( new THREE.Color( colors[ colorIndex ] ) );
+					face.color.setHex( colors[ colorIndex ] );
 
 				}
 
-			}
 
-			geometry.faces.push( face );
+				if ( hasFaceVertexColor ) {
+
+					for ( i = 0; i < 3; i++ ) {
+
+						colorIndex = faces[ offset ++ ];
+						face.vertexColors.push( new THREE.Color( colors[ colorIndex ] ) );
+
+					}
+
+				}
+
+				geometry.faces.push( face );
+
+			}
 
 		}