diff --git a/examples/jsm/loaders/OBJLoader2.d.ts b/examples/jsm/loaders/OBJLoader2.d.ts index 5b50c42047b9f8b1c1d551de151a0028e1bf7160..1f3e244f9a4be9644bdcef427872861f272e9baa 100644 --- a/examples/jsm/loaders/OBJLoader2.d.ts +++ b/examples/jsm/loaders/OBJLoader2.d.ts @@ -1,6 +1,7 @@ import { LoadingManager, - Group + Group, + Object3D } from '../../../src/Three'; import { MaterialHandler } from './obj2/shared/MaterialHandler'; diff --git a/examples/jsm/loaders/OBJLoader2.js b/examples/jsm/loaders/OBJLoader2.js index fb3edb5d73c91f2efb0cb883f9f39143a8652718..30999503d9d9b0d3051d25d894a9bcbbaf1ffd28 100644 --- a/examples/jsm/loaders/OBJLoader2.js +++ b/examples/jsm/loaders/OBJLoader2.js @@ -269,7 +269,7 @@ OBJLoader2.prototype = { * @param {function} onLoad A function to be called after loading is successfully completed. The function receives loaded Object3D as an argument. * @param {function} [onFileLoadProgress] A function to be called while the loading is in progress. The argument will be the XMLHttpRequest instance, which contains total and Integer bytes. * @param {function} [onError] A function to be called if an error occurs during loading. The function receives the error as an argument. - * @param {function} [onMeshAlter] Called after worker successfully delivered a single mesh + * @param {function} [onMeshAlter] Called after every single mesh is made available by the parser */ load: function ( url, onLoad, onFileLoadProgress, onError, onMeshAlter ) { diff --git a/examples/jsm/loaders/OBJLoader2Parallel.js b/examples/jsm/loaders/OBJLoader2Parallel.js index 5a30fbc37e3a0f3b5ddfa64ecd006d9fff33d2a3..99e6a9855690305ff7054357b16c8e0a6a32995b 100644 --- a/examples/jsm/loaders/OBJLoader2Parallel.js +++ b/examples/jsm/loaders/OBJLoader2Parallel.js @@ -116,8 +116,6 @@ OBJLoader2Parallel.prototype.buildWorkerCode = function () { codeBuilderInstructions.addCodeFragment( codeParserPayloadHandler ); codeBuilderInstructions.addCodeFragment( codeWorkerRunner ); - // allows to include full libraries as importScripts - // codeBuilderInstructions.addLibraryImport( '../../node_modules/three/build/three.js' ); codeBuilderInstructions.addStartCode( 'new WorkerRunner( new DefaultWorkerPayloadHandler( new OBJLoader2Parser() ) );' ); } diff --git a/examples/jsm/loaders/obj2/bridge/MtlObjBridge.d.ts b/examples/jsm/loaders/obj2/bridge/MtlObjBridge.d.ts index 09e4fa33cc6941fc0c19e84ee6fdabfbd2f011c8..2912ace7c6f5ae9df3c66a151afe1f779f15c8c6 100644 --- a/examples/jsm/loaders/obj2/bridge/MtlObjBridge.d.ts +++ b/examples/jsm/loaders/obj2/bridge/MtlObjBridge.d.ts @@ -1,4 +1,8 @@ +import { + MaterialCreator +} from '../../MTLLoader'; + export namespace MtlObjBridge { export function link(processResult: object, assetLoader: object): void; - export function addMaterialsFromMtlLoader(materialCreator: object): void; + export function addMaterialsFromMtlLoader(materialCreator: MaterialCreator): void; } diff --git a/examples/jsm/loaders/obj2/utils/CodeSerializer.d.ts b/examples/jsm/loaders/obj2/utils/CodeSerializer.d.ts index 73c0af6e26c736aeffbd16d8626e71b832113143..b6da013bf48ecf5b9f345ef7efedca41bf2f34a7 100644 --- a/examples/jsm/loaders/obj2/utils/CodeSerializer.d.ts +++ b/examples/jsm/loaders/obj2/utils/CodeSerializer.d.ts @@ -1,4 +1,19 @@ export namespace CodeSerializer { - export function serializeObject(fullName: string, object: object): string; - export function serializeClass(fullName: string, object: object, constructorName?: string, basePrototypeName?: string, ignoreFunctions?: string[], includeFunctions?: string[], overrideFunctions?: string[]): string; + export function serializeObject(fullName: string, serializationTarget: object): string; + export function serializeClass(fullObjectName: string, serializationTarget: object, basePrototypeName?: string, overrideFunctions?: CodeSerializationInstruction[]): string; +} + +export class CodeSerializationInstruction { + constructor(name: string, fullName: string); + name: string; + fullName: string; + code: string; + removeCode: boolean; + + getName(): string; + getFullName(): string; + setCode(code: string): this; + getCode(): string; + setRemoveCode(removeCode: boolean): this; + isRemoveCode(): boolean; } diff --git a/examples/jsm/loaders/obj2/utils/CodeSerializer.js b/examples/jsm/loaders/obj2/utils/CodeSerializer.js index a5c3d7d3707ea274cbcb637ad671e2c52d93a7e4..a8c484a3385b2c472c44f44498597a3a58b52ae7 100644 --- a/examples/jsm/loaders/obj2/utils/CodeSerializer.js +++ b/examples/jsm/loaders/obj2/utils/CodeSerializer.js @@ -6,18 +6,19 @@ const CodeSerializer = { /** + * Serialize an object without specific prototype definition. * - * @param fullName - * @param object - * @returns {string} + * @param {String} fullObjectName complete object name + * @param {Object} serializationTarget The object that should be serialized + * @returns {String} */ - serializeObject: function ( fullName, object ) { + serializeObject: function ( fullObjectName, serializationTarget ) { - let objectString = fullName + ' = {\n\n'; + let objectString = fullObjectName + ' = {\n\n'; let part; - for ( let name in object ) { + for ( let name in serializationTarget ) { - part = object[ name ]; + part = serializationTarget[ name ]; if ( typeof ( part ) === 'string' || part instanceof String ) { part = part.replace( '\n', '\\n' ); @@ -30,7 +31,7 @@ const CodeSerializer = { } else if ( typeof part === 'object' ) { - // TODO: Short-cut for now. Recursion required? + console.log( 'Omitting object "' + name + '" and replace it with empty object.'); objectString += '\t' + name + ': {},\n'; } else { @@ -47,50 +48,56 @@ const CodeSerializer = { }, /** + * Serialize an object with specific prototype definition. * - * @param fullName - * @param object - * @param basePrototypeName - * @param ignoreFunctions - * @returns {string} + * @param {String} fullObjectName Specifies the complete object name + * @param {Object} serializationTarget The object that should be serialized + * @param {String} [basePrototypeName] Name of the prototype + * @param {Object} [overrideFunctions} Array of {@Link CodeSerializationInstruction} allows to replace or remove function with provided content + * + * @returns {String} */ - serializeClass: function ( fullName, object, constructorName, basePrototypeName, ignoreFunctions, includeFunctions, overrideFunctions ) { + serializeClass: function ( fullObjectName, serializationTarget, basePrototypeName, overrideFunctions ) { - let valueString, objectPart, constructorString, i, funcOverride; + let objectPart, constructorString, i, funcInstructions, funcTemp; let prototypeFunctions = []; let objectProperties = []; let objectFunctions = []; let isExtended = ( basePrototypeName !== null && basePrototypeName !== undefined ); - if ( ! Array.isArray( ignoreFunctions ) ) ignoreFunctions = []; - if ( ! Array.isArray( includeFunctions ) ) includeFunctions = null; if ( ! Array.isArray( overrideFunctions ) ) overrideFunctions = []; - for ( let name in object.prototype ) { + for ( let name in serializationTarget.prototype ) { + + objectPart = serializationTarget.prototype[ name ]; + funcInstructions = new CodeSerializationInstruction( name, fullObjectName + '.prototype.' + name ); + funcInstructions.setCode( objectPart.toString() ); - objectPart = object.prototype[ name ]; - valueString = objectPart.toString(); if ( name === 'constructor' ) { - constructorString = fullName + ' = ' + valueString + ';\n\n'; + if ( !funcInstructions.isRemoveCode() ) { + + constructorString = fullObjectName + ' = ' + funcInstructions.getCode() + ';\n\n'; + + } } else if ( typeof objectPart === 'function' ) { - if ( ignoreFunctions.indexOf( name ) < 0 && ( includeFunctions === null || includeFunctions.indexOf( name ) >= 0 ) ) { + funcTemp = overrideFunctions[ name ]; + if ( funcTemp instanceof CodeSerializationInstruction && funcTemp.getName() === funcInstructions.getName() ) { - funcOverride = overrideFunctions[ name ]; - if ( funcOverride && funcOverride.fullName === fullName + '.prototype.' + name ) { + funcInstructions = funcTemp; - valueString = funcOverride.code; + } + if ( !funcInstructions.isRemoveCode() ) { - } if ( isExtended ) { - prototypeFunctions.push( fullName + '.prototype.' + name + ' = ' + valueString + ';\n\n' ); + prototypeFunctions.push( funcInstructions.getFullName() + ' = ' + funcInstructions.getCode() + ';\n\n' ); } else { - prototypeFunctions.push( '\t' + name + ': ' + valueString + ',\n\n' ); + prototypeFunctions.push( '\t' + funcInstructions.getName() + ': ' + funcInstructions.getCode() + ',\n\n' ); } @@ -99,25 +106,25 @@ const CodeSerializer = { } } - for ( let name in object ) { - - objectPart = object[ name ]; + for ( let name in serializationTarget ) { + objectPart = serializationTarget[ name ]; + funcInstructions = new CodeSerializationInstruction( name, fullObjectName + '.' + name ); if ( typeof objectPart === 'function' ) { - if ( ignoreFunctions.indexOf( name ) < 0 && ( includeFunctions === null || includeFunctions.indexOf( name ) >= 0 ) ) { + funcTemp = overrideFunctions[ name ]; + if ( funcTemp instanceof CodeSerializationInstruction && funcTemp.getName() === funcInstructions.getName() ) { - funcOverride = overrideFunctions[ name ]; - if ( funcOverride && funcOverride.fullName === fullName + '.' + name ) { + funcInstructions = funcTemp; - valueString = funcOverride.code; + } else { - } else { + funcInstructions.setCode( objectPart.toString() ); - valueString = objectPart.toString(); + } + if ( ! funcInstructions.isRemoveCode() ) { - } - objectFunctions.push( fullName + '.' + name + ' = ' + valueString + ';\n\n' ); + objectFunctions.push( funcInstructions.getFullName() + ' = ' + funcInstructions.getCode() + ';\n\n' ); } @@ -125,51 +132,66 @@ const CodeSerializer = { if ( typeof ( objectPart ) === 'string' || objectPart instanceof String ) { - valueString = '\"' + objectPart.toString() + '\"'; + funcInstructions.setCode( '\"' + objectPart.toString() + '\"' ); } else if ( typeof objectPart === 'object' ) { - // TODO: Short-cut for now. Recursion required? - valueString = "{}"; + console.log( 'Omitting object "' + funcInstructions.getName() + '" and replace it with empty object.'); + funcInstructions.setCode( "{}" ); } else { - valueString = objectPart; + funcInstructions.setCode( objectPart ); } - objectProperties.push( fullName + '.' + name + ' = ' + valueString + ';\n' ); + if ( ! funcInstructions.isRemoveCode() ) { - } + objectProperties.push( funcInstructions.getFullName() + ' = ' + funcInstructions.getCode() + ';\n' ); - } - if ( ( constructorString === undefined || constructorString === null ) && typeof object.prototype.constructor === 'function' ) { + } - constructorString = fullName + ' = ' + object.prototype.constructor.toString().replace( constructorName, '' ); + } } let objectString = constructorString + '\n\n'; if ( isExtended ) { - objectString += fullName + '.prototype = Object.create( ' + basePrototypeName + '.prototype );\n'; + objectString += fullObjectName + '.prototype = Object.create( ' + basePrototypeName + '.prototype );\n'; } - objectString += fullName + '.prototype.constructor = ' + fullName + ';\n'; + objectString += fullObjectName + '.prototype.constructor = ' + fullObjectName + ';\n'; objectString += '\n\n'; - for ( i = 0; i < objectProperties.length; i ++ ) objectString += objectProperties[ i ]; + for ( i = 0; i < objectProperties.length; i ++ ) { + + objectString += objectProperties[ i ]; + + } objectString += '\n\n'; - for ( i = 0; i < objectFunctions.length; i ++ ) objectString += objectFunctions[ i ]; + for ( i = 0; i < objectFunctions.length; i ++ ) { + + objectString += objectFunctions[ i ]; + + } objectString += '\n\n'; if ( isExtended ) { - for ( i = 0; i < prototypeFunctions.length; i ++ ) objectString += prototypeFunctions[ i ]; + for ( i = 0; i < prototypeFunctions.length; i ++ ) { + + objectString += prototypeFunctions[ i ]; + + } } else { - objectString += fullName + '.prototype = {\n\n'; - for ( i = 0; i < prototypeFunctions.length; i ++ ) objectString += prototypeFunctions[ i ]; + objectString += fullObjectName + '.prototype = {\n\n'; + for ( i = 0; i < prototypeFunctions.length; i ++ ) { + + objectString += prototypeFunctions[ i ]; + + } objectString += '\n};'; } @@ -180,4 +202,92 @@ const CodeSerializer = { }, }; -export { CodeSerializer }; +/** + * Allows to define instructions to override or remove + * @param {String} name Usually the name of a function + * @param {String} fullName The name plus full object description + * @constructor + */ +const CodeSerializationInstruction = function ( name, fullName ) { + + this.name = name; + this.fullName = fullName; + this.code = null; + this.removeCode = false; + +}; + +CodeSerializationInstruction.prototype = { + + constructor: CodeSerializationInstruction, + + /** + * Returns the name of the function + * @return {String} + */ + getName: function () { + + return this.name; + + }, + + /** + * Returns the full name of the function + * @return {String} + */ + getFullName: function () { + + return this.fullName; + + }, + + /** + * Set the string containing the serialized function + * @param {String} code + * @return {CodeSerializationInstruction} + */ + setCode: function ( code ) { + + this.code = code; + return this; + + }, + + /** + * Returns the serialized function code + * @return {String} + */ + getCode: function() { + + return this.code; + + }, + + /** + * Set if function should be removed + * @param {boolean} removeCode + * @return {CodeSerializationInstruction} + */ + setRemoveCode: function ( removeCode ) { + + this.removeCode = removeCode; + return this; + + }, + + /** + * If function should be completely removed + * @return {boolean} + */ + isRemoveCode: function () { + + return this.removeCode; + + } + +}; + +export { + CodeSerializer, + CodeSerializationInstruction +}; diff --git a/examples/jsm/loaders/obj2/utils/ObjectManipulator.js b/examples/jsm/loaders/obj2/utils/ObjectManipulator.js index 190b427f08f9c1958a207b4f39cd909118aaf87e..d8f9dafd65e5dd640395ffc88288c1cf89d8ab38 100644 --- a/examples/jsm/loaders/obj2/utils/ObjectManipulator.js +++ b/examples/jsm/loaders/obj2/utils/ObjectManipulator.js @@ -16,7 +16,7 @@ const ObjectManipulator = { // fast-fail if ( objToAlter === undefined || objToAlter === null || params === undefined || params === null ) return; - var property, funcName, values; + let property, funcName, values; for ( property in params ) { funcName = 'set' + property.substring( 0, 1 ).toLocaleUpperCase() + property.substring( 1 ); diff --git a/examples/jsm/loaders/obj2/worker/main/WorkerExecutionSupport.d.ts b/examples/jsm/loaders/obj2/worker/main/WorkerExecutionSupport.d.ts index cd6c44cf1ef2bddda8d641b2a393b570b245a318..021f0a6ed3d920843d8c9eb1ad03d9aa4ccd0fd6 100644 --- a/examples/jsm/loaders/obj2/worker/main/WorkerExecutionSupport.d.ts +++ b/examples/jsm/loaders/obj2/worker/main/WorkerExecutionSupport.d.ts @@ -30,7 +30,7 @@ export class WorkerExecutionSupport { }; worker: { - native: null; + native: Worker; jsmWorker: boolean; logging: boolean; workerRunner: { @@ -41,13 +41,13 @@ export class WorkerExecutionSupport { terminateWorkerOnLoad: boolean; forceWorkerDataCopy: boolean; started: boolean; - queuedMessage: null; + queuedMessage: object; callbacks: { onAssetAvailable: Function; onLoad: Function; terminate: Function; }; - } + }; setLogging(enabled: boolean, debug: boolean): this; setForceWorkerDataCopy(forceWorkerDataCopy: boolean): this; diff --git a/examples/jsm/loaders/obj2/worker/parallel/OBJLoader2Parser.js b/examples/jsm/loaders/obj2/worker/parallel/OBJLoader2Parser.js index 20ccc506e95ab62ebe5997a1d7518f36dd24237f..27b715ebfaf39c979ababf91318198db87f8f881 100644 --- a/examples/jsm/loaders/obj2/worker/parallel/OBJLoader2Parser.js +++ b/examples/jsm/loaders/obj2/worker/parallel/OBJLoader2Parser.js @@ -1,10 +1,10 @@ /** - * @author Kai Salmen / www.kaisalmen.de + * @author Kai Salmen / https://kaisalmen.de + * Development repository: https://github.com/kaisalmen/WWOBJLoader */ /** * Parse OBJ data either from ArrayBuffer or string - * @class */ const OBJLoader2Parser = function () { diff --git a/examples/jsm/loaders/obj2/worker/parallel/WorkerRunner.js b/examples/jsm/loaders/obj2/worker/parallel/WorkerRunner.js index 7fe267690c8d38abc7fd7c384111e9b85074d922..d300d1c01e396eaaab3c3ca4d3bdb83c2e87201b 100644 --- a/examples/jsm/loaders/obj2/worker/parallel/WorkerRunner.js +++ b/examples/jsm/loaders/obj2/worker/parallel/WorkerRunner.js @@ -1,5 +1,6 @@ /** - * @author Kai Salmen / www.kaisalmen.de + * @author Kai Salmen / https://kaisalmen.de + * Development repository: https://github.com/kaisalmen/WWOBJLoader */ import { ObjectManipulator } from "../../utils/ObjectManipulator.js"; diff --git a/examples/jsm/loaders/obj2/worker/parallel/jsm/OBJLoader2Worker.js b/examples/jsm/loaders/obj2/worker/parallel/jsm/OBJLoader2Worker.js index 85ca6b55f7ff2d12e45faed1fe52187017139afc..c9f2de76ead706d44b1030839adf623d92ad9b55 100644 --- a/examples/jsm/loaders/obj2/worker/parallel/jsm/OBJLoader2Worker.js +++ b/examples/jsm/loaders/obj2/worker/parallel/jsm/OBJLoader2Worker.js @@ -1,5 +1,6 @@ /** - * @author Kai Salmen / www.kaisalmen.de + * @author Kai Salmen / https://kaisalmen.de + * Development repository: https://github.com/kaisalmen/WWOBJLoader */ import { OBJLoader2Parser } from "../OBJLoader2Parser.js";