r3-legacy/src/game-lib-d3-api-material.js

637 lines
16 KiB
JavaScript

/**
* Raw material API object - should always correspond with the Material Schema
* @param id
* @param materialType
* @param name
* @param opacity
* @param side
* @param transparent
* @param specular
* @param lightMapIntensity
* @param aoMapIntensity
* @param color
* @param emissive
* @param emissiveIntensity
* @param combine
* @param shininess
* @param reflectivity
* @param refractionRatio
* @param fog
* @param wireframe
* @param wireframeLineWidth
* @param wireframeLineCap
* @param wireframeLineJoin
* @param vertexColors
* @param skinning
* @param morphTargets
* @param morphNormals
* @param lineWidth
* @param lineCap
* @param lineJoin
* @param dashSize
* @param gapWidth
* @param blending
* @param blendSrc
* @param blendDst
* @param blendEquation
* @param depthTest
* @param depthFunc
* @param depthWrite
* @param polygonOffset
* @param polygonOffsetFactor
* @param polygonOffsetUnits
* @param alphaTest
* @param clippingPlanes
* @param clipShadows
* @param visible
* @param overdraw
* @param shading
* @param bumpScale
* @param normalScale
* @param displacementScale
* @param displacementBias
* @param roughness
* @param metalness
* @param pointSize
* @param pointSizeAttenuation
* @param spriteRotation
* @param envMapIntensity
* @param alphaMap
* @param aoMap
* @param bumpMap
* @param diffuseMap
* @param displacementMap
* @param emissiveMap
* @param environmentMap
* @param lightMap
* @param metalnessMap
* @param normalMap
* @param roughnessMap
* @param specularMap
* @param parentEntity
* @constructor
*/
GameLib.D3.API.Material = function(
id,
materialType,
name,
opacity,
side,
transparent,
specular,
lightMapIntensity,
aoMapIntensity,
color,
emissive,
emissiveIntensity,
combine,
shininess,
reflectivity,
refractionRatio,
fog,
wireframe,
wireframeLineWidth,
wireframeLineCap,
wireframeLineJoin,
vertexColors,
skinning,
morphTargets,
morphNormals,
lineWidth,
lineCap,
lineJoin,
dashSize,
gapWidth,
blending,
blendSrc,
blendDst,
blendEquation,
depthTest,
depthFunc,
depthWrite,
polygonOffset,
polygonOffsetFactor,
polygonOffsetUnits,
alphaTest,
clippingPlanes,
clipShadows,
visible,
overdraw,
shading,
bumpScale,
normalScale,
displacementScale,
displacementBias,
roughness,
metalness,
pointSize,
pointSizeAttenuation,
spriteRotation,
envMapIntensity,
alphaMap,
aoMap,
bumpMap,
diffuseMap,
displacementMap,
emissiveMap,
environmentMap,
lightMap,
metalnessMap,
normalMap,
roughnessMap,
specularMap,
parentEntity
) {
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
this.id = id;
if (GameLib.Utils.UndefinedOrNull(materialType)) {
materialType = GameLib.D3.Material.MATERIAL_TYPE_STANDARD;
}
this.materialType = materialType;
if (GameLib.Utils.UndefinedOrNull(name)) {
name = 'Material (' + materialType + ')';
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(opacity)) {
opacity = 1.0;
}
this.opacity = opacity;
if (GameLib.Utils.UndefinedOrNull(side)) {
side = GameLib.D3.Material.TYPE_FRONT_SIDE;
}
this.side = side;
if (GameLib.Utils.UndefinedOrNull(transparent)) {
transparent = false;
}
this.transparent = transparent;
if (GameLib.Utils.UndefinedOrNull(specular)) {
specular = new GameLib.API.Color(0.06, 0.06, 0.06, 0.06);
}
this.specular = specular;
if (GameLib.Utils.UndefinedOrNull(lightMapIntensity)) {
lightMapIntensity = 1;
}
this.lightMapIntensity = lightMapIntensity;
if (GameLib.Utils.UndefinedOrNull(aoMapIntensity)) {
aoMapIntensity = 1;
}
this.aoMapIntensity = aoMapIntensity;
if (GameLib.Utils.UndefinedOrNull(color)) {
color = new GameLib.API.Color(1, 1, 1, 1)
}
this.color = color;
if (GameLib.Utils.UndefinedOrNull(emissive)) {
emissive = new GameLib.API.Color(0, 0, 0, 0);
}
this.emissive = emissive;
if (GameLib.Utils.UndefinedOrNull(emissiveIntensity)) {
emissiveIntensity = 1;
}
this.emissiveIntensity = emissiveIntensity;
if (GameLib.Utils.UndefinedOrNull(combine)) {
combine = GameLib.D3.Material.TYPE_MULTIPLY_OPERATION;
}
this.combine = combine;
if (GameLib.Utils.UndefinedOrNull(shininess)) {
shininess = 30;
}
this.shininess = shininess;
if (GameLib.Utils.UndefinedOrNull(reflectivity)) {
reflectivity = 1;
}
this.reflectivity = reflectivity;
if (GameLib.Utils.UndefinedOrNull(refractionRatio)) {
refractionRatio = 0.98;
}
this.refractionRatio = refractionRatio;
if (GameLib.Utils.UndefinedOrNull(fog)) {
fog = true;
}
this.fog = fog;
if (GameLib.Utils.UndefinedOrNull(wireframe)) {
wireframe = false;
}
this.wireframe = wireframe;
if (GameLib.Utils.UndefinedOrNull(wireframeLineWidth)) {
wireframeLineWidth = 1;
}
this.wireframeLineWidth = wireframeLineWidth;
if (GameLib.Utils.UndefinedOrNull(wireframeLineCap)) {
wireframeLineCap = 'round';
}
this.wireframeLineCap = wireframeLineCap;
if (GameLib.Utils.UndefinedOrNull(wireframeLineJoin)) {
wireframeLineJoin = 'round';
}
this.wireframeLineJoin = wireframeLineJoin;
if (GameLib.Utils.UndefinedOrNull(vertexColors)) {
vertexColors = GameLib.D3.Material.TYPE_NO_COLORS;
}
this.vertexColors = vertexColors;
if (GameLib.Utils.UndefinedOrNull(skinning)) {
skinning = false;
}
this.skinning = skinning;
if (GameLib.Utils.UndefinedOrNull(morphTargets)) {
morphTargets = false;
}
this.morphTargets = morphTargets;
if (GameLib.Utils.UndefinedOrNull(morphNormals)) {
morphNormals = false;
}
this.morphNormals = morphNormals;
if (GameLib.Utils.UndefinedOrNull(overdraw)) {
overdraw = 0;
}
this.overdraw = overdraw;
if (GameLib.Utils.UndefinedOrNull(lineWidth)) {
lineWidth = 1;
}
this.lineWidth = lineWidth;
if (GameLib.Utils.UndefinedOrNull(lineCap)) {
lineCap = 'round';
}
this.lineCap = lineCap;
if (GameLib.Utils.UndefinedOrNull(lineJoin)) {
lineJoin = 'round';
}
this.lineJoin = lineJoin;
if (GameLib.Utils.UndefinedOrNull(dashSize)) {
dashSize = 3;
}
this.dashSize = dashSize;
if (GameLib.Utils.UndefinedOrNull(gapWidth)) {
gapWidth = 1;
}
this.gapWidth = gapWidth;
if (GameLib.Utils.UndefinedOrNull(blending)) {
blending = GameLib.D3.Material.TYPE_NORMAL_BLENDING;
}
this.blending = blending;
if (GameLib.Utils.UndefinedOrNull(blendSrc)) {
blendSrc = GameLib.D3.Material.TYPE_SRC_ALPHA_FACTOR;
}
this.blendSrc = blendSrc;
if (GameLib.Utils.UndefinedOrNull(blendDst)) {
blendDst = GameLib.D3.Material.TYPE_ONE_MINUS_SRC_ALPHA_FACTOR;
}
this.blendDst = blendDst;
if (GameLib.Utils.UndefinedOrNull(blendEquation)) {
blendEquation = GameLib.D3.Material.TYPE_ADD_EQUATION;
}
this.blendEquation = blendEquation;
if (GameLib.Utils.UndefinedOrNull(depthTest)) {
depthTest = true;
}
this.depthTest = depthTest;
if (GameLib.Utils.UndefinedOrNull(depthFunc)) {
depthFunc = GameLib.D3.Material.TYPE_LESS_EQUAL_DEPTH;
}
this.depthFunc = depthFunc;
if (GameLib.Utils.UndefinedOrNull(depthWrite)) {
depthWrite = true;
}
this.depthWrite = depthWrite;
if (GameLib.Utils.UndefinedOrNull(polygonOffset)) {
polygonOffset = false;
}
this.polygonOffset = polygonOffset;
if (GameLib.Utils.UndefinedOrNull(polygonOffsetFactor)) {
polygonOffsetFactor = 1;
}
this.polygonOffsetFactor = polygonOffsetFactor;
if (GameLib.Utils.UndefinedOrNull(polygonOffsetUnits)) {
polygonOffsetUnits = 1;
}
this.polygonOffsetUnits = polygonOffsetUnits;
if (GameLib.Utils.UndefinedOrNull(alphaTest)) {
alphaTest = 0;
}
this.alphaTest = alphaTest;
if (GameLib.Utils.UndefinedOrNull(clippingPlanes)) {
clippingPlanes = [];
}
this.clippingPlanes = clippingPlanes;
if (GameLib.Utils.UndefinedOrNull(clipShadows)) {
clipShadows = false;
}
this.clipShadows = clipShadows;
if (GameLib.Utils.UndefinedOrNull(visible)) {
visible = true;
}
this.visible = visible;
if (GameLib.Utils.UndefinedOrNull(shading)) {
shading = GameLib.D3.Material.TYPE_FLAT_SHADING;
}
this.shading = shading;
if (GameLib.Utils.UndefinedOrNull(bumpScale)) {
bumpScale = 1;
}
this.bumpScale = bumpScale;
if (GameLib.Utils.UndefinedOrNull(normalScale)) {
normalScale = 1;
}
this.normalScale = normalScale;
if (GameLib.Utils.UndefinedOrNull(displacementScale)) {
displacementScale = 1;
}
this.displacementScale = displacementScale;
if (GameLib.Utils.UndefinedOrNull(displacementBias)) {
displacementBias = 0;
}
this.displacementBias = displacementBias;
if (GameLib.Utils.UndefinedOrNull(roughness)) {
roughness = 0.5;
}
this.roughness = roughness;
if (GameLib.Utils.UndefinedOrNull(metalness)) {
metalness = 0.5;
}
this.metalness = metalness;
if (GameLib.Utils.UndefinedOrNull(pointSize)) {
pointSize = 1;
}
this.pointSize = pointSize;
if (GameLib.Utils.UndefinedOrNull(pointSizeAttenuation)) {
pointSizeAttenuation = true;
}
this.pointSizeAttenuation = pointSizeAttenuation;
if (GameLib.Utils.UndefinedOrNull(spriteRotation)) {
spriteRotation = 0;
}
this.spriteRotation = spriteRotation;
if (GameLib.Utils.UndefinedOrNull(envMapIntensity)) {
envMapIntensity = 1.0;
}
this.envMapIntensity = envMapIntensity;
if (GameLib.Utils.UndefinedOrNull(alphaMap)) {
alphaMap = null;
}
this.alphaMap = alphaMap;
if (GameLib.Utils.UndefinedOrNull(aoMap)) {
aoMap = null;
}
this.aoMap = aoMap;
if (GameLib.Utils.UndefinedOrNull(bumpMap)) {
bumpMap = null;
}
this.bumpMap = bumpMap;
if (GameLib.Utils.UndefinedOrNull(diffuseMap)) {
diffuseMap = null;
}
this.diffuseMap = diffuseMap;
if (GameLib.Utils.UndefinedOrNull(displacementMap)) {
displacementMap = null;
}
this.displacementMap = displacementMap;
if (GameLib.Utils.UndefinedOrNull(emissiveMap)) {
emissiveMap = null;
}
this.emissiveMap = emissiveMap;
if (GameLib.Utils.UndefinedOrNull(environmentMap)) {
environmentMap = null;
}
this.environmentMap = environmentMap;
if (GameLib.Utils.UndefinedOrNull(lightMap)) {
lightMap = null;
}
this.lightMap = lightMap;
if (GameLib.Utils.UndefinedOrNull(metalnessMap)) {
metalnessMap = null;
}
this.metalnessMap = metalnessMap;
if (GameLib.Utils.UndefinedOrNull(normalMap)) {
normalMap = null;
}
this.normalMap = normalMap;
if (GameLib.Utils.UndefinedOrNull(roughnessMap)) {
roughnessMap = null;
}
this.roughnessMap = roughnessMap;
if (GameLib.Utils.UndefinedOrNull(specularMap)) {
specularMap = null;
}
this.specularMap = specularMap;
if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
parentEntity = null;
}
this.parentEntity = parentEntity;
this.needsUpdate = false;
};
GameLib.D3.API.Material.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.API.Material.prototype.constructor = GameLib.D3.API.Material;
/**
* Returns an API Material from an Object material
* @param objectMaterial
* @constructor
*/
GameLib.D3.API.Material.FromObject = function(objectMaterial) {
var apiAlphaMap = null;
var apiAoMap = null;
var apiBumpMap = null;
var apiDiffuseMap = null;
var apiDisplacementMap = null;
var apiEmissiveMap = null;
var apiEnvironmentMap = null;
var apiLightMap = null;
var apiMetalnessMap = null;
var apiNormalMap = null;
var apiRoughnessMap = null;
var apiSpecularMap = null;
if (objectMaterial.alphaMap) {
apiAlphaMap = objectMaterial.alphaMap;
}
if (objectMaterial.aoMap) {
apiAoMap = objectMaterial.aoMap;
}
if (objectMaterial.bumpMap) {
apiBumpMap = objectMaterial.bumpMap;
}
if (objectMaterial.diffuseMap) {
apiDiffuseMap = objectMaterial.diffuseMap;
}
if (objectMaterial.displacementMap) {
apiDisplacementMap = objectMaterial.displacementMap;
}
if (objectMaterial.emissiveMap) {
apiEmissiveMap = objectMaterial.emissiveMap;
}
if (objectMaterial.environmentMap) {
apiEnvironmentMap = objectMaterial.environmentMap;
}
if (objectMaterial.lightMap) {
apiLightMap = objectMaterial.lightMap;
}
if (objectMaterial.metalnessMap) {
apiMetalnessMap = objectMaterial.metalnessMap;
}
if (objectMaterial.normalMap) {
apiNormalMap = objectMaterial.normalMap;
}
if (objectMaterial.roughnessMap) {
apiRoughnessMap = objectMaterial.roughnessMap;
}
if (objectMaterial.specularMap) {
apiSpecularMap = objectMaterial.specularMap;
}
return new GameLib.D3.API.Material(
objectMaterial.id,
objectMaterial.materialType,
objectMaterial.name,
objectMaterial.opacity,
objectMaterial.side,
objectMaterial.transparent,
GameLib.API.Color.FromObject(objectMaterial.specular),
objectMaterial.lightMapIntensity,
objectMaterial.aoMapIntensity,
GameLib.API.Color.FromObject(objectMaterial.color),
GameLib.API.Color.FromObject(objectMaterial.emissive),
objectMaterial.emissiveIntensity,
objectMaterial.combine,
objectMaterial.shininess,
objectMaterial.reflectivity,
objectMaterial.refractionRatio,
objectMaterial.fog,
objectMaterial.wireframe,
objectMaterial.wireframeLineWidth,
objectMaterial.wireframeLineCap,
objectMaterial.wireframeLineJoin,
objectMaterial.vertexColors,
objectMaterial.skinning,
objectMaterial.morphTargets,
objectMaterial.morphNormals,
objectMaterial.lineWidth,
objectMaterial.lineCap,
objectMaterial.lineJoin,
objectMaterial.dashSize,
objectMaterial.gapWidth,
objectMaterial.blending,
objectMaterial.blendSrc,
objectMaterial.blendDst,
objectMaterial.blendEquation,
objectMaterial.depthTest,
objectMaterial.depthFunc,
objectMaterial.depthWrite,
objectMaterial.polygonOffset,
objectMaterial.polygonOffsetFactor,
objectMaterial.polygonOffsetUnits,
objectMaterial.alphaTest,
objectMaterial.clippingPlanes,
objectMaterial.clipShadows,
objectMaterial.visible,
objectMaterial.overdraw,
objectMaterial.shading,
objectMaterial.bumpScale,
objectMaterial.normalScale,
objectMaterial.displacementScale,
objectMaterial.displacementBias,
objectMaterial.roughness,
objectMaterial.metalness,
objectMaterial.pointSize,
objectMaterial.pointSizeAttenuation,
objectMaterial.spriteRotation,
objectMaterial.envMapIntensity,
apiAlphaMap,
apiAoMap,
apiBumpMap,
apiDiffuseMap,
apiDisplacementMap,
apiEmissiveMap,
apiEnvironmentMap,
apiLightMap,
apiMetalnessMap,
apiNormalMap,
apiRoughnessMap,
apiSpecularMap,
objectMaterial.parentEntity
)
};