Skip to content

Commit

Permalink
TaskManager:
Browse files Browse the repository at this point in the history
- Is now able to properly handle multiple requests for initTaskType. If one is ongoing the other requests are not answered until the first one is complete. This prevents that parsing starts in any sequentially triggered loaders before init is really complete.
- Introduced execution loop

OBJLoader2Parser
- Fix: Was not properly reset when reused in Worker.

General:
- Updated typescript definitions
  • Loading branch information
kaisalmen committed Aug 17, 2020
1 parent 827c658 commit 7643a0d
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 322 deletions.
1 change: 0 additions & 1 deletion examples/jsm/loaders/OBJLoader2.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export class OBJLoader2 extends Loader {
constructor( manager?: LoadingManager );
parser: OBJLoader2Parser;
modelName: string;
instanceNo: number;
path: string;
resourcePath: string;
baseObject3d: Object3D;
Expand Down
1 change: 0 additions & 1 deletion examples/jsm/loaders/OBJLoader2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const OBJLoader2 = function ( manager ) {
this.parser = new OBJLoader2Parser();

this.modelName = '';
this.instanceNo = 0;
this.baseObject3d = new Object3D();

this.materialHandler = new MaterialHandler();
Expand Down
50 changes: 19 additions & 31 deletions examples/jsm/loaders/OBJLoader2Parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ const OBJLoader2Parallel = function ( manager ) {

OBJLoader2.call( this, manager );
this.preferJsmWorker = false;
this.initPerformed = false;
this.jsmWorkerUrl = null;

this.executeParallel = true;

this.taskManager = new TaskManager();
this.taskManager = null;
this.taskName = 'tmOBJLoader2';
};

Expand Down Expand Up @@ -101,6 +100,12 @@ OBJLoader2Parallel.prototype = Object.assign( Object.create( OBJLoader2.prototyp
*/
_buildWorkerCode: async function () {

if ( ! this.taskManager instanceof TaskManager ) {

if ( this.parser.logging.debug ) console.log( 'Needed to create new TaskManager' );
this.taskManager = new TaskManager();

}
if ( ! this.taskManager.supportsTaskType( this.taskName ) ) {

if ( this.preferJsmWorker ) {
Expand All @@ -110,19 +115,13 @@ OBJLoader2Parallel.prototype = Object.assign( Object.create( OBJLoader2.prototyp
} else {

let obj2ParserDep = 'const OBJLoader2Parser = ' + OBJLoader2Parser.toString() + ';\n\n';
this.taskManager.registerTaskType( this.taskName, OBJ2LoaderWorker.init, OBJ2LoaderWorker.execute, null, false, [{ code: obj2ParserDep }] );
this.taskManager.registerTaskType( this.taskName, OBJ2LoaderWorker.init, OBJ2LoaderWorker.execute, null, false,
[{ code: obj2ParserDep }] );

}
await this.taskManager.initTaskType( this.taskName, {} ).catch( e => console.error( e ) );

}
else {

await new Promise( resolve => resolve( true ) );
await this.taskManager.initTaskType( this.taskName, {} );

}
this.initPerformed = true;
return this.initPerformed;

},

Expand All @@ -149,7 +148,6 @@ OBJLoader2Parallel.prototype = Object.assign( Object.create( OBJLoader2.prototyp
}

}

OBJLoader2.prototype.load.call( this, content, interceptOnLoad, onFileLoadProgress, onError, onMeshAlter );

},
Expand All @@ -163,21 +161,14 @@ OBJLoader2Parallel.prototype = Object.assign( Object.create( OBJLoader2.prototyp

if ( this.executeParallel ) {

// check if worker has been initialize before. If yes, skip init
if ( ! this.initPerformed ) {

this._buildWorkerCode()
.then( x => {
this._buildWorkerCode()
.then(
x => {
if ( this.parser.logging.debug ) console.log( 'OBJLoader2Parallel init was performed: ' + x );
this._executeWorkerParse( content )
} );
this._executeWorkerParse( content );
}
).catch( e => console.error( e ) );

}
else {

this._executeWorkerParse( content );

}
let dummy = new Object3D();
dummy.name = 'OBJLoader2ParallelDummy';
return dummy;
Expand All @@ -196,23 +187,22 @@ OBJLoader2Parallel.prototype = Object.assign( Object.create( OBJLoader2.prototyp
this.materialHandler.createDefaultMaterials( false );

let config = {
id: 42,
id: Math.floor( Math.random() * Math.floor( 65536 ) ),
buffer: content,
params: {
modelName: this.modelName,
instanceNo: this.instanceNo,
useIndices: this.parser.useIndices,
disregardNormals: this.parser.disregardNormals,
materialPerSmoothingGroup: this.parser.materialPerSmoothingGroup,
useOAsMesh: this.parser.useOAsMesh,
materials: this.materialHandler.getMaterialsJSON()
},
buffer: content,
logging: {
enabled: this.parser.logging.enabled,
debug: this.parser.logging.debug
}
};
this.taskManager.enqueueForExecution( this.taskName, config,data => this._onAssetAvailable( data ), { buffer: content.buffer } )
this.taskManager.enqueueForExecution( this.taskName, config, data => this._onAssetAvailable( data ), { buffer: content } )
.then( data => {
this._onAssetAvailable( data );
this.parser.callbacks.onLoad( this.baseObject3d, 'finished' );
Expand All @@ -222,8 +212,6 @@ OBJLoader2Parallel.prototype = Object.assign( Object.create( OBJLoader2.prototyp

}



} );

export { OBJLoader2Parallel };
132 changes: 70 additions & 62 deletions examples/jsm/loaders/obj2/OBJLoader2Parser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/**
* @author Kai Salmen / https://kaisalmen.de
* Development repository: https://github.com/kaisalmen/WWOBJLoader
*/

Expand All @@ -7,11 +8,6 @@
*/
const OBJLoader2Parser = function () {

this.logging = {
enabled: false,
debug: false
};

let scope = this;
this.callbacks = {

Expand Down Expand Up @@ -60,54 +56,65 @@ const OBJLoader2Parser = function () {

}
};
this.contentRef = null;
this.legacyMode = false;

this.materials = {};
this.materialPerSmoothingGroup = false;
this.useOAsMesh = false;
this.useIndices = false;
this.disregardNormals = false;

this.vertices = [];
this.colors = [];
this.normals = [];
this.uvs = [];
this.objectId = 0;

this.rawMesh = {
objectName: '',
groupName: '',
activeMtlName: '',
mtllibName: '',

// reset with new mesh
faceType: - 1,
subGroups: [],
subGroupInUse: null,
smoothingGroup: {
splitMaterials: false,
normalized: - 1,
real: - 1
},
counts: {

this._init = function () {

this.logging = {
enabled: false,
debug: false
};

this.contentRef = null;
this.legacyMode = false;

this.materials = {};
this.materialPerSmoothingGroup = false;
this.useOAsMesh = false;
this.useIndices = false;
this.disregardNormals = false;

this.vertices = [];
this.colors = [];
this.normals = [];
this.uvs = [];
this.objectId = 0;

this.rawMesh = {
objectName: '',
groupName: '',
activeMtlName: '',
mtllibName: '',

// reset with new mesh
faceType: - 1,
subGroups: [],
subGroupInUse: null,
smoothingGroup: {
splitMaterials: false,
normalized: - 1,
real: - 1
},
counts: {
doubleIndicesCount: 0,
faceCount: 0,
mtlCount: 0,
smoothingGroupCount: 0
}
};

this.inputObjectCount = 1;
this.outputObjectCount = 1;
this.globalCounts = {
vertices: 0,
faces: 0,
doubleIndicesCount: 0,
faceCount: 0,
mtlCount: 0,
smoothingGroupCount: 0
}
};
lineByte: 0,
currentByte: 0,
totalBytes: 0
};

this.inputObjectCount = 1;
this.outputObjectCount = 1;
this.globalCounts = {
vertices: 0,
faces: 0,
doubleIndicesCount: 0,
lineByte: 0,
currentByte: 0,
totalBytes: 0
};
}
this._init();

this._resetRawMesh = function () {

Expand Down Expand Up @@ -842,12 +849,6 @@ const OBJLoader2Parser = function () {
this._resetRawMesh();

}
this.callbacks.onAssetAvailable(
{
cmd: 'execComplete',
type: 'void'
} );

return haveMesh;

}
Expand Down Expand Up @@ -940,6 +941,7 @@ const OBJLoader2Parser = function () {
let payload = {
cmd: 'assetAvailable',
type: 'material',
id: this.objectId,
materials: {
materialCloneInstructions: materialCloneInstructions
}
Expand Down Expand Up @@ -1070,11 +1072,11 @@ const OBJLoader2Parser = function () {
uvs: uvFA
}
},
[ vertexFA.buffer ],
indexUA !== null ? [ indexUA.buffer ] : null,
colorFA !== null ? [ colorFA.buffer ] : null,
normalFA !== null ? [ normalFA.buffer ] : null,
uvFA !== null ? [ uvFA.buffer ] : null
[ vertexFA.buffer,
indexUA !== null ? indexUA.buffer : null,
colorFA !== null ? colorFA.buffer : null,
normalFA !== null ? normalFA.buffer : null,
uvFA !== null ? uvFA.buffer : null ]
);

}
Expand All @@ -1091,6 +1093,12 @@ const OBJLoader2Parser = function () {
console.info( parserFinalReport );

}
this.callbacks.onAssetAvailable(
{
cmd: 'execComplete',
type: 'void',
id: this.objectId
} );

}
};
Expand Down
24 changes: 0 additions & 24 deletions examples/jsm/loaders/obj2/shared/MaterialHandler.d.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,2 @@
import {
Material
} from '../../../../../src/Three';

export class MaterialHandler {

constructor();
logging: {
enabled: boolean;
debug: boolean;
};
callbacks: {
onLoadMaterials: Function;
};
materials: object;

createDefaultMaterials( overrideExisting: boolean ): void;
addMaterials( materials: object, overrideExisting: boolean, newMaterials?: object ): object;
addPayloadMaterials( materialPayload: object ): object;
setLogging( enabled: boolean, debug: boolean ): void;
getMaterials(): object;
getMaterial( materialName: string ): Material;
getMaterialsJSON(): object;
clearMaterials(): void;

}
Loading

0 comments on commit 7643a0d

Please sign in to comment.