/** * Material Superset - The apiMaterial properties get moved into the Material object itself, and then the instance is * created * @param graphics GameLib.D3.Graphics * @param apiMaterial GameLib.D3.API.Material * @param imageFactory GameLib.D3.ImageFactory * @constructor * @returns {GameLib.D3.Material | GameLib.D3.API.Material} */ GameLib.D3.Material = function Material( graphics, apiMaterial, imageFactory ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); GameLib.D3.API.Material.call( this, apiMaterial.id, apiMaterial.materialType, apiMaterial.name, apiMaterial.opacity, apiMaterial.side, apiMaterial.transparent, apiMaterial.specular, apiMaterial.lightMapIntensity, apiMaterial.aoMapIntensity, apiMaterial.color, apiMaterial.emissive, apiMaterial.emissiveIntensity, apiMaterial.combine, apiMaterial.shininess, apiMaterial.reflectivity, apiMaterial.refractionRatio, apiMaterial.fog, apiMaterial.wireframe, apiMaterial.wireframeLineWidth, apiMaterial.wireframeLineCap, apiMaterial.wireframeLineJoin, apiMaterial.vertexColors, apiMaterial.skinning, apiMaterial.morphTargets, apiMaterial.morphNormals, apiMaterial.lineWidth, apiMaterial.lineCap, apiMaterial.lineJoin, apiMaterial.dashSize, apiMaterial.gapWidth, apiMaterial.blending, apiMaterial.blendSrc, apiMaterial.blendDst, apiMaterial.blendEquation, apiMaterial.depthTest, apiMaterial.depthFunc, apiMaterial.depthWrite, apiMaterial.polygonOffset, apiMaterial.polygonOffsetFactor, apiMaterial.polygonOffsetUnits, apiMaterial.alphaTest, apiMaterial.clippingPlanes, apiMaterial.clipShadows, apiMaterial.visible, apiMaterial.overdraw, apiMaterial.shading, apiMaterial.bumpScale, apiMaterial.normalScale, apiMaterial.displacementScale, apiMaterial.displacementBias, apiMaterial.roughness, apiMaterial.metalness, apiMaterial.pointSize, apiMaterial.pointSizeAttenuation, apiMaterial.spriteRotation, apiMaterial.envMapIntensity, apiMaterial.alphaMap, apiMaterial.aoMap, apiMaterial.bumpMap, apiMaterial.diffuseMap, apiMaterial.displacementMap, apiMaterial.emissiveMap, apiMaterial.environmentMap, apiMaterial.lightMap, apiMaterial.metalnessMap, apiMaterial.normalMap, apiMaterial.roughnessMap, apiMaterial.specularMap, apiMaterial.parentEntity ); this.specular = new GameLib.Color( graphics, this, this.specular ); this.color = new GameLib.Color( graphics, this, this.color ); this.emissive = new GameLib.Color( graphics, this, this.emissive ); if (this.alphaMap) { this.alphaMap = new GameLib.D3.Texture( this.graphics, this.alphaMap, this, 'alphaMap', imageFactory ); } if (this.aoMap) { this.aoMap = new GameLib.D3.Texture( this.graphics, this.aoMap, this, 'aoMap', imageFactory ); } if (this.bumpMap) { this.bumpMap = new GameLib.D3.Texture( this.graphics, this.bumpMap, this, 'bumpMap', imageFactory ); } if (this.diffuseMap) { this.diffuseMap = new GameLib.D3.Texture( this.graphics, this.diffuseMap, this, 'map', imageFactory ); } if (this.displacementMap) { this.displacementMap = new GameLib.D3.Texture( this.graphics, this.displacementMap, this, 'displacementMap', imageFactory ); } if (this.emissiveMap) { this.emissiveMap = new GameLib.D3.Texture( this.graphics, this.emissiveMap, this, 'emissiveMap', imageFactory ); } if (this.environmentMap) { this.environmentMap = new GameLib.D3.Texture( this.graphics, this.environmentMap, this, 'envMap', imageFactory ); } if (this.lightMap) { this.lightMap = new GameLib.D3.Texture( this.graphics, this.lightMap, this, 'lightMap', imageFactory ); } if (this.metalnessMap) { this.metalnessMap = new GameLib.D3.Texture( this.graphics, this.metalnessMap, this, 'metalnessMap', imageFactory ); } if (this.normalMap) { this.normalMap = new GameLib.D3.Texture( this.graphics, this.normalMap, this, 'normalMap', imageFactory ); } if (this.roughnessMap) { this.roughnessMap = new GameLib.D3.Texture( this.graphics, this.roughnessMap, this, 'roughnessMap', imageFactory ); } if (this.specularMap) { this.specularMap = new GameLib.D3.Texture( this.graphics, this.specularMap, this, 'specularMap', imageFactory ); } this.instance = this.createInstance(); }; GameLib.D3.Material.prototype = Object.create(GameLib.D3.API.Material.prototype); GameLib.D3.Material.prototype.constructor = GameLib.D3.Material; /** * Combine Method * @type {number} */ GameLib.D3.Material.TYPE_MULTIPLY_OPERATION = 0; GameLib.D3.Material.TYPE_MIX_OPERATION = 1; GameLib.D3.Material.TYPE_ADD_OPERATION = 2; /** * Vertex Color Mode * @type {number} */ GameLib.D3.Material.TYPE_NO_COLORS = 0; GameLib.D3.Material.TYPE_FACE_COLORS = 1; GameLib.D3.Material.TYPE_VERTEX_COLORS = 2; /** * Blending Mode * @type {number} */ GameLib.D3.Material.TYPE_NORMAL_BLENDING = 1; GameLib.D3.Material.TYPE_ADDITIVE_BLENDING = 2; GameLib.D3.Material.TYPE_SUBTRACTIVE_BLENDING = 3; GameLib.D3.Material.TYPE_MULTIPLY_BLENDING = 4; GameLib.D3.Material.TYPE_CUSTOM_BLENDING = 5; /** * Blend Source and Destination * @type {number} */ GameLib.D3.Material.TYPE_ZERO_FACTOR = 200; GameLib.D3.Material.TYPE_ONE_FACTOR = 201; GameLib.D3.Material.TYPE_SRC_COLOR_FACTOR = 202; GameLib.D3.Material.TYPE_ONE_MINUS_SRC_COLOR_FACTOR = 203; GameLib.D3.Material.TYPE_SRC_ALPHA_FACTOR = 204; GameLib.D3.Material.TYPE_ONE_MINUS_SRC_ALPHA_FACTOR = 205; GameLib.D3.Material.TYPE_DST_ALPHA_FACTOR = 206; GameLib.D3.Material.TYPE_ONE_MINUS_DST_ALPHA_FACTOR = 207; GameLib.D3.Material.TYPE_DST_COLOR_FACTOR = 208; GameLib.D3.Material.TYPE_ONE_MINUS_DST_COLOR_FACTOR = 209; GameLib.D3.Material.TYPE_SRC_ALPHA_SATURATE_FACTOR = 210; /** * Blend Operation * @type {number} */ GameLib.D3.Material.TYPE_ADD_EQUATION = 100; GameLib.D3.Material.TYPE_SUBTRACT_EQUATION = 101; GameLib.D3.Material.TYPE_REVERSE_SUBTRACT_EQUATION = 102; GameLib.D3.Material.TYPE_MIN_EQUATION = 103; GameLib.D3.Material.TYPE_MAX_EQUATION = 104; /** * Depth Function * @type {number} */ GameLib.D3.Material.TYPE_NEVER_DEPTH = 0; GameLib.D3.Material.TYPE_ALWAYS_DEPTH = 1; GameLib.D3.Material.TYPE_LESS_DEPTH = 2; GameLib.D3.Material.TYPE_LESS_EQUAL_DEPTH = 3; GameLib.D3.Material.TYPE_EQUAL_DEPTH = 4; GameLib.D3.Material.TYPE_GREATER_EQUAL_DEPTH = 5; GameLib.D3.Material.TYPE_GREATER_DEPTH = 6; GameLib.D3.Material.TYPE_NOT_EQUAL_DEPTH = 7; /** * Culling Mode * @type {number} */ GameLib.D3.Material.TYPE_FRONT_SIDE = 0; GameLib.D3.Material.TYPE_BACK_SIDE = 1; GameLib.D3.Material.TYPE_DOUBLE_SIDE = 2; /** * Shading Type * @type {number} */ GameLib.D3.Material.TYPE_FLAT_SHADING = 1; GameLib.D3.Material.TYPE_SMOOTH_SHADING = 2; /** * Material Type * @type {string} */ GameLib.D3.Material.MATERIAL_TYPE_LINE_BASIC = "LineBasicMaterial"; GameLib.D3.Material.MATERIAL_TYPE_LINE_DASHED = "LineDashedMaterial"; GameLib.D3.Material.MATERIAL_TYPE_BASIC = "MeshBasicMaterial"; GameLib.D3.Material.MATERIAL_TYPE_DEPTH = "MeshDepthMaterial"; GameLib.D3.Material.MATERIAL_TYPE_LAMBERT = "MeshLambertMaterial"; GameLib.D3.Material.MATERIAL_TYPE_NORMAL = "MeshNormalMaterial"; GameLib.D3.Material.MATERIAL_TYPE_PHONG = "MeshPhongMaterial"; GameLib.D3.Material.MATERIAL_TYPE_STANDARD = "MeshStandardMaterial"; GameLib.D3.Material.MATERIAL_TYPE_POINTS = "PointsMaterial"; GameLib.D3.Material.MATERIAL_TYPE_SPRITE = "SpriteMaterial"; GameLib.D3.Material.MATERIAL_TYPE_MULTIPLE = "MultiMaterial"; /** * Material instance * @returns {*} */ GameLib.D3.Material.prototype.createInstance = function(update) { var instance = null; if (update) { instance = this.instance; } if (!instance) { if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_STANDARD) { instance = new THREE.MeshStandardMaterial({ name: this.name, opacity: this.opacity, transparent: this.transparent, blending: this.blending, blendSrc: this.blendSrc, blendDst: this.blendDst, blendEquation: this.blendEquation, depthTest: this.depthTest, depthFunc: this.depthFunc, depthWrite: this.depthWrite, polygonOffset: this.polygonOffset, polygonOffsetFactor: this.polygonOffsetFactor, polygonOffsetUnits: this.polygonOffsetUnits, alphaTest: this.alphaTest, clippingPlanes: this.clippingPlanes, clipShadows: this.clipShadows, overdraw: this.overdraw, visible: this.visible, side: this.side, color: this.color.instance, roughness: this.roughness, metalness: this.metalness, lightMapIntensity: this.lightMapIntensity, aoMapIntensity: this.aoMapIntensity, emissive: this.emissive.instance, emissiveIntensity: this.emissiveIntensity, bumpScale: this.bumpScale, normalScale: this.normalScale, displacementScale: this.displacementScale, refractionRatio: this.refractionRatio, fog: this.fog, shading: this.shading, wireframe: this.wireframe, wireframeLinewidth: this.wireframeLineWidth, wireframeLinecap: this.wireframeLineCap, wireframeLinejoin: this.wireframeLineJoin, vertexColors: this.vertexColors, skinning: this.skinning, morphTargets: this.morphTargets, morphNormals: this.morphNormals }); } else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_PHONG) { instance = new THREE.MeshPhongMaterial({ name: this.name, opacity: this.opacity, transparent: this.transparent, blending: this.blending, blendSrc: this.blendSrc, blendDst: this.blendDst, blendEquation: this.blendEquation, depthTest: this.depthTest, depthFunc: this.depthFunc, depthWrite: this.depthWrite, polygonOffset: this.polygonOffset, polygonOffsetFactor: this.polygonOffsetFactor, polygonOffsetUnits: this.polygonOffsetUnits, alphaTest: this.alphaTest, clippingPlanes: this.clippingPlanes, clipShadows: this.clipShadows, overdraw: this.overdraw, visible: this.visible, side: this.side, color: this.color.instance, specular: this.specular.instance, shininess: this.shininess, lightMapIntensity: this.lightMapIntensity, aoMapIntensity: this.aoMapIntensity, emissive: this.emissive.instance, emissiveIntensity: this.emissiveIntensity, bumpScale: this.bumpScale, normalScale: this.normalScale, displacementScale: this.displacementScale, combine: this.combine, refractionRatio: this.refractionRatio, fog: this.fog, shading: this.shading, wireframe: this.wireframe, wireframeLinewidth: this.wireframeLineWidth, wireframeLinecap: this.wireframeLineCap, wireframeLinejoin: this.wireframeLineJoin, vertexColors: this.vertexColors, skinning: this.skinning, morphTargets: this.morphTargets, morphNormals: this.morphNormals }); } else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_POINTS) { instance = new this.graphics.instance.PointsMaterial({ name: this.name, opacity: this.opacity, transparent: this.transparent, blending: this.blending, blendSrc: this.blendSrc, blendDst: this.blendDst, blendEquation: this.blendEquation, depthTest: this.depthTest, depthFunc: this.depthFunc, depthWrite: this.depthWrite, polygonOffset: this.polygonOffset, polygonOffsetFactor: this.polygonOffsetFactor, polygonOffsetUnits: this.polygonOffsetUnits, alphaTest: this.alphaTest, clippingPlanes: this.clippingPlanes, clipShadows: this.clipShadows, overdraw: this.overdraw, visible: this.visible, side: this.side, color: this.color.instance, size: this.pointSize, sizeAttenuation: this.pointSizeAttenuation, vertexColors: GameLib.D3.Material.TYPE_NO_COLORS, fog: this.fog }); } else if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_BASIC) { instance = new THREE.MeshBasicMaterial({ name: this.name, opacity: this.opacity, transparent: this.transparent, blending: this.blending, blendSrc: this.blendSrc, blendDst: this.blendDst, blendEquation: this.blendEquation, depthTest: this.depthTest, depthFunc: this.depthFunc, depthWrite: this.depthWrite, polygonOffset: this.polygonOffset, polygonOffsetFactor: this.polygonOffsetFactor, polygonOffsetUnits: this.polygonOffsetUnits, alphaTest: this.alphaTest, clippingPlanes: this.clippingPlanes, clipShadows: this.clipShadows, overdraw: this.overdraw, visible: this.visible, side: this.side, color: this.color.instance, vertexColors: GameLib.D3.Material.TYPE_NO_COLORS, fog: this.fog }); } else { console.log("material type is not implemented yet: " + this.materialType + " - material indexes could be screwed up"); } } if (update) { for (var property in instance) { if (instance.hasOwnProperty(property)) { if (property == 'alphaMap') { if (this.alphaMap) { instance.alphaMap = this.alphaMap.instance; } } else if (property == 'aoMap') { if (this.aoMap) { instance.aoMap = this.aoMap.instance; } } else if (property == 'bumpMap') { if (this.bumpMap) { instance.bumpMap = this.bumpMap.instance; } } else if (property == 'map') { if (this.diffuseMap) { instance.map = this.diffuseMap.instance; } } else if (property == 'displacementMap') { if (this.displacementMap) { instance.displacementMap = this.displacementMap.instance; } } else if (property == 'emissiveMap') { if (this.emissiveMap) { instance.emissiveMap = this.emissiveMap.instance; } } else if (property == 'envMap') { if (this.environmentMap) { instance.envMap = this.environmentMap.instance; } } else if (property == 'lightMap') { if (this.lightMap) { instance.lightMap = this.lightMap.instance; } } else if (property == 'metalnessMap') { if (this.metalnessMap) { instance.metalnessMap = this.metalnessMap.instance; } } else if (property == 'normalMap') { if (this.normalMap) { instance.normalMap = this.normalMap.instance; } } else if (property == 'roughnessMap') { if (this.roughnessMap) { instance.roughnessMap = this.roughnessMap.instance; } } else if (property == 'specularMap') { if (this.specularMap) { instance.specularMap = this.specularMap.instance; } } else if (property == 'size') { instance.size = this.pointSize; } else if (property == 'sizeAttenuation') { instance.sizeAttenuation = this.pointSizeAttenuation; } else if (instance[property] instanceof THREE.Color) { instance[property].copy(this[property]) } else if (this.hasOwnProperty(property)) { instance[property] = this[property]; } } } instance.needsUpdate = true; } return instance; }; /** * Updates the instance with the current state */ GameLib.D3.Material.prototype.updateInstance = function() { this.instance = this.createInstance(true); }; GameLib.D3.Material.prototype.clone = function() { return _.cloneDeep(this); }; /** * Converts a GameLib.D3.Material to a GameLib.D3.API.Material * @returns {GameLib.D3.API.Material} */ GameLib.D3.Material.prototype.toApiMaterial = function() { var apiAlphaMap = null; if (this.alphaMap) { apiAlphaMap = this.alphaMap.toApiTexture(); } var apiAoMap = null; if (this.aoMap) { apiAoMap = this.aoMap.toApiTexture(); } var apiBumpMap = null; if (this.bumpMap) { apiBumpMap = this.bumpMap.toApiTexture(); } var apiDiffuseMap = null; if (this.diffuseMap) { apiDiffuseMap = this.diffuseMap.toApiTexture(); } var apiDisplacementMap = null; if (this.displacementMap) { apiDisplacementMap = this.displacementMap.toApiTexture(); } var apiEmissiveMap = null; if (this.emissiveMap) { apiEmissiveMap = this.emissiveMap.toApiTexture(); } var apiEnvironmentMap = null; if (this.environmentMap) { apiEnvironmentMap = this.environmentMap.toApiTexture(); } var apiLightMap = null; if (this.lightMap) { apiLightMap = this.lightMap.toApiTexture(); } var apiMetalnessMap = null; if (this.metalnessMap) { apiMetalnessMap = this.metalnessMap.toApiTexture(); } var apiNormalMap = null; if (this.normalMap) { apiNormalMap = this.normalMap.toApiTexture(); } var apiRoughnessMap = null; if (this.roughnessMap) { apiRoughnessMap = this.roughnessMap.toApiTexture(); } var apiSpecularMap = null; if (this.specularMap) { apiSpecularMap = this.specularMap.toApiTexture(); } return new GameLib.D3.API.Material( this.id, this.materialType, this.name, this.opacity, this.side, this.transparent, this.specular.toApiColor(), this.lightMapIntensity, this.aoMapIntensity, this.color.toApiColor(), this.emissive.toApiColor(), this.emissiveIntensity, this.combine, this.shininess, this.reflectivity, this.refractionRatio, this.fog, this.wireframe, this.wireframeLineWidth, this.wireframeLineCap, this.wireframeLineJoin, this.vertexColors, this.skinning, this.morphTargets, this.morphNormals, this.lineWidth, this.lineCap, this.lineJoin, this.dashSize, this.gapWidth, this.blending, this.blendSrc, this.blendDst, this.blendEquation, this.depthTest, this.depthFunc, this.depthWrite, this.polygonOffset, this.polygonOffsetFactor, this.polygonOffsetUnits, this.alphaTest, this.clippingPlanes, this.clipShadows, this.visible, this.overdraw, this.shading, this.bumpScale, this.normalScale, this.displacementScale, this.displacementBias, this.roughness, this.metalness, this.pointSize, this.pointSizeAttenuation, this.spriteRotation, this.envMapIntensity, apiAlphaMap, apiAoMap, apiBumpMap, apiDiffuseMap, apiDisplacementMap, apiEmissiveMap, apiEnvironmentMap, apiLightMap, apiMetalnessMap, apiNormalMap, apiRoughnessMap, apiSpecularMap, GameLib.Utils.IdOrNull(this.parentEntity) ); }; /** * Creates a GameLib.D3.Material from a material Object * @param graphics GameLib.D3.Graphics * @param objectMaterial Object * @param imageFactory GameLib.D3.ImageFactory * @constructor */ GameLib.D3.Material.FromObjectMaterial = function(graphics, objectMaterial, imageFactory) { var gameLibMaterial = new GameLib.D3.Material( graphics, GameLib.D3.API.Material.FromObjectMaterial(objectMaterial), imageFactory ); return gameLibMaterial; };