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

637 lines
16 KiB
JavaScript
Raw Normal View History

2016-11-29 12:54:25 +01:00
/**
* 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
2017-01-05 19:34:28 +01:00
* @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
2016-11-29 12:54:25 +01:00
* @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,
2017-01-05 19:34:28 +01:00
envMapIntensity,
alphaMap,
aoMap,
bumpMap,
diffuseMap,
displacementMap,
emissiveMap,
environmentMap,
lightMap,
metalnessMap,
normalMap,
roughnessMap,
specularMap,
2017-06-13 14:09:18 +02:00
parentEntity
2016-11-29 12:54:25 +01:00
) {
2017-01-20 13:40:27 +01:00
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
2016-11-29 12:54:25 +01:00
}
this.id = id;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(materialType)) {
2016-11-29 12:54:25 +01:00
materialType = GameLib.D3.Material.MATERIAL_TYPE_STANDARD;
}
this.materialType = materialType;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(name)) {
2016-11-29 12:54:25 +01:00
name = 'Material (' + materialType + ')';
}
this.name = name;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(opacity)) {
2016-11-29 12:54:25 +01:00
opacity = 1.0;
}
this.opacity = opacity;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(side)) {
2016-11-29 12:54:25 +01:00
side = GameLib.D3.Material.TYPE_FRONT_SIDE;
}
this.side = side;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(transparent)) {
2016-11-29 12:54:25 +01:00
transparent = false;
}
this.transparent = transparent;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(specular)) {
2017-01-06 16:53:53 +01:00
specular = new GameLib.API.Color(0.06, 0.06, 0.06, 0.06);
2016-11-29 12:54:25 +01:00
}
this.specular = specular;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(lightMapIntensity)) {
2016-11-29 12:54:25 +01:00
lightMapIntensity = 1;
}
this.lightMapIntensity = lightMapIntensity;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(aoMapIntensity)) {
2016-11-29 12:54:25 +01:00
aoMapIntensity = 1;
}
this.aoMapIntensity = aoMapIntensity;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(color)) {
2017-01-06 16:53:53 +01:00
color = new GameLib.API.Color(1, 1, 1, 1)
2016-11-29 12:54:25 +01:00
}
this.color = color;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(emissive)) {
2017-01-06 16:53:53 +01:00
emissive = new GameLib.API.Color(0, 0, 0, 0);
2016-11-29 12:54:25 +01:00
}
this.emissive = emissive;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(emissiveIntensity)) {
2016-11-29 12:54:25 +01:00
emissiveIntensity = 1;
}
this.emissiveIntensity = emissiveIntensity;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(combine)) {
2016-11-29 12:54:25 +01:00
combine = GameLib.D3.Material.TYPE_MULTIPLY_OPERATION;
}
this.combine = combine;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(shininess)) {
2016-11-29 12:54:25 +01:00
shininess = 30;
}
this.shininess = shininess;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(reflectivity)) {
2016-11-29 12:54:25 +01:00
reflectivity = 1;
}
this.reflectivity = reflectivity;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(refractionRatio)) {
2016-11-29 12:54:25 +01:00
refractionRatio = 0.98;
}
this.refractionRatio = refractionRatio;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(fog)) {
2016-11-29 12:54:25 +01:00
fog = true;
}
this.fog = fog;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(wireframe)) {
2016-11-29 12:54:25 +01:00
wireframe = false;
}
this.wireframe = wireframe;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(wireframeLineWidth)) {
2016-11-29 12:54:25 +01:00
wireframeLineWidth = 1;
}
this.wireframeLineWidth = wireframeLineWidth;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(wireframeLineCap)) {
2016-11-29 12:54:25 +01:00
wireframeLineCap = 'round';
}
this.wireframeLineCap = wireframeLineCap;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(wireframeLineJoin)) {
2016-11-29 12:54:25 +01:00
wireframeLineJoin = 'round';
}
this.wireframeLineJoin = wireframeLineJoin;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(vertexColors)) {
2016-11-29 12:54:25 +01:00
vertexColors = GameLib.D3.Material.TYPE_NO_COLORS;
}
this.vertexColors = vertexColors;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(skinning)) {
2016-11-29 12:54:25 +01:00
skinning = false;
}
this.skinning = skinning;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(morphTargets)) {
2016-11-29 12:54:25 +01:00
morphTargets = false;
}
this.morphTargets = morphTargets;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(morphNormals)) {
2016-11-29 12:54:25 +01:00
morphNormals = false;
}
this.morphNormals = morphNormals;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(overdraw)) {
2016-11-29 12:54:25 +01:00
overdraw = 0;
}
this.overdraw = overdraw;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(lineWidth)) {
2016-11-29 12:54:25 +01:00
lineWidth = 1;
}
this.lineWidth = lineWidth;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(lineCap)) {
2016-11-29 12:54:25 +01:00
lineCap = 'round';
}
this.lineCap = lineCap;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(lineJoin)) {
2016-11-29 12:54:25 +01:00
lineJoin = 'round';
}
this.lineJoin = lineJoin;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(dashSize)) {
2016-11-29 12:54:25 +01:00
dashSize = 3;
}
this.dashSize = dashSize;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(gapWidth)) {
2016-11-29 12:54:25 +01:00
gapWidth = 1;
}
this.gapWidth = gapWidth;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(blending)) {
2016-11-29 12:54:25 +01:00
blending = GameLib.D3.Material.TYPE_NORMAL_BLENDING;
}
this.blending = blending;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(blendSrc)) {
2016-11-29 12:54:25 +01:00
blendSrc = GameLib.D3.Material.TYPE_SRC_ALPHA_FACTOR;
}
this.blendSrc = blendSrc;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(blendDst)) {
2016-11-29 12:54:25 +01:00
blendDst = GameLib.D3.Material.TYPE_ONE_MINUS_SRC_ALPHA_FACTOR;
}
this.blendDst = blendDst;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(blendEquation)) {
2016-11-29 12:54:25 +01:00
blendEquation = GameLib.D3.Material.TYPE_ADD_EQUATION;
}
this.blendEquation = blendEquation;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(depthTest)) {
2016-11-29 12:54:25 +01:00
depthTest = true;
}
this.depthTest = depthTest;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(depthFunc)) {
2016-11-29 12:54:25 +01:00
depthFunc = GameLib.D3.Material.TYPE_LESS_EQUAL_DEPTH;
}
this.depthFunc = depthFunc;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(depthWrite)) {
2016-11-29 12:54:25 +01:00
depthWrite = true;
}
this.depthWrite = depthWrite;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(polygonOffset)) {
2016-11-29 12:54:25 +01:00
polygonOffset = false;
}
this.polygonOffset = polygonOffset;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(polygonOffsetFactor)) {
2016-11-29 12:54:25 +01:00
polygonOffsetFactor = 1;
}
this.polygonOffsetFactor = polygonOffsetFactor;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(polygonOffsetUnits)) {
2016-11-29 12:54:25 +01:00
polygonOffsetUnits = 1;
}
this.polygonOffsetUnits = polygonOffsetUnits;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(alphaTest)) {
2016-11-29 12:54:25 +01:00
alphaTest = 0;
}
this.alphaTest = alphaTest;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(clippingPlanes)) {
2016-11-29 12:54:25 +01:00
clippingPlanes = [];
}
this.clippingPlanes = clippingPlanes;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(clipShadows)) {
2016-11-29 12:54:25 +01:00
clipShadows = false;
}
this.clipShadows = clipShadows;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(visible)) {
2016-11-29 12:54:25 +01:00
visible = true;
}
this.visible = visible;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(shading)) {
2016-11-29 12:54:25 +01:00
shading = GameLib.D3.Material.TYPE_FLAT_SHADING;
}
this.shading = shading;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(bumpScale)) {
2016-11-29 12:54:25 +01:00
bumpScale = 1;
}
this.bumpScale = bumpScale;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(normalScale)) {
2016-11-29 12:54:25 +01:00
normalScale = 1;
}
this.normalScale = normalScale;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(displacementScale)) {
2016-11-29 12:54:25 +01:00
displacementScale = 1;
}
this.displacementScale = displacementScale;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(displacementBias)) {
2016-11-29 12:54:25 +01:00
displacementBias = 0;
}
this.displacementBias = displacementBias;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(roughness)) {
2016-11-29 12:54:25 +01:00
roughness = 0.5;
}
this.roughness = roughness;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(metalness)) {
2016-11-29 12:54:25 +01:00
metalness = 0.5;
}
this.metalness = metalness;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(pointSize)) {
2016-11-29 12:54:25 +01:00
pointSize = 1;
}
this.pointSize = pointSize;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(pointSizeAttenuation)) {
2016-11-29 12:54:25 +01:00
pointSizeAttenuation = true;
}
this.pointSizeAttenuation = pointSizeAttenuation;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(spriteRotation)) {
2016-11-29 12:54:25 +01:00
spriteRotation = 0;
}
this.spriteRotation = spriteRotation;
2016-12-15 14:53:39 +01:00
if (GameLib.Utils.UndefinedOrNull(envMapIntensity)) {
2016-11-29 12:54:25 +01:00
envMapIntensity = 1.0;
}
this.envMapIntensity = envMapIntensity;
2017-01-05 19:34:28 +01:00
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;
2017-01-06 16:53:53 +01:00
if (GameLib.Utils.UndefinedOrNull(environmentMap)) {
environmentMap = null;
}
this.environmentMap = environmentMap;
2017-01-05 19:34:28 +01:00
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;
2017-05-22 15:42:05 +02:00
2017-06-16 15:49:53 +02:00
if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
parentEntity = null;
}
this.parentEntity = parentEntity;
2017-06-08 18:17:03 +02:00
this.needsUpdate = false;
2017-01-05 19:34:28 +01:00
};
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
*/
2017-06-14 14:21:57 +02:00
GameLib.D3.API.Material.FromObject = function(objectMaterial) {
2017-01-05 19:34:28 +01:00
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) {
2017-06-09 16:03:05 +02:00
apiAlphaMap = objectMaterial.alphaMap;
2017-01-05 19:34:28 +01:00
}
if (objectMaterial.aoMap) {
2017-06-09 16:03:05 +02:00
apiAoMap = objectMaterial.aoMap;
2017-01-05 19:34:28 +01:00
}
if (objectMaterial.bumpMap) {
2017-06-09 16:03:05 +02:00
apiBumpMap = objectMaterial.bumpMap;
2017-01-05 19:34:28 +01:00
}
if (objectMaterial.diffuseMap) {
2017-06-09 16:03:05 +02:00
apiDiffuseMap = objectMaterial.diffuseMap;
2017-01-05 19:34:28 +01:00
}
if (objectMaterial.displacementMap) {
2017-06-09 16:03:05 +02:00
apiDisplacementMap = objectMaterial.displacementMap;
2017-01-05 19:34:28 +01:00
}
if (objectMaterial.emissiveMap) {
2017-06-09 16:03:05 +02:00
apiEmissiveMap = objectMaterial.emissiveMap;
2017-01-05 19:34:28 +01:00
}
if (objectMaterial.environmentMap) {
2017-06-09 16:03:05 +02:00
apiEnvironmentMap = objectMaterial.environmentMap;
2017-01-05 19:34:28 +01:00
}
if (objectMaterial.lightMap) {
2017-06-09 16:03:05 +02:00
apiLightMap = objectMaterial.lightMap;
2017-01-05 19:34:28 +01:00
}
if (objectMaterial.metalnessMap) {
2017-06-09 16:03:05 +02:00
apiMetalnessMap = objectMaterial.metalnessMap;
2017-01-05 19:34:28 +01:00
}
if (objectMaterial.normalMap) {
2017-06-09 16:03:05 +02:00
apiNormalMap = objectMaterial.normalMap;
2017-01-05 19:34:28 +01:00
}
if (objectMaterial.roughnessMap) {
2017-06-09 16:03:05 +02:00
apiRoughnessMap = objectMaterial.roughnessMap;
2017-01-05 19:34:28 +01:00
}
if (objectMaterial.specularMap) {
2017-06-09 16:03:05 +02:00
apiSpecularMap = objectMaterial.specularMap;
2017-01-05 19:34:28 +01:00
}
return new GameLib.D3.API.Material(
objectMaterial.id,
objectMaterial.materialType,
objectMaterial.name,
objectMaterial.opacity,
objectMaterial.side,
objectMaterial.transparent,
2017-06-14 14:21:57 +02:00
GameLib.API.Color.FromObject(objectMaterial.specular),
2017-01-05 19:34:28 +01:00
objectMaterial.lightMapIntensity,
objectMaterial.aoMapIntensity,
2017-06-14 14:21:57 +02:00
GameLib.API.Color.FromObject(objectMaterial.color),
GameLib.API.Color.FromObject(objectMaterial.emissive),
2017-01-05 19:34:28 +01:00
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,
2017-05-22 15:42:05 +02:00
apiSpecularMap,
2017-06-16 15:49:53 +02:00
objectMaterial.parentEntity
2017-01-05 19:34:28 +01:00
)
};