WWOBJLoader2.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455
  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. * OBJ data will be loaded by dynamically created web worker.
  9. * First feed instructions with: prepareRun
  10. * Then: Execute with: run
  11. * @class
  12. */
  13. THREE.OBJLoader2.WWOBJLoader2 = (function () {
  14. var WWOBJLOADER2_VERSION = '1.3.1';
  15. var Validator = THREE.OBJLoader2.prototype._getValidator();
  16. function WWOBJLoader2() {
  17. this._init();
  18. }
  19. WWOBJLoader2.prototype._init = function () {
  20. console.log( "Using THREE.OBJLoader2.WWOBJLoader2 version: " + WWOBJLOADER2_VERSION );
  21. // check worker support first
  22. if ( window.Worker === undefined ) throw "This browser does not support web workers!";
  23. if ( window.Blob === undefined ) throw "This browser does not support Blob!";
  24. if ( typeof window.URL.createObjectURL !== 'function' ) throw "This browser does not support Object creation from URL!";
  25. this.instanceNo = 0;
  26. this.worker = null;
  27. this.workerCode = null;
  28. this.debug = false;
  29. this.sceneGraphBaseNode = null;
  30. this.streamMeshes = true;
  31. this.meshStore = null;
  32. this.modelName = '';
  33. this.validated = false;
  34. this.running = false;
  35. this.requestTerminate = false;
  36. this.clearAllCallbacks();
  37. this.manager = THREE.DefaultLoadingManager;
  38. this.fileLoader = new THREE.FileLoader( this.manager );
  39. this.mtlLoader = null;
  40. this.crossOrigin = null;
  41. this.dataAvailable = false;
  42. this.objAsArrayBuffer = null;
  43. this.fileObj = null;
  44. this.pathObj = null;
  45. this.fileMtl = null;
  46. this.mtlAsString = null;
  47. this.texturePath = null;
  48. this.materials = [];
  49. this.counter = 0;
  50. };
  51. /**
  52. * Enable or disable debug logging.
  53. * @memberOf THREE.OBJLoader2.WWOBJLoader2
  54. *
  55. * @param {boolean} enabled True or false
  56. */
  57. WWOBJLoader2.prototype.setDebug = function ( enabled ) {
  58. this.debug = enabled;
  59. };
  60. /**
  61. * Sets the CORS string to be used.
  62. * @memberOf THREE.OBJLoader2.WWOBJLoader2
  63. *
  64. * @param {string} crossOrigin CORS value
  65. */
  66. WWOBJLoader2.prototype.setCrossOrigin = function ( crossOrigin ) {
  67. this.crossOrigin = crossOrigin;
  68. };
  69. /**
  70. * Register callback function that is invoked by internal function "_announceProgress" to print feedback.
  71. * @memberOf THREE.OBJLoader2.WWOBJLoader2
  72. *
  73. * @param {callback} callbackProgress Callback function for described functionality
  74. */
  75. WWOBJLoader2.prototype.registerCallbackProgress = function ( callbackProgress ) {
  76. if ( Validator.isValid( callbackProgress ) ) this.callbacks.progress.push( callbackProgress );
  77. };
  78. /**
  79. * Register callback function that is called once loading of the complete model is completed.
  80. * @memberOf THREE.OBJLoader2.WWOBJLoader2
  81. *
  82. * @param {callback} callbackCompletedLoading Callback function for described functionality
  83. */
  84. WWOBJLoader2.prototype.registerCallbackCompletedLoading = function ( callbackCompletedLoading ) {
  85. if ( Validator.isValid( callbackCompletedLoading ) ) this.callbacks.completedLoading.push( callbackCompletedLoading );
  86. };
  87. /**
  88. * Register callback function that is called once materials have been loaded. It allows to alter and return materials.
  89. * @memberOf THREE.OBJLoader2.WWOBJLoader2
  90. *
  91. * @param {callback} callbackMaterialsLoaded Callback function for described functionality
  92. */
  93. WWOBJLoader2.prototype.registerCallbackMaterialsLoaded = function ( callbackMaterialsLoaded ) {
  94. if ( Validator.isValid( callbackMaterialsLoaded ) ) this.callbacks.materialsLoaded.push( callbackMaterialsLoaded );
  95. };
  96. /**
  97. * Register callback function that is called every time a mesh was loaded.
  98. * Use {@link THREE.OBJLoader2.WWOBJLoader2.LoadedMeshUserOverride} for alteration instructions (geometry, material or disregard mesh).
  99. * @memberOf THREE.OBJLoader2.WWOBJLoader2
  100. *
  101. * @param {callback} callbackMeshLoaded Callback function for described functionality
  102. */
  103. WWOBJLoader2.prototype.registerCallbackMeshLoaded = function ( callbackMeshLoaded ) {
  104. if ( Validator.isValid( callbackMeshLoaded ) ) this.callbacks.meshLoaded.push( callbackMeshLoaded );
  105. };
  106. /**
  107. * Register callback function that is called to report an error that prevented loading.
  108. * @memberOf THREE.OBJLoader2.WWOBJLoader2
  109. *
  110. * @param {callback} callbackErrorWhileLoading Callback function for described functionality
  111. */
  112. WWOBJLoader2.prototype.registerCallbackErrorWhileLoading = function ( callbackErrorWhileLoading ) {
  113. if ( Validator.isValid( callbackErrorWhileLoading ) ) this.callbacks.errorWhileLoading.push( callbackErrorWhileLoading );
  114. };
  115. /**
  116. * Clears all registered callbacks.
  117. * @memberOf THREE.OBJLoader2.WWOBJLoader2
  118. */
  119. WWOBJLoader2.prototype.clearAllCallbacks = function () {
  120. this.callbacks = {
  121. progress: [],
  122. completedLoading: [],
  123. errorWhileLoading: [],
  124. materialsLoaded: [],
  125. meshLoaded: []
  126. };
  127. };
  128. /**
  129. * Call requestTerminate to terminate the web worker and free local resource after execution.
  130. * @memberOf THREE.OBJLoader2.WWOBJLoader2
  131. *
  132. * @param {boolean} requestTerminate True or false
  133. */
  134. WWOBJLoader2.prototype.setRequestTerminate = function ( requestTerminate ) {
  135. this.requestTerminate = requestTerminate === true;
  136. };
  137. WWOBJLoader2.prototype._validate = function () {
  138. if ( this.validated ) return;
  139. if ( ! Validator.isValid( this.worker ) ) {
  140. this._buildWebWorkerCode();
  141. var blob = new Blob( [ this.workerCode ], { type: 'text/plain' } );
  142. this.worker = new Worker( window.URL.createObjectURL( blob ) );
  143. var scope = this;
  144. var scopeFunction = function ( e ) {
  145. scope._receiveWorkerMessage( e );
  146. };
  147. this.worker.addEventListener( 'message', scopeFunction, false );
  148. }
  149. this.sceneGraphBaseNode = null;
  150. this.streamMeshes = true;
  151. this.meshStore = [];
  152. this.modelName = '';
  153. this.validated = true;
  154. this.running = true;
  155. this.requestTerminate = false;
  156. this.fileLoader = Validator.verifyInput( this.fileLoader, new THREE.FileLoader( this.manager ) );
  157. this.mtlLoader = Validator.verifyInput( this.mtlLoader, new THREE.MTLLoader() );
  158. if ( Validator.isValid( this.crossOrigin ) ) this.mtlLoader.setCrossOrigin( this.crossOrigin );
  159. this.dataAvailable = false;
  160. this.fileObj = null;
  161. this.pathObj = null;
  162. this.fileMtl = null;
  163. this.texturePath = null;
  164. this.objAsArrayBuffer = null;
  165. this.mtlAsString = null;
  166. this.materials = [];
  167. var defaultMaterial = new THREE.MeshStandardMaterial( { color: 0xDCF1FF } );
  168. defaultMaterial.name = 'defaultMaterial';
  169. this.materials[ defaultMaterial.name ] = defaultMaterial;
  170. var vertexColorMaterial = new THREE.MeshBasicMaterial( { color: 0xDCF1FF } );
  171. vertexColorMaterial.name = 'vertexColorMaterial';
  172. vertexColorMaterial.vertexColors = THREE.VertexColors;
  173. this.materials[ 'vertexColorMaterial' ] = vertexColorMaterial;
  174. this.counter = 0;
  175. };
  176. /**
  177. * Set all parameters for required for execution of "run".
  178. * @memberOf THREE.OBJLoader2.WWOBJLoader2
  179. *
  180. * @param {Object} params Either {@link THREE.OBJLoader2.WWOBJLoader2.PrepDataArrayBuffer} or {@link THREE.OBJLoader2.WWOBJLoader2.PrepDataFile}
  181. */
  182. WWOBJLoader2.prototype.prepareRun = function ( params ) {
  183. this._validate();
  184. this.dataAvailable = params.dataAvailable;
  185. this.modelName = params.modelName;
  186. console.time( 'WWOBJLoader2' );
  187. if ( this.dataAvailable ) {
  188. // fast-fail on bad type
  189. if ( ! ( params.objAsArrayBuffer instanceof Uint8Array ) ) {
  190. throw 'Provided input is not of type arraybuffer! Aborting...';
  191. }
  192. this.worker.postMessage( {
  193. cmd: 'init',
  194. debug: this.debug
  195. } );
  196. this.objAsArrayBuffer = params.objAsArrayBuffer;
  197. this.mtlAsString = params.mtlAsString;
  198. } else {
  199. // fast-fail on bad type
  200. if ( ! ( typeof( params.fileObj ) === 'string' || params.fileObj instanceof String ) ) {
  201. throw 'Provided file is not properly defined! Aborting...';
  202. }
  203. this.worker.postMessage( {
  204. cmd: 'init',
  205. debug: this.debug
  206. } );
  207. this.fileObj = params.fileObj;
  208. this.pathObj = params.pathObj;
  209. this.fileMtl = params.fileMtl;
  210. }
  211. this.setRequestTerminate( params.requestTerminate );
  212. this.pathTexture = params.pathTexture;
  213. this.sceneGraphBaseNode = params.sceneGraphBaseNode;
  214. this.streamMeshes = params.streamMeshes;
  215. if ( ! this.streamMeshes ) this.meshStore = [];
  216. };
  217. /**
  218. * Run the loader according the preparation instruction provided in "prepareRun".
  219. * @memberOf THREE.OBJLoader2.WWOBJLoader2
  220. */
  221. WWOBJLoader2.prototype.run = function () {
  222. var scope = this;
  223. var processLoadedMaterials = function ( materialCreator ) {
  224. var materialCreatorMaterials = [];
  225. var materialNames = [];
  226. if ( Validator.isValid( materialCreator ) ) {
  227. materialCreator.preload();
  228. materialCreatorMaterials = materialCreator.materials;
  229. for ( var materialName in materialCreatorMaterials ) {
  230. if ( materialCreatorMaterials.hasOwnProperty( materialName ) ) {
  231. materialNames.push( materialName );
  232. scope.materials[ materialName ] = materialCreatorMaterials[ materialName ];
  233. }
  234. }
  235. }
  236. scope.worker.postMessage( {
  237. cmd: 'setMaterials',
  238. materialNames: materialNames
  239. } );
  240. var materialsFromCallback;
  241. var callbackMaterialsLoaded;
  242. for ( var index in scope.callbacks.materialsLoaded ) {
  243. callbackMaterialsLoaded = scope.callbacks.materialsLoaded[ index ];
  244. materialsFromCallback = callbackMaterialsLoaded( scope.materials );
  245. if ( Validator.isValid( materialsFromCallback ) ) scope.materials = materialsFromCallback;
  246. }
  247. if ( scope.dataAvailable && scope.objAsArrayBuffer ) {
  248. scope.worker.postMessage({
  249. cmd: 'run',
  250. objAsArrayBuffer: scope.objAsArrayBuffer
  251. }, [ scope.objAsArrayBuffer.buffer ] );
  252. } else {
  253. var refPercentComplete = 0;
  254. var percentComplete = 0;
  255. var onLoad = function ( objAsArrayBuffer ) {
  256. scope._announceProgress( 'Running web worker!' );
  257. scope.objAsArrayBuffer = new Uint8Array( objAsArrayBuffer );
  258. scope.worker.postMessage( {
  259. cmd: 'run',
  260. objAsArrayBuffer: scope.objAsArrayBuffer
  261. }, [ scope.objAsArrayBuffer.buffer ] );
  262. };
  263. var onProgress = function ( event ) {
  264. if ( ! event.lengthComputable ) return;
  265. percentComplete = Math.round( event.loaded / event.total * 100 );
  266. if ( percentComplete > refPercentComplete ) {
  267. refPercentComplete = percentComplete;
  268. var output = 'Download of "' + scope.fileObj + '": ' + percentComplete + '%';
  269. console.log( output );
  270. scope._announceProgress( output );
  271. }
  272. };
  273. var onError = function ( event ) {
  274. var output = 'Error occurred while downloading "' + scope.fileObj + '"';
  275. console.error( output + ': ' + event );
  276. scope._announceProgress( output );
  277. scope._finalize( 'error' );
  278. };
  279. scope.fileLoader.setPath( scope.pathObj );
  280. scope.fileLoader.setResponseType( 'arraybuffer' );
  281. scope.fileLoader.load( scope.fileObj, onLoad, onProgress, onError );
  282. }
  283. console.timeEnd( 'Loading MTL textures' );
  284. };
  285. this.mtlLoader.setPath( this.pathTexture );
  286. if ( this.dataAvailable ) {
  287. processLoadedMaterials( Validator.isValid( this.mtlAsString ) ? this.mtlLoader.parse( this.mtlAsString ) : null );
  288. } else {
  289. if ( Validator.isValid( this.fileMtl ) ) {
  290. var onError = function ( event ) {
  291. var output = 'Error occurred while downloading "' + scope.fileMtl + '"';
  292. console.error( output + ': ' + event );
  293. scope._announceProgress( output );
  294. scope._finalize( 'error' );
  295. };
  296. this.mtlLoader.load( this.fileMtl, processLoadedMaterials, undefined, onError );
  297. } else {
  298. processLoadedMaterials();
  299. }
  300. }
  301. };
  302. WWOBJLoader2.prototype._receiveWorkerMessage = function ( event ) {
  303. var payload = event.data;
  304. switch ( payload.cmd ) {
  305. case 'objData':
  306. this.counter++;
  307. var meshName = payload.meshName;
  308. var bufferGeometry = new THREE.BufferGeometry();
  309. bufferGeometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( payload.vertices ), 3 ) );
  310. var haveVertexColors = Validator.isValid( payload.colors );
  311. if ( haveVertexColors ) {
  312. bufferGeometry.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( payload.colors ), 3 ) );
  313. }
  314. if ( Validator.isValid( payload.normals ) ) {
  315. bufferGeometry.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( payload.normals ), 3 ) );
  316. } else {
  317. bufferGeometry.computeVertexNormals();
  318. }
  319. if ( Validator.isValid( payload.uvs ) ) {
  320. bufferGeometry.addAttribute( 'uv', new THREE.BufferAttribute( new Float32Array( payload.uvs ), 2 ) );
  321. }
  322. var materialDescriptions = payload.materialDescriptions;
  323. var materialDescription;
  324. var material;
  325. var materialName;
  326. var createMultiMaterial = payload.multiMaterial;
  327. var multiMaterials = [];
  328. var key;
  329. for ( key in materialDescriptions ) {
  330. materialDescription = materialDescriptions[ key ];
  331. material = haveVertexColors ? this.materials[ 'vertexColorMaterial' ] : this.materials[ materialDescription.name ];
  332. if ( ! material ) material = this.materials[ 'defaultMaterial' ];
  333. if ( materialDescription.default ) {
  334. material = this.materials[ 'defaultMaterial' ];
  335. } else if ( materialDescription.flat ) {
  336. materialName = material.name + '_flat';
  337. var materialClone = this.materials[ materialName ];
  338. if ( ! materialClone ) {
  339. materialClone = material.clone();
  340. materialClone.name = materialName;
  341. materialClone.flatShading = true;
  342. this.materials[ materialName ] = name;
  343. }
  344. }
  345. if ( materialDescription.vertexColors ) material.vertexColors = THREE.VertexColors;
  346. if ( createMultiMaterial ) multiMaterials.push( material );
  347. }
  348. if ( createMultiMaterial ) {
  349. material = multiMaterials;
  350. var materialGroups = payload.materialGroups;
  351. var materialGroup;
  352. for ( key in materialGroups ) {
  353. materialGroup = materialGroups[ key ];
  354. bufferGeometry.addGroup( materialGroup.start, materialGroup.count, materialGroup.index );
  355. }
  356. }
  357. var callbackMeshLoaded;
  358. var callbackMeshLoadedResult;
  359. var disregardMesh = false;
  360. for ( var index in this.callbacks.meshLoaded ) {
  361. callbackMeshLoaded = this.callbacks.meshLoaded[ index ];
  362. callbackMeshLoadedResult = callbackMeshLoaded( meshName, bufferGeometry, material );
  363. if ( Validator.isValid( callbackMeshLoadedResult ) ) {
  364. if ( callbackMeshLoadedResult.disregardMesh ) {
  365. // if one callback disregards the mesh, then processing stops
  366. disregardMesh = true;
  367. break;
  368. }
  369. if ( callbackMeshLoadedResult.replaceBufferGeometry ) bufferGeometry = callbackMeshLoadedResult.bufferGeometry;
  370. if ( callbackMeshLoadedResult.replaceMaterial ) material = callbackMeshLoadedResult.material;
  371. }
  372. }
  373. if ( !disregardMesh ) {
  374. var mesh = new THREE.Mesh( bufferGeometry, material );
  375. mesh.name = meshName;
  376. if ( this.streamMeshes ) {
  377. this.sceneGraphBaseNode.add( mesh );
  378. } else {
  379. this.meshStore.push( mesh );
  380. }
  381. this._announceProgress( 'Adding mesh (' + this.counter + '):', meshName );
  382. } else {
  383. this._announceProgress( 'Removing mesh:', meshName );
  384. }
  385. break;
  386. case 'complete':
  387. if ( ! this.streamMeshes ) {
  388. for ( var meshStoreKey in this.meshStore ) {
  389. if ( this.meshStore.hasOwnProperty( meshStoreKey ) ) this.sceneGraphBaseNode.add( this.meshStore[ meshStoreKey ] );
  390. }
  391. }
  392. console.timeEnd( 'WWOBJLoader2' );
  393. if ( Validator.isValid( payload.msg ) ) {
  394. this._announceProgress( payload.msg );
  395. } else {
  396. this._announceProgress( '' );
  397. }
  398. this._finalize( 'complete' );
  399. break;
  400. case 'report_progress':
  401. this._announceProgress( '', payload.output );
  402. break;
  403. default:
  404. console.error( 'Received unknown command: ' + payload.cmd );
  405. break;
  406. }
  407. };
  408. WWOBJLoader2.prototype._terminate = function () {
  409. if ( Validator.isValid( this.worker ) ) {
  410. if ( this.running ) throw 'Unable to gracefully terminate worker as it is currently running!';
  411. this.worker.terminate();
  412. this.worker = null;
  413. this.workerCode = null;
  414. this._finalize( 'terminate' );
  415. }
  416. this.fileLoader = null;
  417. this.mtlLoader = null;
  418. };
  419. WWOBJLoader2.prototype._finalize = function ( reason, requestTerminate ) {
  420. this.running = false;
  421. var index;
  422. var callback;
  423. if ( reason === 'complete' ) {
  424. for ( index in this.callbacks.completedLoading ) {
  425. callback = this.callbacks.completedLoading[ index ];
  426. callback( this.modelName, this.instanceNo, this.requestTerminate );
  427. }
  428. } else if ( reason === 'error' ) {
  429. for ( index in this.callbacks.errorWhileLoading ) {
  430. callback = this.callbacks.errorWhileLoading[ index ];
  431. callback( this.modelName, this.instanceNo, this.requestTerminate );
  432. }
  433. }
  434. this.validated = false;
  435. this.setRequestTerminate( requestTerminate );
  436. if ( this.requestTerminate ) {
  437. this._terminate();
  438. }
  439. };
  440. WWOBJLoader2.prototype._announceProgress = function ( baseText, text ) {
  441. var output = Validator.isValid( baseText ) ? baseText: "";
  442. output = Validator.isValid( text ) ? output + " " + text : output;
  443. var callbackProgress;
  444. for ( var index in this.callbacks.progress ) {
  445. callbackProgress = this.callbacks.progress[ index ];
  446. callbackProgress( output, this.instanceNo );
  447. }
  448. if ( this.debug ) console.log( output );
  449. };
  450. WWOBJLoader2.prototype._buildWebWorkerCode = function ( existingWorkerCode ) {
  451. if ( Validator.isValid( existingWorkerCode ) ) this.workerCode = existingWorkerCode;
  452. if ( ! Validator.isValid( this.workerCode ) ) {
  453. console.time( 'buildWebWorkerCode' );
  454. var wwDef = (function () {
  455. function WWOBJLoader() {
  456. this.wwMeshCreator = new WWMeshCreator();
  457. this.parser = new Parser( this.wwMeshCreator );
  458. this.validated = false;
  459. this.cmdState = 'created';
  460. this.debug = false;
  461. }
  462. /**
  463. * Allows to set debug mode for the parser and the meshCreatorDebug
  464. *
  465. * @param parserDebug
  466. * @param meshCreatorDebug
  467. */
  468. WWOBJLoader.prototype.setDebug = function ( parserDebug, meshCreatorDebug ) {
  469. this.parser.setDebug( parserDebug );
  470. this.wwMeshCreator.setDebug( meshCreatorDebug );
  471. };
  472. /**
  473. * Validate status, then parse arrayBuffer, finalize and return objGroup
  474. *
  475. * @param arrayBuffer
  476. */
  477. WWOBJLoader.prototype.parse = function ( arrayBuffer ) {
  478. console.log( 'Parsing arrayBuffer...' );
  479. console.time( 'parseArrayBuffer' );
  480. this.validate();
  481. this.parser.parseArrayBuffer( arrayBuffer );
  482. var objGroup = this._finalize();
  483. console.timeEnd( 'parseArrayBuffer' );
  484. return objGroup;
  485. };
  486. WWOBJLoader.prototype.validate = function () {
  487. if ( this.validated ) return;
  488. this.parser.validate();
  489. this.wwMeshCreator.validate();
  490. this.validated = true;
  491. };
  492. WWOBJLoader.prototype._finalize = function () {
  493. console.log( 'Global output object count: ' + this.wwMeshCreator.globalObjectCount );
  494. this.parser.finalize();
  495. this.wwMeshCreator.finalize();
  496. this.validated = false;
  497. };
  498. WWOBJLoader.prototype.init = function ( payload ) {
  499. this.cmdState = 'init';
  500. this.setDebug( payload.debug, payload.debug );
  501. };
  502. WWOBJLoader.prototype.setMaterials = function ( payload ) {
  503. this.cmdState = 'setMaterials';
  504. this.wwMeshCreator.setMaterials( payload.materialNames );
  505. };
  506. WWOBJLoader.prototype.run = function ( payload ) {
  507. this.cmdState = 'run';
  508. this.parse( payload.objAsArrayBuffer );
  509. console.log( 'OBJ loading complete!' );
  510. this.cmdState = 'complete';
  511. self.postMessage( {
  512. cmd: this.cmdState,
  513. msg: null
  514. } );
  515. };
  516. return WWOBJLoader;
  517. })();
  518. var wwMeshCreatorDef = (function () {
  519. function WWMeshCreator() {
  520. this.materials = null;
  521. this.debug = false;
  522. this.globalObjectCount = 1;
  523. this.validated = false;
  524. }
  525. WWMeshCreator.prototype.setMaterials = function ( materials ) {
  526. this.materials = Validator.verifyInput( materials, this.materials );
  527. this.materials = Validator.verifyInput( this.materials, { materials: [] } );
  528. };
  529. WWMeshCreator.prototype.setDebug = function ( debug ) {
  530. if ( debug === true || debug === false ) this.debug = debug;
  531. };
  532. WWMeshCreator.prototype.validate = function () {
  533. if ( this.validated ) return;
  534. this.setMaterials( null );
  535. this.setDebug( null );
  536. this.globalObjectCount = 1;
  537. };
  538. WWMeshCreator.prototype.finalize = function () {
  539. this.materials = null;
  540. this.validated = false;
  541. };
  542. /**
  543. * RawObjectDescriptions are transformed to THREE.Mesh.
  544. * It is ensured that rawObjectDescriptions only contain objects with vertices (no need to check).
  545. *
  546. * @param rawObjectDescriptions
  547. * @param inputObjectCount
  548. * @param absoluteVertexCount
  549. * @param absoluteNormalCount
  550. * @param absoluteUvCount
  551. */
  552. WWMeshCreator.prototype.buildMesh = function ( rawObjectDescriptions, inputObjectCount, absoluteVertexCount,
  553. absoluteColorCount, absoluteNormalCount, absoluteUvCount ) {
  554. if ( this.debug ) console.log( 'OBJLoader.buildMesh:\nInput object no.: ' + inputObjectCount );
  555. var vertexFA = new Float32Array( absoluteVertexCount );
  556. var colorFA = ( absoluteColorCount > 0 ) ? new Float32Array( absoluteColorCount ) : null;
  557. var normalFA = ( absoluteNormalCount > 0 ) ? new Float32Array( absoluteNormalCount ) : null;
  558. var uvFA = ( absoluteUvCount > 0 ) ? new Float32Array( absoluteUvCount ) : null;
  559. var rawObjectDescription;
  560. var materialDescription;
  561. var materialDescriptions = [];
  562. var createMultiMaterial = ( rawObjectDescriptions.length > 1 );
  563. var materialIndex = 0;
  564. var materialIndexMapping = [];
  565. var selectedMaterialIndex;
  566. var materialGroup;
  567. var materialGroups = [];
  568. var vertexFAOffset = 0;
  569. var vertexGroupOffset = 0;
  570. var vertexLength;
  571. var colorFAOffset = 0;
  572. var normalFAOffset = 0;
  573. var uvFAOffset = 0;
  574. for ( var oodIndex in rawObjectDescriptions ) {
  575. if ( ! rawObjectDescriptions.hasOwnProperty( oodIndex ) ) continue;
  576. rawObjectDescription = rawObjectDescriptions[ oodIndex ];
  577. materialDescription = {
  578. name: rawObjectDescription.materialName,
  579. flat: false,
  580. vertexColors: false,
  581. default: false
  582. };
  583. if ( this.materials[ materialDescription.name ] === null ) {
  584. materialDescription.default = true;
  585. console.warn( 'object_group "' + rawObjectDescription.objectName + '_' + rawObjectDescription.groupName + '" was defined without material! Assigning "defaultMaterial".' );
  586. }
  587. // Attach '_flat' to materialName in case flat shading is needed due to smoothingGroup 0
  588. if ( rawObjectDescription.smoothingGroup === 0 ) materialDescription.flat = true;
  589. vertexLength = rawObjectDescription.vertices.length;
  590. if ( createMultiMaterial ) {
  591. // re-use material if already used before. Reduces materials array size and eliminates duplicates
  592. selectedMaterialIndex = materialIndexMapping[ materialDescription.name ];
  593. if ( ! selectedMaterialIndex ) {
  594. selectedMaterialIndex = materialIndex;
  595. materialIndexMapping[ materialDescription.name ] = materialIndex;
  596. materialDescriptions.push( materialDescription );
  597. materialIndex++;
  598. }
  599. materialGroup = {
  600. start: vertexGroupOffset,
  601. count: vertexLength / 3,
  602. index: selectedMaterialIndex
  603. };
  604. materialGroups.push( materialGroup );
  605. vertexGroupOffset += vertexLength / 3;
  606. } else {
  607. materialDescriptions.push( materialDescription );
  608. }
  609. vertexFA.set( rawObjectDescription.vertices, vertexFAOffset );
  610. vertexFAOffset += vertexLength;
  611. if ( colorFA ) {
  612. colorFA.set( rawObjectDescription.colors, colorFAOffset );
  613. colorFAOffset += rawObjectDescription.colors.length;
  614. materialDescription.vertexColors = true;
  615. }
  616. if ( normalFA ) {
  617. normalFA.set( rawObjectDescription.normals, normalFAOffset );
  618. normalFAOffset += rawObjectDescription.normals.length;
  619. }
  620. if ( uvFA ) {
  621. uvFA.set( rawObjectDescription.uvs, uvFAOffset );
  622. uvFAOffset += rawObjectDescription.uvs.length;
  623. }
  624. if ( this.debug ) this.printReport( rawObjectDescription, selectedMaterialIndex );
  625. }
  626. self.postMessage(
  627. {
  628. cmd: 'objData',
  629. meshName: rawObjectDescription.groupName !== '' ? rawObjectDescription.groupName : rawObjectDescription.objectName,
  630. multiMaterial: createMultiMaterial,
  631. materialDescriptions: materialDescriptions,
  632. materialGroups: materialGroups,
  633. vertices: vertexFA,
  634. colors: colorFA,
  635. normals: normalFA,
  636. uvs: uvFA
  637. },
  638. [ vertexFA.buffer ],
  639. colorFA !== null ? [ colorFA.buffer ] : null,
  640. normalFA !== null ? [ normalFA.buffer ] : null,
  641. uvFA !== null ? [ uvFA.buffer ] : null
  642. );
  643. this.globalObjectCount++;
  644. };
  645. WWMeshCreator.prototype.printReport = function ( rawObjectDescription, selectedMaterialIndex ) {
  646. var materialIndexLine = Validator.isValid( selectedMaterialIndex ) ? '\n materialIndex: ' + selectedMaterialIndex : '';
  647. console.log(
  648. ' Output Object no.: ' + this.globalObjectCount +
  649. '\n objectName: ' + rawObjectDescription.objectName +
  650. '\n groupName: ' + rawObjectDescription.groupName +
  651. '\n materialName: ' + rawObjectDescription.materialName +
  652. materialIndexLine +
  653. '\n smoothingGroup: ' + rawObjectDescription.smoothingGroup +
  654. '\n #vertices: ' + rawObjectDescription.vertices.length / 3 +
  655. '\n #colors: ' + rawObjectDescription.colors.length / 3 +
  656. '\n #uvs: ' + rawObjectDescription.uvs.length / 2 +
  657. '\n #normals: ' + rawObjectDescription.normals.length / 3
  658. );
  659. };
  660. return WWMeshCreator;
  661. })();
  662. var wwObjLoaderRunnerDef = (function () {
  663. function WWOBJLoaderRunner() {
  664. self.addEventListener( 'message', this.runner, false );
  665. }
  666. WWOBJLoaderRunner.prototype.runner = function ( event ) {
  667. var payload = event.data;
  668. console.log( 'Command state before: ' + WWOBJLoaderRef.cmdState );
  669. switch ( payload.cmd ) {
  670. case 'init':
  671. WWOBJLoaderRef.init( payload );
  672. break;
  673. case 'setMaterials':
  674. WWOBJLoaderRef.setMaterials( payload );
  675. break;
  676. case 'run':
  677. WWOBJLoaderRef.run( payload );
  678. break;
  679. default:
  680. console.error( 'OBJLoader: Received unknown command: ' + payload.cmd );
  681. break;
  682. }
  683. console.log( 'Command state after: ' + WWOBJLoaderRef.cmdState );
  684. };
  685. return WWOBJLoaderRunner;
  686. })();
  687. var buildObject = function ( fullName, object ) {
  688. var objectString = fullName + ' = {\n';
  689. var part;
  690. for ( var name in object ) {
  691. part = object[ name ];
  692. if ( typeof( part ) === 'string' || part instanceof String ) {
  693. part = part.replace( '\n', '\\n' );
  694. part = part.replace( '\r', '\\r' );
  695. objectString += '\t' + name + ': "' + part + '",\n';
  696. } else if ( part instanceof Array ) {
  697. objectString += '\t' + name + ': [' + part + '],\n';
  698. } else if ( Number.isInteger( part ) ) {
  699. objectString += '\t' + name + ': ' + part + ',\n';
  700. } else if ( typeof part === 'function' ) {
  701. objectString += '\t' + name + ': ' + part + ',\n';
  702. }
  703. }
  704. objectString += '}\n\n';
  705. return objectString;
  706. };
  707. var buildSingelton = function ( fullName, internalName, object ) {
  708. var objectString = fullName + ' = (function () {\n\n';
  709. objectString += '\t' + object.prototype.constructor.toString() + '\n\n';
  710. var funcString;
  711. var objectPart;
  712. for ( var name in object.prototype ) {
  713. objectPart = object.prototype[ name ];
  714. if ( typeof objectPart === 'function' ) {
  715. funcString = objectPart.toString();
  716. objectString += '\t' + internalName + '.prototype.' + name + ' = ' + funcString + ';\n\n';
  717. }
  718. }
  719. objectString += '\treturn ' + internalName + ';\n';
  720. objectString += '})();\n\n';
  721. return objectString;
  722. };
  723. this.workerCode = '';
  724. this.workerCode += '/**\n';
  725. this.workerCode += ' * This code was constructed by WWOBJLoader2._buildWebWorkerCode\n';
  726. this.workerCode += ' */\n\n';
  727. // parser re-construction
  728. this.workerCode += THREE.OBJLoader2.prototype._buildWebWorkerCode( buildObject, buildSingelton );
  729. // web worker construction
  730. this.workerCode += buildSingelton( 'WWOBJLoader', 'WWOBJLoader', wwDef );
  731. this.workerCode += buildSingelton( 'WWMeshCreator', 'WWMeshCreator', wwMeshCreatorDef );
  732. this.workerCode += 'WWOBJLoaderRef = new WWOBJLoader();\n\n';
  733. this.workerCode += buildSingelton( 'WWOBJLoaderRunner', 'WWOBJLoaderRunner', wwObjLoaderRunnerDef );
  734. this.workerCode += 'new WWOBJLoaderRunner();\n\n';
  735. console.timeEnd( 'buildWebWorkerCode' );
  736. }
  737. return this.workerCode;
  738. };
  739. return WWOBJLoader2;
  740. })();
  741. /**
  742. * Instruction to configure {@link THREE.OBJLoader2.WWOBJLoader2}.prepareRun to load OBJ from given ArrayBuffer and MTL from given String.
  743. *
  744. * @param {string} modelName Overall name of the model
  745. * @param {Uint8Array} objAsArrayBuffer OBJ file content as ArrayBuffer
  746. * @param {string} pathTexture Path to texture files
  747. * @param {string} mtlAsString MTL file content as string
  748. *
  749. * @returns {{modelName: string, dataAvailable: boolean, objAsArrayBuffer: null, pathTexture: null, mtlAsString: null, sceneGraphBaseNode: null, streamMeshes: boolean, requestTerminate: boolean}}
  750. * @constructor
  751. */
  752. THREE.OBJLoader2.WWOBJLoader2.PrepDataArrayBuffer = function ( modelName, objAsArrayBuffer, pathTexture, mtlAsString ) {
  753. var Validator = THREE.OBJLoader2.prototype._getValidator();
  754. return {
  755. /**
  756. * {@link THREE.Object3D} where meshes will be attached.
  757. * @memberOf THREE.OBJLoader2.WWOBJLoader2.PrepDataArrayBuffer
  758. *
  759. * @param {THREE.Object3D} sceneGraphBaseNode Scene graph object
  760. */
  761. setSceneGraphBaseNode: function ( sceneGraphBaseNode ) {
  762. this.sceneGraphBaseNode = Validator.verifyInput( sceneGraphBaseNode, null );
  763. },
  764. /**
  765. * Singles meshes are directly integrated into scene when loaded or later.
  766. * @memberOf THREE.OBJLoader2.WWOBJLoader2.PrepDataArrayBuffer
  767. *
  768. * @param {boolean} streamMeshes=true Default is true
  769. */
  770. setStreamMeshes: function ( streamMeshes ) {
  771. this.streamMeshes = streamMeshes !== false;
  772. },
  773. /**
  774. * Request termination of web worker and free local resources after execution.
  775. * @memberOf THREE.OBJLoader2.WWOBJLoader2.PrepDataArrayBuffer
  776. *
  777. * @param {boolean} requestTerminate=false Default is false
  778. */
  779. setRequestTerminate: function ( requestTerminate ) {
  780. this.requestTerminate = requestTerminate === true;
  781. },
  782. /**
  783. * Returns all callbacks as {@link THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks}
  784. * @memberOf THREE.OBJLoader2.WWOBJLoader2.PrepDataArrayBuffer
  785. *
  786. * @returns {THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks}
  787. */
  788. getCallbacks: function () {
  789. return this.callbacks;
  790. },
  791. modelName: Validator.verifyInput( modelName, '' ),
  792. dataAvailable: true,
  793. objAsArrayBuffer: Validator.verifyInput( objAsArrayBuffer, null ),
  794. pathTexture: Validator.verifyInput( pathTexture, null ),
  795. mtlAsString: Validator.verifyInput( mtlAsString, null ),
  796. sceneGraphBaseNode: null,
  797. streamMeshes: true,
  798. requestTerminate: false,
  799. callbacks: new THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks()
  800. };
  801. };
  802. /**
  803. * Instruction to configure {@link THREE.OBJLoader2.WWOBJLoader2}.prepareRun to load OBJ and MTL from files.
  804. *
  805. * @param {string} modelName Overall name of the model
  806. * @param {string} pathObj Path to OBJ file
  807. * @param {string} fileObj OBJ file name
  808. * @param {string} pathTexture Path to texture files
  809. * @param {string} fileMtl MTL file name
  810. *
  811. * @returns {{modelName: string, dataAvailable: boolean, pathObj: null, fileObj: null, pathTexture: null, fileMtl: null, sceneGraphBaseNode: null, streamMeshes: boolean, requestTerminate: boolean}}
  812. * @constructor
  813. */
  814. THREE.OBJLoader2.WWOBJLoader2.PrepDataFile = function ( modelName, pathObj, fileObj, pathTexture, fileMtl ) {
  815. var Validator = THREE.OBJLoader2.prototype._getValidator();
  816. return {
  817. /**
  818. * {@link THREE.Object3D} where meshes will be attached.
  819. * @memberOf THREE.OBJLoader2.WWOBJLoader2.PrepDataFile
  820. *
  821. * @param {THREE.Object3D} sceneGraphBaseNode Scene graph object
  822. */
  823. setSceneGraphBaseNode: function ( sceneGraphBaseNode ) {
  824. this.sceneGraphBaseNode = Validator.verifyInput( sceneGraphBaseNode, null );
  825. },
  826. /**
  827. * Singles meshes are directly integrated into scene when loaded or later.
  828. * @memberOf THREE.OBJLoader2.WWOBJLoader2.PrepDataFile
  829. *
  830. * @param {boolean} streamMeshes=true Default is true
  831. */
  832. setStreamMeshes: function ( streamMeshes ) {
  833. this.streamMeshes = streamMeshes !== false;
  834. },
  835. /**
  836. * Request termination of web worker and free local resources after execution.
  837. * @memberOf THREE.OBJLoader2.WWOBJLoader2.PrepDataFile
  838. *
  839. * @param {boolean} requestTerminate=false Default is false
  840. */
  841. setRequestTerminate: function ( requestTerminate ) {
  842. this.requestTerminate = requestTerminate === true;
  843. },
  844. /**
  845. * Returns all callbacks as {@link THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks}
  846. * @memberOf THREE.OBJLoader2.WWOBJLoader2.PrepDataFile
  847. *
  848. * @returns {THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks}
  849. */
  850. getCallbacks: function () {
  851. return this.callbacks;
  852. },
  853. modelName: Validator.verifyInput( modelName, '' ),
  854. dataAvailable: false,
  855. pathObj: Validator.verifyInput( pathObj, null ),
  856. fileObj: Validator.verifyInput( fileObj, null ),
  857. pathTexture: Validator.verifyInput( pathTexture, null ),
  858. fileMtl: Validator.verifyInput( fileMtl, null ),
  859. sceneGraphBaseNode: null,
  860. streamMeshes: true,
  861. requestTerminate: false,
  862. callbacks: new THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks()
  863. };
  864. };
  865. /**
  866. * Callbacks utilized by functions working with {@link THREE.OBJLoader2.WWOBJLoader2.PrepDataArrayBuffer} or {@link THREE.OBJLoader2.WWOBJLoader2.PrepDataFile}
  867. *
  868. * @returns {{registerCallbackProgress: THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks.registerCallbackProgress, registerCallbackCompletedLoading: THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks.registerCallbackCompletedLoading, registerCallbackMaterialsLoaded: THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks.registerCallbackMaterialsLoaded, registerCallbackMeshLoaded: THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks.registerCallbackMeshLoaded, registerCallbackErrorWhileLoading: THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks.registerCallbackErrorWhileLoading, progress: null, completedLoading: null, errorWhileLoading: null, materialsLoaded: null, meshLoaded: null}}
  869. * @constructor
  870. */
  871. THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks = function () {
  872. var Validator = THREE.OBJLoader2.prototype._getValidator();
  873. return {
  874. /**
  875. * Register callback function that is invoked by internal function "_announceProgress" to print feedback.
  876. * @memberOf THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks
  877. *
  878. * @param {callback} callbackProgress Callback function for described functionality
  879. */
  880. registerCallbackProgress: function ( callbackProgress ) {
  881. if ( Validator.isValid( callbackProgress ) ) this.progress = callbackProgress;
  882. },
  883. /**
  884. * Register callback function that is called once loading of the complete model is completed.
  885. * @memberOf THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks
  886. *
  887. * @param {callback} callbackCompletedLoading Callback function for described functionality
  888. */
  889. registerCallbackCompletedLoading: function ( callbackCompletedLoading ) {
  890. if ( Validator.isValid( callbackCompletedLoading ) ) this.completedLoading = callbackCompletedLoading;
  891. },
  892. /**
  893. * Register callback function that is called once materials have been loaded. It allows to alter and return materials.
  894. * @memberOf THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks
  895. *
  896. * @param {callback} callbackMaterialsLoaded Callback function for described functionality
  897. */
  898. registerCallbackMaterialsLoaded: function ( callbackMaterialsLoaded ) {
  899. if ( Validator.isValid( callbackMaterialsLoaded ) ) this.materialsLoaded = callbackMaterialsLoaded;
  900. },
  901. /**
  902. * Register callback function that is called every time a mesh was loaded.
  903. * Use {@link THREE.OBJLoader2.WWOBJLoader2.LoadedMeshUserOverride} for alteration instructions (geometry, material or disregard mesh).
  904. * @memberOf THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks
  905. *
  906. * @param {callback} callbackMeshLoaded Callback function for described functionality
  907. */
  908. registerCallbackMeshLoaded: function ( callbackMeshLoaded ) {
  909. if ( Validator.isValid( callbackMeshLoaded ) ) this.meshLoaded = callbackMeshLoaded;
  910. },
  911. /**
  912. * Report if an error prevented loading.
  913. * @memberOf THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks
  914. *
  915. * @param {callback} callbackErrorWhileLoading Callback function for described functionality
  916. */
  917. registerCallbackErrorWhileLoading: function ( callbackErrorWhileLoading ) {
  918. if ( Validator.isValid( callbackErrorWhileLoading ) ) this.errorWhileLoading = callbackErrorWhileLoading;
  919. },
  920. progress: null,
  921. completedLoading: null,
  922. errorWhileLoading: null,
  923. materialsLoaded: null,
  924. meshLoaded: null
  925. };
  926. };
  927. /**
  928. * Object to return by {@link THREE.OBJLoader2.WWOBJLoader2}.callbacks.meshLoaded. Used to adjust bufferGeometry or material or prevent complete loading of mesh
  929. *
  930. * @param {boolean} disregardMesh=false Tell WWOBJLoader2 to completely disregard this mesh
  931. * @param {THREE.BufferGeometry} bufferGeometry The {@link THREE.BufferGeometry} to be used
  932. * @param {THREE.Material} material The {@link THREE.Material} to be used
  933. *
  934. * @returns {{ disregardMesh: boolean, replaceBufferGeometry: boolean, bufferGeometry: THREE.BufferGeometry, replaceMaterial: boolean, material: THREE.Material}}
  935. * @constructor
  936. */
  937. THREE.OBJLoader2.WWOBJLoader2.LoadedMeshUserOverride = function ( disregardMesh, bufferGeometry, material ) {
  938. var Validator = THREE.OBJLoader2.prototype._getValidator();
  939. return {
  940. disregardMesh: disregardMesh === true,
  941. replaceBufferGeometry: Validator.isValid( bufferGeometry ),
  942. bufferGeometry: Validator.verifyInput( bufferGeometry, null ),
  943. replaceMaterial: Validator.isValid( material ),
  944. material: Validator.verifyInput( material, null )
  945. };
  946. };
  947. /**
  948. * Orchestrate loading of multiple OBJ files/data from an instruction queue with a configurable amount of workers (1-16).
  949. * Workflow:
  950. * prepareWorkers
  951. * enqueueForRun
  952. * processQueue
  953. * deregister
  954. *
  955. * @class
  956. */
  957. THREE.OBJLoader2.WWOBJLoader2Director = (function () {
  958. var Validator = THREE.OBJLoader2.prototype._getValidator();
  959. var MAX_WEB_WORKER = 16;
  960. var MAX_QUEUE_SIZE = 1024;
  961. function WWOBJLoader2Director() {
  962. this.maxQueueSize = MAX_QUEUE_SIZE ;
  963. this.maxWebWorkers = MAX_WEB_WORKER;
  964. this.crossOrigin = null;
  965. this.workerDescription = {
  966. prototypeDef: THREE.OBJLoader2.WWOBJLoader2.prototype,
  967. globalCallbacks: {},
  968. webWorkers: [],
  969. codeBuffer: null
  970. };
  971. this.objectsCompleted = 0;
  972. this.instructionQueue = [];
  973. }
  974. /**
  975. * Returns the maximum length of the instruction queue.
  976. * @memberOf THREE.OBJLoader2.WWOBJLoader2Director
  977. *
  978. * @returns {number}
  979. */
  980. WWOBJLoader2Director.prototype.getMaxQueueSize = function () {
  981. return this.maxQueueSize;
  982. };
  983. /**
  984. * Returns the maximum number of workers.
  985. * @memberOf THREE.OBJLoader2.WWOBJLoader2Director
  986. *
  987. * @returns {number}
  988. */
  989. WWOBJLoader2Director.prototype.getMaxWebWorkers = function () {
  990. return this.maxWebWorkers;
  991. };
  992. /**
  993. * Sets the CORS string to be used.
  994. * @memberOf THREE.OBJLoader2.WWOBJLoader2Director
  995. *
  996. * @param {string} crossOrigin CORS value
  997. */
  998. WWOBJLoader2Director.prototype.setCrossOrigin = function ( crossOrigin ) {
  999. this.crossOrigin = crossOrigin;
  1000. };
  1001. /**
  1002. * Create or destroy workers according limits. Set the name and register callbacks for dynamically created web workers.
  1003. * @memberOf THREE.OBJLoader2.WWOBJLoader2Director
  1004. *
  1005. * @param {THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks} globalCallbacks Register global callbacks used by all web workers
  1006. * @param {number} maxQueueSize Set the maximum size of the instruction queue (1-1024)
  1007. * @param {number} maxWebWorkers Set the maximum amount of workers (1-16)
  1008. */
  1009. WWOBJLoader2Director.prototype.prepareWorkers = function ( globalCallbacks, maxQueueSize, maxWebWorkers ) {
  1010. if ( Validator.isValid( globalCallbacks ) ) this.workerDescription.globalCallbacks = globalCallbacks;
  1011. this.maxQueueSize = Math.min( maxQueueSize, MAX_QUEUE_SIZE );
  1012. this.maxWebWorkers = Math.min( maxWebWorkers, MAX_WEB_WORKER );
  1013. this.objectsCompleted = 0;
  1014. this.instructionQueue = [];
  1015. var start = this.workerDescription.webWorkers.length;
  1016. if ( start < this.maxWebWorkers ) {
  1017. for ( i = start; i < this.maxWebWorkers; i ++ ) {
  1018. webWorker = this._buildWebWorker();
  1019. this.workerDescription.webWorkers[ i ] = webWorker;
  1020. }
  1021. } else {
  1022. for ( var webWorker, i = start - 1; i >= this.maxWebWorkers; i-- ) {
  1023. webWorker = this.workerDescription.webWorkers[ i ];
  1024. webWorker.setRequestTerminate( true );
  1025. this.workerDescription.webWorkers.pop();
  1026. }
  1027. }
  1028. };
  1029. /**
  1030. * Store run instructions in internal instructionQueue.
  1031. * @memberOf THREE.OBJLoader2.WWOBJLoader2Director
  1032. *
  1033. * @param {Object} runParams Either {@link THREE.OBJLoader2.WWOBJLoader2.PrepDataArrayBuffer} or {@link THREE.OBJLoader2.WWOBJLoader2.PrepDataFile}
  1034. */
  1035. WWOBJLoader2Director.prototype.enqueueForRun = function ( runParams ) {
  1036. if ( this.instructionQueue.length < this.maxQueueSize ) {
  1037. this.instructionQueue.push( runParams );
  1038. }
  1039. };
  1040. /**
  1041. * Process the instructionQueue until it is depleted.
  1042. * @memberOf THREE.OBJLoader2.WWOBJLoader2Director
  1043. */
  1044. WWOBJLoader2Director.prototype.processQueue = function () {
  1045. if ( this.instructionQueue.length === 0 ) return;
  1046. var length = Math.min( this.maxWebWorkers, this.instructionQueue.length );
  1047. for ( var i = 0; i < length; i++ ) {
  1048. this._kickWebWorkerRun( this.workerDescription.webWorkers[ i ], this.instructionQueue[ 0 ] );
  1049. this.instructionQueue.shift();
  1050. }
  1051. };
  1052. WWOBJLoader2Director.prototype._kickWebWorkerRun = function( worker, runParams ) {
  1053. worker.clearAllCallbacks();
  1054. var key;
  1055. var globalCallbacks = this.workerDescription.globalCallbacks;
  1056. var workerCallbacks = worker.callbacks;
  1057. var selectedGlobalCallback;
  1058. for ( key in globalCallbacks ) {
  1059. if ( workerCallbacks.hasOwnProperty( key ) && globalCallbacks.hasOwnProperty( key ) ) {
  1060. selectedGlobalCallback = globalCallbacks[ key ];
  1061. if ( Validator.isValid( selectedGlobalCallback ) ) workerCallbacks[ key ].push( selectedGlobalCallback );
  1062. }
  1063. }
  1064. // register per object callbacks
  1065. var runCallbacks = runParams.callbacks;
  1066. if ( Validator.isValid( runCallbacks ) ) {
  1067. for ( key in runCallbacks ) {
  1068. if ( workerCallbacks.hasOwnProperty( key ) && runCallbacks.hasOwnProperty( key ) && Validator.isValid( runCallbacks[ key ] ) ) {
  1069. workerCallbacks[ key ].push( runCallbacks[ key ] );
  1070. }
  1071. }
  1072. }
  1073. var scope = this;
  1074. var directorCompletedLoading = function ( modelName, instanceNo, requestTerminate ) {
  1075. scope.objectsCompleted++;
  1076. if ( ! requestTerminate ) {
  1077. var worker = scope.workerDescription.webWorkers[ instanceNo ];
  1078. var runParams = scope.instructionQueue[ 0 ];
  1079. if ( Validator.isValid( runParams ) ) {
  1080. console.log( '\nAssigning next item from queue to worker (queue length: ' + scope.instructionQueue.length + ')\n\n' );
  1081. scope._kickWebWorkerRun( worker, runParams );
  1082. scope.instructionQueue.shift();
  1083. }
  1084. }
  1085. };
  1086. worker.registerCallbackCompletedLoading( directorCompletedLoading );
  1087. worker.prepareRun( runParams );
  1088. worker.run();
  1089. };
  1090. WWOBJLoader2Director.prototype._buildWebWorker = function () {
  1091. var webWorker = Object.create( this.workerDescription.prototypeDef );
  1092. webWorker._init();
  1093. if ( Validator.isValid( this.crossOrigin ) ) webWorker.setCrossOrigin( this.crossOrigin );
  1094. // Ensure code string is built once and then it is just passed on to every new instance
  1095. if ( Validator.isValid( this.workerDescription.codeBuffer ) ) {
  1096. webWorker._buildWebWorkerCode( this.workerDescription.codeBuffer );
  1097. } else {
  1098. this.workerDescription.codeBuffer = webWorker._buildWebWorkerCode();
  1099. }
  1100. webWorker.instanceNo = this.workerDescription.webWorkers.length;
  1101. this.workerDescription.webWorkers.push( webWorker );
  1102. return webWorker;
  1103. };
  1104. /**
  1105. * Terminate all workers.
  1106. * @memberOf THREE.OBJLoader2.WWOBJLoader2Director
  1107. */
  1108. WWOBJLoader2Director.prototype.deregister = function () {
  1109. console.log( 'WWOBJLoader2Director received the unregister call. Terminating all workers!' );
  1110. for ( var i = 0, webWorker, length = this.workerDescription.webWorkers.length; i < length; i++ ) {
  1111. webWorker = this.workerDescription.webWorkers[ i ];
  1112. webWorker.setRequestTerminate( true );
  1113. }
  1114. this.workerDescription.globalCallbacks = {};
  1115. this.workerDescription.webWorkers = [];
  1116. this.workerDescription.codeBuffer = null;
  1117. };
  1118. return WWOBJLoader2Director;
  1119. })();