Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/jsm/loaders/OBJLoader2.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
LoadingManager,
Group
Group,
Object3D
} from '../../../src/Three';

import { MaterialHandler } from './obj2/shared/MaterialHandler';
Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/loaders/OBJLoader2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {

Expand Down
2 changes: 0 additions & 2 deletions examples/jsm/loaders/OBJLoader2Parallel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) );' );

}
Expand Down
6 changes: 5 additions & 1 deletion examples/jsm/loaders/obj2/bridge/MtlObjBridge.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 17 additions & 2 deletions examples/jsm/loaders/obj2/utils/CodeSerializer.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
222 changes: 166 additions & 56 deletions examples/jsm/loaders/obj2/utils/CodeSerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand All @@ -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 {
Expand All @@ -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' );

}

Expand All @@ -99,77 +106,92 @@ 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' );

}

} else {

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};';

}
Expand All @@ -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
};
2 changes: 1 addition & 1 deletion examples/jsm/loaders/obj2/utils/ObjectManipulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Loading