2
0
Mr.doob 8 жил өмнө
parent
commit
052dc43266

+ 71 - 55
src/loaders/JSONLoader.js

@@ -81,28 +81,9 @@ Object.assign( JSONLoader.prototype, {
 
 
 	},
 	},
 
 
-	parse: function ( json, texturePath ) {
+	parse: ( function () {
 
 
-		if ( json.data !== undefined ) {
-
-			// Geometry 4.0 spec
-			json = json.data;
-
-		}
-
-		var geometry = new Geometry(),
-		scale = ( json.scale !== undefined ) ? 1.0 / json.scale : 1.0;
-
-		parseModel( scale );
-
-		parseSkin();
-		parseMorphing( scale );
-		parseAnimations();
-
-		geometry.computeFaceNormals();
-		geometry.computeBoundingSphere();
-
-		function parseModel( scale ) {
+		function parseModel( json, geometry ) {
 
 
 			function isBitSet( value, position ) {
 			function isBitSet( value, position ) {
 
 
@@ -112,27 +93,30 @@ Object.assign( JSONLoader.prototype, {
 
 
 			var i, j, fi,
 			var i, j, fi,
 
 
-			offset, zLength,
+				offset, zLength,
 
 
-		colorIndex, normalIndex, uvIndex, materialIndex,
+				colorIndex, normalIndex, uvIndex, materialIndex,
 
 
-			type,
-			isQuad,
-			hasMaterial,
-			hasFaceVertexUv,
-			hasFaceNormal, hasFaceVertexNormal,
-			hasFaceColor, hasFaceVertexColor,
+				type,
+				isQuad,
+				hasMaterial,
+				hasFaceVertexUv,
+				hasFaceNormal, hasFaceVertexNormal,
+				hasFaceColor, hasFaceVertexColor,
 
 
-		vertex, face, faceA, faceB, hex, normal,
+				vertex, face, faceA, faceB, hex, normal,
 
 
-			uvLayer, uv, u, v,
+				uvLayer, uv, u, v,
 
 
-			faces = json.faces,
-			vertices = json.vertices,
-			normals = json.normals,
-			colors = json.colors,
+				faces = json.faces,
+				vertices = json.vertices,
+				normals = json.normals,
+				colors = json.colors,
+
+				scale = json.scale,
+
+				nUvLayers = 0;
 
 
-			nUvLayers = 0;
 
 
 			if ( json.uvs !== undefined ) {
 			if ( json.uvs !== undefined ) {
 
 
@@ -174,14 +158,13 @@ Object.assign( JSONLoader.prototype, {
 
 
 				type = faces[ offset ++ ];
 				type = faces[ offset ++ ];
 
 
-
-				isQuad              = isBitSet( type, 0 );
-				hasMaterial         = isBitSet( type, 1 );
-				hasFaceVertexUv     = isBitSet( type, 3 );
-				hasFaceNormal       = isBitSet( type, 4 );
+				isQuad = isBitSet( type, 0 );
+				hasMaterial = isBitSet( type, 1 );
+				hasFaceVertexUv = isBitSet( type, 3 );
+				hasFaceNormal = isBitSet( type, 4 );
 				hasFaceVertexNormal = isBitSet( type, 5 );
 				hasFaceVertexNormal = isBitSet( type, 5 );
-				hasFaceColor	     = isBitSet( type, 6 );
-				hasFaceVertexColor  = isBitSet( type, 7 );
+				hasFaceColor = isBitSet( type, 6 );
+				hasFaceVertexColor = isBitSet( type, 7 );
 
 
 				// console.log("type", type, "bits", isQuad, hasMaterial, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);
 				// console.log("type", type, "bits", isQuad, hasMaterial, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);
 
 
@@ -402,7 +385,7 @@ Object.assign( JSONLoader.prototype, {
 
 
 		}
 		}
 
 
-		function parseSkin() {
+		function parseSkin( json, geometry ) {
 
 
 			var influencesPerVertex = ( json.influencesPerVertex !== undefined ) ? json.influencesPerVertex : 2;
 			var influencesPerVertex = ( json.influencesPerVertex !== undefined ) ? json.influencesPerVertex : 2;
 
 
@@ -410,7 +393,7 @@ Object.assign( JSONLoader.prototype, {
 
 
 				for ( var i = 0, l = json.skinWeights.length; i < l; i += influencesPerVertex ) {
 				for ( var i = 0, l = json.skinWeights.length; i < l; i += influencesPerVertex ) {
 
 
-					var x =                               json.skinWeights[ i ];
+					var x = json.skinWeights[ i ];
 					var y = ( influencesPerVertex > 1 ) ? json.skinWeights[ i + 1 ] : 0;
 					var y = ( influencesPerVertex > 1 ) ? json.skinWeights[ i + 1 ] : 0;
 					var z = ( influencesPerVertex > 2 ) ? json.skinWeights[ i + 2 ] : 0;
 					var z = ( influencesPerVertex > 2 ) ? json.skinWeights[ i + 2 ] : 0;
 					var w = ( influencesPerVertex > 3 ) ? json.skinWeights[ i + 3 ] : 0;
 					var w = ( influencesPerVertex > 3 ) ? json.skinWeights[ i + 3 ] : 0;
@@ -425,7 +408,7 @@ Object.assign( JSONLoader.prototype, {
 
 
 				for ( var i = 0, l = json.skinIndices.length; i < l; i += influencesPerVertex ) {
 				for ( var i = 0, l = json.skinIndices.length; i < l; i += influencesPerVertex ) {
 
 
-					var a =                               json.skinIndices[ i ];
+					var a = json.skinIndices[ i ];
 					var b = ( influencesPerVertex > 1 ) ? json.skinIndices[ i + 1 ] : 0;
 					var b = ( influencesPerVertex > 1 ) ? json.skinIndices[ i + 1 ] : 0;
 					var c = ( influencesPerVertex > 2 ) ? json.skinIndices[ i + 2 ] : 0;
 					var c = ( influencesPerVertex > 2 ) ? json.skinIndices[ i + 2 ] : 0;
 					var d = ( influencesPerVertex > 3 ) ? json.skinIndices[ i + 3 ] : 0;
 					var d = ( influencesPerVertex > 3 ) ? json.skinIndices[ i + 3 ] : 0;
@@ -447,7 +430,9 @@ Object.assign( JSONLoader.prototype, {
 
 
 		}
 		}
 
 
-		function parseMorphing( scale ) {
+		function parseMorphing( json, geometry ) {
+
+			var scale = json.scale;
 
 
 			if ( json.morphTargets !== undefined ) {
 			if ( json.morphTargets !== undefined ) {
 
 
@@ -492,7 +477,7 @@ Object.assign( JSONLoader.prototype, {
 
 
 		}
 		}
 
 
-		function parseAnimations() {
+		function parseAnimations( json, geometry ) {
 
 
 			var outputAnimations = [];
 			var outputAnimations = [];
 
 
@@ -539,19 +524,50 @@ Object.assign( JSONLoader.prototype, {
 
 
 		}
 		}
 
 
-		if ( json.materials === undefined || json.materials.length === 0 ) {
+		return function ( json, texturePath ) {
 
 
-			return { geometry: geometry };
+			if ( json.data !== undefined ) {
 
 
-		} else {
+				// Geometry 4.0 spec
+				json = json.data;
 
 
-			var materials = Loader.prototype.initMaterials( json.materials, texturePath, this.crossOrigin );
+			}
 
 
-			return { geometry: geometry, materials: materials };
+			if ( json.scale !== undefined ) {
 
 
-		}
+				json.scale = 1.0 / json.scale;
 
 
-	}
+			} else {
+
+				json.scale = 1.0;
+
+			}
+
+			var geometry = new Geometry();
+
+			parseModel( json, geometry );
+			parseSkin( json, geometry );
+			parseMorphing( json, geometry );
+			parseAnimations( json, geometry );
+
+			geometry.computeFaceNormals();
+			geometry.computeBoundingSphere();
+
+			if ( json.materials === undefined || json.materials.length === 0 ) {
+
+				return { geometry: geometry };
+
+			} else {
+
+				var materials = Loader.prototype.initMaterials( json.materials, texturePath, this.crossOrigin );
+
+				return { geometry: geometry, materials: materials };
+
+			}
+
+		};
+
+	} )()
 
 
 } );
 } );