OBJLoader.js 21 KB

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