495 lines
16 KiB
JavaScript
495 lines
16 KiB
JavaScript
/**
|
|
* Default implementation of the WorkerRunner responsible for creation and configuration of the parser within the worker.
|
|
*
|
|
* @class
|
|
*/
|
|
THREE.LoaderSupport.WorkerRunnerRefImpl = (function () {
|
|
|
|
function WorkerRunnerRefImpl() {
|
|
var scope = this;
|
|
var scopedRunner = function( event ) {
|
|
scope.processMessage( event.data );
|
|
};
|
|
self.addEventListener( 'message', scopedRunner, false );
|
|
}
|
|
|
|
/**
|
|
* Applies values from parameter object via set functions or via direct assignment.
|
|
* @memberOf THREE.LoaderSupport.WorkerRunnerRefImpl
|
|
*
|
|
* @param {Object} parser The parser instance
|
|
* @param {Object} params The parameter object
|
|
*/
|
|
WorkerRunnerRefImpl.prototype.applyProperties = function ( parser, params ) {
|
|
var property, funcName, values;
|
|
for ( property in params ) {
|
|
funcName = 'set' + property.substring( 0, 1 ).toLocaleUpperCase() + property.substring( 1 );
|
|
values = params[ property ];
|
|
|
|
if ( typeof parser[ funcName ] === 'function' ) {
|
|
|
|
parser[ funcName ]( values );
|
|
|
|
} else if ( parser.hasOwnProperty( property ) ) {
|
|
|
|
parser[ property ] = values;
|
|
|
|
}
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Configures the Parser implementation according the supplied configuration object.
|
|
* @memberOf THREE.LoaderSupport.WorkerRunnerRefImpl
|
|
*
|
|
* @param {Object} payload Raw mesh description (buffers, params, materials) used to build one to many meshes.
|
|
*/
|
|
WorkerRunnerRefImpl.prototype.processMessage = function ( payload ) {
|
|
if ( payload.cmd === 'run' ) {
|
|
|
|
var callbacks = {
|
|
callbackMeshBuilder: function ( payload ) {
|
|
self.postMessage( payload );
|
|
},
|
|
callbackProgress: function ( text ) {
|
|
if ( payload.logging.enabled && payload.logging.debug ) console.debug( 'WorkerRunner: progress: ' + text );
|
|
}
|
|
};
|
|
|
|
// Parser is expected to be named as such
|
|
var parser = new Parser();
|
|
if ( typeof parser[ 'setLogging' ] === 'function' ) parser.setLogging( payload.logging.enabled, payload.logging.debug );
|
|
this.applyProperties( parser, payload.params );
|
|
this.applyProperties( parser, payload.materials );
|
|
this.applyProperties( parser, callbacks );
|
|
parser.workerScope = self;
|
|
parser.parse( payload.data.input, payload.data.options );
|
|
|
|
if ( payload.logging.enabled ) console.log( 'WorkerRunner: Run complete!' );
|
|
|
|
callbacks.callbackMeshBuilder( {
|
|
cmd: 'complete',
|
|
msg: 'WorkerRunner completed run.'
|
|
} );
|
|
|
|
} else {
|
|
|
|
console.error( 'WorkerRunner: Received unknown command: ' + payload.cmd );
|
|
|
|
}
|
|
};
|
|
|
|
return WorkerRunnerRefImpl;
|
|
})();
|
|
|
|
/**
|
|
* This class provides means to transform existing parser code into a web worker. It defines a simple communication protocol
|
|
* which allows to configure the worker and receive raw mesh data during execution.
|
|
* @class
|
|
*/
|
|
THREE.LoaderSupport.WorkerSupport = (function () {
|
|
|
|
var WORKER_SUPPORT_VERSION = '2.2.0';
|
|
|
|
var Validator = THREE.LoaderSupport.Validator;
|
|
|
|
var LoaderWorker = (function () {
|
|
|
|
function LoaderWorker() {
|
|
this._reset();
|
|
}
|
|
|
|
LoaderWorker.prototype._reset = function () {
|
|
this.logging = {
|
|
enabled: true,
|
|
debug: false
|
|
};
|
|
this.worker = null;
|
|
this.runnerImplName = null;
|
|
this.callbacks = {
|
|
meshBuilder: null,
|
|
onLoad: null
|
|
};
|
|
this.terminateRequested = false;
|
|
this.queuedMessage = null;
|
|
this.started = false;
|
|
this.forceCopy = false;
|
|
};
|
|
|
|
LoaderWorker.prototype.setLogging = function ( enabled, debug ) {
|
|
this.logging.enabled = enabled === true;
|
|
this.logging.debug = debug === true;
|
|
};
|
|
|
|
LoaderWorker.prototype.setForceCopy = function ( forceCopy ) {
|
|
this.forceCopy = forceCopy === true;
|
|
};
|
|
|
|
LoaderWorker.prototype.initWorker = function ( code, runnerImplName ) {
|
|
this.runnerImplName = runnerImplName;
|
|
var blob = new Blob( [ code ], { type: 'application/javascript' } );
|
|
this.worker = new Worker( window.URL.createObjectURL( blob ) );
|
|
this.worker.onmessage = this._receiveWorkerMessage;
|
|
|
|
// set referemce to this, then processing in worker scope within "_receiveWorkerMessage" can access members
|
|
this.worker.runtimeRef = this;
|
|
|
|
// process stored queuedMessage
|
|
this._postMessage();
|
|
};
|
|
|
|
/**
|
|
* Executed in worker scope
|
|
*/
|
|
LoaderWorker.prototype._receiveWorkerMessage = function ( e ) {
|
|
var payload = e.data;
|
|
switch ( payload.cmd ) {
|
|
case 'meshData':
|
|
case 'materialData':
|
|
case 'imageData':
|
|
this.runtimeRef.callbacks.meshBuilder( payload );
|
|
break;
|
|
|
|
case 'complete':
|
|
this.runtimeRef.queuedMessage = null;
|
|
this.started = false;
|
|
this.runtimeRef.callbacks.onLoad( payload.msg );
|
|
|
|
if ( this.runtimeRef.terminateRequested ) {
|
|
|
|
if ( this.runtimeRef.logging.enabled ) console.info( 'WorkerSupport [' + this.runtimeRef.runnerImplName + ']: Run is complete. Terminating application on request!' );
|
|
this.runtimeRef._terminate();
|
|
|
|
}
|
|
break;
|
|
|
|
case 'error':
|
|
console.error( 'WorkerSupport [' + this.runtimeRef.runnerImplName + ']: Reported error: ' + payload.msg );
|
|
this.runtimeRef.queuedMessage = null;
|
|
this.started = false;
|
|
this.runtimeRef.callbacks.onLoad( payload.msg );
|
|
|
|
if ( this.runtimeRef.terminateRequested ) {
|
|
|
|
if ( this.runtimeRef.logging.enabled ) console.info( 'WorkerSupport [' + this.runtimeRef.runnerImplName + ']: Run reported error. Terminating application on request!' );
|
|
this.runtimeRef._terminate();
|
|
|
|
}
|
|
break;
|
|
|
|
default:
|
|
console.error( 'WorkerSupport [' + this.runtimeRef.runnerImplName + ']: Received unknown command: ' + payload.cmd );
|
|
break;
|
|
|
|
}
|
|
};
|
|
|
|
LoaderWorker.prototype.setCallbacks = function ( meshBuilder, onLoad ) {
|
|
this.callbacks.meshBuilder = Validator.verifyInput( meshBuilder, this.callbacks.meshBuilder );
|
|
this.callbacks.onLoad = Validator.verifyInput( onLoad, this.callbacks.onLoad );
|
|
};
|
|
|
|
LoaderWorker.prototype.run = function( payload ) {
|
|
if ( Validator.isValid( this.queuedMessage ) ) {
|
|
|
|
console.warn( 'Already processing message. Rejecting new run instruction' );
|
|
return;
|
|
|
|
} else {
|
|
|
|
this.queuedMessage = payload;
|
|
this.started = true;
|
|
|
|
}
|
|
if ( ! Validator.isValid( this.callbacks.meshBuilder ) ) throw 'Unable to run as no "MeshBuilder" callback is set.';
|
|
if ( ! Validator.isValid( this.callbacks.onLoad ) ) throw 'Unable to run as no "onLoad" callback is set.';
|
|
if ( payload.cmd !== 'run' ) payload.cmd = 'run';
|
|
if ( Validator.isValid( payload.logging ) ) {
|
|
|
|
payload.logging.enabled = payload.logging.enabled === true;
|
|
payload.logging.debug = payload.logging.debug === true;
|
|
|
|
} else {
|
|
|
|
payload.logging = {
|
|
enabled: true,
|
|
debug: false
|
|
}
|
|
|
|
}
|
|
this._postMessage();
|
|
};
|
|
|
|
LoaderWorker.prototype._postMessage = function () {
|
|
if ( Validator.isValid( this.queuedMessage ) && Validator.isValid( this.worker ) ) {
|
|
|
|
if ( this.queuedMessage.data.input instanceof ArrayBuffer ) {
|
|
|
|
var content;
|
|
if ( this.forceCopy ) {
|
|
|
|
content = this.queuedMessage.data.input.slice( 0 );
|
|
|
|
} else {
|
|
|
|
content = this.queuedMessage.data.input;
|
|
|
|
}
|
|
this.worker.postMessage( this.queuedMessage, [ content ] );
|
|
|
|
} else {
|
|
|
|
this.worker.postMessage( this.queuedMessage );
|
|
|
|
}
|
|
|
|
}
|
|
};
|
|
|
|
LoaderWorker.prototype.setTerminateRequested = function ( terminateRequested ) {
|
|
this.terminateRequested = terminateRequested === true;
|
|
if ( this.terminateRequested && Validator.isValid( this.worker ) && ! Validator.isValid( this.queuedMessage ) && this.started ) {
|
|
|
|
if ( this.logging.enabled ) console.info( 'Worker is terminated immediately as it is not running!' );
|
|
this._terminate();
|
|
|
|
}
|
|
};
|
|
|
|
LoaderWorker.prototype._terminate = function () {
|
|
this.worker.terminate();
|
|
this._reset();
|
|
};
|
|
|
|
return LoaderWorker;
|
|
|
|
})();
|
|
|
|
function WorkerSupport() {
|
|
console.info( 'Using THREE.LoaderSupport.WorkerSupport version: ' + WORKER_SUPPORT_VERSION );
|
|
this.logging = {
|
|
enabled: true,
|
|
debug: false
|
|
};
|
|
|
|
// check worker support first
|
|
if ( window.Worker === undefined ) throw "This browser does not support web workers!";
|
|
if ( window.Blob === undefined ) throw "This browser does not support Blob!";
|
|
if ( typeof window.URL.createObjectURL !== 'function' ) throw "This browser does not support Object creation from URL!";
|
|
|
|
this.loaderWorker = new LoaderWorker();
|
|
}
|
|
|
|
/**
|
|
* Enable or disable logging in general (except warn and error), plus enable or disable debug logging.
|
|
* @memberOf THREE.LoaderSupport.WorkerSupport
|
|
*
|
|
* @param {boolean} enabled True or false.
|
|
* @param {boolean} debug True or false.
|
|
*/
|
|
WorkerSupport.prototype.setLogging = function ( enabled, debug ) {
|
|
this.logging.enabled = enabled === true;
|
|
this.logging.debug = debug === true;
|
|
this.loaderWorker.setLogging( this.logging.enabled, this.logging.debug );
|
|
};
|
|
|
|
/**
|
|
* Forces all ArrayBuffers to be transferred to worker to be copied.
|
|
* @memberOf THREE.LoaderSupport.WorkerSupport
|
|
*
|
|
* @param {boolean} forceWorkerDataCopy True or false.
|
|
*/
|
|
WorkerSupport.prototype.setForceWorkerDataCopy = function ( forceWorkerDataCopy ) {
|
|
this.loaderWorker.setForceCopy( forceWorkerDataCopy );
|
|
};
|
|
|
|
/**
|
|
* Validate the status of worker code and the derived worker.
|
|
* @memberOf THREE.LoaderSupport.WorkerSupport
|
|
*
|
|
* @param {Function} functionCodeBuilder Function that is invoked with funcBuildObject and funcBuildSingleton that allows stringification of objects and singletons.
|
|
* @param {String} parserName Name of the Parser object
|
|
* @param {String[]} libLocations URL of libraries that shall be added to worker code relative to libPath
|
|
* @param {String} libPath Base path used for loading libraries
|
|
* @param {THREE.LoaderSupport.WorkerRunnerRefImpl} runnerImpl The default worker parser wrapper implementation (communication and execution). An extended class could be passed here.
|
|
*/
|
|
WorkerSupport.prototype.validate = function ( functionCodeBuilder, parserName, libLocations, libPath, runnerImpl ) {
|
|
if ( Validator.isValid( this.loaderWorker.worker ) ) return;
|
|
|
|
if ( this.logging.enabled ) {
|
|
|
|
console.info( 'WorkerSupport: Building worker code...' );
|
|
console.time( 'buildWebWorkerCode' );
|
|
|
|
}
|
|
if ( Validator.isValid( runnerImpl ) ) {
|
|
|
|
if ( this.logging.enabled ) console.info( 'WorkerSupport: Using "' + runnerImpl.name + '" as Runner class for worker.' );
|
|
|
|
} else {
|
|
|
|
runnerImpl = THREE.LoaderSupport.WorkerRunnerRefImpl;
|
|
if ( this.logging.enabled ) console.info( 'WorkerSupport: Using DEFAULT "THREE.LoaderSupport.WorkerRunnerRefImpl" as Runner class for worker.' );
|
|
|
|
}
|
|
|
|
var userWorkerCode = functionCodeBuilder( buildObject, buildSingleton );
|
|
userWorkerCode += 'var Parser = '+ parserName + ';\n\n';
|
|
userWorkerCode += buildSingleton( runnerImpl.name, runnerImpl );
|
|
userWorkerCode += 'new ' + runnerImpl.name + '();\n\n';
|
|
|
|
var scope = this;
|
|
if ( Validator.isValid( libLocations ) && libLocations.length > 0 ) {
|
|
|
|
var libsContent = '';
|
|
var loadAllLibraries = function ( path, locations ) {
|
|
if ( locations.length === 0 ) {
|
|
|
|
scope.loaderWorker.initWorker( libsContent + userWorkerCode, runnerImpl.name );
|
|
if ( scope.logging.enabled ) console.timeEnd( 'buildWebWorkerCode' );
|
|
|
|
} else {
|
|
|
|
var loadedLib = function ( contentAsString ) {
|
|
libsContent += contentAsString;
|
|
loadAllLibraries( path, locations );
|
|
};
|
|
|
|
var fileLoader = new THREE.FileLoader();
|
|
fileLoader.setPath( path );
|
|
fileLoader.setResponseType( 'text' );
|
|
fileLoader.load( locations[ 0 ], loadedLib );
|
|
locations.shift();
|
|
|
|
}
|
|
};
|
|
loadAllLibraries( libPath, libLocations );
|
|
|
|
} else {
|
|
|
|
this.loaderWorker.initWorker( userWorkerCode, runnerImpl.name );
|
|
if ( this.logging.enabled ) console.timeEnd( 'buildWebWorkerCode' );
|
|
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Specify functions that should be build when new raw mesh data becomes available and when the parser is finished.
|
|
* @memberOf THREE.LoaderSupport.WorkerSupport
|
|
*
|
|
* @param {Function} meshBuilder The mesh builder function. Default is {@link THREE.LoaderSupport.MeshBuilder}.
|
|
* @param {Function} onLoad The function that is called when parsing is complete.
|
|
*/
|
|
WorkerSupport.prototype.setCallbacks = function ( meshBuilder, onLoad ) {
|
|
this.loaderWorker.setCallbacks( meshBuilder, onLoad );
|
|
};
|
|
|
|
/**
|
|
* Runs the parser with the provided configuration.
|
|
* @memberOf THREE.LoaderSupport.WorkerSupport
|
|
*
|
|
* @param {Object} payload Raw mesh description (buffers, params, materials) used to build one to many meshes.
|
|
*/
|
|
WorkerSupport.prototype.run = function ( payload ) {
|
|
this.loaderWorker.run( payload );
|
|
};
|
|
|
|
/**
|
|
* Request termination of worker once parser is finished.
|
|
* @memberOf THREE.LoaderSupport.WorkerSupport
|
|
*
|
|
* @param {boolean} terminateRequested True or false.
|
|
*/
|
|
WorkerSupport.prototype.setTerminateRequested = function ( terminateRequested ) {
|
|
this.loaderWorker.setTerminateRequested( terminateRequested );
|
|
};
|
|
|
|
var buildObject = function ( fullName, object ) {
|
|
var objectString = fullName + ' = {\n';
|
|
var part;
|
|
for ( var name in object ) {
|
|
|
|
part = object[ name ];
|
|
if ( typeof( part ) === 'string' || part instanceof String ) {
|
|
|
|
part = part.replace( '\n', '\\n' );
|
|
part = part.replace( '\r', '\\r' );
|
|
objectString += '\t' + name + ': "' + part + '",\n';
|
|
|
|
} else if ( part instanceof Array ) {
|
|
|
|
objectString += '\t' + name + ': [' + part + '],\n';
|
|
|
|
} else if ( Number.isInteger( part ) ) {
|
|
|
|
objectString += '\t' + name + ': ' + part + ',\n';
|
|
|
|
} else if ( typeof part === 'function' ) {
|
|
|
|
objectString += '\t' + name + ': ' + part + ',\n';
|
|
|
|
}
|
|
|
|
}
|
|
objectString += '}\n\n';
|
|
|
|
return objectString;
|
|
};
|
|
|
|
var buildSingleton = function ( fullName, object, internalName, basePrototypeName, ignoreFunctions ) {
|
|
var objectString = '';
|
|
var objectName = ( Validator.isValid( internalName ) ) ? internalName : object.name;
|
|
|
|
var funcString, objectPart, constructorString;
|
|
ignoreFunctions = Validator.verifyInput( ignoreFunctions, [] );
|
|
for ( var name in object.prototype ) {
|
|
|
|
objectPart = object.prototype[ name ];
|
|
if ( name === 'constructor' ) {
|
|
|
|
funcString = objectPart.toString();
|
|
funcString = funcString.replace( 'function', '' );
|
|
constructorString = '\tfunction ' + objectName + funcString + ';\n\n';
|
|
|
|
} else if ( typeof objectPart === 'function' ) {
|
|
|
|
if ( ignoreFunctions.indexOf( name ) < 0 ) {
|
|
|
|
funcString = objectPart.toString();
|
|
objectString += '\t' + objectName + '.prototype.' + name + ' = ' + funcString + ';\n\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
objectString += '\treturn ' + objectName + ';\n';
|
|
objectString += '})();\n\n';
|
|
|
|
var inheritanceBlock = '';
|
|
if ( Validator.isValid( basePrototypeName ) ) {
|
|
|
|
inheritanceBlock += '\n';
|
|
inheritanceBlock += objectName + '.prototype = Object.create( ' + basePrototypeName + '.prototype );\n';
|
|
inheritanceBlock += objectName + '.constructor = ' + objectName + ';\n';
|
|
inheritanceBlock += '\n';
|
|
}
|
|
if ( ! Validator.isValid( constructorString ) ) {
|
|
|
|
constructorString = fullName + ' = (function () {\n\n';
|
|
constructorString += inheritanceBlock + '\t' + object.prototype.constructor.toString() + '\n\n';
|
|
objectString = constructorString + objectString;
|
|
|
|
} else {
|
|
|
|
objectString = fullName + ' = (function () {\n\n' + inheritanceBlock + constructorString + objectString;
|
|
|
|
}
|
|
|
|
return objectString;
|
|
};
|
|
|
|
return WorkerSupport;
|
|
|
|
})();
|