OBJLoader.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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. const _color = new THREE.Color();
  12. function ParserState() {
  13. const state = {
  14. objects: [],
  15. object: {},
  16. vertices: [],
  17. normals: [],
  18. colors: [],
  19. uvs: [],
  20. materials: {},
  21. materialLibraries: [],
  22. startObject: function ( name, fromDeclaration ) {
  23. // If the current object (initial from reset) is not from a g/o declaration in the parsed
  24. // file. We need to use it for the first parsed g/o to keep things in sync.
  25. if ( this.object && this.object.fromDeclaration === false ) {
  26. this.object.name = name;
  27. this.object.fromDeclaration = fromDeclaration !== false;
  28. return;
  29. }
  30. const previousMaterial = this.object && typeof this.object.currentMaterial === 'function' ? this.object.currentMaterial() : undefined;
  31. if ( this.object && typeof this.object._finalize === 'function' ) {
  32. this.object._finalize( true );
  33. }
  34. this.object = {
  35. name: name || '',
  36. fromDeclaration: fromDeclaration !== false,
  37. geometry: {
  38. vertices: [],
  39. normals: [],
  40. colors: [],
  41. uvs: [],
  42. hasUVIndices: false
  43. },
  44. materials: [],
  45. smooth: true,
  46. startMaterial: function ( name, libraries ) {
  47. const previous = this._finalize( false ); // New usemtl declaration overwrites an inherited material, except if faces were declared
  48. // after the material, then it must be preserved for proper MultiMaterial continuation.
  49. if ( previous && ( previous.inherited || previous.groupCount <= 0 ) ) {
  50. this.materials.splice( previous.index, 1 );
  51. }
  52. const material = {
  53. index: this.materials.length,
  54. name: name || '',
  55. mtllib: Array.isArray( libraries ) && libraries.length > 0 ? libraries[ libraries.length - 1 ] : '',
  56. smooth: previous !== undefined ? previous.smooth : this.smooth,
  57. groupStart: previous !== undefined ? previous.groupEnd : 0,
  58. groupEnd: - 1,
  59. groupCount: - 1,
  60. inherited: false,
  61. clone: function ( index ) {
  62. const cloned = {
  63. index: typeof index === 'number' ? index : this.index,
  64. name: this.name,
  65. mtllib: this.mtllib,
  66. smooth: this.smooth,
  67. groupStart: 0,
  68. groupEnd: - 1,
  69. groupCount: - 1,
  70. inherited: false
  71. };
  72. cloned.clone = this.clone.bind( cloned );
  73. return cloned;
  74. }
  75. };
  76. this.materials.push( material );
  77. return material;
  78. },
  79. currentMaterial: function () {
  80. if ( this.materials.length > 0 ) {
  81. return this.materials[ this.materials.length - 1 ];
  82. }
  83. return undefined;
  84. },
  85. _finalize: function ( end ) {
  86. const lastMultiMaterial = this.currentMaterial();
  87. if ( lastMultiMaterial && lastMultiMaterial.groupEnd === - 1 ) {
  88. lastMultiMaterial.groupEnd = this.geometry.vertices.length / 3;
  89. lastMultiMaterial.groupCount = lastMultiMaterial.groupEnd - lastMultiMaterial.groupStart;
  90. lastMultiMaterial.inherited = false;
  91. } // Ignore objects tail materials if no face declarations followed them before a new o/g started.
  92. if ( end && this.materials.length > 1 ) {
  93. for ( let mi = this.materials.length - 1; mi >= 0; mi -- ) {
  94. if ( this.materials[ mi ].groupCount <= 0 ) {
  95. this.materials.splice( mi, 1 );
  96. }
  97. }
  98. } // Guarantee at least one empty material, this makes the creation later more straight forward.
  99. if ( end && this.materials.length === 0 ) {
  100. this.materials.push( {
  101. name: '',
  102. smooth: this.smooth
  103. } );
  104. }
  105. return lastMultiMaterial;
  106. }
  107. }; // Inherit previous objects material.
  108. // Spec tells us that a declared material must be set to all objects until a new material is declared.
  109. // If a usemtl declaration is encountered while this new object is being parsed, it will
  110. // overwrite the inherited material. Exception being that there was already face declarations
  111. // to the inherited material, then it will be preserved for proper MultiMaterial continuation.
  112. if ( previousMaterial && previousMaterial.name && typeof previousMaterial.clone === 'function' ) {
  113. const declared = previousMaterial.clone( 0 );
  114. declared.inherited = true;
  115. this.object.materials.push( declared );
  116. }
  117. this.objects.push( this.object );
  118. },
  119. finalize: function () {
  120. if ( this.object && typeof this.object._finalize === 'function' ) {
  121. this.object._finalize( true );
  122. }
  123. },
  124. parseVertexIndex: function ( value, len ) {
  125. const index = parseInt( value, 10 );
  126. return ( index >= 0 ? index - 1 : index + len / 3 ) * 3;
  127. },
  128. parseNormalIndex: function ( value, len ) {
  129. const index = parseInt( value, 10 );
  130. return ( index >= 0 ? index - 1 : index + len / 3 ) * 3;
  131. },
  132. parseUVIndex: function ( value, len ) {
  133. const index = parseInt( value, 10 );
  134. return ( index >= 0 ? index - 1 : index + len / 2 ) * 2;
  135. },
  136. addVertex: function ( a, b, c ) {
  137. const src = this.vertices;
  138. const dst = this.object.geometry.vertices;
  139. dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] );
  140. dst.push( src[ b + 0 ], src[ b + 1 ], src[ b + 2 ] );
  141. dst.push( src[ c + 0 ], src[ c + 1 ], src[ c + 2 ] );
  142. },
  143. addVertexPoint: function ( a ) {
  144. const src = this.vertices;
  145. const dst = this.object.geometry.vertices;
  146. dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] );
  147. },
  148. addVertexLine: function ( a ) {
  149. const src = this.vertices;
  150. const dst = this.object.geometry.vertices;
  151. dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] );
  152. },
  153. addNormal: function ( a, b, c ) {
  154. const src = this.normals;
  155. const dst = this.object.geometry.normals;
  156. dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] );
  157. dst.push( src[ b + 0 ], src[ b + 1 ], src[ b + 2 ] );
  158. dst.push( src[ c + 0 ], src[ c + 1 ], src[ c + 2 ] );
  159. },
  160. addFaceNormal: function ( a, b, c ) {
  161. const src = this.vertices;
  162. const dst = this.object.geometry.normals;
  163. _vA.fromArray( src, a );
  164. _vB.fromArray( src, b );
  165. _vC.fromArray( src, c );
  166. _cb.subVectors( _vC, _vB );
  167. _ab.subVectors( _vA, _vB );
  168. _cb.cross( _ab );
  169. _cb.normalize();
  170. dst.push( _cb.x, _cb.y, _cb.z );
  171. dst.push( _cb.x, _cb.y, _cb.z );
  172. dst.push( _cb.x, _cb.y, _cb.z );
  173. },
  174. addColor: function ( a, b, c ) {
  175. const src = this.colors;
  176. const dst = this.object.geometry.colors;
  177. if ( src[ a ] !== undefined ) dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] );
  178. if ( src[ b ] !== undefined ) dst.push( src[ b + 0 ], src[ b + 1 ], src[ b + 2 ] );
  179. if ( src[ c ] !== undefined ) dst.push( src[ c + 0 ], src[ c + 1 ], src[ c + 2 ] );
  180. },
  181. addUV: function ( a, b, c ) {
  182. const src = this.uvs;
  183. const dst = this.object.geometry.uvs;
  184. dst.push( src[ a + 0 ], src[ a + 1 ] );
  185. dst.push( src[ b + 0 ], src[ b + 1 ] );
  186. dst.push( src[ c + 0 ], src[ c + 1 ] );
  187. },
  188. addDefaultUV: function () {
  189. const dst = this.object.geometry.uvs;
  190. dst.push( 0, 0 );
  191. dst.push( 0, 0 );
  192. dst.push( 0, 0 );
  193. },
  194. addUVLine: function ( a ) {
  195. const src = this.uvs;
  196. const dst = this.object.geometry.uvs;
  197. dst.push( src[ a + 0 ], src[ a + 1 ] );
  198. },
  199. addFace: function ( a, b, c, ua, ub, uc, na, nb, nc ) {
  200. const vLen = this.vertices.length;
  201. let ia = this.parseVertexIndex( a, vLen );
  202. let ib = this.parseVertexIndex( b, vLen );
  203. let ic = this.parseVertexIndex( c, vLen );
  204. this.addVertex( ia, ib, ic );
  205. this.addColor( ia, ib, ic ); // normals
  206. if ( na !== undefined && na !== '' ) {
  207. const nLen = this.normals.length;
  208. ia = this.parseNormalIndex( na, nLen );
  209. ib = this.parseNormalIndex( nb, nLen );
  210. ic = this.parseNormalIndex( nc, nLen );
  211. this.addNormal( ia, ib, ic );
  212. } else {
  213. this.addFaceNormal( ia, ib, ic );
  214. } // uvs
  215. if ( ua !== undefined && ua !== '' ) {
  216. const uvLen = this.uvs.length;
  217. ia = this.parseUVIndex( ua, uvLen );
  218. ib = this.parseUVIndex( ub, uvLen );
  219. ic = this.parseUVIndex( uc, uvLen );
  220. this.addUV( ia, ib, ic );
  221. this.object.geometry.hasUVIndices = true;
  222. } else {
  223. // add placeholder values (for inconsistent face definitions)
  224. this.addDefaultUV();
  225. }
  226. },
  227. addPointGeometry: function ( vertices ) {
  228. this.object.geometry.type = 'Points';
  229. const vLen = this.vertices.length;
  230. for ( let vi = 0, l = vertices.length; vi < l; vi ++ ) {
  231. const index = this.parseVertexIndex( vertices[ vi ], vLen );
  232. this.addVertexPoint( index );
  233. this.addColor( index );
  234. }
  235. },
  236. addLineGeometry: function ( vertices, uvs ) {
  237. this.object.geometry.type = 'Line';
  238. const vLen = this.vertices.length;
  239. const uvLen = this.uvs.length;
  240. for ( let vi = 0, l = vertices.length; vi < l; vi ++ ) {
  241. this.addVertexLine( this.parseVertexIndex( vertices[ vi ], vLen ) );
  242. }
  243. for ( let uvi = 0, l = uvs.length; uvi < l; uvi ++ ) {
  244. this.addUVLine( this.parseUVIndex( uvs[ uvi ], uvLen ) );
  245. }
  246. }
  247. };
  248. state.startObject( '', false );
  249. return state;
  250. } //
  251. class OBJLoader extends THREE.Loader {
  252. constructor( manager ) {
  253. super( manager );
  254. this.materials = null;
  255. }
  256. load( url, onLoad, onProgress, onError ) {
  257. const scope = this;
  258. const loader = new THREE.FileLoader( this.manager );
  259. loader.setPath( this.path );
  260. loader.setRequestHeader( this.requestHeader );
  261. loader.setWithCredentials( this.withCredentials );
  262. loader.load( url, function ( text ) {
  263. try {
  264. onLoad( scope.parse( text ) );
  265. } catch ( e ) {
  266. if ( onError ) {
  267. onError( e );
  268. } else {
  269. console.error( e );
  270. }
  271. scope.manager.itemError( url );
  272. }
  273. }, onProgress, onError );
  274. }
  275. setMaterials( materials ) {
  276. this.materials = materials;
  277. return this;
  278. }
  279. parse( text ) {
  280. const state = new ParserState();
  281. if ( text.indexOf( '\r\n' ) !== - 1 ) {
  282. // This is faster than String.split with regex that splits on both
  283. text = text.replace( /\r\n/g, '\n' );
  284. }
  285. if ( text.indexOf( '\\\n' ) !== - 1 ) {
  286. // join lines separated by a line continuation character (\)
  287. text = text.replace( /\\\n/g, '' );
  288. }
  289. const lines = text.split( '\n' );
  290. let line = '',
  291. lineFirstChar = '';
  292. let lineLength = 0;
  293. let result = []; // Faster to just trim left side of the line. Use if available.
  294. const trimLeft = typeof ''.trimLeft === 'function';
  295. for ( let i = 0, l = lines.length; i < l; i ++ ) {
  296. line = lines[ i ];
  297. line = trimLeft ? line.trimLeft() : line.trim();
  298. lineLength = line.length;
  299. if ( lineLength === 0 ) continue;
  300. lineFirstChar = line.charAt( 0 ); // @todo invoke passed in handler if any
  301. if ( lineFirstChar === '#' ) continue;
  302. if ( lineFirstChar === 'v' ) {
  303. const data = line.split( /\s+/ );
  304. switch ( data[ 0 ] ) {
  305. case 'v':
  306. state.vertices.push( parseFloat( data[ 1 ] ), parseFloat( data[ 2 ] ), parseFloat( data[ 3 ] ) );
  307. if ( data.length >= 7 ) {
  308. _color.setRGB( parseFloat( data[ 4 ] ), parseFloat( data[ 5 ] ), parseFloat( data[ 6 ] ) ).convertSRGBToLinear();
  309. state.colors.push( _color.r, _color.g, _color.b );
  310. } else {
  311. // if no colors are defined, add placeholders so color and vertex indices match
  312. state.colors.push( undefined, undefined, undefined );
  313. }
  314. break;
  315. case 'vn':
  316. state.normals.push( parseFloat( data[ 1 ] ), parseFloat( data[ 2 ] ), parseFloat( data[ 3 ] ) );
  317. break;
  318. case 'vt':
  319. state.uvs.push( parseFloat( data[ 1 ] ), parseFloat( data[ 2 ] ) );
  320. break;
  321. }
  322. } else if ( lineFirstChar === 'f' ) {
  323. const lineData = line.slice( 1 ).trim();
  324. const vertexData = lineData.split( /\s+/ );
  325. const faceVertices = []; // Parse the face vertex data into an easy to work with format
  326. for ( let j = 0, jl = vertexData.length; j < jl; j ++ ) {
  327. const vertex = vertexData[ j ];
  328. if ( vertex.length > 0 ) {
  329. const vertexParts = vertex.split( '/' );
  330. faceVertices.push( vertexParts );
  331. }
  332. } // Draw an edge between the first vertex and all subsequent vertices to form an n-gon
  333. const v1 = faceVertices[ 0 ];
  334. for ( let j = 1, jl = faceVertices.length - 1; j < jl; j ++ ) {
  335. const v2 = faceVertices[ j ];
  336. const v3 = faceVertices[ j + 1 ];
  337. state.addFace( v1[ 0 ], v2[ 0 ], v3[ 0 ], v1[ 1 ], v2[ 1 ], v3[ 1 ], v1[ 2 ], v2[ 2 ], v3[ 2 ] );
  338. }
  339. } else if ( lineFirstChar === 'l' ) {
  340. const lineParts = line.substring( 1 ).trim().split( ' ' );
  341. let lineVertices = [];
  342. const lineUVs = [];
  343. if ( line.indexOf( '/' ) === - 1 ) {
  344. lineVertices = lineParts;
  345. } else {
  346. for ( let li = 0, llen = lineParts.length; li < llen; li ++ ) {
  347. const parts = lineParts[ li ].split( '/' );
  348. if ( parts[ 0 ] !== '' ) lineVertices.push( parts[ 0 ] );
  349. if ( parts[ 1 ] !== '' ) lineUVs.push( parts[ 1 ] );
  350. }
  351. }
  352. state.addLineGeometry( lineVertices, lineUVs );
  353. } else if ( lineFirstChar === 'p' ) {
  354. const lineData = line.slice( 1 ).trim();
  355. const pointData = lineData.split( ' ' );
  356. state.addPointGeometry( pointData );
  357. } else if ( ( result = _object_pattern.exec( line ) ) !== null ) {
  358. // o object_name
  359. // or
  360. // g group_name
  361. // WORKAROUND: https://bugs.chromium.org/p/v8/issues/detail?id=2869
  362. // let name = result[ 0 ].slice( 1 ).trim();
  363. const name = ( ' ' + result[ 0 ].slice( 1 ).trim() ).slice( 1 );
  364. state.startObject( name );
  365. } else if ( _material_use_pattern.test( line ) ) {
  366. // material
  367. state.object.startMaterial( line.substring( 7 ).trim(), state.materialLibraries );
  368. } else if ( _material_library_pattern.test( line ) ) {
  369. // mtl file
  370. state.materialLibraries.push( line.substring( 7 ).trim() );
  371. } else if ( _map_use_pattern.test( line ) ) {
  372. // the line is parsed but ignored since the loader assumes textures are defined MTL files
  373. // (according to https://www.okino.com/conv/imp_wave.htm, 'usemap' is the old-style Wavefront texture reference method)
  374. console.warn( 'THREE.OBJLoader: Rendering identifier "usemap" not supported. Textures must be defined in MTL files.' );
  375. } else if ( lineFirstChar === 's' ) {
  376. result = line.split( ' ' ); // smooth shading
  377. // @todo Handle files that have varying smooth values for a set of faces inside one geometry,
  378. // but does not define a usemtl for each face set.
  379. // This should be detected and a dummy material created (later MultiMaterial and geometry groups).
  380. // This requires some care to not create extra material on each smooth value for "normal" obj files.
  381. // where explicit usemtl defines geometry groups.
  382. // Example asset: examples/models/obj/cerberus/Cerberus.obj
  383. /*
  384. * http://paulbourke.net/dataformats/obj/
  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. } )();