OBJLoader.js 21 KB

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