OBJLoader.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. import {
  2. BufferGeometry,
  3. FileLoader,
  4. Float32BufferAttribute,
  5. Group,
  6. LineBasicMaterial,
  7. LineSegments,
  8. Loader,
  9. Material,
  10. Mesh,
  11. MeshPhongMaterial,
  12. Points,
  13. PointsMaterial,
  14. Vector3
  15. } from "../../../build/three.module.js";
  16. var OBJLoader = ( function () {
  17. // o object_name | g group_name
  18. var object_pattern = /^[og]\s*(.+)?/;
  19. // mtllib file_reference
  20. var material_library_pattern = /^mtllib /;
  21. // usemtl material_name
  22. var material_use_pattern = /^usemtl /;
  23. // usemap map_name
  24. var map_use_pattern = /^usemap /;
  25. var vA = new Vector3();
  26. var vB = new Vector3();
  27. var vC = new Vector3();
  28. var ab = new Vector3();
  29. var cb = new Vector3();
  30. function ParserState() {
  31. var state = {
  32. objects: [],
  33. object: {},
  34. vertices: [],
  35. normals: [],
  36. colors: [],
  37. uvs: [],
  38. materials: {},
  39. materialLibraries: [],
  40. startObject: function ( name, fromDeclaration ) {
  41. // If the current object (initial from reset) is not from a g/o declaration in the parsed
  42. // file. We need to use it for the first parsed g/o to keep things in sync.
  43. if ( this.object && this.object.fromDeclaration === false ) {
  44. this.object.name = name;
  45. this.object.fromDeclaration = ( fromDeclaration !== false );
  46. return;
  47. }
  48. var previousMaterial = ( this.object && typeof this.object.currentMaterial === 'function' ? this.object.currentMaterial() : undefined );
  49. if ( this.object && typeof this.object._finalize === 'function' ) {
  50. this.object._finalize( true );
  51. }
  52. this.object = {
  53. name: name || '',
  54. fromDeclaration: ( fromDeclaration !== false ),
  55. geometry: {
  56. vertices: [],
  57. normals: [],
  58. colors: [],
  59. uvs: [],
  60. hasUVIndices: false
  61. },
  62. materials: [],
  63. smooth: true,
  64. startMaterial: function ( name, libraries ) {
  65. var previous = this._finalize( false );
  66. // New usemtl declaration overwrites an inherited material, except if faces were declared
  67. // after the material, then it must be preserved for proper MultiMaterial continuation.
  68. if ( previous && ( previous.inherited || previous.groupCount <= 0 ) ) {
  69. this.materials.splice( previous.index, 1 );
  70. }
  71. var material = {
  72. index: this.materials.length,
  73. name: name || '',
  74. mtllib: ( Array.isArray( libraries ) && libraries.length > 0 ? libraries[ libraries.length - 1 ] : '' ),
  75. smooth: ( previous !== undefined ? previous.smooth : this.smooth ),
  76. groupStart: ( previous !== undefined ? previous.groupEnd : 0 ),
  77. groupEnd: - 1,
  78. groupCount: - 1,
  79. inherited: false,
  80. clone: function ( index ) {
  81. var cloned = {
  82. index: ( typeof index === 'number' ? index : this.index ),
  83. name: this.name,
  84. mtllib: this.mtllib,
  85. smooth: this.smooth,
  86. groupStart: 0,
  87. groupEnd: - 1,
  88. groupCount: - 1,
  89. inherited: false
  90. };
  91. cloned.clone = this.clone.bind( cloned );
  92. return cloned;
  93. }
  94. };
  95. this.materials.push( material );
  96. return material;
  97. },
  98. currentMaterial: function () {
  99. if ( this.materials.length > 0 ) {
  100. return this.materials[ this.materials.length - 1 ];
  101. }
  102. return undefined;
  103. },
  104. _finalize: function ( end ) {
  105. var lastMultiMaterial = this.currentMaterial();
  106. if ( lastMultiMaterial && lastMultiMaterial.groupEnd === - 1 ) {
  107. lastMultiMaterial.groupEnd = this.geometry.vertices.length / 3;
  108. lastMultiMaterial.groupCount = lastMultiMaterial.groupEnd - lastMultiMaterial.groupStart;
  109. lastMultiMaterial.inherited = false;
  110. }
  111. // Ignore objects tail materials if no face declarations followed them before a new o/g started.
  112. if ( end && this.materials.length > 1 ) {
  113. for ( var mi = this.materials.length - 1; mi >= 0; mi -- ) {
  114. if ( this.materials[ mi ].groupCount <= 0 ) {
  115. this.materials.splice( mi, 1 );
  116. }
  117. }
  118. }
  119. // Guarantee at least one empty material, this makes the creation later more straight forward.
  120. if ( end && this.materials.length === 0 ) {
  121. this.materials.push( {
  122. name: '',
  123. smooth: this.smooth
  124. } );
  125. }
  126. return lastMultiMaterial;
  127. }
  128. };
  129. // Inherit previous objects material.
  130. // Spec tells us that a declared material must be set to all objects until a new material is declared.
  131. // If a usemtl declaration is encountered while this new object is being parsed, it will
  132. // overwrite the inherited material. Exception being that there was already face declarations
  133. // to the inherited material, then it will be preserved for proper MultiMaterial continuation.
  134. if ( previousMaterial && previousMaterial.name && typeof previousMaterial.clone === 'function' ) {
  135. var declared = previousMaterial.clone( 0 );
  136. declared.inherited = true;
  137. this.object.materials.push( declared );
  138. }
  139. this.objects.push( this.object );
  140. },
  141. finalize: function () {
  142. if ( this.object && typeof this.object._finalize === 'function' ) {
  143. this.object._finalize( true );
  144. }
  145. },
  146. parseVertexIndex: function ( value, len ) {
  147. var index = parseInt( value, 10 );
  148. return ( index >= 0 ? index - 1 : index + len / 3 ) * 3;
  149. },
  150. parseNormalIndex: function ( value, len ) {
  151. var index = parseInt( value, 10 );
  152. return ( index >= 0 ? index - 1 : index + len / 3 ) * 3;
  153. },
  154. parseUVIndex: function ( value, len ) {
  155. var index = parseInt( value, 10 );
  156. return ( index >= 0 ? index - 1 : index + len / 2 ) * 2;
  157. },
  158. addVertex: function ( a, b, c ) {
  159. var src = this.vertices;
  160. var dst = this.object.geometry.vertices;
  161. dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] );
  162. dst.push( src[ b + 0 ], src[ b + 1 ], src[ b + 2 ] );
  163. dst.push( src[ c + 0 ], src[ c + 1 ], src[ c + 2 ] );
  164. },
  165. addVertexPoint: function ( a ) {
  166. var src = this.vertices;
  167. var dst = this.object.geometry.vertices;
  168. dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] );
  169. },
  170. addVertexLine: function ( a ) {
  171. var src = this.vertices;
  172. var dst = this.object.geometry.vertices;
  173. dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] );
  174. },
  175. addNormal: function ( a, b, c ) {
  176. var src = this.normals;
  177. var dst = this.object.geometry.normals;
  178. dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] );
  179. dst.push( src[ b + 0 ], src[ b + 1 ], src[ b + 2 ] );
  180. dst.push( src[ c + 0 ], src[ c + 1 ], src[ c + 2 ] );
  181. },
  182. addFaceNormal: function ( a, b, c ) {
  183. var src = this.vertices;
  184. var dst = this.object.geometry.normals;
  185. vA.fromArray( src, a );
  186. vB.fromArray( src, b );
  187. vC.fromArray( src, c );
  188. cb.subVectors( vC, vB );
  189. ab.subVectors( vA, vB );
  190. cb.cross( ab );
  191. cb.normalize();
  192. dst.push( cb.x, cb.y, cb.z );
  193. dst.push( cb.x, cb.y, cb.z );
  194. dst.push( cb.x, cb.y, cb.z );
  195. },
  196. addColor: function ( a, b, c ) {
  197. var src = this.colors;
  198. var dst = this.object.geometry.colors;
  199. if ( src[ a ] !== undefined ) dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] );
  200. if ( src[ b ] !== undefined ) dst.push( src[ b + 0 ], src[ b + 1 ], src[ b + 2 ] );
  201. if ( src[ c ] !== undefined ) dst.push( src[ c + 0 ], src[ c + 1 ], src[ c + 2 ] );
  202. },
  203. addUV: function ( a, b, c ) {
  204. var src = this.uvs;
  205. var dst = this.object.geometry.uvs;
  206. dst.push( src[ a + 0 ], src[ a + 1 ] );
  207. dst.push( src[ b + 0 ], src[ b + 1 ] );
  208. dst.push( src[ c + 0 ], src[ c + 1 ] );
  209. },
  210. addDefaultUV: function () {
  211. var dst = this.object.geometry.uvs;
  212. dst.push( 0, 0 );
  213. dst.push( 0, 0 );
  214. dst.push( 0, 0 );
  215. },
  216. addUVLine: function ( a ) {
  217. var src = this.uvs;
  218. var dst = this.object.geometry.uvs;
  219. dst.push( src[ a + 0 ], src[ a + 1 ] );
  220. },
  221. addFace: function ( a, b, c, ua, ub, uc, na, nb, nc ) {
  222. var vLen = this.vertices.length;
  223. var ia = this.parseVertexIndex( a, vLen );
  224. var ib = this.parseVertexIndex( b, vLen );
  225. var ic = this.parseVertexIndex( c, vLen );
  226. this.addVertex( ia, ib, ic );
  227. this.addColor( ia, ib, ic );
  228. // normals
  229. if ( na !== undefined && na !== '' ) {
  230. var nLen = this.normals.length;
  231. ia = this.parseNormalIndex( na, nLen );
  232. ib = this.parseNormalIndex( nb, nLen );
  233. ic = this.parseNormalIndex( nc, nLen );
  234. this.addNormal( ia, ib, ic );
  235. } else {
  236. this.addFaceNormal( ia, ib, ic );
  237. }
  238. // uvs
  239. if ( ua !== undefined && ua !== '' ) {
  240. var uvLen = this.uvs.length;
  241. ia = this.parseUVIndex( ua, uvLen );
  242. ib = this.parseUVIndex( ub, uvLen );
  243. ic = this.parseUVIndex( uc, uvLen );
  244. this.addUV( ia, ib, ic );
  245. this.object.geometry.hasUVIndices = true;
  246. } else {
  247. // add placeholder values (for inconsistent face definitions)
  248. this.addDefaultUV();
  249. }
  250. },
  251. addPointGeometry: function ( vertices ) {
  252. this.object.geometry.type = 'Points';
  253. var vLen = this.vertices.length;
  254. for ( var vi = 0, l = vertices.length; vi < l; vi ++ ) {
  255. this.addVertexPoint( this.parseVertexIndex( vertices[ vi ], vLen ) );
  256. }
  257. },
  258. addLineGeometry: function ( vertices, uvs ) {
  259. this.object.geometry.type = 'Line';
  260. var vLen = this.vertices.length;
  261. var uvLen = this.uvs.length;
  262. for ( var vi = 0, l = vertices.length; vi < l; vi ++ ) {
  263. this.addVertexLine( this.parseVertexIndex( vertices[ vi ], vLen ) );
  264. }
  265. for ( var uvi = 0, l = uvs.length; uvi < l; uvi ++ ) {
  266. this.addUVLine( this.parseUVIndex( uvs[ uvi ], uvLen ) );
  267. }
  268. }
  269. };
  270. state.startObject( '', false );
  271. return state;
  272. }
  273. //
  274. function OBJLoader( manager ) {
  275. Loader.call( this, manager );
  276. this.materials = null;
  277. }
  278. OBJLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
  279. constructor: OBJLoader,
  280. load: function ( url, onLoad, onProgress, onError ) {
  281. var scope = this;
  282. var loader = new FileLoader( this.manager );
  283. loader.setPath( this.path );
  284. loader.setRequestHeader( this.requestHeader );
  285. loader.setWithCredentials( this.withCredentials );
  286. loader.load( url, function ( text ) {
  287. try {
  288. onLoad( scope.parse( text ) );
  289. } catch ( e ) {
  290. if ( onError ) {
  291. onError( e );
  292. } else {
  293. console.error( e );
  294. }
  295. scope.manager.itemError( url );
  296. }
  297. }, onProgress, onError );
  298. },
  299. setMaterials: function ( materials ) {
  300. this.materials = materials;
  301. return this;
  302. },
  303. parse: function ( text ) {
  304. var state = new ParserState();
  305. if ( text.indexOf( '\r\n' ) !== - 1 ) {
  306. // This is faster than String.split with regex that splits on both
  307. text = text.replace( /\r\n/g, '\n' );
  308. }
  309. if ( text.indexOf( '\\\n' ) !== - 1 ) {
  310. // join lines separated by a line continuation character (\)
  311. text = text.replace( /\\\n/g, '' );
  312. }
  313. var lines = text.split( '\n' );
  314. var line = '', lineFirstChar = '';
  315. var lineLength = 0;
  316. var result = [];
  317. // Faster to just trim left side of the line. Use if available.
  318. var trimLeft = ( typeof ''.trimLeft === 'function' );
  319. for ( var i = 0, l = lines.length; i < l; i ++ ) {
  320. line = lines[ i ];
  321. line = trimLeft ? line.trimLeft() : line.trim();
  322. lineLength = line.length;
  323. if ( lineLength === 0 ) continue;
  324. lineFirstChar = line.charAt( 0 );
  325. // @todo invoke passed in handler if any
  326. if ( lineFirstChar === '#' ) continue;
  327. if ( lineFirstChar === 'v' ) {
  328. var data = line.split( /\s+/ );
  329. switch ( data[ 0 ] ) {
  330. case 'v':
  331. state.vertices.push(
  332. parseFloat( data[ 1 ] ),
  333. parseFloat( data[ 2 ] ),
  334. parseFloat( data[ 3 ] )
  335. );
  336. if ( data.length >= 7 ) {
  337. state.colors.push(
  338. parseFloat( data[ 4 ] ),
  339. parseFloat( data[ 5 ] ),
  340. parseFloat( data[ 6 ] )
  341. );
  342. } else {
  343. // if no colors are defined, add placeholders so color and vertex indices match
  344. state.colors.push( undefined, undefined, undefined );
  345. }
  346. break;
  347. case 'vn':
  348. state.normals.push(
  349. parseFloat( data[ 1 ] ),
  350. parseFloat( data[ 2 ] ),
  351. parseFloat( data[ 3 ] )
  352. );
  353. break;
  354. case 'vt':
  355. state.uvs.push(
  356. parseFloat( data[ 1 ] ),
  357. parseFloat( data[ 2 ] )
  358. );
  359. break;
  360. }
  361. } else if ( lineFirstChar === 'f' ) {
  362. var lineData = line.substr( 1 ).trim();
  363. var vertexData = lineData.split( /\s+/ );
  364. var faceVertices = [];
  365. // Parse the face vertex data into an easy to work with format
  366. for ( var j = 0, jl = vertexData.length; j < jl; j ++ ) {
  367. var vertex = vertexData[ j ];
  368. if ( vertex.length > 0 ) {
  369. var vertexParts = vertex.split( '/' );
  370. faceVertices.push( vertexParts );
  371. }
  372. }
  373. // Draw an edge between the first vertex and all subsequent vertices to form an n-gon
  374. var v1 = faceVertices[ 0 ];
  375. for ( var j = 1, jl = faceVertices.length - 1; j < jl; j ++ ) {
  376. var v2 = faceVertices[ j ];
  377. var v3 = faceVertices[ j + 1 ];
  378. state.addFace(
  379. v1[ 0 ], v2[ 0 ], v3[ 0 ],
  380. v1[ 1 ], v2[ 1 ], v3[ 1 ],
  381. v1[ 2 ], v2[ 2 ], v3[ 2 ]
  382. );
  383. }
  384. } else if ( lineFirstChar === 'l' ) {
  385. var lineParts = line.substring( 1 ).trim().split( " " );
  386. var lineVertices = [], lineUVs = [];
  387. if ( line.indexOf( "/" ) === - 1 ) {
  388. lineVertices = lineParts;
  389. } else {
  390. for ( var li = 0, llen = lineParts.length; li < llen; li ++ ) {
  391. var parts = lineParts[ li ].split( "/" );
  392. if ( parts[ 0 ] !== "" ) lineVertices.push( parts[ 0 ] );
  393. if ( parts[ 1 ] !== "" ) lineUVs.push( parts[ 1 ] );
  394. }
  395. }
  396. state.addLineGeometry( lineVertices, lineUVs );
  397. } else if ( lineFirstChar === 'p' ) {
  398. var lineData = line.substr( 1 ).trim();
  399. var pointData = lineData.split( " " );
  400. state.addPointGeometry( pointData );
  401. } else if ( ( result = object_pattern.exec( line ) ) !== null ) {
  402. // o object_name
  403. // or
  404. // g group_name
  405. // WORKAROUND: https://bugs.chromium.org/p/v8/issues/detail?id=2869
  406. // var name = result[ 0 ].substr( 1 ).trim();
  407. var name = ( " " + result[ 0 ].substr( 1 ).trim() ).substr( 1 );
  408. state.startObject( name );
  409. } else if ( material_use_pattern.test( line ) ) {
  410. // material
  411. state.object.startMaterial( line.substring( 7 ).trim(), state.materialLibraries );
  412. } else if ( material_library_pattern.test( line ) ) {
  413. // mtl file
  414. state.materialLibraries.push( line.substring( 7 ).trim() );
  415. } else if ( map_use_pattern.test( line ) ) {
  416. // the line is parsed but ignored since the loader assumes textures are defined MTL files
  417. // (according to https://www.okino.com/conv/imp_wave.htm, 'usemap' is the old-style Wavefront texture reference method)
  418. console.warn( 'THREE.OBJLoader: Rendering identifier "usemap" not supported. Textures must be defined in MTL files.' );
  419. } else if ( lineFirstChar === 's' ) {
  420. result = line.split( ' ' );
  421. // smooth shading
  422. // @todo Handle files that have varying smooth values for a set of faces inside one geometry,
  423. // but does not define a usemtl for each face set.
  424. // This should be detected and a dummy material created (later MultiMaterial and geometry groups).
  425. // This requires some care to not create extra material on each smooth value for "normal" obj files.
  426. // where explicit usemtl defines geometry groups.
  427. // Example asset: examples/models/obj/cerberus/Cerberus.obj
  428. /*
  429. * http://paulbourke.net/dataformats/obj/
  430. * or
  431. * http://www.cs.utah.edu/~boulos/cs3505/obj_spec.pdf
  432. *
  433. * From chapter "Grouping" Syntax explanation "s group_number":
  434. * "group_number is the smoothing group number. To turn off smoothing groups, use a value of 0 or off.
  435. * Polygonal elements use group numbers to put elements in different smoothing groups. For free-form
  436. * surfaces, smoothing groups are either turned on or off; there is no difference between values greater
  437. * than 0."
  438. */
  439. if ( result.length > 1 ) {
  440. var value = result[ 1 ].trim().toLowerCase();
  441. state.object.smooth = ( value !== '0' && value !== 'off' );
  442. } else {
  443. // ZBrush can produce "s" lines #11707
  444. state.object.smooth = true;
  445. }
  446. var material = state.object.currentMaterial();
  447. if ( material ) material.smooth = state.object.smooth;
  448. } else {
  449. // Handle null terminated files without exception
  450. if ( line === '\0' ) continue;
  451. console.warn( 'THREE.OBJLoader: Unexpected line: "' + line + '"' );
  452. }
  453. }
  454. state.finalize();
  455. var container = new Group();
  456. container.materialLibraries = [].concat( state.materialLibraries );
  457. for ( var i = 0, l = state.objects.length; i < l; i ++ ) {
  458. var object = state.objects[ i ];
  459. var geometry = object.geometry;
  460. var materials = object.materials;
  461. var isLine = ( geometry.type === 'Line' );
  462. var isPoints = ( geometry.type === 'Points' );
  463. var hasVertexColors = false;
  464. // Skip o/g line declarations that did not follow with any faces
  465. if ( geometry.vertices.length === 0 ) continue;
  466. var buffergeometry = new BufferGeometry();
  467. buffergeometry.setAttribute( 'position', new Float32BufferAttribute( geometry.vertices, 3 ) );
  468. buffergeometry.setAttribute( 'normal', new Float32BufferAttribute( geometry.normals, 3 ) );
  469. if ( geometry.colors.length > 0 ) {
  470. hasVertexColors = true;
  471. buffergeometry.setAttribute( 'color', new Float32BufferAttribute( geometry.colors, 3 ) );
  472. }
  473. if ( geometry.hasUVIndices === true ) {
  474. buffergeometry.setAttribute( 'uv', new Float32BufferAttribute( geometry.uvs, 2 ) );
  475. }
  476. // Create materials
  477. var createdMaterials = [];
  478. for ( var mi = 0, miLen = materials.length; mi < miLen; mi ++ ) {
  479. var sourceMaterial = materials[ mi ];
  480. var materialHash = sourceMaterial.name + '_' + sourceMaterial.smooth + '_' + hasVertexColors;
  481. var material = state.materials[ materialHash ];
  482. if ( this.materials !== null ) {
  483. material = this.materials.create( sourceMaterial.name );
  484. // mtl etc. loaders probably can't create line materials correctly, copy properties to a line material.
  485. if ( isLine && material && ! ( material instanceof LineBasicMaterial ) ) {
  486. var materialLine = new LineBasicMaterial();
  487. Material.prototype.copy.call( materialLine, material );
  488. materialLine.color.copy( material.color );
  489. material = materialLine;
  490. } else if ( isPoints && material && ! ( material instanceof PointsMaterial ) ) {
  491. var materialPoints = new PointsMaterial( { size: 10, sizeAttenuation: false } );
  492. Material.prototype.copy.call( materialPoints, material );
  493. materialPoints.color.copy( material.color );
  494. materialPoints.map = material.map;
  495. material = materialPoints;
  496. }
  497. }
  498. if ( material === undefined ) {
  499. if ( isLine ) {
  500. material = new LineBasicMaterial();
  501. } else if ( isPoints ) {
  502. material = new PointsMaterial( { size: 1, sizeAttenuation: false } );
  503. } else {
  504. material = new MeshPhongMaterial();
  505. }
  506. material.name = sourceMaterial.name;
  507. material.flatShading = sourceMaterial.smooth ? false : true;
  508. material.vertexColors = hasVertexColors;
  509. state.materials[ materialHash ] = material;
  510. }
  511. createdMaterials.push( material );
  512. }
  513. // Create mesh
  514. var mesh;
  515. if ( createdMaterials.length > 1 ) {
  516. for ( var mi = 0, miLen = materials.length; mi < miLen; mi ++ ) {
  517. var sourceMaterial = materials[ mi ];
  518. buffergeometry.addGroup( sourceMaterial.groupStart, sourceMaterial.groupCount, mi );
  519. }
  520. if ( isLine ) {
  521. mesh = new LineSegments( buffergeometry, createdMaterials );
  522. } else if ( isPoints ) {
  523. mesh = new Points( buffergeometry, createdMaterials );
  524. } else {
  525. mesh = new Mesh( buffergeometry, createdMaterials );
  526. }
  527. } else {
  528. if ( isLine ) {
  529. mesh = new LineSegments( buffergeometry, createdMaterials[ 0 ] );
  530. } else if ( isPoints ) {
  531. mesh = new Points( buffergeometry, createdMaterials[ 0 ] );
  532. } else {
  533. mesh = new Mesh( buffergeometry, createdMaterials[ 0 ] );
  534. }
  535. }
  536. mesh.name = object.name;
  537. container.add( mesh );
  538. }
  539. return container;
  540. }
  541. } );
  542. return OBJLoader;
  543. } )();
  544. export { OBJLoader };