2
0

OBJLoader2Parallel.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. *
  21. * @param [LoadingManager] manager
  22. * @constructor
  23. */
  24. const OBJLoader2Parallel = function ( manager ) {
  25. OBJLoader2.call( this, manager );
  26. this.useJsmWorker = false;
  27. this.callbackOnLoad = null;
  28. this.executeParallel = true;
  29. this.workerExecutionSupport = new WorkerExecutionSupport();
  30. };
  31. OBJLoader2.OBJLOADER2_PARALLEL_VERSION = '3.0.0-beta2';
  32. console.info( 'Using OBJLoader2Parallel version: ' + OBJLoader2.OBJLOADER2PARALLEL_VERSION );
  33. OBJLoader2Parallel.prototype = Object.create( OBJLoader2.prototype );
  34. OBJLoader2Parallel.prototype.constructor = OBJLoader2Parallel;
  35. OBJLoader2Parallel.prototype.setUseJsmWorker = function ( useJsmWorker ) {
  36. this.useJsmWorker = useJsmWorker === true;
  37. return this;
  38. };
  39. OBJLoader2Parallel.prototype.setCallbackOnLoad = function ( callbackOnLoad ) {
  40. if ( callbackOnLoad !== undefined && callbackOnLoad !== null ) {
  41. this.callbackOnLoad = callbackOnLoad;
  42. }
  43. else {
  44. throw "No callbackOnLoad was provided! Aborting!"
  45. }
  46. return this;
  47. };
  48. OBJLoader2Parallel.prototype.setExecuteParallel = function ( executeParallel ) {
  49. this.executeParallel = executeParallel === true;
  50. return this;
  51. };
  52. OBJLoader2Parallel.prototype.getWorkerExecutionSupport = function () {
  53. return this.workerExecutionSupport;
  54. };
  55. OBJLoader2Parallel.prototype._configure = function () {
  56. if ( this.callbackOnLoad === null ) {
  57. "No callbackOnLoad was provided! Aborting!"
  58. }
  59. // check if worker is already available and if so, then fast-fail
  60. if ( this.workerExecutionSupport.isWorkerLoaded( this.useJsmWorker ) ) return;
  61. let codeBuilderInstructions = new CodeBuilderInstructions();
  62. let jsmSuccess = false;
  63. if ( this.useJsmWorker ) {
  64. codeBuilderInstructions.setJsmWorkerFile( '../../src/loaders/worker/parallel/jsm/OBJLoader2Worker.js' );
  65. jsmSuccess = this.workerExecutionSupport.buildWorkerJsm( codeBuilderInstructions );
  66. }
  67. if ( ! jsmSuccess ) {
  68. let codeOBJLoader2Parser = CodeSerializer.serializeClass( 'OBJLoader2Parser', OBJLoader2Parser );
  69. let codeObjectManipulator = CodeSerializer.serializeObject( 'ObjectManipulator', ObjectManipulator );
  70. let codeParserPayloadHandler = CodeSerializer.serializeClass( 'DefaultWorkerPayloadHandler', DefaultWorkerPayloadHandler );
  71. let codeWorkerRunner = CodeSerializer.serializeClass( 'WorkerRunner', WorkerRunner );
  72. codeBuilderInstructions.addCodeFragment( codeOBJLoader2Parser );
  73. codeBuilderInstructions.addCodeFragment( codeObjectManipulator );
  74. codeBuilderInstructions.addCodeFragment( codeParserPayloadHandler );
  75. codeBuilderInstructions.addCodeFragment( codeWorkerRunner );
  76. // codeBuilderInstructions.addLibraryImport( '../../node_modules/three/build/three.js' );
  77. codeBuilderInstructions.addStartCode( 'new WorkerRunner( new DefaultWorkerPayloadHandler( new OBJLoader2Parser() ) );' );
  78. this.workerExecutionSupport.buildWorkerStandard( codeBuilderInstructions );
  79. }
  80. let scope = this;
  81. let scopedOnAssetAvailable = function ( payload ) {
  82. scope._onAssetAvailable( payload );
  83. };
  84. this.workerExecutionSupport.updateCallbacks( scopedOnAssetAvailable, this.callbackOnLoad );
  85. };
  86. /**
  87. * Load is intercepted from OBJLoader2.
  88. * @inheritDoc
  89. */
  90. OBJLoader2Parallel.prototype.load = function( content, onLoad, onFileLoadProgress, onError, onMeshAlter ) {
  91. this.setCallbackOnLoad( onLoad );
  92. OBJLoader2.prototype.load.call( this, content, function () {}, onFileLoadProgress, onError, onMeshAlter );
  93. };
  94. /**
  95. * @inheritDoc
  96. */
  97. OBJLoader2Parallel.prototype.parse = function( content ) {
  98. if ( this.executeParallel ) {
  99. this._configure();
  100. this.workerExecutionSupport.executeParallel(
  101. {
  102. params: {
  103. modelName: this.modelName,
  104. instanceNo: this.instanceNo,
  105. useIndices: this.useIndices,
  106. disregardNormals: this.disregardNormals,
  107. materialPerSmoothingGroup: this.materialPerSmoothingGroup,
  108. useOAsMesh: this.useOAsMesh,
  109. },
  110. materials: this.materialHandler.getMaterialsJSON(),
  111. data: {
  112. input: content,
  113. options: null
  114. },
  115. logging: {
  116. enabled: this.logging.enabled,
  117. debug: this.logging.debug
  118. }
  119. } );
  120. } else {
  121. this.callbackOnLoad( OBJLoader2.prototype.parse.call( this, content ) );
  122. }
  123. };
  124. export { OBJLoader2Parallel }