OBJLoader2Parallel.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. OBJLoader2Parallel.OBJLOADER2_PARALLEL_VERSION = '3.0.0';
  36. console.info( 'Using OBJLoader2Parallel version: ' + OBJLoader2Parallel.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. codeBuilderInstructions.addStartCode( 'new WorkerRunner( new DefaultWorkerPayloadHandler( new OBJLoader2Parser() ) );' );
  91. }
  92. return codeBuilderInstructions;
  93. };
  94. /**
  95. * @private
  96. */
  97. OBJLoader2Parallel.prototype._configure = function () {
  98. if ( this.callbacks.onParseComplete === null ) {
  99. throw "No callbackOnLoad was provided! Aborting!";
  100. }
  101. // check if worker is already available and if so, then fast-fail
  102. if ( this.workerExecutionSupport.isWorkerLoaded( this.preferJsmWorker ) ) return;
  103. this.workerExecutionSupport.buildWorker( this.buildWorkerCode() );
  104. let scope = this;
  105. let scopedOnAssetAvailable = function ( payload ) {
  106. scope._onAssetAvailable( payload );
  107. };
  108. this.workerExecutionSupport.updateCallbacks( scopedOnAssetAvailable, this.callbacks.onParseComplete );
  109. };
  110. /**
  111. * Load is intercepted from {OBJLoader2}. It replaces the regular onLoad callback as the final worker result will be
  112. * returned later by its own callbackOnLoad.
  113. *
  114. * @param {string} url A string containing the path/URL of the file to be loaded.
  115. * @param {function} onLoad A function to be called after loading is successfully completed. The function receives loaded Object3D as an argument.
  116. * @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.
  117. * @param {function} [onError] A function to be called if an error occurs during loading. The function receives the error as an argument.
  118. * @param {function} [onMeshAlter] Called after worker successfully delivered a single mesh
  119. */
  120. OBJLoader2Parallel.prototype.load = function ( content, onLoad, onFileLoadProgress, onError, onMeshAlter ) {
  121. this.setCallbackOnParseComplete( onLoad );
  122. OBJLoader2.prototype.load.call( this, content, function () {}, onFileLoadProgress, onError, onMeshAlter );
  123. };
  124. /**
  125. * Parses OBJ data in parallel with web worker.
  126. *
  127. * @param {arraybuffer} content OBJ data as Uint8Array or String
  128. */
  129. OBJLoader2Parallel.prototype.parse = function ( content ) {
  130. if ( this.executeParallel ) {
  131. this._configure();
  132. this.workerExecutionSupport.executeParallel(
  133. {
  134. params: {
  135. modelName: this.modelName,
  136. instanceNo: this.instanceNo,
  137. useIndices: this.useIndices,
  138. disregardNormals: this.disregardNormals,
  139. materialPerSmoothingGroup: this.materialPerSmoothingGroup,
  140. useOAsMesh: this.useOAsMesh,
  141. },
  142. materials: this.materialHandler.getMaterialsJSON(),
  143. data: {
  144. input: content,
  145. options: null
  146. },
  147. logging: {
  148. enabled: this.logging.enabled,
  149. debug: this.logging.debug
  150. }
  151. } );
  152. } else {
  153. this.callbacks.onParseComplete( OBJLoader2.prototype.parse.call( this, content ) );
  154. }
  155. };
  156. export { OBJLoader2Parallel };