OBJLoader.js 21 KB

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