OBJLoader.js 21 KB

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