OBJLoader2.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  1. /**
  2. * @author Kai Salmen / https://kaisalmen.de
  3. * Development repository: https://github.com/kaisalmen/WWOBJLoader
  4. */
  5. 'use strict';
  6. if ( THREE.OBJLoader2 === undefined ) { THREE.OBJLoader2 = {} }
  7. /**
  8. * Use this class to load OBJ data from files or to parse OBJ data from an arraybuffer
  9. * @class
  10. *
  11. * @param {THREE.DefaultLoadingManager} [manager] The loadingManager for the loader to use. Default is {@link THREE.DefaultLoadingManager}
  12. */
  13. THREE.OBJLoader2 = (function () {
  14. var OBJLOADER2_VERSION = '2.0.0-dev';
  15. var Validator = THREE.LoaderSupport.Validator;
  16. var Commons = THREE.LoaderSupport.Commons;
  17. OBJLoader2.prototype = Object.create( THREE.LoaderSupport.Commons.prototype );
  18. OBJLoader2.prototype.constructor = OBJLoader2;
  19. function OBJLoader2( manager ) {
  20. THREE.LoaderSupport.Commons.call( this, manager );
  21. console.log( "Using THREE.OBJLoader2 version: " + OBJLOADER2_VERSION );
  22. this.materialPerSmoothingGroup = false;
  23. this.fileLoader = Validator.verifyInput( this.fileLoader, new THREE.FileLoader( this.manager ) );
  24. this.workerSupport = null;
  25. this.terminateWorkerOnLoad = true;
  26. };
  27. /**
  28. * Tells whether a material shall be created per smoothing group
  29. * @memberOf THREE.OBJLoader2
  30. *
  31. * @param {boolean} materialPerSmoothingGroup=false Default is false
  32. */
  33. OBJLoader2.prototype.setMaterialPerSmoothingGroup = function ( materialPerSmoothingGroup ) {
  34. this.materialPerSmoothingGroup = materialPerSmoothingGroup === true;
  35. };
  36. /**
  37. * Sets debug mode for the parser
  38. * @memberOf THREE.OBJLoader2
  39. *
  40. * @param {boolean} enabled
  41. */
  42. OBJLoader2.prototype.setDebug = function ( enabled ) {
  43. THREE.LoaderSupport.Commons.prototype.setDebug.call( this, enabled );
  44. };
  45. /**
  46. * Use this convenient method to load an OBJ file at the given URL. Per default the fileLoader uses an arraybuffer
  47. * @memberOf THREE.OBJLoader2
  48. *
  49. * @param {string} url URL of the file to load
  50. * @param {callback} onLoad Called after loading was successfully completed
  51. * @param {callback} onProgress Called to report progress of loading. The argument will be the XMLHttpRequest instance, which contains {integer total} and {integer loaded} bytes.
  52. * @param {callback} onError Called after an error occurred during loading
  53. * @param {callback} onMeshAlter Called after a new mesh raw data becomes available
  54. * @param {boolean} useAsync Set this to use async loading
  55. */
  56. OBJLoader2.prototype.load = function ( url, onLoad, onProgress, onError, onMeshAlter, useAsync ) {
  57. var scope = this;
  58. if ( ! Validator.isValid( onProgress ) ) {
  59. var refPercentComplete = 0;
  60. var percentComplete = 0;
  61. onProgress = function ( event ) {
  62. if ( ! event.lengthComputable ) return;
  63. percentComplete = Math.round( event.loaded / event.total * 100 );
  64. if ( percentComplete > refPercentComplete ) {
  65. refPercentComplete = percentComplete;
  66. var output = 'Download of "' + url + '": ' + percentComplete + '%';
  67. console.log( output );
  68. scope.onProgress( output );
  69. }
  70. };
  71. }
  72. if ( ! Validator.isValid( onError ) ) {
  73. onError = function ( event ) {
  74. var output = 'Error occurred while downloading "' + url + '"';
  75. console.error( output + ': ' + event );
  76. scope.onProgress( output );
  77. };
  78. }
  79. this.fileLoader.setPath( this.path );
  80. this.fileLoader.setResponseType( 'arraybuffer' );
  81. this.fileLoader.load( url, function ( content ) {
  82. if ( useAsync ) {
  83. scope.parseAsync( content, onLoad );
  84. } else {
  85. scope._setCallbacks( null, onMeshAlter, null );
  86. onLoad( scope.parse( content ), scope.modelName, scope.instanceNo );
  87. }
  88. }, onProgress, onError );
  89. };
  90. /**
  91. * Run the loader according the provided instructions.
  92. * @memberOf THREE.OBJLoader2
  93. *
  94. * @param {THREE.LoaderSupport.PrepData} prepData All parameters and resources required for execution
  95. * @param {THREE.LoaderSupport.WorkerSupport} [workerSupportExternal] Use pre-existing WorkerSupport
  96. */
  97. OBJLoader2.prototype.run = function ( prepData, workerSupportExternal ) {
  98. this._applyPrepData( prepData );
  99. var available = this._checkFiles( prepData.resources );
  100. if ( Validator.isValid( workerSupportExternal ) ) {
  101. this.terminateWorkerOnLoad = false;
  102. this.workerSupport = workerSupportExternal;
  103. } else {
  104. this.terminateWorkerOnLoad = true;
  105. }
  106. var scope = this;
  107. var onMaterialsLoaded = function ( materials ) {
  108. scope.builder.setMaterials( materials );
  109. if ( Validator.isValid( available.obj.content ) ) {
  110. if ( prepData.useAsync ) {
  111. scope.parseAsync( available.obj.content, scope.callbacks.onLoad );
  112. } else {
  113. scope.parse( available.obj.content );
  114. }
  115. } else {
  116. scope.setPath( available.obj.path );
  117. scope.load( available.obj.name, scope.callbacks.onLoad, null, null, scope.callbacks.onMeshAlter, prepData.useAsync );
  118. }
  119. };
  120. this._loadMtl( available.mtl, onMaterialsLoaded, prepData.crossOrigin );
  121. };
  122. OBJLoader2.prototype._applyPrepData = function ( prepData ) {
  123. THREE.LoaderSupport.Commons.prototype._applyPrepData.call( this, prepData );
  124. if ( Validator.isValid( prepData ) ) {
  125. this.setMaterialPerSmoothingGroup( prepData.materialPerSmoothingGroup );
  126. }
  127. };
  128. /**
  129. * Parses OBJ content synchronously.
  130. * @memberOf THREE.OBJLoader2
  131. *
  132. * @param content
  133. */
  134. OBJLoader2.prototype.parse = function ( content ) {
  135. console.time( 'OBJLoader2 parse: ' + this.modelName );
  136. this.parser = new Parser();
  137. this.parser.setMaterialPerSmoothingGroup( this.materialPerSmoothingGroup );
  138. this.parser.setMaterialNames( this.builder.materialNames );
  139. this.parser.setDebug( this.debug );
  140. var scope = this;
  141. var onMeshLoaded = function ( payload ) {
  142. var meshes = scope.builder.buildMeshes( payload );
  143. var mesh;
  144. for ( var i in meshes ) {
  145. mesh = meshes[ i ];
  146. scope.loaderRootNode.add( mesh );
  147. }
  148. };
  149. this.parser.setCallbackBuilder( onMeshLoaded );
  150. var onProgressScoped = function ( message ) {
  151. scope.onProgress( message );
  152. };
  153. this.parser.setCallbackProgress( onProgressScoped );
  154. if ( content instanceof ArrayBuffer || content instanceof Uint8Array ) {
  155. console.log( 'Parsing arrayBuffer...' );
  156. this.parser.parse( content );
  157. } else if ( typeof( content ) === 'string' || content instanceof String ) {
  158. console.log( 'Parsing text...' );
  159. this.parser.parseText( content );
  160. } else {
  161. throw 'Provided content was neither of type String nor Uint8Array! Aborting...';
  162. }
  163. console.timeEnd( 'OBJLoader2 parse: ' + this.modelName );
  164. return this.loaderRootNode;
  165. };
  166. /**
  167. * Parses OBJ content asynchronously.
  168. * @memberOf THREE.OBJLoader2
  169. *
  170. * @param {arraybuffer} content
  171. * @param {callback} onLoad
  172. */
  173. OBJLoader2.prototype.parseAsync = function ( content, onLoad ) {
  174. console.time( 'OBJLoader2 parseAsync: ' + this.modelName);
  175. var scope = this;
  176. var scopedOnLoad = function ( message ) {
  177. onLoad( scope.loaderRootNode, scope.modelName, scope.instanceNo, message );
  178. if ( scope.terminateWorkerOnLoad ) scope.workerSupport.terminateWorker();
  179. console.timeEnd( 'OBJLoader2 parseAsync: ' + scope.modelName );
  180. };
  181. var scopedOnMeshLoaded = function ( payload ) {
  182. var meshes = scope.builder.buildMeshes( payload );
  183. var mesh;
  184. for ( var i in meshes ) {
  185. mesh = meshes[ i ];
  186. scope.loaderRootNode.add( mesh );
  187. }
  188. };
  189. this.workerSupport = Validator.verifyInput( this.workerSupport, new THREE.LoaderSupport.WorkerSupport() );
  190. var buildCode = function ( funcBuildObject, funcBuildSingelton ) {
  191. var workerCode = '';
  192. workerCode += '/**\n';
  193. workerCode += ' * This code was constructed by OBJLoader2 buildWorkerCode.\n';
  194. workerCode += ' */\n\n';
  195. workerCode += funcBuildSingelton( 'Commons', 'Commons', Commons );
  196. workerCode += funcBuildObject( 'Consts', Consts );
  197. workerCode += funcBuildObject( 'Validator', Validator );
  198. workerCode += funcBuildSingelton( 'Parser', 'Parser', Parser );
  199. workerCode += funcBuildSingelton( 'RawObject', 'RawObject', RawObject );
  200. workerCode += funcBuildSingelton( 'RawObjectDescription', 'RawObjectDescription', RawObjectDescription );
  201. return workerCode;
  202. };
  203. this.workerSupport.validate( buildCode, false );
  204. this.workerSupport.setCallbacks( scopedOnMeshLoaded, scopedOnLoad );
  205. this.workerSupport.run(
  206. {
  207. cmd: 'run',
  208. params: {
  209. debug: this.debug,
  210. materialPerSmoothingGroup: this.materialPerSmoothingGroup
  211. },
  212. materials: {
  213. materialNames: this.builder.materialNames
  214. },
  215. buffers: {
  216. input: content
  217. }
  218. },
  219. [ content.buffer ]
  220. );
  221. };
  222. /**
  223. * Constants used by THREE.OBJLoader2
  224. */
  225. var Consts = {
  226. CODE_LF: 10,
  227. CODE_CR: 13,
  228. CODE_SPACE: 32,
  229. CODE_SLASH: 47,
  230. STRING_LF: '\n',
  231. STRING_CR: '\r',
  232. STRING_SPACE: ' ',
  233. STRING_SLASH: '/',
  234. LINE_F: 'f',
  235. LINE_G: 'g',
  236. LINE_L: 'l',
  237. LINE_O: 'o',
  238. LINE_S: 's',
  239. LINE_V: 'v',
  240. LINE_VT: 'vt',
  241. LINE_VN: 'vn',
  242. LINE_MTLLIB: 'mtllib',
  243. LINE_USEMTL: 'usemtl'
  244. };
  245. /**
  246. * Parse OBJ data either from ArrayBuffer or string
  247. * @class
  248. */
  249. var Parser = (function () {
  250. function Parser() {
  251. this.callbackProgress = null;
  252. this.inputObjectCount = 1;
  253. this.debug = false;
  254. this.materialPerSmoothingGroup = false;
  255. this.rawObject = new RawObject( this.materialPerSmoothingGroup );
  256. // build mesh related
  257. this.callbackBuilder = null;
  258. this.materialNames = [];
  259. this.outputObjectCount = 1;
  260. };
  261. Parser.prototype.setDebug = function ( debug ) {
  262. if ( debug === true || debug === false ) this.debug = debug;
  263. };
  264. Parser.prototype.setMaterialPerSmoothingGroup = function ( materialPerSmoothingGroup ) {
  265. this.materialPerSmoothingGroup = materialPerSmoothingGroup;
  266. this.rawObject.setMaterialPerSmoothingGroup( this.materialPerSmoothingGroup );
  267. };
  268. Parser.prototype.setMaterialNames = function ( materialNames ) {
  269. this.materialNames = Validator.verifyInput( materialNames, this.materialNames );
  270. this.materialNames = Validator.verifyInput( this.materialNames, [] );
  271. };
  272. Parser.prototype.setCallbackBuilder = function ( callbackBuilder ) {
  273. this.callbackBuilder = callbackBuilder;
  274. if ( ! Validator.isValid( this.callbackBuilder ) ) throw 'Unable to run as no "builder" callback is set.';
  275. };
  276. Parser.prototype.setCallbackProgress = function ( callbackProgress ) {
  277. this.callbackProgress = callbackProgress;
  278. };
  279. /**
  280. * Parse the provided arraybuffer
  281. * @memberOf Parser
  282. *
  283. * @param {Uint8Array} arrayBuffer OBJ data as Uint8Array
  284. */
  285. Parser.prototype.parse = function ( arrayBuffer ) {
  286. console.time( 'OBJLoader2.Parser.parse' );
  287. var arrayBufferView = new Uint8Array( arrayBuffer );
  288. var length = arrayBufferView.byteLength;
  289. var buffer = new Array( 128 );
  290. var bufferPointer = 0;
  291. var slashesCount = 0;
  292. var reachedFaces = false;
  293. var code;
  294. var word = '';
  295. for ( var i = 0; i < length; i++ ) {
  296. code = arrayBufferView[ i ];
  297. switch ( code ) {
  298. case Consts.CODE_SPACE:
  299. if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
  300. word = '';
  301. break;
  302. case Consts.CODE_SLASH:
  303. slashesCount++;
  304. if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
  305. word = '';
  306. break;
  307. case Consts.CODE_LF:
  308. if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
  309. word = '';
  310. reachedFaces = this.processLine( buffer, bufferPointer, slashesCount, reachedFaces );
  311. bufferPointer = 0;
  312. slashesCount = 0;
  313. break;
  314. case Consts.CODE_CR:
  315. break;
  316. default:
  317. word += String.fromCharCode( code );
  318. break;
  319. }
  320. }
  321. this.finalize();
  322. console.timeEnd( 'OBJLoader2.Parser.parse' );
  323. };
  324. /**
  325. * Parse the provided text
  326. * @memberOf Parser
  327. *
  328. * @param {string} text OBJ data as string
  329. */
  330. Parser.prototype.parseText = function ( text ) {
  331. console.time( 'OBJLoader2.Parser.parseText' );
  332. var length = text.length;
  333. var buffer = new Array( 128 );
  334. var bufferPointer = 0;
  335. var slashesCount = 0;
  336. var reachedFaces = false;
  337. var char;
  338. var word = '';
  339. for ( var i = 0; i < length; i++ ) {
  340. char = text[ i ];
  341. switch ( char ) {
  342. case Consts.STRING_SPACE:
  343. if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
  344. word = '';
  345. break;
  346. case Consts.STRING_SLASH:
  347. slashesCount++;
  348. if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
  349. word = '';
  350. break;
  351. case Consts.STRING_LF:
  352. if ( word.length > 0 ) buffer[ bufferPointer++ ] = word;
  353. word = '';
  354. reachedFaces = this.processLine( buffer, bufferPointer, slashesCount, reachedFaces );
  355. bufferPointer = 0;
  356. slashesCount = 0;
  357. break;
  358. case Consts.STRING_CR:
  359. break;
  360. default:
  361. word += char;
  362. }
  363. }
  364. this.finalize();
  365. console.timeEnd( 'OBJLoader2.Parser.parseText' );
  366. };
  367. Parser.prototype.processLine = function ( buffer, bufferPointer, slashesCount, reachedFaces ) {
  368. if ( bufferPointer < 1 ) return reachedFaces;
  369. var bufferLength = bufferPointer - 1;
  370. var concatBuffer;
  371. switch ( buffer[ 0 ] ) {
  372. case Consts.LINE_V:
  373. // object complete instance required if reached faces already (= reached next block of v)
  374. if ( reachedFaces ) {
  375. if ( this.rawObject.colors.length > 0 && this.rawObject.colors.length !== this.rawObject.vertices.length ) {
  376. throw 'Vertex Colors were detected, but vertex count and color count do not match!';
  377. }
  378. this.processCompletedObject( null, this.rawObject.groupName );
  379. reachedFaces = false;
  380. }
  381. if ( bufferLength === 3 ) {
  382. this.rawObject.pushVertex( buffer )
  383. } else {
  384. this.rawObject.pushVertexAndVertextColors( buffer );
  385. }
  386. break;
  387. case Consts.LINE_VT:
  388. this.rawObject.pushUv( buffer );
  389. break;
  390. case Consts.LINE_VN:
  391. this.rawObject.pushNormal( buffer );
  392. break;
  393. case Consts.LINE_F:
  394. reachedFaces = true;
  395. this.rawObject.processFaces( buffer, bufferPointer, slashesCount );
  396. break;
  397. case Consts.LINE_L:
  398. if ( bufferLength === slashesCount * 2 ) {
  399. this.rawObject.buildLineVvt( buffer );
  400. } else {
  401. this.rawObject.buildLineV( buffer );
  402. }
  403. break;
  404. case Consts.LINE_S:
  405. this.rawObject.pushSmoothingGroup( buffer[ 1 ] );
  406. this.flushStringBuffer( buffer, bufferPointer );
  407. break;
  408. case Consts.LINE_G:
  409. concatBuffer = bufferLength > 1 ? buffer.slice( 1, bufferPointer ).join( ' ' ) : buffer[ 1 ];
  410. this.processCompletedGroup( concatBuffer );
  411. this.flushStringBuffer( buffer, bufferPointer );
  412. break;
  413. case Consts.LINE_O:
  414. concatBuffer = bufferLength > 1 ? buffer.slice( 1, bufferPointer ).join( ' ' ) : buffer[ 1 ];
  415. if ( this.rawObject.vertices.length > 0 ) {
  416. this.processCompletedObject( concatBuffer, null );
  417. reachedFaces = false;
  418. } else {
  419. this.rawObject.pushObject( concatBuffer );
  420. }
  421. this.flushStringBuffer( buffer, bufferPointer );
  422. break;
  423. case Consts.LINE_MTLLIB:
  424. concatBuffer = bufferLength > 1 ? buffer.slice( 1, bufferPointer ).join( ' ' ) : buffer[ 1 ];
  425. this.rawObject.pushMtllib( concatBuffer );
  426. this.flushStringBuffer( buffer, bufferPointer );
  427. break;
  428. case Consts.LINE_USEMTL:
  429. concatBuffer = bufferLength > 1 ? buffer.slice( 1, bufferPointer ).join( ' ' ) : buffer[ 1 ];
  430. this.rawObject.pushUsemtl( concatBuffer );
  431. this.flushStringBuffer( buffer, bufferPointer );
  432. break;
  433. default:
  434. break;
  435. }
  436. return reachedFaces;
  437. };
  438. Parser.prototype.flushStringBuffer = function ( buffer, bufferLength ) {
  439. for ( var i = 0; i < bufferLength; i++ ) {
  440. buffer[ i ] = '';
  441. }
  442. };
  443. Parser.prototype.processCompletedObject = function ( objectName, groupName ) {
  444. var result = this.rawObject.finalize( this.debug );
  445. if ( Validator.isValid( result ) ) {
  446. this.inputObjectCount++;
  447. if ( this.debug ) this.createReport( this.inputObjectCount, true );
  448. var message = this.buildMesh( result, this.inputObjectCount );
  449. this.onProgress( message );
  450. }
  451. this.rawObject = this.rawObject.newInstanceFromObject( objectName, groupName );
  452. };
  453. Parser.prototype.processCompletedGroup = function ( groupName ) {
  454. var result = this.rawObject.finalize();
  455. if ( Validator.isValid( result ) ) {
  456. this.inputObjectCount++;
  457. if ( this.debug ) this.createReport( this.inputObjectCount, true );
  458. var message = this.buildMesh( result, this.inputObjectCount );
  459. this.onProgress( message );
  460. this.rawObject = this.rawObject.newInstanceFromGroup( groupName );
  461. } else {
  462. // if a group was set that did not lead to object creation in finalize, then the group name has to be updated
  463. this.rawObject.pushGroup( groupName );
  464. }
  465. };
  466. Parser.prototype.finalize = function () {
  467. console.log( 'Global output object count: ' + this.outputObjectCount );
  468. var result = Validator.isValid( this.rawObject ) ? this.rawObject.finalize() : null;
  469. if ( Validator.isValid( result ) ) {
  470. this.inputObjectCount++;
  471. if ( this.debug ) this.createReport( this.inputObjectCount, true );
  472. var message = this.buildMesh( result, this.inputObjectCount );
  473. this.onProgress( message );
  474. }
  475. };
  476. Parser.prototype.onProgress = function ( text ) {
  477. if ( Validator.isValid( text ) && Validator.isValid( this.callbackProgress) ) this.callbackProgress( text );
  478. };
  479. /**
  480. * RawObjectDescriptions are transformed to too intermediate format that is forwarded to the Builder.
  481. * It is ensured that rawObjectDescriptions only contain objects with vertices (no need to check).
  482. *
  483. * @param result
  484. * @param inputObjectCount
  485. */
  486. Parser.prototype.buildMesh = function ( result, inputObjectCount ) {
  487. if ( this.debug ) console.log( 'OBJLoader.buildMesh:\nInput object no.: ' + inputObjectCount );
  488. var rawObjectDescriptions = result.rawObjectDescriptions;
  489. var vertexFA = new Float32Array( result.absoluteVertexCount );
  490. var indexUA = ( result.absoluteIndexCount > 0 ) ? new Uint32Array( result.absoluteIndexCount ) : null;
  491. var colorFA = ( result.absoluteColorCount > 0 ) ? new Float32Array( result.absoluteColorCount ) : null;
  492. var normalFA = ( result.absoluteNormalCount > 0 ) ? new Float32Array( result.absoluteNormalCount ) : null;
  493. var uvFA = ( result.absoluteUvCount > 0 ) ? new Float32Array( result.absoluteUvCount ) : null;
  494. var rawObjectDescription;
  495. var materialDescription;
  496. var materialDescriptions = [];
  497. var createMultiMaterial = ( rawObjectDescriptions.length > 1 );
  498. var materialIndex = 0;
  499. var materialIndexMapping = [];
  500. var selectedMaterialIndex;
  501. var materialGroup;
  502. var materialGroups = [];
  503. var vertexFAOffset = 0;
  504. var vertexGroupOffset = 0;
  505. var vertexLength;
  506. var indexUAOffset = 0;
  507. var colorFAOffset = 0;
  508. var normalFAOffset = 0;
  509. var uvFAOffset = 0;
  510. for ( var oodIndex in rawObjectDescriptions ) {
  511. if ( ! rawObjectDescriptions.hasOwnProperty( oodIndex ) ) continue;
  512. rawObjectDescription = rawObjectDescriptions[ oodIndex ];
  513. materialDescription = {
  514. name: rawObjectDescription.materialName,
  515. flat: false,
  516. default: false
  517. };
  518. if ( this.materialNames[ materialDescription.name ] === null ) {
  519. materialDescription.default = true;
  520. console.warn( 'object_group "' + rawObjectDescription.objectName + '_' + rawObjectDescription.groupName + '" was defined without material! Assigning "defaultMaterial".' );
  521. }
  522. // Attach '_flat' to materialName in case flat shading is needed due to smoothingGroup 0
  523. if ( rawObjectDescription.smoothingGroup === 0 ) materialDescription.flat = true;
  524. vertexLength = rawObjectDescription.vertices.length;
  525. if ( createMultiMaterial ) {
  526. // re-use material if already used before. Reduces materials array size and eliminates duplicates
  527. selectedMaterialIndex = materialIndexMapping[ materialDescription.name ];
  528. if ( ! selectedMaterialIndex ) {
  529. selectedMaterialIndex = materialIndex;
  530. materialIndexMapping[ materialDescription.name ] = materialIndex;
  531. materialDescriptions.push( materialDescription );
  532. materialIndex++;
  533. }
  534. materialGroup = {
  535. start: vertexGroupOffset,
  536. count: vertexLength / 3,
  537. index: selectedMaterialIndex
  538. };
  539. materialGroups.push( materialGroup );
  540. vertexGroupOffset += vertexLength / 3;
  541. } else {
  542. materialDescriptions.push( materialDescription );
  543. }
  544. vertexFA.set( rawObjectDescription.vertices, vertexFAOffset );
  545. vertexFAOffset += vertexLength;
  546. if ( indexUA ) {
  547. indexUA.set( rawObjectDescription.indices, indexUAOffset );
  548. indexUAOffset += rawObjectDescription.indices.length;
  549. }
  550. if ( colorFA ) {
  551. colorFA.set( rawObjectDescription.colors, colorFAOffset );
  552. colorFAOffset += rawObjectDescription.colors.length;
  553. }
  554. if ( normalFA ) {
  555. normalFA.set( rawObjectDescription.normals, normalFAOffset );
  556. normalFAOffset += rawObjectDescription.normals.length;
  557. }
  558. if ( uvFA ) {
  559. uvFA.set( rawObjectDescription.uvs, uvFAOffset );
  560. uvFAOffset += rawObjectDescription.uvs.length;
  561. }
  562. if ( this.debug ) this.printReport( rawObjectDescription, selectedMaterialIndex );
  563. }
  564. this.outputObjectCount++;
  565. this.callbackBuilder(
  566. {
  567. cmd: 'meshData',
  568. params: {
  569. meshName: rawObjectDescription.groupName !== '' ? rawObjectDescription.groupName : rawObjectDescription.objectName
  570. },
  571. materials: {
  572. multiMaterial: createMultiMaterial,
  573. materialDescriptions: materialDescriptions,
  574. materialGroups: materialGroups
  575. },
  576. buffers: {
  577. vertices: vertexFA,
  578. indices: indexUA,
  579. colors: colorFA,
  580. normals: normalFA,
  581. uvs: uvFA
  582. }
  583. },
  584. [ vertexFA.buffer ],
  585. Validator.isValid( indexUA ) ? [ indexUA.buffer ] : null,
  586. Validator.isValid( colorFA ) ? [ colorFA.buffer ] : null,
  587. Validator.isValid( normalFA ) ? [ normalFA.buffer ] : null,
  588. Validator.isValid( uvFA ) ? [ uvFA.buffer ] : null
  589. );
  590. };
  591. Parser.prototype.printReport = function ( rawObjectDescription, selectedMaterialIndex ) {
  592. var materialIndexLine = Validator.isValid( selectedMaterialIndex ) ? '\n materialIndex: ' + selectedMaterialIndex : '';
  593. console.log(
  594. ' Output Object no.: ' + this.outputObjectCount +
  595. '\n objectName: ' + rawObjectDescription.objectName +
  596. '\n groupName: ' + rawObjectDescription.groupName +
  597. '\n materialName: ' + rawObjectDescription.materialName +
  598. materialIndexLine +
  599. '\n smoothingGroup: ' + rawObjectDescription.smoothingGroup +
  600. '\n #vertices: ' + rawObjectDescription.vertices.length / 3 +
  601. '\n #colors: ' + rawObjectDescription.colors.length / 3 +
  602. '\n #uvs: ' + rawObjectDescription.uvs.length / 2 +
  603. '\n #normals: ' + rawObjectDescription.normals.length / 3
  604. );
  605. };
  606. return Parser;
  607. })();
  608. /**
  609. * {@link RawObject} is only used by {@link Parser}.
  610. * The user of OBJLoader2 does not need to care about this class.
  611. * It is defined publicly for inclusion in web worker based OBJ loader ({@link THREE.OBJLoader2.WWOBJLoader2})
  612. */
  613. var RawObject = (function () {
  614. function RawObject( materialPerSmoothingGroup, objectName, groupName, activeMtlName ) {
  615. this.globalVertexOffset = 1;
  616. this.globalUvOffset = 1;
  617. this.globalNormalOffset = 1;
  618. this.vertices = [];
  619. this.colors = [];
  620. this.normals = [];
  621. this.uvs = [];
  622. // faces are stored according combined index of group, material and smoothingGroup (0 or not)
  623. this.activeMtlName = Validator.verifyInput( activeMtlName, '' );
  624. this.objectName = Validator.verifyInput( objectName, '' );
  625. this.groupName = Validator.verifyInput( groupName, '' );
  626. this.mtllibName = '';
  627. this.activeSmoothingGroup = 1;
  628. this.materialPerSmoothingGroup = materialPerSmoothingGroup;
  629. this.mtlCount = 0;
  630. this.smoothingGroupCount = 0;
  631. this.rawObjectDescriptions = [];
  632. // this default index is required as it is possible to define faces without 'g' or 'usemtl'
  633. var index = this.buildIndex( this.activeMtlName, this.activeSmoothingGroup );
  634. this.rawObjectDescriptionInUse = new RawObjectDescription( this.objectName, this.groupName, this.activeMtlName, this.activeSmoothingGroup );
  635. this.rawObjectDescriptions[ index ] = this.rawObjectDescriptionInUse;
  636. }
  637. RawObject.prototype.setMaterialPerSmoothingGroup = function ( materialPerSmoothingGroup ) {
  638. this.materialPerSmoothingGroup = materialPerSmoothingGroup;
  639. };
  640. RawObject.prototype.buildIndex = function ( materialName, smoothingGroup ) {
  641. var normalizedSmoothingGroup = this.materialPerSmoothingGroup ? smoothingGroup : ( smoothingGroup === 0 ) ? 0 : 1;
  642. return materialName + '|' + normalizedSmoothingGroup;
  643. };
  644. RawObject.prototype.newInstanceFromObject = function ( objectName, groupName ) {
  645. var newRawObject = new RawObject( this.materialPerSmoothingGroup, objectName, groupName, this.activeMtlName );
  646. // move indices forward
  647. newRawObject.globalVertexOffset = this.globalVertexOffset + this.vertices.length / 3;
  648. newRawObject.globalUvOffset = this.globalUvOffset + this.uvs.length / 2;
  649. newRawObject.globalNormalOffset = this.globalNormalOffset + this.normals.length / 3;
  650. return newRawObject;
  651. };
  652. RawObject.prototype.newInstanceFromGroup = function ( groupName ) {
  653. var newRawObject = new RawObject( this.materialPerSmoothingGroup, this.objectName, groupName, this.activeMtlName );
  654. // keep current buffers and indices forward
  655. newRawObject.vertices = this.vertices;
  656. newRawObject.colors = this.colors;
  657. newRawObject.uvs = this.uvs;
  658. newRawObject.normals = this.normals;
  659. newRawObject.globalVertexOffset = this.globalVertexOffset;
  660. newRawObject.globalUvOffset = this.globalUvOffset;
  661. newRawObject.globalNormalOffset = this.globalNormalOffset;
  662. return newRawObject;
  663. };
  664. RawObject.prototype.pushVertex = function ( buffer ) {
  665. this.vertices.push( parseFloat( buffer[ 1 ] ) );
  666. this.vertices.push( parseFloat( buffer[ 2 ] ) );
  667. this.vertices.push( parseFloat( buffer[ 3 ] ) );
  668. };
  669. RawObject.prototype.pushVertexAndVertextColors = function ( buffer ) {
  670. this.vertices.push( parseFloat( buffer[ 1 ] ) );
  671. this.vertices.push( parseFloat( buffer[ 2 ] ) );
  672. this.vertices.push( parseFloat( buffer[ 3 ] ) );
  673. this.colors.push( parseFloat( buffer[ 4 ] ) );
  674. this.colors.push( parseFloat( buffer[ 5 ] ) );
  675. this.colors.push( parseFloat( buffer[ 6 ] ) );
  676. };
  677. RawObject.prototype.pushUv = function ( buffer ) {
  678. this.uvs.push( parseFloat( buffer[ 1 ] ) );
  679. this.uvs.push( parseFloat( buffer[ 2 ] ) );
  680. };
  681. RawObject.prototype.pushNormal = function ( buffer ) {
  682. this.normals.push( parseFloat( buffer[ 1 ] ) );
  683. this.normals.push( parseFloat( buffer[ 2 ] ) );
  684. this.normals.push( parseFloat( buffer[ 3 ] ) );
  685. };
  686. RawObject.prototype.pushObject = function ( objectName ) {
  687. this.objectName = objectName;
  688. };
  689. RawObject.prototype.pushMtllib = function ( mtllibName ) {
  690. this.mtllibName = mtllibName;
  691. };
  692. RawObject.prototype.pushGroup = function ( groupName ) {
  693. this.groupName = groupName;
  694. this.verifyIndex();
  695. };
  696. RawObject.prototype.pushUsemtl = function ( mtlName ) {
  697. if ( this.activeMtlName === mtlName || ! Validator.isValid( mtlName ) ) return;
  698. this.activeMtlName = mtlName;
  699. this.mtlCount++;
  700. this.verifyIndex();
  701. };
  702. RawObject.prototype.pushSmoothingGroup = function ( activeSmoothingGroup ) {
  703. var normalized = parseInt( activeSmoothingGroup );
  704. if ( isNaN( normalized ) ) {
  705. normalized = activeSmoothingGroup === "off" ? 0 : 1;
  706. }
  707. if ( this.activeSmoothingGroup === normalized ) return;
  708. this.activeSmoothingGroup = normalized;
  709. this.smoothingGroupCount++;
  710. this.verifyIndex();
  711. };
  712. RawObject.prototype.verifyIndex = function () {
  713. var index = this.buildIndex( this.activeMtlName, this.activeSmoothingGroup );
  714. this.rawObjectDescriptionInUse = this.rawObjectDescriptions[ index ];
  715. if ( ! Validator.isValid( this.rawObjectDescriptionInUse ) ) {
  716. this.rawObjectDescriptionInUse = new RawObjectDescription( this.objectName, this.groupName, this.activeMtlName, this.activeSmoothingGroup );
  717. this.rawObjectDescriptions[ index ] = this.rawObjectDescriptionInUse;
  718. }
  719. };
  720. RawObject.prototype.processFaces = function ( buffer, bufferPointer, slashesCount ) {
  721. var bufferLength = bufferPointer - 1;
  722. var i, length;
  723. // "f vertex ..."
  724. if ( slashesCount === 0 ) {
  725. for ( i = 2, length = bufferLength - 1; i < length; i ++ ) {
  726. this.buildFace( buffer[ 1 ] );
  727. this.buildFace( buffer[ i ] );
  728. this.buildFace( buffer[ i + 1 ] );
  729. }
  730. // "f vertex/uv ..."
  731. } else if ( bufferLength === slashesCount * 2 ) {
  732. for ( i = 3, length = bufferLength - 2; i < length; i += 2 ) {
  733. this.buildFace( buffer[ 1 ], buffer[ 2 ] );
  734. this.buildFace( buffer[ i ], buffer[ i + 1 ] );
  735. this.buildFace( buffer[ i + 2 ], buffer[ i + 3 ] );
  736. }
  737. // "f vertex/uv/normal ..."
  738. } else if ( bufferLength * 2 === slashesCount * 3 ) {
  739. for ( i = 4, length = bufferLength - 3; i < length; i += 3 ) {
  740. this.buildFace( buffer[ 1 ], buffer[ 2 ], buffer[ 3 ] );
  741. this.buildFace( buffer[ i ], buffer[ i + 1 ], buffer[ i + 2 ] );
  742. this.buildFace( buffer[ i + 3 ], buffer[ i + 4 ], buffer[ i + 5 ] );
  743. }
  744. // "f vertex//normal ..."
  745. } else {
  746. for ( i = 3, length = bufferLength - 2; i < length; i += 2 ) {
  747. this.buildFace( buffer[ 1 ], undefined, buffer[ 2 ] );
  748. this.buildFace( buffer[ i ], undefined, buffer[ i + 1 ] );
  749. this.buildFace( buffer[ i + 2 ], undefined, buffer[ i + 3 ] );
  750. }
  751. }
  752. };
  753. RawObject.prototype.buildFace = function ( faceIndexV, faceIndexU, faceIndexN ) {
  754. var indexV = ( parseInt( faceIndexV ) - this.globalVertexOffset ) * 3;
  755. var vertices = this.rawObjectDescriptionInUse.vertices;
  756. vertices.push( this.vertices[ indexV ++ ] );
  757. vertices.push( this.vertices[ indexV ++ ] );
  758. vertices.push( this.vertices[ indexV ] );
  759. if ( this.colors.length > 0 ) {
  760. indexV -= 2;
  761. var colors = this.rawObjectDescriptionInUse.colors;
  762. colors.push( this.colors[ indexV ++ ] );
  763. colors.push( this.colors[ indexV ++ ] );
  764. colors.push( this.colors[ indexV ] );
  765. }
  766. if ( faceIndexU ) {
  767. var indexU = ( parseInt( faceIndexU ) - this.globalUvOffset ) * 2;
  768. var uvs = this.rawObjectDescriptionInUse.uvs;
  769. uvs.push( this.uvs[ indexU ++ ] );
  770. uvs.push( this.uvs[ indexU ] );
  771. }
  772. if ( faceIndexN ) {
  773. var indexN = ( parseInt( faceIndexN ) - this.globalNormalOffset ) * 3;
  774. var normals = this.rawObjectDescriptionInUse.normals;
  775. normals.push( this.normals[ indexN ++ ] );
  776. normals.push( this.normals[ indexN ++ ] );
  777. normals.push( this.normals[ indexN ] );
  778. }
  779. };
  780. /*
  781. * Support for lines with or without texture. irst element in indexArray is the line identification
  782. * 0: "f vertex/uv vertex/uv ..."
  783. * 1: "f vertex vertex ..."
  784. */
  785. RawObject.prototype.buildLineVvt = function ( lineArray ) {
  786. for ( var i = 1, length = lineArray.length; i < length; i ++ ) {
  787. this.vertices.push( parseInt( lineArray[ i ] ) );
  788. this.uvs.push( parseInt( lineArray[ i ] ) );
  789. }
  790. };
  791. RawObject.prototype.buildLineV = function ( lineArray ) {
  792. for ( var i = 1, length = lineArray.length; i < length; i++ ) {
  793. this.vertices.push( parseInt( lineArray[ i ] ) );
  794. }
  795. };
  796. /**
  797. * Clear any empty rawObjectDescription and calculate absolute vertex, normal and uv counts
  798. */
  799. RawObject.prototype.finalize = function () {
  800. var temp = [];
  801. var rawObjectDescription;
  802. var absoluteVertexCount = 0;
  803. var absoluteIndexCount = 0;
  804. var absoluteColorCount = 0;
  805. var absoluteNormalCount = 0;
  806. var absoluteUvCount = 0;
  807. for ( var name in this.rawObjectDescriptions ) {
  808. rawObjectDescription = this.rawObjectDescriptions[ name ];
  809. if ( rawObjectDescription.vertices.length > 0 ) {
  810. temp.push( rawObjectDescription );
  811. absoluteVertexCount += rawObjectDescription.vertices.length;
  812. absoluteIndexCount += rawObjectDescription.indices.length;
  813. absoluteColorCount += rawObjectDescription.colors.length;
  814. absoluteUvCount += rawObjectDescription.uvs.length;
  815. absoluteNormalCount += rawObjectDescription.normals.length;
  816. }
  817. }
  818. // don not continue if no result
  819. var result = null;
  820. if ( temp.length > 0 ) {
  821. result = {
  822. rawObjectDescriptions: temp,
  823. absoluteVertexCount: absoluteVertexCount,
  824. absoluteIndexCount: absoluteIndexCount,
  825. absoluteColorCount: absoluteColorCount,
  826. absoluteNormalCount: absoluteNormalCount,
  827. absoluteUvCount: absoluteUvCount
  828. };
  829. }
  830. return result;
  831. };
  832. RawObject.prototype.createReport = function ( inputObjectCount, printDirectly ) {
  833. var report = {
  834. name: this.objectName ? this.objectName : 'groups',
  835. mtllibName: this.mtllibName,
  836. vertexCount: this.vertices.length / 3,
  837. indexCount: this.indices.length,
  838. normalCount: this.normals.length / 3,
  839. uvCount: this.uvs.length / 2,
  840. smoothingGroupCount: this.smoothingGroupCount,
  841. mtlCount: this.mtlCount,
  842. rawObjectDescriptions: this.rawObjectDescriptions.length
  843. };
  844. if ( printDirectly ) {
  845. console.log( 'Input Object number: ' + inputObjectCount + ' Object name: ' + report.name );
  846. console.log( 'Mtllib name: ' + report.mtllibName );
  847. console.log( 'Vertex count: ' + report.vertexCount );
  848. console.log( 'Index count: ' + report.indexCount );
  849. console.log( 'Normal count: ' + report.normalCount );
  850. console.log( 'UV count: ' + report.uvCount );
  851. console.log( 'SmoothingGroup count: ' + report.smoothingGroupCount );
  852. console.log( 'Material count: ' + report.mtlCount );
  853. console.log( 'Real RawObjectDescription count: ' + report.rawObjectDescriptions );
  854. console.log( '' );
  855. }
  856. return report;
  857. };
  858. return RawObject;
  859. })();
  860. /**
  861. * Descriptive information and data (vertices, normals, uvs) to passed on to mesh building function.
  862. * @class
  863. *
  864. * @param {string} objectName Name of the mesh
  865. * @param {string} groupName Name of the group
  866. * @param {string} materialName Name of the material
  867. * @param {number} smoothingGroup Normalized smoothingGroup (0: flat shading, 1: smooth shading)
  868. */
  869. var RawObjectDescription = (function () {
  870. function RawObjectDescription( objectName, groupName, materialName, smoothingGroup ) {
  871. this.objectName = objectName;
  872. this.groupName = groupName;
  873. this.materialName = materialName;
  874. this.smoothingGroup = smoothingGroup;
  875. this.vertices = [];
  876. this.indices = [];
  877. this.colors = [];
  878. this.uvs = [];
  879. this.normals = [];
  880. }
  881. return RawObjectDescription;
  882. })();
  883. OBJLoader2.prototype._checkFiles = function ( resources ) {
  884. var resource;
  885. var result = {
  886. mtl: null,
  887. obj: null
  888. };
  889. for ( var index in resources ) {
  890. resource = resources[ index ];
  891. if ( ! Validator.isValid( resource.name ) ) continue;
  892. if ( Validator.isValid( resource.content ) ) {
  893. if ( resource.extension === 'OBJ' ) {
  894. // fast-fail on bad type
  895. if ( ! ( resource.content instanceof Uint8Array ) ) throw 'Provided content is not of type arraybuffer! Aborting...';
  896. result.obj = resource;
  897. } else if ( resource.extension === 'MTL' && Validator.isValid( resource.name ) ) {
  898. if ( ! ( typeof( resource.content ) === 'string' || resource.content instanceof String ) ) throw 'Provided content is not of type String! Aborting...';
  899. result.mtl = resource;
  900. } else if ( resource.extension === "ZIP" ) {
  901. // ignore
  902. } else {
  903. throw 'Unidentified resource "' + resource.name + '": ' + resource.url;
  904. }
  905. } else {
  906. // fast-fail on bad type
  907. if ( ! ( typeof( resource.name ) === 'string' || resource.name instanceof String ) ) throw 'Provided file is not properly defined! Aborting...';
  908. if ( resource.extension === 'OBJ' ) {
  909. result.obj = resource;
  910. } else if ( resource.extension === 'MTL' ) {
  911. result.mtl = resource;
  912. } else if ( resource.extension === "ZIP" ) {
  913. // ignore
  914. } else {
  915. throw 'Unidentified resource "' + resource.name + '": ' + resource.url;
  916. }
  917. }
  918. }
  919. return result;
  920. };
  921. /**
  922. * Utility method for loading an mtl file according resource description.
  923. * @memberOf THREE.OBJLoader2
  924. *
  925. * @param {string} url URL to the file
  926. * @param {string} name The name of the object
  927. * @param {Object} content The file content as arraybuffer or text
  928. * @param {function} callbackOnLoad
  929. * @param {string} [crossOrigin] CORS value
  930. */
  931. OBJLoader2.prototype.loadMtl = function ( url, name, content, callbackOnLoad, crossOrigin ) {
  932. var resource = new THREE.LoaderSupport.ResourceDescriptor( url, 'MTL' );
  933. resource.setContent( content );
  934. this._loadMtl( resource, callbackOnLoad, crossOrigin );
  935. };
  936. /**
  937. * Utility method for loading an mtl file according resource description.
  938. * @memberOf THREE.OBJLoader2
  939. *
  940. * @param {THREE.LoaderSupport.ResourceDescriptor} resource
  941. * @param {function} callbackOnLoad
  942. * @param {string} [crossOrigin] CORS value
  943. */
  944. OBJLoader2.prototype._loadMtl = function ( resource, callbackOnLoad, crossOrigin ) {
  945. if ( Validator.isValid( resource ) ) console.time( 'Loading MTL: ' + resource.name );
  946. var materials = [];
  947. var processMaterials = function ( materialCreator ) {
  948. var materialCreatorMaterials = [];
  949. if ( Validator.isValid( materialCreator ) ) {
  950. materialCreator.preload();
  951. materialCreatorMaterials = materialCreator.materials;
  952. for ( var materialName in materialCreatorMaterials ) {
  953. if ( materialCreatorMaterials.hasOwnProperty( materialName ) ) {
  954. materials[ materialName ] = materialCreatorMaterials[ materialName ];
  955. }
  956. }
  957. }
  958. if ( Validator.isValid( resource ) ) console.timeEnd( 'Loading MTL: ' + resource.name );
  959. callbackOnLoad( materials );
  960. };
  961. var mtlLoader = new THREE.MTLLoader();
  962. crossOrigin = Validator.verifyInput( crossOrigin, 'anonymous' );
  963. mtlLoader.setCrossOrigin( crossOrigin );
  964. // fast-fail
  965. if ( ! Validator.isValid( resource ) || ( ! Validator.isValid( resource.content ) && ! Validator.isValid( resource.url ) ) ) {
  966. processMaterials();
  967. } else {
  968. mtlLoader.setPath( resource.path );
  969. if ( Validator.isValid( resource.content ) ) {
  970. processMaterials( Validator.isValid( resource.content ) ? mtlLoader.parse( resource.content ) : null );
  971. } else if ( Validator.isValid( resource.url ) ) {
  972. var onError = function ( event ) {
  973. var output = 'Error occurred while downloading "' + resource.url + '"';
  974. console.error( output + ': ' + event );
  975. throw output;
  976. };
  977. mtlLoader.load( resource.name, processMaterials, undefined, onError );
  978. }
  979. }
  980. };
  981. return OBJLoader2;
  982. })();