Skip to content

Commit

Permalink
mrdoob#12456 Loads image 128 times with four workers kicked by Worker…
Browse files Browse the repository at this point in the history
…Director. Adds latest updates from main branch to LoaderSupport and OBJLoader2.
  • Loading branch information
kaisalmen committed Oct 30, 2017
1 parent de8acf7 commit f5e0204
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 91 deletions.
56 changes: 45 additions & 11 deletions examples/js/loaders/ImageBitmapLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ THREE.ImageBitmapLoader = function ( manager ) {

this.manager = manager !== undefined ? manager : THREE.DefaultLoadingManager;
this.options = undefined;
this.workerSupport = null;
this.instanceNo = 0;
this.callbacks = new THREE.LoaderSupport.Callbacks();

};

Expand All @@ -138,10 +141,6 @@ THREE.ImageBitmapLoader.prototype = {

},

run: function run( prepData ) {

},

load: function load( url, onLoad, onProgress, onError ) {

if ( url === undefined ) url = '';
Expand All @@ -168,7 +167,30 @@ THREE.ImageBitmapLoader.prototype = {

}

var workerSupport = new THREE.LoaderSupport.WorkerSupport();
this.workerSupport = new THREE.LoaderSupport.WorkerSupport();
this.workerSupport.setTerminateRequested( true );
this._execWorker( url, onLoad, onError );
},

run: function run( prepData, workerSupportExternal ) {
var Validator = THREE.LoaderSupport.Validator;

if ( Validator.isValid( workerSupportExternal ) ) {

this.workerSupport = workerSupportExternal;

} else {

this.workerSupport = Validator.verifyInput( this.workerSupport, new THREE.LoaderSupport.WorkerSupport() );

}

this._execWorker( prepData.resources[ 0 ].url, prepData.getCallbacks().onLoad, prepData.getCallbacks().onLoad );
},

