Przeglądaj źródła

Added UV support to OBJLoader.

Mr.doob 13 lat temu
rodzic
commit
b6dcf718c7
2 zmienionych plików z 61 dodań i 9 usunięć
  1. 53 4
      examples/js/loaders/OBJLoader.js
  2. 8 5
      examples/webgl_loader_obj.html

+ 53 - 4
examples/js/loaders/OBJLoader.js

@@ -43,6 +43,12 @@ THREE.OBJLoader.prototype.parse = function ( data ) {
 
 	}
 
+	function uv( u, v ) {
+
+		return new THREE.UV( u, 1.0 - v );
+
+	}
+
 	function face3( a, b, c, normals ) {
 
 		return new THREE.Face3( a, b, c, normals );
@@ -55,9 +61,11 @@ THREE.OBJLoader.prototype.parse = function ( data ) {
 
 	}
 
-	var objects = [];
+	var group = new THREE.Object3D();
+
 	var vertices = [];
 	var normals = [];
+	var uvs = [];
 
 	var pattern, result;
 
@@ -84,7 +92,7 @@ THREE.OBJLoader.prototype.parse = function ( data ) {
 
 	while ( ( result = pattern.exec( data ) ) != null ) {
 
-		// ["v 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
+		// ["vn 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
 
 		normals.push( vector(
 			parseFloat( result[ 1 ] ),
@@ -94,6 +102,21 @@ THREE.OBJLoader.prototype.parse = function ( data ) {
 
 	}
 
+	// vt float float
+
+	pattern = /vt( [\d|\.|\+|\-|e]+)( [\d|\.|\+|\-|e]+)/g;
+
+	while ( ( result = pattern.exec( data ) ) != null ) {
+
+		// ["vt 0.1 0.2", "0.1", "0.2"]
+
+		uvs.push( uv(
+			parseFloat( result[ 1 ] ),
+			parseFloat( result[ 2 ] )
+		) );
+
+	}
+
 	var data = data.split( '\no ');
 
 	for ( var i = 0, l = data.length; i < l; i ++ ) {
@@ -149,6 +172,12 @@ THREE.OBJLoader.prototype.parse = function ( data ) {
 					parseInt( result[ 8 ] ) - 1
 				) );
 
+				geometry.faceVertexUvs[ 0 ].push( [
+					uvs[ parseInt( result[ 3 ] ) - 1 ],
+					uvs[ parseInt( result[ 6 ] ) - 1 ],
+					uvs[ parseInt( result[ 9 ] ) - 1 ]
+				] );
+
 			} else {
 
 				geometry.faces.push( face4(
@@ -158,6 +187,13 @@ THREE.OBJLoader.prototype.parse = function ( data ) {
 					parseInt( result[ 11 ] ) - 1
 				) );
 
+				geometry.faceVertexUvs[ 0 ].push( [
+					uvs[ parseInt( result[ 3 ] ) - 1 ],
+					uvs[ parseInt( result[ 6 ] ) - 1 ],
+					uvs[ parseInt( result[ 9 ] ) - 1 ],
+					uvs[ parseInt( result[ 12 ] ) - 1 ]
+				] );
+
 			}
 
 		}
@@ -183,6 +219,12 @@ THREE.OBJLoader.prototype.parse = function ( data ) {
 					]
 				) );
 
+				geometry.faceVertexUvs[ 0 ].push( [
+					uvs[ parseInt( result[ 3 ] ) - 1 ],
+					uvs[ parseInt( result[ 7 ] ) - 1 ],
+					uvs[ parseInt( result[ 11 ] ) - 1 ]
+				] );
+
 			} else {
 
 				geometry.faces.push( face4(
@@ -198,6 +240,13 @@ THREE.OBJLoader.prototype.parse = function ( data ) {
 					]
 				) );
 
+				geometry.faceVertexUvs[ 0 ].push( [
+					uvs[ parseInt( result[ 3 ] ) - 1 ],
+					uvs[ parseInt( result[ 7 ] ) - 1 ],
+					uvs[ parseInt( result[ 11 ] ) - 1 ],
+					uvs[ parseInt( result[ 15 ] ) - 1 ]
+				] );
+
 			}
 
 
@@ -245,10 +294,10 @@ THREE.OBJLoader.prototype.parse = function ( data ) {
 
 		geometry.computeCentroids();
 
-		objects.push( new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) ) );
+		group.add( new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) ) );
 
 	}
 
-	return objects;
+	return group;
 
 }

+ 8 - 5
examples/webgl_loader_obj.html

@@ -70,17 +70,20 @@
 				directionalLight.position.set( 0, 0, 1 ).normalize();
 				scene.add( directionalLight );
 
+				var texture = THREE.ImageUtils.loadTexture( 'textures/ash_uvgrid01.jpg' );
+
 				var loader = new THREE.OBJLoader();
-				loader.load( "obj/male02/male02.obj", function ( objects ) {
+				loader.load( "obj/male02/male02.obj", function ( object ) {
 
-					for ( var i = 0; i < objects.length; i ++ ) {
+					for ( var i = 0, l = object.children.length; i < l; i ++ ) {
 
-						var object = objects[ i ];
-						object.position.y = - 80;
-						scene.add( object );
+						object.children[ i ].material.map = texture;
 
 					}
 
+					object.position.y = - 80;
+					scene.add( object );
+
 				} );
 
 				// RENDERER