OBJLoader.js 21 KB

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