_execWorker: function ( url, onLoad, onError ) {
var scope = this;

var buildWorkerCode = function ( funcBuildObject, funcBuildSingelton ) {
var workerCode = '';
workerCode += '/**\n';
Expand All @@ -184,8 +206,16 @@ THREE.ImageBitmapLoader.prototype = {

THREE.Cache.add( url, result );

if ( onLoad ) onLoad( result );
if ( onLoad ) {

onLoad( {
detail: {
imageBitmap: result,
instanceNo: scope.instanceNo
}
} );

}
scope.manager.itemEnd( url );

} else {
Expand All @@ -200,25 +230,29 @@ THREE.ImageBitmapLoader.prototype = {
var scopeFuncComplete = function ( message ) {
};

workerSupport.validate( buildWorkerCode, false );
workerSupport.setCallbacks( scopeBuilderFunc, scopeFuncComplete );
this.workerSupport.validate( buildWorkerCode, false );
this.workerSupport.setCallbacks( scopeBuilderFunc, scopeFuncComplete );
fetch( url ).then( function ( response ) {

return response.blob();

} ).then( function ( blob ) {

workerSupport.run(
scope.workerSupport.run(
{
data: {
input: blob,
options: scope.options
}
},
logger: {
debug: false,
enabled: false
},
}
);

} )

}


};
68 changes: 47 additions & 21 deletions examples/js/loaders/LoaderSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ THREE.LoaderSupport.Callbacks = (function () {
this.onProgress = null;
this.onMeshAlter = null;
this.onLoad = null;
this.onLoadMaterials = null;
}

/**
Expand Down Expand Up @@ -196,6 +197,16 @@ THREE.LoaderSupport.Callbacks = (function () {
this.onLoad = Validator.verifyInput( callbackOnLoad, this.onLoad );
};

/**
* Register callback function that is called when materials have been loaded.
* @memberOf THREE.LoaderSupport.Callbacks
*
* @param {callback} callbackOnLoadMaterials Callback function for described functionality
*/
Callbacks.prototype.setCallbackOnLoadMaterials = function ( callbackOnLoadMaterials ) {
this.onLoadMaterials = Validator.verifyInput( callbackOnLoadMaterials, this.onLoadMaterials );
};

return Callbacks;
})();

Expand Down Expand Up @@ -424,7 +435,7 @@ THREE.LoaderSupport.PrepData = (function () {
*/
THREE.LoaderSupport.Builder = (function () {

var LOADER_BUILDER_VERSION = '1.1.0';
var LOADER_BUILDER_VERSION = '1.1.1';

var Validator = THREE.LoaderSupport.Validator;
var ConsoleLogger = THREE.LoaderSupport.ConsoleLogger;
Expand All @@ -448,16 +459,17 @@ THREE.LoaderSupport.Builder = (function () {
materials: {
materialCloneInstructions: null,
serializedMaterials: null,
runtimeMaterials: materials
runtimeMaterials: Validator.isValid( this.callbacks.onLoadMaterials ) ? this.callbacks.onLoadMaterials( materials ) : materials
}
};
this.updateMaterials( payload );
};

Builder.prototype._setCallbacks = function ( callbackOnProgress, callbackOnMeshAlter, callbackOnLoad ) {
this.callbacks.setCallbackOnProgress( callbackOnProgress );
this.callbacks.setCallbackOnMeshAlter( callbackOnMeshAlter );
this.callbacks.setCallbackOnLoad( callbackOnLoad );
Builder.prototype._setCallbacks = function ( callbacks ) {
if ( Validator.isValid( callbacks.onProgress ) ) this.callbacks.setCallbackOnProgress( callbacks.onProgress );
if ( Validator.isValid( callbacks.onMeshAlter ) ) this.callbacks.setCallbackOnMeshAlter( callbacks.onMeshAlter );
if ( Validator.isValid( callbacks.onLoad ) ) this.callbacks.setCallbackOnLoad( callbacks.onLoad );
if ( Validator.isValid( callbacks.onLoadMaterials ) ) this.callbacks.setCallbackOnLoadMaterials( callbacks.onLoadMaterials );
};

/**
Expand Down Expand Up @@ -771,16 +783,17 @@ THREE.LoaderSupport.LoaderBase = (function () {
this.setUseIndices( prepData.useIndices );
this.setDisregardNormals( prepData.disregardNormals );

this._setCallbacks( prepData.getCallbacks().onProgress, prepData.getCallbacks().onMeshAlter, prepData.getCallbacks().onLoad );
this._setCallbacks( prepData.getCallbacks() );
}
};

LoaderBase.prototype._setCallbacks = function ( callbackOnProgress, callbackOnMeshAlter, callbackOnLoad ) {
this.callbacks.setCallbackOnProgress( callbackOnProgress );
this.callbacks.setCallbackOnMeshAlter( callbackOnMeshAlter );
this.callbacks.setCallbackOnLoad( callbackOnLoad );
LoaderBase.prototype._setCallbacks = function ( callbacks ) {
if ( Validator.isValid( callbacks.onProgress ) ) this.callbacks.setCallbackOnProgress( callbacks.onProgress );
if ( Validator.isValid( callbacks.onMeshAlter ) ) this.callbacks.setCallbackOnMeshAlter( callbacks.onMeshAlter );
if ( Validator.isValid( callbacks.onLoad ) ) this.callbacks.setCallbackOnLoad( callbacks.onLoad );
if ( Validator.isValid( callbacks.onLoadMaterials ) ) this.callbacks.setCallbackOnLoadMaterials( callbacks.onLoadMaterials );

this.builder._setCallbacks( callbackOnProgress, callbackOnMeshAlter, callbackOnLoad );
this.builder._setCallbacks( this.callbacks );
};

/**
Expand Down Expand Up @@ -980,7 +993,7 @@ THREE.LoaderSupport.WorkerRunnerRefImpl = (function () {
*/
THREE.LoaderSupport.WorkerSupport = (function () {

var WORKER_SUPPORT_VERSION = '1.1.0';
var WORKER_SUPPORT_VERSION = '1.1.1';

var Validator = THREE.LoaderSupport.Validator;

Expand Down Expand Up @@ -1080,18 +1093,18 @@ THREE.LoaderSupport.WorkerSupport = (function () {

if ( scope.terminateRequested ) {

scope.logger.logInfo( 'WorkerSupport [' + workerRunner + ']: Run is complete. Terminating application on request!' );
scope.logger.logInfo( 'WorkerSupport [' + workerRunner.name + ']: Run is complete. Terminating application on request!' );
scope.terminateWorker();

}
break;

case 'error':
scope.logger.logError( 'WorkerSupport [' + workerRunner + ']: Reported error: ' + payload.msg );
scope.logger.logError( 'WorkerSupport [' + workerRunner.name + ']: Reported error: ' + payload.msg );
break;

default:
scope.logger.logError( 'WorkerSupport [' + workerRunner + ']: Received unknown command: ' + payload.cmd );
scope.logger.logError( 'WorkerSupport [' + workerRunner.name + ']: Received unknown command: ' + payload.cmd );
break;

}
Expand Down Expand Up @@ -1443,7 +1456,7 @@ THREE.LoaderSupport.WorkerDirector = (function () {
WorkerDirector.prototype._buildLoader = function ( instanceNo ) {
var classDef = this.workerDescription.classDef;
var loader = Object.create( classDef.prototype );
this.workerDescription.classDef.call( loader, null, this.logger );
this.workerDescription.classDef.call( loader, THREE.DefaultLoadingManager, this.logger );

// verify that all required functions are implemented
if ( ! loader.hasOwnProperty( 'instanceNo' ) ) throw classDef.name + ' has no property "instanceNo".';
Expand All @@ -1453,12 +1466,17 @@ THREE.LoaderSupport.WorkerDirector = (function () {

throw classDef.name + ' has no property "workerSupport".';

} else if ( ! classDef.workerSupport instanceof THREE.LoaderSupport.WorkerSupport ) {
}
if ( typeof loader.run !== 'function' ) throw classDef.name + ' has no function "run".';
if ( ! loader.hasOwnProperty( 'callbacks' ) ) {

throw classDef.name + ' has no property "callbacks".';

} else if ( ! ( loader.callbacks instanceof THREE.LoaderSupport.Callbacks ) ) {

throw classDef.name + '.workerSupport is not of type "THREE.LoaderSupport.WorkerSupport".';
throw classDef.name + '.callbacks is not of type "THREE.LoaderSupport.Callbacks".';

}
if ( typeof loader.run !== 'function' ) throw classDef.name + ' has no function "run".';

return loader;
};
Expand All @@ -1473,7 +1491,15 @@ THREE.LoaderSupport.WorkerDirector = (function () {
for ( var i = 0, length = this.workerDescription.workerSupports.length; i < length; i++ ) {

var supportTuple = this.workerDescription.workerSupports[ i ];
supportTuple.workerSupport.setTerminateRequested( true );
if ( supportTuple.workerSupport.running ) {

supportTuple.workerSupport.setTerminateRequested( true );

} else {

supportTuple.workerSupport.terminateWorker();

}
this.logger.logInfo( 'Requested termination of worker.' );

var loaderCallbacks = supportTuple.loader.callbacks;
Expand Down
Loading

0 comments on commit f5e0204

Please sign in to comment.