Browse Source

OBJLoader2(Parallel) and all sub-classes only log if it was enabled beforehand (see webgl_lodaer_obj2.html)
MaterialHandler does not longer initialize default materials in constructor. This is now handled by OBJLoader2(Parallel)

Kai Salmen 5 years ago
parent
commit
afb8b61318

+ 11 - 14
examples/jsm/loaders/OBJLoader2.js

@@ -32,7 +32,12 @@ const OBJLoader2 = function ( manager ) {
 	this.materialHandler = new MaterialHandler();
 	this.materialHandler = new MaterialHandler();
 	this.meshReceiver = new MeshReceiver( this.materialHandler );
 	this.meshReceiver = new MeshReceiver( this.materialHandler );
 
 
-	this._init();
+	// as OBJLoader2 is no longer derived from OBJLoader2Parser, we need to override the default onAssetAvailable callback
+	let scope = this;
+	let defaultOnAssetAvailable = function ( payload ) {
+		scope._onAssetAvailable( payload )
+	};
+	this.parser.setCallbackOnAssetAvailable( defaultOnAssetAvailable );
 };
 };
 
 
 OBJLoader2.OBJLOADER2_VERSION = '3.0.1';
 OBJLoader2.OBJLOADER2_VERSION = '3.0.1';
@@ -43,17 +48,6 @@ OBJLoader2.prototype = Object.assign( Object.create( Loader.prototype ), {
 
 
 	constructor: OBJLoader2,
 	constructor: OBJLoader2,
 
 
-	_init: function () {
-
-		// as OBJLoader2 is no longer derived from OBJLoader2Parser, we need to override the default onAssetAvailable callback
-		let scope = this;
-		let defaultOnAssetAvailable = function ( payload ) {
-			scope._onAssetAvailable( payload )
-		};
-		this.parser.setCallbackOnAssetAvailable( defaultOnAssetAvailable );
-
-	},
-
 	/**
 	/**
 	 * See {@link OBJLoader2Parser.setLogging}
 	 * See {@link OBJLoader2Parser.setLogging}
 	 * @return {OBJLoader2}
 	 * @return {OBJLoader2}
@@ -326,8 +320,11 @@ OBJLoader2.prototype = Object.assign( Object.create( Loader.prototype ), {
 
 
 		}
 		}
 
 
-		// sync code works directly on the material references
-		this.parser._setMaterials( this.materialHandler.getMaterials() );
+		// Create default materials beforehand, but do not override previously set materials (e.g. during init)
+		this.materialHandler.createDefaultMaterials( false );
+
+		// code works directly on the material references, parser clear its materials before updating
+		this.parser.setMaterials( this.materialHandler.getMaterials() );
 
 
 		if ( content instanceof ArrayBuffer || content instanceof Uint8Array ) {
 		if ( content instanceof ArrayBuffer || content instanceof Uint8Array ) {
 
 

+ 26 - 27
examples/jsm/loaders/OBJLoader2Parallel.js

@@ -114,32 +114,6 @@ OBJLoader2Parallel.prototype = Object.assign( Object.create( OBJLoader2.prototyp
 
 
 	},
 	},
 
 
-	_configure: function () {
-
-		if ( this.parser.callbacks.onLoad === this.parser._onLoad ) {
-
-			throw "No callback other than the default callback was provided! Aborting!";
-
-		}
-		// check if worker is already available and if so, then fast-fail
-		if ( this.workerExecutionSupport.isWorkerLoaded( this.preferJsmWorker ) ) return;
-
-		this.workerExecutionSupport.buildWorker( this.buildWorkerCode() );
-
-		let scope = this;
-		let scopedOnAssetAvailable = function ( payload ) {
-
-			scope._onAssetAvailable( payload );
-
-		};
-
-		function scopedOnLoad ( message ) {
-			scope.parser.callbacks.onLoad( scope.baseObject3d, message );
-		}
-		this.workerExecutionSupport.updateCallbacks( scopedOnAssetAvailable, scopedOnLoad );
-
-	},
-
 	/**
 	/**
 	 * See {@link OBJLoader2.load}
 	 * See {@link OBJLoader2.load}
 	 */
 	 */
@@ -178,7 +152,32 @@ OBJLoader2Parallel.prototype = Object.assign( Object.create( OBJLoader2.prototyp
 
 
 		if ( this.executeParallel ) {
 		if ( this.executeParallel ) {
 
 
-			this._configure();
+			if ( this.parser.callbacks.onLoad === this.parser._onLoad ) {
+
+				throw "No callback other than the default callback was provided! Aborting!";
+
+			}
+			// check if worker has been initialize before. If yes, skip init
+			if ( ! this.workerExecutionSupport.isWorkerLoaded( this.preferJsmWorker ) ) {
+
+				this.workerExecutionSupport.buildWorker( this.buildWorkerCode() );
+
+				let scope = this;
+				let scopedOnAssetAvailable = function ( payload ) {
+
+					scope._onAssetAvailable( payload );
+
+				};
+				function scopedOnLoad( message ) {
+					scope.parser.callbacks.onLoad( scope.baseObject3d, message );
+				}
+
+				this.workerExecutionSupport.updateCallbacks( scopedOnAssetAvailable, scopedOnLoad );
+
+			}
+
+			// Create default materials beforehand, but do not override previously set materials (e.g. during init)
+			this.materialHandler.createDefaultMaterials( false );
 
 
 			this.workerExecutionSupport.executeParallel(
 			this.workerExecutionSupport.executeParallel(
 			{
 			{

+ 1 - 1
examples/jsm/loaders/obj2/bridge/MtlObjBridge.js

@@ -17,7 +17,7 @@ const MtlObjBridge = {
 
 
 		if ( typeof assetLoader.addMaterials === 'function' ) {
 		if ( typeof assetLoader.addMaterials === 'function' ) {
 
 
-			assetLoader.addMaterials( this.addMaterialsFromMtlLoader( processResult ) );
+			assetLoader.addMaterials( this.addMaterialsFromMtlLoader( processResult ), true );
 
 
 		}
 		}
 
 

+ 3 - 1
examples/jsm/loaders/obj2/shared/MaterialHandler.d.ts

@@ -13,10 +13,12 @@ export class MaterialHandler {
   };
   };
   materials: object;
   materials: object;
 
 
-  addMaterials(materials: object, newMaterials: object): object;
+  createDefaultMaterials(overrideExisting: boolean): void;
+  addMaterials(materials: object, overrideExisting: boolean, newMaterials: object): object;
   addPayloadMaterials(materialPayload: object): object;
   addPayloadMaterials(materialPayload: object): object;
   setLogging(enabled: boolean, debug: boolean): void;
   setLogging(enabled: boolean, debug: boolean): void;
   getMaterials(): object;
   getMaterials(): object;
   getMaterial(materialName: string): Material;
   getMaterial(materialName: string): Material;
   getMaterialsJSON(): object;
   getMaterialsJSON(): object;
+  clearMaterials(): void;
 }
 }

+ 48 - 11
examples/jsm/loaders/obj2/shared/MaterialHandler.js

@@ -15,7 +15,7 @@ import {
 const MaterialHandler = function () {
 const MaterialHandler = function () {
 
 
 	this.logging = {
 	this.logging = {
-		enabled: true,
+		enabled: false,
 		debug: false
 		debug: false
 	};
 	};
 
 
@@ -23,7 +23,6 @@ const MaterialHandler = function () {
 		onLoadMaterials: null
 		onLoadMaterials: null
 	};
 	};
 	this.materials = {};
 	this.materials = {};
-	this._createDefaultMaterials();
 
 
 };
 };
 
 
@@ -54,7 +53,12 @@ MaterialHandler.prototype = {
 
 
 	},
 	},
 
 
-	_createDefaultMaterials: function () {
+	/**
+	 * Creates default materials and adds them to the materials object.
+	 *
+	 * @param overrideExisting boolean Override existing material
+	 */
+	createDefaultMaterials: function ( overrideExisting ) {
 
 
 		let defaultMaterial = new MeshStandardMaterial( { color: 0xDCF1FF } );
 		let defaultMaterial = new MeshStandardMaterial( { color: 0xDCF1FF } );
 		defaultMaterial.name = 'defaultMaterial';
 		defaultMaterial.name = 'defaultMaterial';
@@ -75,7 +79,7 @@ MaterialHandler.prototype = {
 		runtimeMaterials[ defaultLineMaterial.name ] = defaultLineMaterial;
 		runtimeMaterials[ defaultLineMaterial.name ] = defaultLineMaterial;
 		runtimeMaterials[ defaultPointMaterial.name ] = defaultPointMaterial;
 		runtimeMaterials[ defaultPointMaterial.name ] = defaultPointMaterial;
 
 
-		this.addMaterials( runtimeMaterials );
+		this.addMaterials( runtimeMaterials, overrideExisting );
 
 
 	},
 	},
 
 
@@ -118,7 +122,11 @@ MaterialHandler.prototype = {
 
 
 			} else {
 			} else {
 
 
-				console.info( 'Requested material "' + materialNameOrg + '" is not available!' );
+				if ( this.logging.enabled) {
+
+					console.info( 'Requested material "' + materialNameOrg + '" is not available!' );
+
+				}
 
 
 			}
 			}
 
 
@@ -135,7 +143,11 @@ MaterialHandler.prototype = {
 				if ( materialJson !== undefined && materialJson !== null ) {
 				if ( materialJson !== undefined && materialJson !== null ) {
 
 
 					material = loader.parse( materialJson );
 					material = loader.parse( materialJson );
-					if ( this.logging.enabled ) console.info( 'De-serialized material with name "' + materialName + '" will be added.' );
+					if ( this.logging.enabled ) {
+
+						console.info( 'De-serialized material with name "' + materialName + '" will be added.' );
+
+					}
 					this.materials[ materialName ] = material;
 					this.materials[ materialName ] = material;
 					newMaterials[ materialName ] = material;
 					newMaterials[ materialName ] = material;
 
 
@@ -145,7 +157,7 @@ MaterialHandler.prototype = {
 
 
 		}
 		}
 		materials = materialPayload.materials.runtimeMaterials;
 		materials = materialPayload.materials.runtimeMaterials;
-		newMaterials = this.addMaterials( materials, newMaterials );
+		newMaterials = this.addMaterials( materials, true, newMaterials );
 
 
 		return newMaterials;
 		return newMaterials;
 
 
@@ -155,9 +167,10 @@ MaterialHandler.prototype = {
 	 * Set materials loaded by any supplier of an Array of {@link Material}.
 	 * Set materials loaded by any supplier of an Array of {@link Material}.
 	 *
 	 *
 	 * @param materials Object with named {@link Material}
 	 * @param materials Object with named {@link Material}
+	 * @param overrideExisting boolean Override existing material
 	 * @param newMaterials [Object] with named {@link Material}
 	 * @param newMaterials [Object] with named {@link Material}
 	 */
 	 */
-	addMaterials: function ( materials, newMaterials ) {
+	addMaterials: function ( materials, overrideExisting, newMaterials ) {
 
 
 		if ( newMaterials === undefined || newMaterials === null ) {
 		if ( newMaterials === undefined || newMaterials === null ) {
 
 
@@ -167,12 +180,29 @@ MaterialHandler.prototype = {
 		if ( materials !== undefined && materials !== null && Object.keys( materials ).length > 0 ) {
 		if ( materials !== undefined && materials !== null && Object.keys( materials ).length > 0 ) {
 
 
 			let material;
 			let material;
+			let existingMaterial;
+			let add;
 			for ( let materialName in materials ) {
 			for ( let materialName in materials ) {
 
 
 				material = materials[ materialName ];
 				material = materials[ materialName ];
-				this.materials[ materialName ] = material;
-				newMaterials[ materialName ] = material;
-				if ( this.logging.enabled && this.logging.debug ) console.info( 'Material with name "' + materialName + '" was added.' );
+				add = overrideExisting === true;
+				if ( ! add ) {
+
+					existingMaterial = this.materials[ materialName ];
+					add = ( existingMaterial === null || existingMaterial === undefined );
+
+				}
+				if ( add ) {
+
+					this.materials[ materialName ] = material;
+					newMaterials[ materialName ] = material;
+
+				}
+				if ( this.logging.enabled && this.logging.debug ) {
+
+					console.info( 'Material with name "' + materialName + '" was added.' );
+
+				}
 
 
 			}
 			}
 
 
@@ -227,6 +257,13 @@ MaterialHandler.prototype = {
 
 
 		return materialsJSON;
 		return materialsJSON;
 
 
+	},
+
+	/**
+	 * Removes all materials
+	 */
+	clearMaterials: function () {
+		this.materials = {};
 	}
 	}
 
 
 };
 };

+ 1 - 1
examples/jsm/loaders/obj2/shared/MeshReceiver.js

@@ -20,7 +20,7 @@ import {
 const MeshReceiver = function ( materialHandler ) {
 const MeshReceiver = function ( materialHandler ) {
 
 
 	this.logging = {
 	this.logging = {
-		enabled: true,
+		enabled: false,
 		debug: false
 		debug: false
 	};
 	};
 
 

+ 11 - 3
examples/jsm/loaders/obj2/worker/main/WorkerExecutionSupport.js

@@ -138,7 +138,7 @@ WorkerExecutionSupport.prototype = {
 	_reset: function () {
 	_reset: function () {
 
 
 		this.logging = {
 		this.logging = {
-			enabled: true,
+			enabled: false,
 			debug: false
 			debug: false
 		};
 		};
 
 
@@ -437,7 +437,11 @@ WorkerExecutionSupport.prototype = {
 				}
 				}
 				if ( this.worker.terminateWorkerOnLoad ) {
 				if ( this.worker.terminateWorkerOnLoad ) {
 
 
-					if ( this.worker.logging.enabled ) console.info( 'WorkerSupport [' + workerRunnerName + ']: Run is complete. Terminating application on request!' );
+					if ( this.worker.logging.enabled ) {
+
+						console.info( 'WorkerSupport [' + workerRunnerName + ']: Run is complete. Terminating application on request!' );
+
+					}
 					this.worker.callbacks.terminate();
 					this.worker.callbacks.terminate();
 
 
 				}
 				}
@@ -454,7 +458,11 @@ WorkerExecutionSupport.prototype = {
 				}
 				}
 				if ( this.worker.terminateWorkerOnLoad ) {
 				if ( this.worker.terminateWorkerOnLoad ) {
 
 
-					if ( this.worker.logging.enabled ) console.info( 'WorkerSupport [' + workerRunnerName + ']: Run reported error. Terminating application on request!' );
+					if ( this.worker.logging.enabled ) {
+
+						console.info( 'WorkerSupport [' + workerRunnerName + ']: Run reported error. Terminating application on request!' );
+
+					}
 					this.worker.callbacks.terminate();
 					this.worker.callbacks.terminate();
 
 
 				}
 				}

+ 1 - 0
examples/jsm/loaders/obj2/worker/parallel/OBJLoader2Parser.d.ts

@@ -66,6 +66,7 @@ export class OBJLoader2Parser {
   setCallbackOnError(onError: Function): this;
   setCallbackOnError(onError: Function): this;
   setCallbackOnLoad(onLoad: Function): this;
   setCallbackOnLoad(onLoad: Function): this;
   setLogging(enabled: boolean, debug: boolean): this;
   setLogging(enabled: boolean, debug: boolean): this;
+  setMaterials(materials: Object): void;
   execute(arrayBuffer: Uint8Array): void;
   execute(arrayBuffer: Uint8Array): void;
   executeLegacy(text: string): void;
   executeLegacy(text: string): void;
 }
 }

+ 12 - 6
examples/jsm/loaders/obj2/worker/parallel/OBJLoader2Parser.js

@@ -8,6 +8,11 @@
  */
  */
 const OBJLoader2Parser = function () {
 const OBJLoader2Parser = function () {
 
 
+	this.logging = {
+		enabled: false,
+		debug: false
+	};
+
 	let scope = this;
 	let scope = this;
 	this.callbacks = {
 	this.callbacks = {
 		onProgress: function ( text ) {
 		onProgress: function ( text ) {
@@ -71,11 +76,6 @@ const OBJLoader2Parser = function () {
 		totalBytes: 0
 		totalBytes: 0
 	};
 	};
 
 
-	this.logging = {
-		enabled: true,
-		debug: false
-	};
-
 };
 };
 
 
 OBJLoader2Parser.prototype = {
 OBJLoader2Parser.prototype = {
@@ -152,8 +152,14 @@ OBJLoader2Parser.prototype = {
 
 
 	},
 	},
 
 
-	_setMaterials: function ( materials ) {
+	/**
+	 * Clears materials object and sets the new ones.
+	 *
+	 * @param {Object} materials Object with named materials
+	 */
+	setMaterials: function ( materials ) {
 
 
+ 		this.materials = {};
 		if ( materials === undefined || materials === null ) return;
 		if ( materials === undefined || materials === null ) return;
 
 
 		for ( let materialName in materials ) {
 		for ( let materialName in materials ) {