123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- /**
- * @author Kai Salmen / https://kaisalmen.de
- * Development repository: https://github.com/kaisalmen/WWOBJLoader
- */
- import {
- LineBasicMaterial,
- MaterialLoader,
- MeshStandardMaterial,
- PointsMaterial,
- VertexColors
- } from "../../../../../build/three.module.js";
- const MaterialHandler = function () {
- this.logging = {
- enabled: true,
- debug: false
- };
- this.callbacks = {
- onLoadMaterials: null
- };
- this.materials = {};
- this._createDefaultMaterials();
- };
- MaterialHandler.prototype = {
- constructor: MaterialHandler,
- /**
- * Enable or disable logging in general (except warn and error), plus enable or disable debug logging.
- *
- * @param {boolean} enabled True or false.
- * @param {boolean} debug True or false.
- */
- setLogging: function ( enabled, debug ) {
- this.logging.enabled = enabled === true;
- this.logging.debug = debug === true;
- },
- _setCallbacks: function ( onLoadMaterials ) {
- if ( onLoadMaterials !== undefined && onLoadMaterials !== null && onLoadMaterials instanceof Function ) {
- this.callbacks.onLoadMaterials = onLoadMaterials;
- }
- },
- _createDefaultMaterials: function () {
- let defaultMaterial = new MeshStandardMaterial( { color: 0xDCF1FF } );
- defaultMaterial.name = 'defaultMaterial';
- let defaultVertexColorMaterial = new MeshStandardMaterial( { color: 0xDCF1FF } );
- defaultVertexColorMaterial.name = 'defaultVertexColorMaterial';
- defaultVertexColorMaterial.vertexColors = VertexColors;
- let defaultLineMaterial = new LineBasicMaterial();
- defaultLineMaterial.name = 'defaultLineMaterial';
- let defaultPointMaterial = new PointsMaterial( { size: 0.1 } );
- defaultPointMaterial.name = 'defaultPointMaterial';
- let runtimeMaterials = {};
- runtimeMaterials[ defaultMaterial.name ] = defaultMaterial;
- runtimeMaterials[ defaultVertexColorMaterial.name ] = defaultVertexColorMaterial;
- runtimeMaterials[ defaultLineMaterial.name ] = defaultLineMaterial;
- runtimeMaterials[ defaultPointMaterial.name ] = defaultPointMaterial;
- this.addMaterials( runtimeMaterials );
- },
- /**
- * Updates the materials with contained material objects (sync) or from alteration instructions (async).
- *
- * @param {Object} materialPayload Material update instructions
- * @returns {Object} Map of {@link Material}
- */
- addPayloadMaterials: function ( materialPayload ) {
- let material, materialName;
- let materialCloneInstructions = materialPayload.materials.materialCloneInstructions;
- let newMaterials = {};
- if ( materialCloneInstructions !== undefined && materialCloneInstructions !== null ) {
- let materialNameOrg = materialCloneInstructions.materialNameOrg;
- materialNameOrg = ( materialNameOrg !== undefined && materialNameOrg !== null ) ? materialNameOrg : "";
- let materialOrg = this.materials[ materialNameOrg ];
- if ( materialOrg ) {
- material = materialOrg.clone();
- materialName = materialCloneInstructions.materialName;
- material.name = materialName;
- let materialProperties = materialCloneInstructions.materialProperties;
- for ( let key in materialProperties ) {
- if ( material.hasOwnProperty( key ) && materialProperties.hasOwnProperty( key ) ) {
- material[ key ] = materialProperties[ key ];
- }
- }
- this.materials[ materialName ] = material;
- newMaterials[ materialName ] = material;
- } else {
- console.info( 'Requested material "' + materialNameOrg + '" is not available!' );
- }
- }
- let materials = materialPayload.materials.serializedMaterials;
- if ( materials !== undefined && materials !== null && Object.keys( materials ).length > 0 ) {
- let loader = new MaterialLoader();
- let materialJson;
- for ( materialName in materials ) {
- materialJson = materials[ materialName ];
- if ( materialJson !== undefined && materialJson !== null ) {
- material = loader.parse( materialJson );
- if ( this.logging.enabled ) console.info( 'De-serialized material with name "' + materialName + '" will be added.' );
- this.materials[ materialName ] = material;
- newMaterials[ materialName ] = material;
- }
- }
- }
- materials = materialPayload.materials.runtimeMaterials;
- newMaterials = this.addMaterials( materials, newMaterials );
- return newMaterials;
- },
- /**
- * Set materials loaded by any supplier of an Array of {@link Material}.
- *
- * @param materials Object with named {@link Material}
- * @param newMaterials [Object] with named {@link Material}
- */
- addMaterials: function ( materials, newMaterials ) {
- if ( newMaterials === undefined || newMaterials === null ) {
- newMaterials = {};
- }
- if ( materials !== undefined && materials !== null && Object.keys( materials ).length > 0 ) {
- let material;
- for ( let materialName in materials ) {
- material = materials[ materialName ];
- this.materials[ materialName ] = material;
- newMaterials[ materialName ] = material;
- if ( this.logging.enabled ) console.info( 'Material with name "' + materialName + '" was added.' );
- }
- }
- if ( this.callbacks.onLoadMaterials ) {
- this.callbacks.onLoadMaterials( newMaterials );
- }
- return newMaterials;
- },
- /**
- * Returns the mapping object of material name and corresponding material.
- *
- * @returns {Object} Map of {@link Material}
- */
- getMaterials: function () {
- return this.materials;
- },
- /**
- *
- * @param {String} materialName
- * @returns {Material}
- */
- getMaterial: function ( materialName ) {
- return this.materials[ materialName ];
- },
- /**
- * Returns the mapping object of material name and corresponding jsonified material.
- *
- * @returns {Object} Map of Materials in JSON representation
- */
- getMaterialsJSON: function () {
- let materialsJSON = {};
- let material;
- for ( let materialName in this.materials ) {
- material = this.materials[ materialName ];
- materialsJSON[ materialName ] = material.toJSON();
- }
- return materialsJSON;
- }
- };
- export { MaterialHandler };
|