1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/**
* @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: false,
debug: false
};
this.callbacks = {
onLoadMaterials: null
};
this.materials = {};
};
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;
}
},
/**
* 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 } );
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, overrideExisting );
},
/**
* 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;
Object.assign( material, materialCloneInstructions.materialProperties );
this.materials[ materialName ] = material;
newMaterials[ materialName ] = material;
} else {
if ( this.logging.enabled ) {
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, true, newMaterials );
return newMaterials;
},
/**
* Set materials loaded by any supplier of an Array of {@link Material}.
*
* @param materials Object with named {@link Material}
* @param overrideExisting boolean Override existing material
* @param newMaterials [Object] with named {@link Material}
*/
addMaterials: function ( materials, overrideExisting, newMaterials ) {
if ( newMaterials === undefined || newMaterials === null ) {
newMaterials = {};
}
if ( materials !== undefined && materials !== null && Object.keys( materials ).length > 0 ) {
let material;
let existingMaterial;
let add;
for ( let materialName in materials ) {
material = materials[ materialName ];
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.' );
}
}
}
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;
},
/**
* Removes all materials
*/
clearMaterials: function () {
this.materials = {};
}
};
export { MaterialHandler };
|