浏览代码

OBJLoader: Clean up

Mugen87 7 年之前
父节点
当前提交
8af1260742
共有 3 个文件被更改,包括 78 次插入74 次删除
  1. 64 52
      examples/js/loaders/OBJLoader.js
  2. 9 17
      examples/webgl_loader_obj.html
  3. 5 5
      examples/webgl_loader_obj_mtl.html

+ 64 - 52
examples/js/loaders/OBJLoader.js

@@ -5,24 +5,24 @@
 THREE.OBJLoader = ( function () {
 
 	// o object_name | g group_name
-	var object_pattern           = /^[og]\s*(.+)?/;
+	var object_pattern = /^[og]\s*(.+)?/;
 	// mtllib file_reference
 	var material_library_pattern = /^mtllib /;
 	// usemtl material_name
-	var material_use_pattern     = /^usemtl /;
+	var material_use_pattern = /^usemtl /;
 
 	function ParserState() {
 
 		var state = {
-			objects  : [],
-			object   : {},
+			objects: [],
+			object: {},
 
-			vertices : [],
-			normals  : [],
-			colors   : [],
-			uvs      : [],
+			vertices: [],
+			normals: [],
+			colors: [],
+			uvs: [],
 
-			materialLibraries : [],
+			materialLibraries: [],
 
 			startObject: function ( name, fromDeclaration ) {
 
@@ -45,17 +45,17 @@ THREE.OBJLoader = ( function () {
 				}
 
 				this.object = {
-					name : name || '',
-					fromDeclaration : ( fromDeclaration !== false ),
-
-					geometry : {
-						vertices : [],
-						normals  : [],
-						colors   : [],
-						uvs      : []
+					name: name || '',
+					fromDeclaration: ( fromDeclaration !== false ),
+
+					geometry: {
+						vertices: [],
+						normals: [],
+						colors: [],
+						uvs: []
 					},
-					materials : [],
-					smooth : true,
+					materials: [],
+					smooth: true,
 
 					startMaterial: function ( name, libraries ) {
 
@@ -70,28 +70,30 @@ THREE.OBJLoader = ( function () {
 						}
 
 						var material = {
-							index      : this.materials.length,
-							name       : name || '',
-							mtllib     : ( Array.isArray( libraries ) && libraries.length > 0 ? libraries[ libraries.length - 1 ] : '' ),
-							smooth     : ( previous !== undefined ? previous.smooth : this.smooth ),
-							groupStart : ( previous !== undefined ? previous.groupEnd : 0 ),
-							groupEnd   : -1,
-							groupCount : -1,
-							inherited  : false,
+							index: this.materials.length,
+							name: name || '',
+							mtllib: ( Array.isArray( libraries ) && libraries.length > 0 ? libraries[ libraries.length - 1 ] : '' ),
+							smooth: ( previous !== undefined ? previous.smooth : this.smooth ),
+							groupStart: ( previous !== undefined ? previous.groupEnd : 0 ),
+							groupEnd: - 1,
+							groupCount: - 1,
+							inherited: false,
 
 							clone: function ( index ) {
+
 								var cloned = {
-									index      : ( typeof index === 'number' ? index : this.index ),
-									name       : this.name,
-									mtllib     : this.mtllib,
-									smooth     : this.smooth,
-									groupStart : 0,
-									groupEnd   : -1,
-									groupCount : -1,
-									inherited  : false
+									index: ( typeof index === 'number' ? index : this.index ),
+									name: this.name,
+									mtllib: this.mtllib,
+									smooth: this.smooth,
+									groupStart: 0,
+									groupEnd: - 1,
+									groupCount: - 1,
+									inherited: false
 								};
-								cloned.clone = this.clone.bind(cloned);
+								cloned.clone = this.clone.bind( cloned );
 								return cloned;
+
 							}
 						};
 
@@ -104,7 +106,9 @@ THREE.OBJLoader = ( function () {
 					currentMaterial: function () {
 
 						if ( this.materials.length > 0 ) {
+
 							return this.materials[ this.materials.length - 1 ];
+
 						}
 
 						return undefined;
@@ -114,7 +118,7 @@ THREE.OBJLoader = ( function () {
 					_finalize: function ( end ) {
 
 						var lastMultiMaterial = this.currentMaterial();
-						if ( lastMultiMaterial && lastMultiMaterial.groupEnd === -1 ) {
+						if ( lastMultiMaterial && lastMultiMaterial.groupEnd === - 1 ) {
 
 							lastMultiMaterial.groupEnd = this.geometry.vertices.length / 3;
 							lastMultiMaterial.groupCount = lastMultiMaterial.groupEnd - lastMultiMaterial.groupStart;
@@ -125,10 +129,14 @@ THREE.OBJLoader = ( function () {
 						// Ignore objects tail materials if no face declarations followed them before a new o/g started.
 						if ( end && this.materials.length > 1 ) {
 
-							for ( var mi = this.materials.length - 1; mi >= 0; mi-- ) {
+							for ( var mi = this.materials.length - 1; mi >= 0; mi -- ) {
+
 								if ( this.materials[ mi ].groupCount <= 0 ) {
+
 									this.materials.splice( mi, 1 );
+
 								}
+
 							}
 
 						}
@@ -136,10 +144,10 @@ THREE.OBJLoader = ( function () {
 						// Guarantee at least one empty material, this makes the creation later more straight forward.
 						if ( end && this.materials.length === 0 ) {
 
-							this.materials.push({
-								name   : '',
-								smooth : this.smooth
-							});
+							this.materials.push( {
+								name: '',
+								smooth: this.smooth
+							} );
 
 						}
 
@@ -339,7 +347,7 @@ THREE.OBJLoader = ( function () {
 
 		this.materials = null;
 
-	};
+	}
 
 	OBJLoader.prototype = {
 
@@ -386,7 +394,7 @@ THREE.OBJLoader = ( function () {
 
 			}
 
-			if ( text.indexOf( '\\\n' ) !== - 1) {
+			if ( text.indexOf( '\\\n' ) !== - 1 ) {
 
 				// join lines separated by a line continuation character (\)
 				text = text.replace( /\\\n/g, '' );
@@ -429,12 +437,14 @@ THREE.OBJLoader = ( function () {
 								parseFloat( data[ 3 ] )
 							);
 							if ( data.length === 8 ) {
+
 								state.colors.push(
 									parseFloat( data[ 4 ] ),
 									parseFloat( data[ 5 ] ),
 									parseFloat( data[ 6 ] )
 
 								);
+
 							}
 							break;
 						case 'vn':
@@ -450,6 +460,7 @@ THREE.OBJLoader = ( function () {
 								parseFloat( data[ 2 ] )
 							);
 							break;
+
 					}
 
 				} else if ( lineFirstChar === 'f' ) {
@@ -580,7 +591,7 @@ THREE.OBJLoader = ( function () {
 					// Handle null terminated files without exception
 					if ( line === '\0' ) continue;
 
-					throw new Error( "Unexpected line: '" + line  + "'" );
+					throw new Error( 'THREE.OBJLoader: Unexpected line: "' + line + '"' );
 
 				}
 
@@ -603,11 +614,11 @@ THREE.OBJLoader = ( function () {
 
 				var buffergeometry = new THREE.BufferGeometry();
 
-				buffergeometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( geometry.vertices ), 3 ) );
+				buffergeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( geometry.vertices, 3 ) );
 
 				if ( geometry.normals.length > 0 ) {
 
-					buffergeometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( geometry.normals ), 3 ) );
+					buffergeometry.addAttribute( 'normal', new THREE.Float32BufferAttribute( geometry.normals, 3 ) );
 
 				} else {
 
@@ -617,13 +628,13 @@ THREE.OBJLoader = ( function () {
 
 				if ( geometry.colors.length > 0 ) {
 
-					buffergeometry.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( geometry.colors ), 3 ) );
+					buffergeometry.addAttribute( 'color', new THREE.Float32BufferAttribute( geometry.colors, 3 ) );
 
 				}
 
 				if ( geometry.uvs.length > 0 ) {
 
-					buffergeometry.addAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( geometry.uvs ), 2 ) );
+					buffergeometry.addAttribute( 'uv', new THREE.Float32BufferAttribute( geometry.uvs, 2 ) );
 
 				}
 
@@ -631,7 +642,7 @@ THREE.OBJLoader = ( function () {
 
 				var createdMaterials = [];
 
-				for ( var mi = 0, miLen = materials.length; mi < miLen ; mi++ ) {
+				for ( var mi = 0, miLen = materials.length; mi < miLen; mi ++ ) {
 
 					var sourceMaterial = materials[ mi ];
 					var material = undefined;
@@ -660,7 +671,7 @@ THREE.OBJLoader = ( function () {
 
 					material.flatShading = sourceMaterial.smooth ? false : true;
 
-					createdMaterials.push(material);
+					createdMaterials.push( material );
 
 				}
 
@@ -670,7 +681,7 @@ THREE.OBJLoader = ( function () {
 
 				if ( createdMaterials.length > 1 ) {
 
-					for ( var mi = 0, miLen = materials.length; mi < miLen ; mi++ ) {
+					for ( var mi = 0, miLen = materials.length; mi < miLen; mi ++ ) {
 
 						var sourceMaterial = materials[ mi ];
 						buffergeometry.addGroup( sourceMaterial.groupStart, sourceMaterial.groupCount, mi );
@@ -682,6 +693,7 @@ THREE.OBJLoader = ( function () {
 				} else {
 
 					mesh = ( ! isLine ? new THREE.Mesh( buffergeometry, createdMaterials[ 0 ] ) : new THREE.LineSegments( buffergeometry, createdMaterials[ 0 ] ) );
+
 				}
 
 				mesh.name = object.name;

+ 9 - 17
examples/webgl_loader_obj.html

@@ -61,12 +61,12 @@
 
 				scene = new THREE.Scene();
 
-				var ambient = new THREE.AmbientLight( 0x101030 );
-				scene.add( ambient );
+				var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
+				scene.add( ambientLight );
 
-				var directionalLight = new THREE.DirectionalLight( 0xffeedd );
-				directionalLight.position.set( 0, 0, 1 );
-				scene.add( directionalLight );
+				var pointLight = new THREE.PointLight( 0xffffff, 0.8 );
+				camera.add( pointLight );
+				scene.add( camera );
 
 				// texture
 
@@ -77,7 +77,10 @@
 
 				};
 
-				var texture = new THREE.Texture();
+				var textureLoader = new THREE.TextureLoader( manager );
+				var texture = textureLoader.load( 'textures/UV_Grid_Sm.jpg' );
+
+				// model
 
 				var onProgress = function ( xhr ) {
 					if ( xhr.lengthComputable ) {
@@ -89,17 +92,6 @@
 				var onError = function ( xhr ) {
 				};
 
-
-				var loader = new THREE.ImageLoader( manager );
-				loader.load( 'textures/UV_Grid_Sm.jpg', function ( image ) {
-
-					texture.image = image;
-					texture.needsUpdate = true;
-
-				} );
-
-				// model
-
 				var loader = new THREE.OBJLoader( manager );
 				loader.load( 'obj/male02/male02.obj', function ( object ) {
 

+ 5 - 5
examples/webgl_loader_obj_mtl.html

@@ -67,12 +67,12 @@
 
 				scene = new THREE.Scene();
 
-				var ambient = new THREE.AmbientLight( 0x444444 );
-				scene.add( ambient );
+				var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
+				scene.add( ambientLight );
 
-				var directionalLight = new THREE.DirectionalLight( 0xffeedd );
-				directionalLight.position.set( 0, 0, 1 ).normalize();
-				scene.add( directionalLight );
+				var pointLight = new THREE.PointLight( 0xffffff, 0.8 );
+				camera.add( pointLight );
+				scene.add( camera );
 
 				// model