OBJLoader.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.OBJLoader = function ( manager ) {
  5. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  6. this.materials = null;
  7. this.regexp = {
  8. // v float float float
  9. vertex_pattern : /^v\s+([\d|\.|\+|\-|e|E]+)\s+([\d|\.|\+|\-|e|E]+)\s+([\d|\.|\+|\-|e|E]+)/,
  10. // vn float float float
  11. normal_pattern : /^vn\s+([\d|\.|\+|\-|e|E]+)\s+([\d|\.|\+|\-|e|E]+)\s+([\d|\.|\+|\-|e|E]+)/,
  12. // vt float float
  13. uv_pattern : /^vt\s+([\d|\.|\+|\-|e|E]+)\s+([\d|\.|\+|\-|e|E]+)/,
  14. // f vertex vertex vertex
  15. face_vertex : /^f\s+(-?\d+)\s+(-?\d+)\s+(-?\d+)(?:\s+(-?\d+))?/,
  16. // f vertex/uv vertex/uv vertex/uv
  17. face_vertex_uv : /^f\s+(-?\d+)\/(-?\d+)\s+(-?\d+)\/(-?\d+)\s+(-?\d+)\/(-?\d+)(?:\s+(-?\d+)\/(-?\d+))?/,
  18. // f vertex/uv/normal vertex/uv/normal vertex/uv/normal
  19. face_vertex_uv_normal : /^f\s+(-?\d+)\/(-?\d+)\/(-?\d+)\s+(-?\d+)\/(-?\d+)\/(-?\d+)\s+(-?\d+)\/(-?\d+)\/(-?\d+)(?:\s+(-?\d+)\/(-?\d+)\/(-?\d+))?/,
  20. // f vertex//normal vertex//normal vertex//normal
  21. face_vertex_normal : /^f\s+(-?\d+)\/\/(-?\d+)\s+(-?\d+)\/\/(-?\d+)\s+(-?\d+)\/\/(-?\d+)(?:\s+(-?\d+)\/\/(-?\d+))?/,
  22. // o object_name | g group_name
  23. object_pattern : /^[og]\s*(.+)?/,
  24. // s boolean
  25. smoothing_pattern : /^s\s+(\d+|on|off)/,
  26. // mtllib file_reference
  27. material_library_pattern : /^mtllib /,
  28. // usemtl material_name
  29. material_use_pattern : /^usemtl /
  30. };
  31. };
  32. THREE.OBJLoader.prototype = {
  33. constructor: THREE.OBJLoader,
  34. load: function ( url, onLoad, onProgress, onError ) {
  35. var scope = this;
  36. var loader = new THREE.XHRLoader( scope.manager );
  37. loader.setPath( this.path );
  38. loader.load( url, function ( text ) {
  39. onLoad( scope.parse( text ) );
  40. }, onProgress, onError );
  41. },
  42. setPath: function ( value ) {
  43. this.path = value;
  44. },
  45. setMaterials: function ( materials ) {
  46. this.materials = materials;
  47. },
  48. _createParserState : function()
  49. {
  50. var state = {
  51. objects : [],
  52. object : {},
  53. vertices : [],
  54. normals : [],
  55. uvs : [],
  56. materialLibraries : [],
  57. startObject : function(name, fromDeclaration)
  58. {
  59. // If the current object (initial from reset) is not from a g/o declaration in the parsed
  60. // file. We need to use it for the first parsed g/o to keep things in sync.
  61. if ( this.object && this.object.fromDeclaration === false ) {
  62. this.object.name = name;
  63. this.object.fromDeclaration = (fromDeclaration !== false);
  64. return;
  65. }
  66. this.object = {
  67. name : name || '',
  68. geometry : {
  69. vertices : [],
  70. normals : [],
  71. uvs : []
  72. },
  73. material : {
  74. name : '',
  75. smooth : true
  76. },
  77. fromDeclaration : (fromDeclaration !== false)
  78. };
  79. this.objects.push(this.object);
  80. },
  81. parseVertexIndex : function( value ) {
  82. var index = parseInt( value, 10 );
  83. return ( index >= 0 ? index - 1 : index + this.vertices.length / 3 ) * 3;
  84. },
  85. parseNormalIndex : function( value ) {
  86. var index = parseInt( value, 10 );
  87. return ( index >= 0 ? index - 1 : index + this.normals.length / 3 ) * 3;
  88. },
  89. parseUVIndex : function( value ) {
  90. var index = parseInt( value, 10 );
  91. return ( index >= 0 ? index - 1 : index + this.uvs.length / 2 ) * 2;
  92. },
  93. addVertex : function( a, b, c ) {
  94. var src = this.vertices;
  95. this.object.geometry.vertices.push(src[ a ]);
  96. this.object.geometry.vertices.push(src[ a + 1 ]);
  97. this.object.geometry.vertices.push(src[ a + 2 ]);
  98. this.object.geometry.vertices.push(src[ b ]);
  99. this.object.geometry.vertices.push(src[ b + 1 ]);
  100. this.object.geometry.vertices.push(src[ b + 2 ]);
  101. this.object.geometry.vertices.push(src[ c ]);
  102. this.object.geometry.vertices.push(src[ c + 1 ]);
  103. this.object.geometry.vertices.push(src[ c + 2 ]);
  104. },
  105. addNormal : function( a, b, c ) {
  106. var src = this.normals;
  107. this.object.geometry.normals.push(src[ a ]);
  108. this.object.geometry.normals.push(src[ a + 1 ]);
  109. this.object.geometry.normals.push(src[ a + 2 ]);
  110. this.object.geometry.normals.push(src[ b ]);
  111. this.object.geometry.normals.push(src[ b + 1 ]);
  112. this.object.geometry.normals.push(src[ b + 2 ]);
  113. this.object.geometry.normals.push(src[ c ]);
  114. this.object.geometry.normals.push(src[ c + 1 ]);
  115. this.object.geometry.normals.push(src[ c + 2 ]);
  116. },
  117. addUV : function( a, b, c ) {
  118. var src = this.uvs;
  119. this.object.geometry.uvs.push(src[ a ]);
  120. this.object.geometry.uvs.push(src[ a + 1 ]);
  121. this.object.geometry.uvs.push(src[ b ]);
  122. this.object.geometry.uvs.push(src[ b + 1 ]);
  123. this.object.geometry.uvs.push(src[ c ]);
  124. this.object.geometry.uvs.push(src[ c + 1 ]);
  125. },
  126. addFace : function( a, b, c, d, ua, ub, uc, ud, na, nb, nc, nd ) {
  127. var ia = this.parseVertexIndex( a );
  128. var ib = this.parseVertexIndex( b );
  129. var ic = this.parseVertexIndex( c );
  130. var id;
  131. if ( d === undefined ) {
  132. this.addVertex( ia, ib, ic );
  133. } else {
  134. id = this.parseVertexIndex( d );
  135. this.addVertex( ia, ib, id );
  136. this.addVertex( ib, ic, id );
  137. }
  138. if ( ua !== undefined ) {
  139. ia = this.parseUVIndex( ua );
  140. ib = this.parseUVIndex( ub );
  141. ic = this.parseUVIndex( uc );
  142. if ( d === undefined ) {
  143. this.addUV( ia, ib, ic );
  144. } else {
  145. id = this.parseUVIndex( ud );
  146. this.addUV( ia, ib, id );
  147. this.addUV( ib, ic, id );
  148. }
  149. }
  150. if ( na !== undefined ) {
  151. ia = this.parseNormalIndex( na );
  152. ib = this.parseNormalIndex( nb );
  153. ic = this.parseNormalIndex( nc );
  154. if ( d === undefined ) {
  155. this.addNormal( ia, ib, ic );
  156. } else {
  157. id = this.parseNormalIndex( nd );
  158. this.addNormal( ia, ib, id );
  159. this.addNormal( ib, ic, id );
  160. }
  161. }
  162. }
  163. };
  164. state.startObject('', false);
  165. return state;
  166. },
  167. parse: function ( text ) {
  168. console.time( 'OBJLoader' );
  169. var state = this._createParserState();
  170. if ( text.indexOf('\r\n') !== -1 ) {
  171. // This is faster than String.split with regex that splits on both
  172. text = text.replace('\r\n', '\n');
  173. }
  174. var lines = text.split( '\n' );
  175. // Faster to just trim left side of the line. Use if available.
  176. var trimLeft = (typeof ''.trimLeft === 'function');
  177. for ( var i = 0; i < lines.length; i ++ ) {
  178. var line = lines[ i ];
  179. if (trimLeft)
  180. line = line.trimLeft();
  181. else
  182. line = line.trim();
  183. var lineLength = line.length;
  184. if ( lineLength === 0 ) {
  185. continue;
  186. }
  187. var lineFirstChar = line.charAt( 0 );
  188. if ( lineFirstChar === '#' ) {
  189. // @todo invoke passed in handler if any
  190. continue;
  191. }
  192. var result = [];
  193. if ( lineFirstChar === 'v' ) {
  194. var lineSecondChar = line.charAt( 1 );
  195. if ( lineSecondChar === ' ' && ( result = this.regexp.vertex_pattern.exec( line ) ) !== null ) {
  196. // 0 1 2 3
  197. // ["v 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  198. state.vertices.push(
  199. parseFloat( result[ 1 ] ),
  200. parseFloat( result[ 2 ] ),
  201. parseFloat( result[ 3 ] )
  202. );
  203. } else if ( lineSecondChar === 'n' && ( result = this.regexp.normal_pattern.exec( line ) ) !== null ) {
  204. // 0 1 2 3
  205. // ["vn 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
  206. state.normals.push(
  207. parseFloat( result[ 1 ] ),
  208. parseFloat( result[ 2 ] ),
  209. parseFloat( result[ 3 ] )
  210. );
  211. } else if ( lineSecondChar === 't' && ( result = this.regexp.uv_pattern.exec( line ) ) !== null ) {
  212. // 0 1 2
  213. // ["vt 0.1 0.2", "0.1", "0.2"]
  214. state.uvs.push(
  215. parseFloat( result[ 1 ] ),
  216. parseFloat( result[ 2 ] )
  217. );
  218. } else {
  219. throw new Error( "Unexpected vertex/normal/uv line: '" + line + "'");
  220. }
  221. } else if ( lineFirstChar === "f" ) {
  222. if ( ( result = this.regexp.face_vertex_uv_normal.exec( line ) ) !== null ) {
  223. // f vertex/uv/normal vertex/uv/normal vertex/uv/normal
  224. // 0 1 2 3 4 5 6 7 8 9 10 11 12
  225. // ["f 1/1/1 2/2/2 3/3/3", "1", "1", "1", "2", "2", "2", "3", "3", "3", undefined, undefined, undefined]
  226. state.addFace(
  227. result[ 1 ], result[ 4 ], result[ 7 ], result[ 10 ],
  228. result[ 2 ], result[ 5 ], result[ 8 ], result[ 11 ],
  229. result[ 3 ], result[ 6 ], result[ 9 ], result[ 12 ]
  230. );
  231. } else if ( ( result = this.regexp.face_vertex_uv.exec( line ) ) !== null ) {
  232. // f vertex/uv vertex/uv vertex/uv
  233. // 0 1 2 3 4 5 6 7 8
  234. // ["f 1/1 2/2 3/3", "1", "1", "2", "2", "3", "3", undefined, undefined]
  235. state.addFace(
  236. result[ 1 ], result[ 3 ], result[ 5 ], result[ 7 ],
  237. result[ 2 ], result[ 4 ], result[ 6 ], result[ 8 ]
  238. );
  239. } else if ( ( result = this.regexp.face_vertex_normal.exec( line ) ) !== null ) {
  240. // f vertex//normal vertex//normal vertex//normal
  241. // 0 1 2 3 4 5 6 7 8
  242. // ["f 1//1 2//2 3//3", "1", "1", "2", "2", "3", "3", undefined, undefined]
  243. state.addFace(
  244. result[ 1 ], result[ 3 ], result[ 5 ], result[ 7 ],
  245. undefined, undefined, undefined, undefined,
  246. result[ 2 ], result[ 4 ], result[ 6 ], result[ 8 ]
  247. );
  248. } else if ( ( result = this.regexp.face_vertex.exec( line ) ) !== null ) {
  249. // f vertex vertex vertex
  250. // 0 1 2 3 4
  251. // ["f 1 2 3", "1", "2", "3", undefined]
  252. state.addFace(
  253. result[ 1 ], result[ 2 ], result[ 3 ], result[ 4 ]
  254. );
  255. } else {
  256. throw new Error( "Unexpected face line: '" + line + "'");
  257. }
  258. } else if ( ( result = this.regexp.object_pattern.exec( line ) ) !== null ) {
  259. // o object_name
  260. // or
  261. // g group_name
  262. var name = result[ 0 ].substr( 1 ).trim();
  263. state.startObject(name);
  264. } else if ( this.regexp.material_use_pattern.test( line ) ) {
  265. // material
  266. state.object.material.name = line.substring( 7 ).trim();
  267. } else if ( this.regexp.material_library_pattern.test( line ) ) {
  268. // mtl file
  269. state.materialLibraries.push( line.substring( 7 ).trim() );
  270. } else if ( ( result = this.regexp.smoothing_pattern.exec( line ) ) !== null ) {
  271. // smooth shading
  272. var value = result[ 1 ].trim().toLowerCase();
  273. state.object.material.smooth = ( value === '1' || value === 'on' );
  274. } else {
  275. // Handle null terminated files without exception
  276. if (line === "\0")
  277. continue;
  278. throw new Error( "Unexpected line: '" + line + "'");
  279. }
  280. }
  281. var container = new THREE.Group();
  282. container.materialLibraries = [].concat(state.materialLibraries);
  283. for ( var i = 0, l = state.objects.length; i < l; i ++ ) {
  284. var object = state.objects[ i ];
  285. var geometry = object.geometry;
  286. var buffergeometry = new THREE.BufferGeometry();
  287. buffergeometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( geometry.vertices ), 3 ) );
  288. if ( geometry.normals.length > 0 ) {
  289. buffergeometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( geometry.normals ), 3 ) );
  290. } else {
  291. buffergeometry.computeVertexNormals();
  292. }
  293. if ( geometry.uvs.length > 0 ) {
  294. buffergeometry.addAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( geometry.uvs ), 2 ) );
  295. }
  296. var material;
  297. if ( this.materials !== null ) {
  298. material = this.materials.create( object.material.name );
  299. }
  300. if ( !material ) {
  301. material = new THREE.MeshPhongMaterial();
  302. material.name = object.material.name;
  303. }
  304. material.shading = object.material.smooth ? THREE.SmoothShading : THREE.FlatShading;
  305. var mesh = new THREE.Mesh( buffergeometry, material );
  306. mesh.name = object.name;
  307. container.add( mesh );
  308. }
  309. console.timeEnd( 'OBJLoader' );
  310. return container;
  311. }
  312. };