LoaderWorkerSupport.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /**
  2. * Default implementation of the WorkerRunner responsible for creation and configuration of the parser within the worker.
  3. *
  4. * @class
  5. */
  6. THREE.LoaderSupport.WorkerRunnerRefImpl = (function () {
  7. function WorkerRunnerRefImpl() {
  8. var scope = this;
  9. var scopedRunner = function( event ) {
  10. scope.processMessage( event.data );
  11. };
  12. self.addEventListener( 'message', scopedRunner, false );
  13. }
  14. /**
  15. * Applies values from parameter object via set functions or via direct assignment.
  16. * @memberOf THREE.LoaderSupport.WorkerRunnerRefImpl
  17. *
  18. * @param {Object} parser The parser instance
  19. * @param {Object} params The parameter object
  20. */
  21. WorkerRunnerRefImpl.prototype.applyProperties = function ( parser, params ) {
  22. var property, funcName, values;
  23. for ( property in params ) {
  24. funcName = 'set' + property.substring( 0, 1 ).toLocaleUpperCase() + property.substring( 1 );
  25. values = params[ property ];
  26. if ( typeof parser[ funcName ] === 'function' ) {
  27. parser[ funcName ]( values );
  28. } else if ( parser.hasOwnProperty( property ) ) {
  29. parser[ property ] = values;
  30. }
  31. }
  32. };
  33. /**
  34. * Configures the Parser implementation according the supplied configuration object.
  35. * @memberOf THREE.LoaderSupport.WorkerRunnerRefImpl
  36. *
  37. * @param {Object} payload Raw mesh description (buffers, params, materials) used to build one to many meshes.
  38. */
  39. WorkerRunnerRefImpl.prototype.processMessage = function ( payload ) {
  40. if ( payload.cmd === 'run' ) {
  41. var callbacks = {
  42. callbackMeshBuilder: function ( payload ) {
  43. self.postMessage( payload );
  44. },
  45. callbackProgress: function ( text ) {
  46. if ( payload.logging.enabled && payload.logging.debug ) console.debug( 'WorkerRunner: progress: ' + text );
  47. }
  48. };
  49. // Parser is expected to be named as such
  50. var parser = new Parser();
  51. if ( typeof parser[ 'setLogging' ] === 'function' ) parser.setLogging( payload.logging.enabled, payload.logging.debug );
  52. this.applyProperties( parser, payload.params );
  53. this.applyProperties( parser, payload.materials );
  54. this.applyProperties( parser, callbacks );
  55. parser.workerScope = self;
  56. parser.parse( payload.data.input, payload.data.options );
  57. if ( payload.logging.enabled ) console.log( 'WorkerRunner: Run complete!' );
  58. callbacks.callbackMeshBuilder( {
  59. cmd: 'complete',
  60. msg: 'WorkerRunner completed run.'
  61. } );
  62. } else {
  63. console.error( 'WorkerRunner: Received unknown command: ' + payload.cmd );
  64. }
  65. };
  66. return WorkerRunnerRefImpl;
  67. })();
  68. /**
  69. * This class provides means to transform existing parser code into a web worker. It defines a simple communication protocol
  70. * which allows to configure the worker and receive raw mesh data during execution.
  71. * @class
  72. */
  73. THREE.LoaderSupport.WorkerSupport = (function () {
  74. var WORKER_SUPPORT_VERSION = '2.2.0';
  75. var Validator = THREE.LoaderSupport.Validator;
  76. var LoaderWorker = (function () {
  77. function LoaderWorker() {
  78. this._reset();
  79. }
  80. LoaderWorker.prototype._reset = function () {
  81. this.logging = {
  82. enabled: true,
  83. debug: false
  84. };
  85. this.worker = null;
  86. this.runnerImplName = null;
  87. this.callbacks = {
  88. meshBuilder: null,
  89. onLoad: null
  90. };
  91. this.terminateRequested = false;
  92. this.queuedMessage = null;
  93. this.started = false;
  94. this.forceCopy = false;
  95. };
  96. LoaderWorker.prototype.setLogging = function ( enabled, debug ) {
  97. this.logging.enabled = enabled === true;
  98. this.logging.debug = debug === true;
  99. };
  100. LoaderWorker.prototype.setForceCopy = function ( forceCopy ) {
  101. this.forceCopy = forceCopy === true;
  102. };
  103. LoaderWorker.prototype.initWorker = function ( code, runnerImplName ) {
  104. this.runnerImplName = runnerImplName;
  105. var blob = new Blob( [ code ], { type: 'application/javascript' } );
  106. this.worker = new Worker( window.URL.createObjectURL( blob ) );
  107. this.worker.onmessage = this._receiveWorkerMessage;
  108. // set referemce to this, then processing in worker scope within "_receiveWorkerMessage" can access members
  109. this.worker.runtimeRef = this;
  110. // process stored queuedMessage
  111. this._postMessage();
  112. };
  113. /**
  114. * Executed in worker scope
  115. */
  116. LoaderWorker.prototype._receiveWorkerMessage = function ( e ) {
  117. var payload = e.data;
  118. switch ( payload.cmd ) {
  119. case 'meshData':
  120. case 'materialData':
  121. case 'imageData':
  122. this.runtimeRef.callbacks.meshBuilder( payload );
  123. break;
  124. case 'complete':
  125. this.runtimeRef.queuedMessage = null;
  126. this.started = false;
  127. this.runtimeRef.callbacks.onLoad( payload.msg );
  128. if ( this.runtimeRef.terminateRequested ) {
  129. if ( this.runtimeRef.logging.enabled ) console.info( 'WorkerSupport [' + this.runtimeRef.runnerImplName + ']: Run is complete. Terminating application on request!' );
  130. this.runtimeRef._terminate();
  131. }
  132. break;
  133. case 'error':
  134. console.error( 'WorkerSupport [' + this.runtimeRef.runnerImplName + ']: Reported error: ' + payload.msg );
  135. this.runtimeRef.queuedMessage = null;
  136. this.started = false;
  137. this.runtimeRef.callbacks.onLoad( payload.msg );
  138. if ( this.runtimeRef.terminateRequested ) {
  139. if ( this.runtimeRef.logging.enabled ) console.info( 'WorkerSupport [' + this.runtimeRef.runnerImplName + ']: Run reported error. Terminating application on request!' );
  140. this.runtimeRef._terminate();
  141. }
  142. break;
  143. default:
  144. console.error( 'WorkerSupport [' + this.runtimeRef.runnerImplName + ']: Received unknown command: ' + payload.cmd );
  145. break;
  146. }
  147. };
  148. LoaderWorker.prototype.setCallbacks = function ( meshBuilder, onLoad ) {
  149. this.callbacks.meshBuilder = Validator.verifyInput( meshBuilder, this.callbacks.meshBuilder );
  150. this.callbacks.onLoad = Validator.verifyInput( onLoad, this.callbacks.onLoad );
  151. };
  152. LoaderWorker.prototype.run = function( payload ) {
  153. if ( Validator.isValid( this.queuedMessage ) ) {
  154. console.warn( 'Already processing message. Rejecting new run instruction' );
  155. return;
  156. } else {
  157. this.queuedMessage = payload;
  158. this.started = true;
  159. }
  160. if ( ! Validator.isValid( this.callbacks.meshBuilder ) ) throw 'Unable to run as no "MeshBuilder" callback is set.';
  161. if ( ! Validator.isValid( this.callbacks.onLoad ) ) throw 'Unable to run as no "onLoad" callback is set.';
  162. if ( payload.cmd !== 'run' ) payload.cmd = 'run';
  163. if ( Validator.isValid( payload.logging ) ) {
  164. payload.logging.enabled = payload.logging.enabled === true;
  165. payload.logging.debug = payload.logging.debug === true;
  166. } else {
  167. payload.logging = {
  168. enabled: true,
  169. debug: false
  170. }
  171. }
  172. this._postMessage();
  173. };
  174. LoaderWorker.prototype._postMessage = function () {
  175. if ( Validator.isValid( this.queuedMessage ) && Validator.isValid( this.worker ) ) {
  176. if ( this.queuedMessage.data.input instanceof ArrayBuffer ) {
  177. var content;
  178. if ( this.forceCopy ) {
  179. content = this.queuedMessage.data.input.slice( 0 );
  180. } else {
  181. content = this.queuedMessage.data.input;
  182. }
  183. this.worker.postMessage( this.queuedMessage, [ content ] );
  184. } else {
  185. this.worker.postMessage( this.queuedMessage );
  186. }
  187. }
  188. };
  189. LoaderWorker.prototype.setTerminateRequested = function ( terminateRequested ) {
  190. this.terminateRequested = terminateRequested === true;
  191. if ( this.terminateRequested && Validator.isValid( this.worker ) && ! Validator.isValid( this.queuedMessage ) && this.started ) {
  192. if ( this.logging.enabled ) console.info( 'Worker is terminated immediately as it is not running!' );
  193. this._terminate();
  194. }
  195. };
  196. LoaderWorker.prototype._terminate = function () {
  197. this.worker.terminate();
  198. this._reset();
  199. };
  200. return LoaderWorker;
  201. })();
  202. function WorkerSupport() {
  203. console.info( 'Using THREE.LoaderSupport.WorkerSupport version: ' + WORKER_SUPPORT_VERSION );
  204. this.logging = {
  205. enabled: true,
  206. debug: false
  207. };
  208. // check worker support first
  209. if ( window.Worker === undefined ) throw "This browser does not support web workers!";
  210. if ( window.Blob === undefined ) throw "This browser does not support Blob!";
  211. if ( typeof window.URL.createObjectURL !== 'function' ) throw "This browser does not support Object creation from URL!";
  212. this.loaderWorker = new LoaderWorker();
  213. }
  214. /**
  215. * Enable or disable logging in general (except warn and error), plus enable or disable debug logging.
  216. * @memberOf THREE.LoaderSupport.WorkerSupport
  217. *
  218. * @param {boolean} enabled True or false.
  219. * @param {boolean} debug True or false.
  220. */
  221. WorkerSupport.prototype.setLogging = function ( enabled, debug ) {
  222. this.logging.enabled = enabled === true;
  223. this.logging.debug = debug === true;
  224. this.loaderWorker.setLogging( this.logging.enabled, this.logging.debug );
  225. };
  226. /**
  227. * Forces all ArrayBuffers to be transferred to worker to be copied.
  228. * @memberOf THREE.LoaderSupport.WorkerSupport
  229. *
  230. * @param {boolean} forceWorkerDataCopy True or false.
  231. */
  232. WorkerSupport.prototype.setForceWorkerDataCopy = function ( forceWorkerDataCopy ) {
  233. this.loaderWorker.setForceCopy( forceWorkerDataCopy );
  234. };
  235. /**
  236. * Validate the status of worker code and the derived worker.
  237. * @memberOf THREE.LoaderSupport.WorkerSupport
  238. *
  239. * @param {Function} functionCodeBuilder Function that is invoked with funcBuildObject and funcBuildSingleton that allows stringification of objects and singletons.
  240. * @param {String} parserName Name of the Parser object
  241. * @param {String[]} libLocations URL of libraries that shall be added to worker code relative to libPath
  242. * @param {String} libPath Base path used for loading libraries
  243. * @param {THREE.LoaderSupport.WorkerRunnerRefImpl} runnerImpl The default worker parser wrapper implementation (communication and execution). An extended class could be passed here.
  244. */
  245. WorkerSupport.prototype.validate = function ( functionCodeBuilder, parserName, libLocations, libPath, runnerImpl ) {
  246. if ( Validator.isValid( this.loaderWorker.worker ) ) return;
  247. if ( this.logging.enabled ) {
  248. console.info( 'WorkerSupport: Building worker code...' );
  249. console.time( 'buildWebWorkerCode' );
  250. }
  251. if ( Validator.isValid( runnerImpl ) ) {
  252. if ( this.logging.enabled ) console.info( 'WorkerSupport: Using "' + runnerImpl.name + '" as Runner class for worker.' );
  253. } else {
  254. runnerImpl = THREE.LoaderSupport.WorkerRunnerRefImpl;
  255. if ( this.logging.enabled ) console.info( 'WorkerSupport: Using DEFAULT "THREE.LoaderSupport.WorkerRunnerRefImpl" as Runner class for worker.' );
  256. }
  257. var userWorkerCode = functionCodeBuilder( buildObject, buildSingleton );
  258. userWorkerCode += 'var Parser = '+ parserName + ';\n\n';
  259. userWorkerCode += buildSingleton( runnerImpl.name, runnerImpl );
  260. userWorkerCode += 'new ' + runnerImpl.name + '();\n\n';
  261. var scope = this;
  262. if ( Validator.isValid( libLocations ) && libLocations.length > 0 ) {
  263. var libsContent = '';
  264. var loadAllLibraries = function ( path, locations ) {
  265. if ( locations.length === 0 ) {
  266. scope.loaderWorker.initWorker( libsContent + userWorkerCode, runnerImpl.name );
  267. if ( scope.logging.enabled ) console.timeEnd( 'buildWebWorkerCode' );
  268. } else {
  269. var loadedLib = function ( contentAsString ) {
  270. libsContent += contentAsString;
  271. loadAllLibraries( path, locations );
  272. };
  273. var fileLoader = new THREE.FileLoader();
  274. fileLoader.setPath( path );
  275. fileLoader.setResponseType( 'text' );
  276. fileLoader.load( locations[ 0 ], loadedLib );
  277. locations.shift();
  278. }
  279. };
  280. loadAllLibraries( libPath, libLocations );
  281. } else {
  282. this.loaderWorker.initWorker( userWorkerCode, runnerImpl.name );
  283. if ( this.logging.enabled ) console.timeEnd( 'buildWebWorkerCode' );
  284. }
  285. };
  286. /**
  287. * Specify functions that should be build when new raw mesh data becomes available and when the parser is finished.
  288. * @memberOf THREE.LoaderSupport.WorkerSupport
  289. *
  290. * @param {Function} meshBuilder The mesh builder function. Default is {@link THREE.LoaderSupport.MeshBuilder}.
  291. * @param {Function} onLoad The function that is called when parsing is complete.
  292. */
  293. WorkerSupport.prototype.setCallbacks = function ( meshBuilder, onLoad ) {
  294. this.loaderWorker.setCallbacks( meshBuilder, onLoad );
  295. };
  296. /**
  297. * Runs the parser with the provided configuration.
  298. * @memberOf THREE.LoaderSupport.WorkerSupport
  299. *
  300. * @param {Object} payload Raw mesh description (buffers, params, materials) used to build one to many meshes.
  301. */
  302. WorkerSupport.prototype.run = function ( payload ) {
  303. this.loaderWorker.run( payload );
  304. };
  305. /**
  306. * Request termination of worker once parser is finished.
  307. * @memberOf THREE.LoaderSupport.WorkerSupport
  308. *
  309. * @param {boolean} terminateRequested True or false.
  310. */
  311. WorkerSupport.prototype.setTerminateRequested = function ( terminateRequested ) {
  312. this.loaderWorker.setTerminateRequested( terminateRequested );
  313. };
  314. var buildObject = function ( fullName, object ) {
  315. var objectString = fullName + ' = {\n';
  316. var part;
  317. for ( var name in object ) {
  318. part = object[ name ];
  319. if ( typeof( part ) === 'string' || part instanceof String ) {
  320. part = part.replace( '\n', '\\n' );
  321. part = part.replace( '\r', '\\r' );
  322. objectString += '\t' + name + ': "' + part + '",\n';
  323. } else if ( part instanceof Array ) {
  324. objectString += '\t' + name + ': [' + part + '],\n';
  325. } else if ( Number.isInteger( part ) ) {
  326. objectString += '\t' + name + ': ' + part + ',\n';
  327. } else if ( typeof part === 'function' ) {
  328. objectString += '\t' + name + ': ' + part + ',\n';
  329. }
  330. }
  331. objectString += '}\n\n';
  332. return objectString;
  333. };
  334. var buildSingleton = function ( fullName, object, internalName, basePrototypeName, ignoreFunctions ) {
  335. var objectString = '';
  336. var objectName = ( Validator.isValid( internalName ) ) ? internalName : object.name;
  337. var funcString, objectPart, constructorString;
  338. ignoreFunctions = Validator.verifyInput( ignoreFunctions, [] );
  339. for ( var name in object.prototype ) {
  340. objectPart = object.prototype[ name ];
  341. if ( name === 'constructor' ) {
  342. funcString = objectPart.toString();
  343. funcString = funcString.replace( 'function', '' );
  344. constructorString = '\tfunction ' + objectName + funcString + ';\n\n';
  345. } else if ( typeof objectPart === 'function' ) {
  346. if ( ignoreFunctions.indexOf( name ) < 0 ) {
  347. funcString = objectPart.toString();
  348. objectString += '\t' + objectName + '.prototype.' + name + ' = ' + funcString + ';\n\n';
  349. }
  350. }
  351. }
  352. objectString += '\treturn ' + objectName + ';\n';
  353. objectString += '})();\n\n';
  354. var inheritanceBlock = '';
  355. if ( Validator.isValid( basePrototypeName ) ) {
  356. inheritanceBlock += '\n';
  357. inheritanceBlock += objectName + '.prototype = Object.create( ' + basePrototypeName + '.prototype );\n';
  358. inheritanceBlock += objectName + '.constructor = ' + objectName + ';\n';
  359. inheritanceBlock += '\n';
  360. }
  361. if ( ! Validator.isValid( constructorString ) ) {
  362. constructorString = fullName + ' = (function () {\n\n';
  363. constructorString += inheritanceBlock + '\t' + object.prototype.constructor.toString() + '\n\n';
  364. objectString = constructorString + objectString;
  365. } else {
  366. objectString = fullName + ' = (function () {\n\n' + inheritanceBlock + constructorString + objectString;
  367. }
  368. return objectString;
  369. };
  370. return WorkerSupport;
  371. })();