OBJLoader2Parallel.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * @author Kai Salmen / https://kaisalmen.de
  3. * Development repository: https://github.com/kaisalmen/WWOBJLoader
  4. */
  5. // Imports only related to wrapper
  6. import {
  7. CodeBuilderInstructions,
  8. WorkerExecutionSupport
  9. } from "./obj2/worker/main/WorkerExecutionSupport.js";
  10. import { CodeSerializer } from "./obj2/utils/CodeSerializer.js";
  11. import { OBJLoader2 } from "./OBJLoader2.js";
  12. // Imports only related to worker (when standard workers (modules aren't supported) are used)
  13. import { OBJLoader2Parser } from "./obj2/worker/parallel/OBJLoader2Parser.js";
  14. import { ObjectManipulator } from "./obj2/utils/ObjectManipulator.js";
  15. import {
  16. WorkerRunner,
  17. DefaultWorkerPayloadHandler
  18. } from "./obj2/worker/parallel/WorkerRunner.js";
  19. /**
  20. * Extends {OBJLoader2} with the capability to run the parser {OBJLoader2Parser} in web worker
  21. * with help of {WorkerExecutionSupport}.
  22. *
  23. * @param [LoadingManager] manager
  24. * @constructor
  25. */
  26. const OBJLoader2Parallel = function ( manager ) {
  27. OBJLoader2.call( this, manager );
  28. this.preferJsmWorker = false;
  29. this.callbacks.onParseComplete = null;
  30. this.executeParallel = true;
  31. this.workerExecutionSupport = new WorkerExecutionSupport();
  32. };
  33. OBJLoader2Parallel.prototype = Object.create( OBJLoader2.prototype );
  34. OBJLoader2Parallel.prototype.constructor = OBJLoader2Parallel;
  35. OBJLoader2.OBJLOADER2_PARALLEL_VERSION = '3.0.0-beta2';
  36. console.info( 'Using OBJLoader2Parallel version: ' + OBJLoader2.OBJLOADER2_PARALLEL_VERSION );
  37. OBJLoader2Parallel.prototype.setPreferJsmWorker = function ( preferJsmWorker ) {
  38. this.preferJsmWorker = preferJsmWorker === true;
  39. return this;
  40. };
  41. /**
  42. * If this call back is not set, then the completion message from worker will not be received.
  43. *
  44. * @param {function} onParseComplete
  45. * @return {OBJLoader2Parallel}
  46. */
  47. OBJLoader2Parallel.prototype.setCallbackOnParseComplete = function ( onParseComplete ) {
  48. if ( onParseComplete !== undefined && onParseComplete !== null ) {
  49. this.callbacks.onParseComplete = onParseComplete;
  50. }
  51. return this;
  52. };
  53. /**
  54. * Execution of parse in parallel via Worker is default, but normal {OBJLoader2} parsing can be enforced via false here.
  55. *
  56. * @param executeParallel
  57. * @return {OBJLoader2Parallel}
  58. */
  59. OBJLoader2Parallel.prototype.setExecuteParallel = function ( executeParallel ) {
  60. this.executeParallel = executeParallel === true;
  61. return this;
  62. };
  63. /**
  64. * Allow to get hold of {WorkerExecutionSupport} for configuratin purposes
  65. *
  66. * @return {WorkerExecutionSupport|WorkerExecutionSupport}
  67. */
  68. OBJLoader2Parallel.prototype.getWorkerExecutionSupport = function () {
  69. return this.workerExecutionSupport;
  70. };
  71. /**
  72. * Provides instructions on what is to be contained in the worker
  73. *
  74. * @return {CodeBuilderInstructions}
  75. */
  76. OBJLoader2Parallel.prototype.buildWorkerCode = function () {
  77. let codeBuilderInstructions = new CodeBuilderInstructions( true, true, this.preferJsmWorker );
  78. if ( codeBuilderInstructions.isSupportsJsmWorker() ) {
  79. codeBuilderInstructions.setJsmWorkerFile( '../../src/loaders/worker/parallel/jsm/OBJLoader2Worker.js' );
  80. }
  81. if ( codeBuilderInstructions.isSupportsStandardWorker() ) {
  82. let codeOBJLoader2Parser = CodeSerializer.serializeClass( 'OBJLoader2Parser', OBJLoader2Parser );
  83. let codeObjectManipulator = CodeSerializer.serializeObject( 'ObjectManipulator', ObjectManipulator );
  84. let codeParserPayloadHandler = CodeSerializer.serializeClass( 'DefaultWorkerPayloadHandler', DefaultWorkerPayloadHandler );
  85. let codeWorkerRunner = CodeSerializer.serializeClass( 'WorkerRunner', WorkerRunner );
  86. codeBuilderInstructions.addCodeFragment( codeOBJLoader2Parser );
  87. codeBuilderInstructions.addCodeFragment( codeObjectManipulator );
  88. codeBuilderInstructions.addCodeFragment( codeParserPayloadHandler );
  89. codeBuilderInstructions.addCodeFragment( codeWorkerRunner );
  90. // allows to include full libraries as importScripts
  91. // codeBuilderInstructions.addLibraryImport( '../../node_modules/three/build/three.js' );
  92. codeBuilderInstructions.addStartCode( 'new WorkerRunner( new DefaultWorkerPayloadHandler( new OBJLoader2Parser() ) );' );
  93. }
  94. return codeBuilderInstructions;
  95. };
  96. /**
  97. * @private
  98. */
  99. OBJLoader2Parallel.prototype._configure = function () {
  100. if ( this.callbacks.onParseComplete === null ) {
  101. throw "No callbackOnLoad was provided! Aborting!";
  102. }
  103. // check if worker is already available and if so, then fast-fail
  104. if ( this.workerExecutionSupport.isWorkerLoaded( this.preferJsmWorker ) ) return;
  105. this.workerExecutionSupport.buildWorker( this.buildWorkerCode() );
  106. let scope = this;
  107. let scopedOnAssetAvailable = function ( payload ) {
  108. scope._onAssetAvailable( payload );
  109. };
  110. this.workerExecutionSupport.updateCallbacks( scopedOnAssetAvailable, this.callbacks.onParseComplete );
  111. };
  112. /**
  113. * Load is intercepted from {OBJLoader2}. It replaces the regular onLoad callback as the final worker result will be
  114. * returned later by its own callbackOnLoad.
  115. *
  116. * @param {string} url A string containing the path/URL of the file to be loaded.
  117. * @param {function} onLoad A function to be called after loading is successfully completed. The function receives loaded Object3D as an argument.
  118. * @param {function} [onFileLoadProgress] A function to be called while the loading is in progress. The argument will be the XMLHttpRequest instance, which contains total and Integer bytes.
  119. * @param {function} [onError] A function to be called if an error occurs during loading. The function receives the error as an argument.
  120. * @param {function} [onMeshAlter] Called after worker successfully delivered a single mesh
  121. */
  122. OBJLoader2Parallel.prototype.load = function ( content, onLoad, onFileLoadProgress, onError, onMeshAlter ) {
  123. this.setCallbackOnParseComplete( onLoad );
  124. OBJLoader2.prototype.load.call( this, content, function () {}, onFileLoadProgress, onError, onMeshAlter );
  125. };
  126. /**
  127. * Parses OBJ data in parallel with web worker.
  128. *
  129. * @param {arraybuffer} content OBJ data as Uint8Array or String
  130. */
  131. OBJLoader2Parallel.prototype.parse = function ( content ) {
  132. if ( this.executeParallel ) {
  133. this._configure();
  134. this.workerExecutionSupport.executeParallel(
  135. {
  136. params: {
  137. modelName: this.modelName,
  138. instanceNo: this.instanceNo,
  139. useIndices: this.useIndices,
  140. disregardNormals: this.disregardNormals,
  141. materialPerSmoothingGroup: this.materialPerSmoothingGroup,
  142. useOAsMesh: this.useOAsMesh,
  143. },
  144. materials: this.materialHandler.getMaterialsJSON(),
  145. data: {
  146. input: content,
  147. options: null
  148. },
  149. logging: {
  150. enabled: this.logging.enabled,
  151. debug: this.logging.debug
  152. }
  153. } );
  154. } else {
  155. this.callbacks.onParseComplete( OBJLoader2.prototype.parse.call( this, content ) );
  156. }
  157. };
  158. export { OBJLoader2Parallel };