Pārlūkot izejas kodu

Merge pull request #11653 from Mugen87/amf

AMFLoader: Clean up
Mr.doob 8 gadi atpakaļ
vecāks
revīzija
e068dc173d
1 mainītis faili ar 69 papildinājumiem un 74 dzēšanām
  1. 69 74
      examples/js/loaders/AMFLoader.js

+ 69 - 74
examples/js/loaders/AMFLoader.js

@@ -50,12 +50,12 @@ THREE.AMFLoader.prototype = {
 			var view = new DataView( data );
 			var magic = String.fromCharCode( view.getUint8( 0 ), view.getUint8( 1 ) );
 
-			if ( magic === "PK" ) {
+			if ( magic === 'PK' ) {
 
 				var zip = null;
 				var file = null;
 
-				console.log( "Loading Zip" );
+				console.log( 'THREE.AMFLoader: Loading Zip' );
 
 				try {
 
@@ -65,7 +65,7 @@ THREE.AMFLoader.prototype = {
 
 					if ( e instanceof ReferenceError ) {
 
-						console.log( "	jszip missing and file is compressed." );
+						console.log( 'THREE.AMFLoader: jszip missing and file is compressed.' );
 						return null;
 
 					}
@@ -82,14 +82,14 @@ THREE.AMFLoader.prototype = {
 
 				}
 
-				console.log( "	Trying to load file asset: " + file );
+				console.log( 'THREE.AMFLoader: Trying to load file asset: ' + file );
 				view = new DataView( zip.file( file ).asArrayBuffer() );
 
 			}
 
-			if ( TextDecoder === undefined ) {
+			if ( window.TextDecoder === undefined ) {
 
-				console.log( "	TextDecoder not present.	Please use TextDecoder polyfill." );
+				console.log( 'THREE.AMFLoader: TextDecoder not present. Please use TextDecoder polyfill.' );
 				return null;
 
 			}
@@ -97,9 +97,9 @@ THREE.AMFLoader.prototype = {
 			var fileText = new TextDecoder( 'utf-8' ).decode( view );
 			var xmlData = new DOMParser().parseFromString( fileText, 'application/xml' );
 
-			if ( xmlData.documentElement.nodeName.toLowerCase() !== "amf" ) {
+			if ( xmlData.documentElement.nodeName.toLowerCase() !== 'amf' ) {
 
-				console.log( "	Error loading AMF - no AMF document found." );
+				console.log( 'THREE.AMFLoader: Error loading AMF - no AMF document found.' );
 				return null;
 
 			}
@@ -113,18 +113,18 @@ THREE.AMFLoader.prototype = {
 			var scale = 1.0;
 			var unit = 'millimeter';
 
-			if ( node.documentElement.attributes[ 'unit' ] !== undefined ) {
+			if ( node.documentElement.attributes.unit !== undefined ) {
 
-				unit = node.documentElement.attributes[ 'unit' ].value.toLowerCase();
+				unit = node.documentElement.attributes.unit.value.toLowerCase();
 
 			}
 
 			var scaleUnits = {
-				'millimeter': 1.0,
-				'inch': 25.4,
-				'feet': 304.8,
-				'meter': 1000.0,
-				'micron': 0.001
+				millimeter: 1.0,
+				inch: 25.4,
+				feet: 304.8,
+				meter: 1000.0,
+				micron: 0.001
 			};
 
 			if ( scaleUnits[ unit ] !== undefined ) {
@@ -133,15 +133,15 @@ THREE.AMFLoader.prototype = {
 
 			}
 
-			console.log( "	Unit scale: " + scale );
+			console.log( 'THREE.AMFLoader: Unit scale: ' + scale );
 			return scale;
 
 		}
 
 		function loadMaterials( node ) {
 
-			var matName = "AMF Material";
-			var matId = node.attributes[ 'id' ].textContent;
+			var matName = 'AMF Material';
+			var matId = node.attributes.id.textContent;
 			var color = { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
 
 			var loadedMaterial = null;
@@ -150,9 +150,9 @@ THREE.AMFLoader.prototype = {
 
 				var matChildEl = node.children[ i ];
 
-				if ( matChildEl.nodeName === "metadata" && matChildEl.attributes[ 'type' ] !== undefined ) {
+				if ( matChildEl.nodeName === 'metadata' && matChildEl.attributes.type !== undefined ) {
 
-					if ( matChildEl.attributes[ 'type' ].value === 'name' ) {
+					if ( matChildEl.attributes.type.value === 'name' ) {
 
 						matname = matChildEl.textContent;
 
@@ -179,13 +179,13 @@ THREE.AMFLoader.prototype = {
 
 			}
 
-			return { 'id': matId, 'material': loadedMaterial };
+			return { id: matId, material: loadedMaterial };
 
 		}
 
 		function loadColor( node ) {
 
-			var color = { 'r': 1.0, 'g': 1.0, 'b': 1.0, 'a': 1.0 };
+			var color = { r: 1.0, g: 1.0, b: 1.0, a: 1.0 };
 
 			for ( var i = 0; i < node.children.length; i ++ ) {
 
@@ -217,23 +217,23 @@ THREE.AMFLoader.prototype = {
 
 		function loadMeshVolume( node ) {
 
-			var volume = { "name": "", "triangles": [], "materialid": null };
+			var volume = { name: '', triangles: [], materialid: null };
 
 			var currVolumeNode = node.firstElementChild;
 
-			if ( node.attributes[ 'materialid' ] !== undefined ) {
+			if ( node.attributes.materialid !== undefined ) {
 
-				volume.materialId = node.attributes[ 'materialid' ].nodeValue;
+				volume.materialId = node.attributes.materialid.nodeValue;
 
 			}
 
 			while ( currVolumeNode ) {
 
-				if ( currVolumeNode.nodeName === "metadata" ) {
+				if ( currVolumeNode.nodeName === 'metadata' ) {
 
-					if ( currVolumeNode.attributes[ 'type' ] !== undefined ) {
+					if ( currVolumeNode.attributes.type !== undefined ) {
 
-						if ( currVolumeNode.attributes[ 'type' ].value === 'name' ) {
+						if ( currVolumeNode.attributes.type.value === 'name' ) {
 
 							volume.name = currVolumeNode.textContent;
 
@@ -241,15 +241,13 @@ THREE.AMFLoader.prototype = {
 
 					}
 
-				} else if ( currVolumeNode.nodeName === "triangle" ) {
+				} else if ( currVolumeNode.nodeName === 'triangle' ) {
 
-					var v1 = currVolumeNode.getElementsByTagName("v1")[0].textContent;
-					var v2 = currVolumeNode.getElementsByTagName("v2")[0].textContent;
-					var v3 = currVolumeNode.getElementsByTagName("v3")[0].textContent;
+					var v1 = currVolumeNode.getElementsByTagName( 'v1' )[ 0 ].textContent;
+					var v2 = currVolumeNode.getElementsByTagName( 'v2' )[ 0 ].textContent;
+					var v3 = currVolumeNode.getElementsByTagName( 'v3' )[ 0 ].textContent;
 
-					volume.triangles.push( v1 );
-					volume.triangles.push( v2 );
-					volume.triangles.push( v3 );
+					volume.triangles.push( v1, v2, v3 );
 
 				}
 
@@ -269,31 +267,27 @@ THREE.AMFLoader.prototype = {
 
 			while ( currVerticesNode ) {
 
-				if ( currVerticesNode.nodeName === "vertex" ) {
+				if ( currVerticesNode.nodeName === 'vertex' ) {
 
 					var vNode = currVerticesNode.firstElementChild;
 
 					while ( vNode ) {
 
-						if ( vNode.nodeName === "coordinates" ) {
+						if ( vNode.nodeName === 'coordinates' ) {
 
-							var x = vNode.getElementsByTagName("x")[0].textContent;
-							var y = vNode.getElementsByTagName("y")[0].textContent;
-							var z = vNode.getElementsByTagName("z")[0].textContent;
+							var x = vNode.getElementsByTagName( 'x' )[ 0 ].textContent;
+							var y = vNode.getElementsByTagName( 'y' )[ 0 ].textContent;
+							var z = vNode.getElementsByTagName( 'z' )[ 0 ].textContent;
 
-							vertArray.push(x);
-							vertArray.push(y);
-							vertArray.push(z);
+							vertArray.push( x, y, z );
 
-						} else if ( vNode.nodeName === "normal" ) {
+						} else if ( vNode.nodeName === 'normal' ) {
 
-							var nx = vNode.getElementsByTagName("nx")[0].textContent;
-							var ny = vNode.getElementsByTagName("ny")[0].textContent;
-							var nz = vNode.getElementsByTagName("nz")[0].textContent;
+							var nx = vNode.getElementsByTagName( 'nx' )[ 0 ].textContent;
+							var ny = vNode.getElementsByTagName( 'ny' )[ 0 ].textContent;
+							var nz = vNode.getElementsByTagName( 'nz' )[ 0 ].textContent;
 
-							normalArray.push(nx);
-							normalArray.push(ny);
-							normalArray.push(nz);
+							normalArray.push( nx, ny, nz );
 
 						}
 
@@ -306,24 +300,24 @@ THREE.AMFLoader.prototype = {
 
 			}
 
-			return { "vertices": vertArray, "normals": normalArray };
+			return { 'vertices': vertArray, 'normals': normalArray };
 
 		}
 
 		function loadObject( node ) {
 
-			var objId = node.attributes[ 'id' ].textContent;
-			var loadedObject = { "name": "amfobject", "meshes": [] };
+			var objId = node.attributes.id.textContent;
+			var loadedObject = { name: 'amfobject', meshes: [] };
 			var currColor = null;
 			var currObjNode = node.firstElementChild;
 
 			while ( currObjNode ) {
 
-				if ( currObjNode.nodeName === "metadata" ) {
+				if ( currObjNode.nodeName === 'metadata' ) {
 
-					if ( currObjNode.attributes[ 'type' ] !== undefined ) {
+					if ( currObjNode.attributes.type !== undefined ) {
 
-						if ( currObjNode.attributes[ 'type' ].value === 'name' ) {
+						if ( currObjNode.attributes.type.value === 'name' ) {
 
 							loadedObject.name = currObjNode.textContent;
 
@@ -331,25 +325,25 @@ THREE.AMFLoader.prototype = {
 
 					}
 
-				} else if ( currObjNode.nodeName === "color" ) {
+				} else if ( currObjNode.nodeName === 'color' ) {
 
 					currColor = loadColor( currObjNode );
 
-				} else if ( currObjNode.nodeName === "mesh" ) {
+				} else if ( currObjNode.nodeName === 'mesh' ) {
 
 					var currMeshNode = currObjNode.firstElementChild;
-					var mesh = { "vertices": [], "normals": [], "volumes": [], "color": currColor };
+					var mesh = { vertices: [], normals: [], volumes: [], color: currColor };
 
 					while ( currMeshNode ) {
 
-						if ( currMeshNode.nodeName === "vertices" ) {
+						if ( currMeshNode.nodeName === 'vertices' ) {
 
 							var loadedVertices = loadMeshVertices( currMeshNode );
 
 							mesh.normals = mesh.normals.concat( loadedVertices.normals );
 							mesh.vertices = mesh.vertices.concat( loadedVertices.vertices );
 
-						} else if ( currMeshNode.nodeName === "volume" ) {
+						} else if ( currMeshNode.nodeName === 'volume' ) {
 
 							mesh.volumes.push( loadMeshVolume( currMeshNode ) );
 
@@ -372,26 +366,28 @@ THREE.AMFLoader.prototype = {
 		}
 
 		var xmlData = loadDocument( data );
-		var amfName = "";
-		var amfAuthor = "";
+		var amfName = '';
+		var amfAuthor = '';
 		var amfScale = loadDocumentScale( xmlData );
 		var amfMaterials = {};
 		var amfObjects = {};
 		var children = xmlData.documentElement.children;
 
-		for ( var i = 0; i < children.length; i ++ ) {
+		var i, j;
+
+		for ( i = 0; i < children.length; i ++ ) {
 
 			var child = children[ i ];
 
 			if ( child.nodeName === 'metadata' ) {
 
-				if ( child.attributes[ 'type' ] !== undefined ) {
+				if ( child.attributes.type !== undefined ) {
 
-					if ( child.attributes[ 'type' ].value === 'name' ) {
+					if ( child.attributes.type.value === 'name' ) {
 
 						amfName = child.textContent;
 
-					} else if ( child.attributes[ 'type' ].value === 'author' ) {
+					} else if ( child.attributes.type.value === 'author' ) {
 
 						amfAuthor = child.textContent;
 
@@ -420,7 +416,7 @@ THREE.AMFLoader.prototype = {
 
 		sceneObject.name = amfName;
 		sceneObject.userData.author = amfAuthor;
-		sceneObject.userData.loader = "AMF";
+		sceneObject.userData.loader = 'AMF';
 
 		for ( var id in amfObjects ) {
 
@@ -429,16 +425,16 @@ THREE.AMFLoader.prototype = {
 			var newObject = new THREE.Group();
 			newObject.name = part.name || '';
 
-			for ( var i = 0; i < meshes.length; i ++ ) {
+			for ( i = 0; i < meshes.length; i ++ ) {
 
 				var objDefaultMaterial = defaultMaterial;
 				var mesh = meshes[ i ];
-				var vertices = new THREE.BufferAttribute( new Float32Array( mesh.vertices ), 3 );
+				var vertices = new THREE.Float32BufferAttribute( mesh.vertices, 3 );
 				var normals = null;
 
 				if ( mesh.normals.length ) {
 
-					normals = new THREE.BufferAttribute( new Float32Array( mesh.normals ), 3 );
+					normals = new THREE.Float32BufferAttribute( mesh.normals, 3 );
 
 				}
 
@@ -460,14 +456,13 @@ THREE.AMFLoader.prototype = {
 
 				var volumes = mesh.volumes;
 
-				for ( var j = 0; j < volumes.length; j ++ ) {
+				for ( j = 0; j < volumes.length; j ++ ) {
 
 					var volume = volumes[ j ];
 					var newGeometry = new THREE.BufferGeometry();
-					var indexes = new  Uint32Array( volume.triangles );
 					var material = objDefaultMaterial;
 
-					newGeometry.setIndex( new THREE.BufferAttribute( indexes, 1 ) );
+					newGeometry.setIndex( volume.triangles );
 					newGeometry.addAttribute( 'position', vertices.clone() );
 
 					if( normals ) {