Browse Source

OBJLoader: Added support for vertex colors.

Mr.doob 7 years ago
parent
commit
0eeee30112
1 changed files with 33 additions and 0 deletions
  1. 33 0
      examples/js/loaders/OBJLoader.js

+ 33 - 0
examples/js/loaders/OBJLoader.js

@@ -19,6 +19,7 @@ THREE.OBJLoader = ( function () {
 
 
 			vertices : [],
 			vertices : [],
 			normals  : [],
 			normals  : [],
+			colors   : [],
 			uvs      : [],
 			uvs      : [],
 
 
 			materialLibraries : [],
 			materialLibraries : [],
@@ -50,6 +51,7 @@ THREE.OBJLoader = ( function () {
 					geometry : {
 					geometry : {
 						vertices : [],
 						vertices : [],
 						normals  : [],
 						normals  : [],
+						colors   : [],
 						uvs      : []
 						uvs      : []
 					},
 					},
 					materials : [],
 					materials : [],
@@ -226,6 +228,17 @@ THREE.OBJLoader = ( function () {
 
 
 			},
 			},
 
 
+			addColor: function ( a, b, c ) {
+
+				var src = this.colors;
+				var dst = this.object.geometry.colors;
+
+				dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] );
+				dst.push( src[ b + 0 ], src[ b + 1 ], src[ b + 2 ] );
+				dst.push( src[ c + 0 ], src[ c + 1 ], src[ c + 2 ] );
+
+			},
+
 			addUV: function ( a, b, c ) {
 			addUV: function ( a, b, c ) {
 
 
 				var src = this.uvs;
 				var src = this.uvs;
@@ -281,6 +294,12 @@ THREE.OBJLoader = ( function () {
 
 
 				}
 				}
 
 
+				if ( this.colors.length > 0 ) {
+
+					this.addColor( ia, ib, ic );
+
+				}
+
 			},
 			},
 
 
 			addLineGeometry: function ( vertices, uvs ) {
 			addLineGeometry: function ( vertices, uvs ) {
@@ -409,6 +428,14 @@ THREE.OBJLoader = ( function () {
 								parseFloat( data[ 2 ] ),
 								parseFloat( data[ 2 ] ),
 								parseFloat( data[ 3 ] )
 								parseFloat( data[ 3 ] )
 							);
 							);
+							if ( data.length === 8 ) {
+								state.colors.push(
+									parseFloat( data[ 4 ] ),
+									parseFloat( data[ 5 ] ),
+									parseFloat( data[ 6 ] )
+
+								);
+							}
 							break;
 							break;
 						case 'vn':
 						case 'vn':
 							state.normals.push(
 							state.normals.push(
@@ -588,6 +615,12 @@ THREE.OBJLoader = ( function () {
 
 
 				}
 				}
 
 
+				if ( geometry.colors.length > 0 ) {
+
+					buffergeometry.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( geometry.colors ), 3 ) );
+
+				}
+
 				if ( geometry.uvs.length > 0 ) {
 				if ( geometry.uvs.length > 0 ) {
 
 
 					buffergeometry.addAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( geometry.uvs ), 2 ) );
 					buffergeometry.addAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( geometry.uvs ), 2 ) );