瀏覽代碼

Merge pull request #17020 from kaisalmen/OBJLoader2_V300_Polish

OBJLoader2: Clean-up, code doc update and ts alignment
Mr.doob 6 年之前
父節點
當前提交
16d2521ccc

+ 2 - 1
examples/jsm/loaders/OBJLoader2.d.ts

@@ -1,6 +1,7 @@
 import {
   LoadingManager,
-  Group
+  Group,
+  Object3D
 } from '../../../src/Three';
 
 import { MaterialHandler } from './obj2/shared/MaterialHandler';

+ 1 - 1
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 ) {
 

+ 0 - 2
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() ) );' );
 
 	}

+ 5 - 1
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;
 }

+ 17 - 2
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;
 }

+ 166 - 56
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
+};

+ 1 - 1
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 );

+ 3 - 3
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;

+ 2 - 2
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 () {
 

+ 2 - 1
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";

+ 2 - 1
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";