OBJLoader.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. var index = this.parseVertexIndex( vertices[ vi ], vLen );
  256. this.addVertexPoint( index );
  257. this.addColor( index );
  258. }
  259. },
  260. addLineGeometry: function ( vertices, uvs ) {
  261. this.object.geometry.type = 'Line';
  262. var vLen = this.vertices.length;
  263. var uvLen = this.uvs.length;
  264. for ( var vi = 0, l = vertices.length; vi < l; vi ++ ) {
  265. this.addVertexLine( this.parseVertexIndex( vertices[ vi ], vLen ) );
  266. }
  267. for ( var uvi = 0, l = uvs.length; uvi < l; uvi ++ ) {
  268. this.addUVLine( this.parseUVIndex( uvs[ uvi ], uvLen ) );
  269. }
  270. }
  271. };
  272. state.startObject( '', false );
  273. return state;
  274. }
  275. //
  276. function OBJLoader( manager ) {
  277. Loader.call( this, manager );
  278. this.materials = null;
  279. }
  280. OBJLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
  281. constructor: OBJLoader,
  282. load: function ( url, onLoad, onProgress, onError ) {
  283. var scope = this;
  284. var 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: function ( materials ) {
  302. this.materials = materials;
  303. return this;
  304. },
  305. parse: function ( text ) {
  306. var 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. var lines = text.split( '\n' );
  316. var line = '', lineFirstChar = '';
  317. var lineLength = 0;
  318. var result = [];
  319. // Faster to just trim left side of the line. Use if available.
  320. var trimLeft = ( typeof ''.trimLeft === 'function' );
  321. for ( var 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. var 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. state.colors.push(
  340. parseFloat( data[ 4 ] ),
  341. parseFloat( data[ 5 ] ),
  342. parseFloat( data[ 6 ] )
  343. );
  344. } else {
  345. // if no colors are defined, add placeholders so color and vertex indices match
  346. state.colors.push( undefined, undefined, undefined );
  347. }
  348. break;
  349. case 'vn':
  350. state.normals.push(
  351. parseFloat( data[ 1 ] ),
  352. parseFloat( data[ 2 ] ),
  353. parseFloat( data[ 3 ] )
  354. );
  355. break;
  356. case 'vt':
  357. state.uvs.push(
  358. parseFloat( data[ 1 ] ),
  359. parseFloat( data[ 2 ] )
  360. );
  361. break;
  362. }
  363. } else if ( lineFirstChar === 'f' ) {
  364. var lineData = line.substr( 1 ).trim();
  365. var vertexData = lineData.split( /\s+/ );
  366. var faceVertices = [];
  367. // Parse the face vertex data into an easy to work with format
  368. for ( var j = 0, jl = vertexData.length; j < jl; j ++ ) {
  369. var vertex = vertexData[ j ];
  370. if ( vertex.length > 0 ) {
  371. var vertexParts = vertex.split( '/' );
  372. faceVertices.push( vertexParts );
  373. }
  374. }
  375. // Draw an edge between the first vertex and all subsequent vertices to form an n-gon
  376. var v1 = faceVertices[ 0 ];
  377. for ( var j = 1, jl = faceVertices.length - 1; j < jl; j ++ ) {
  378. var v2 = faceVertices[ j ];
  379. var v3 = faceVertices[ j + 1 ];
  380. state.addFace(
  381. v1[ 0 ], v2[ 0 ], v3[ 0 ],
  382. v1[ 1 ], v2[ 1 ], v3[ 1 ],
  383. v1[ 2 ], v2[ 2 ], v3[ 2 ]
  384. );
  385. }
  386. } else if ( lineFirstChar === 'l' ) {
  387. var lineParts = line.substring( 1 ).trim().split( ' ' );
  388. var lineVertices = [], lineUVs = [];
  389. if ( line.indexOf( '/' ) === - 1 ) {
  390. lineVertices = lineParts;
  391. } else {
  392. for ( var li = 0, llen = lineParts.length; li < llen; li ++ ) {
  393. var parts = lineParts[ li ].split( '/' );
  394. if ( parts[ 0 ] !== '' ) lineVertices.push( parts[ 0 ] );
  395. if ( parts[ 1 ] !== '' ) lineUVs.push( parts[ 1 ] );
  396. }
  397. }
  398. state.addLineGeometry( lineVertices, lineUVs );
  399. } else if ( lineFirstChar === 'p' ) {
  400. var lineData = line.substr( 1 ).trim();
  401. var pointData = lineData.split( ' ' );
  402. state.addPointGeometry( pointData );
  403. } else if ( ( result = object_pattern.exec( line ) ) !== null ) {
  404. // o object_name
  405. // or
  406. // g group_name
  407. // WORKAROUND: https://bugs.chromium.org/p/v8/issues/detail?id=2869
  408. // var name = result[ 0 ].substr( 1 ).trim();
  409. var name = ( ' ' + result[ 0 ].substr( 1 ).trim() ).substr( 1 );
  410. state.startObject( name );
  411. } else if ( material_use_pattern.test( line ) ) {
  412. // material
  413. state.object.startMaterial( line.substring( 7 ).trim(), state.materialLibraries );
  414. } else if ( material_library_pattern.test( line ) ) {
  415. // mtl file
  416. state.materialLibraries.push( line.substring( 7 ).trim() );
  417. } else if ( map_use_pattern.test( line ) ) {
  418. // the line is parsed but ignored since the loader assumes textures are defined MTL files
  419. // (according to https://www.okino.com/conv/imp_wave.htm, 'usemap' is the old-style Wavefront texture reference method)
  420. console.warn( 'THREE.OBJLoader: Rendering identifier "usemap" not supported. Textures must be defined in MTL files.' );
  421. } else if ( lineFirstChar === 's' ) {
  422. result = line.split( ' ' );
  423. // smooth shading
  424. // @todo Handle files that have varying smooth values for a set of faces inside one geometry,
  425. // but does not define a usemtl for each face set.
  426. // This should be detected and a dummy material created (later MultiMaterial and geometry groups).
  427. // This requires some care to not create extra material on each smooth value for "normal" obj files.
  428. // where explicit usemtl defines geometry groups.
  429. // Example asset: examples/models/obj/cerberus/Cerberus.obj
  430. /*
  431. * http://paulbourke.net/dataformats/obj/
  432. * or
  433. * http://www.cs.utah.edu/~boulos/cs3505/obj_spec.pdf
  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. var 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. var 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. var container = new Group();
  458. container.materialLibraries = [].concat( state.materialLibraries );
  459. var hasPrimitives = ! ( state.objects.length === 1 && state.objects[ 0 ].geometry.vertices.length === 0 );
  460. if ( hasPrimitives === true ) {
  461. for ( var i = 0, l = state.objects.length; i < l; i ++ ) {
  462. var object = state.objects[ i ];
  463. var geometry = object.geometry;
  464. var materials = object.materials;
  465. var isLine = ( geometry.type === 'Line' );
  466. var isPoints = ( geometry.type === 'Points' );
  467. var hasVertexColors = false;
  468. // Skip o/g line declarations that did not follow with any faces
  469. if ( geometry.vertices.length === 0 ) continue;
  470. var 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. var createdMaterials = [];
  484. for ( var mi = 0, miLen = materials.length; mi < miLen; mi ++ ) {
  485. var sourceMaterial = materials[ mi ];
  486. var materialHash = sourceMaterial.name + '_' + sourceMaterial.smooth + '_' + hasVertexColors;
  487. var 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. var 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. var 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. var mesh;
  521. if ( createdMaterials.length > 1 ) {
  522. for ( var mi = 0, miLen = materials.length; mi < miLen; mi ++ ) {
  523. var 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. var material = new PointsMaterial( { size: 1, sizeAttenuation: false } );
  549. var 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. var points = new Points( buffergeometry, material );
  556. container.add( points );
  557. }
  558. }
  559. return container;
  560. }
  561. } );
  562. return OBJLoader;
  563. } )();
  564. export { OBJLoader };