OBJLoader.js 21 KB

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