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

1130 lines
35 KiB
JavaScript

/**
* 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
* @constructor
* @returns {GameLib.D3.Material | GameLib.D3.API.Material}
*/
GameLib.D3.Material = function(
graphics,
apiMaterial
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiMaterial)) {
apiMaterial = {};
}
if (apiMaterial instanceof GameLib.D3.Material) {
return apiMaterial;
}
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.specular,
this
);
this.color = new GameLib.Color(
graphics,
this.color,
this
);
this.emissive = new GameLib.Color(
graphics,
this.emissive,
this
);
if (this.alphaMap) {
if (this.alphaMap instanceof GameLib.D3.API.Texture) {
this.alphaMap = new GameLib.D3.Texture(
this.graphics,
this.alphaMap
);
}
}
if (this.aoMap) {
if (this.aoMap instanceof GameLib.D3.API.Texture) {
this.aoMap = new GameLib.D3.Texture(
this.graphics,
this.aoMap
);
}
}
if (this.bumpMap) {
if (this.bumpMap instanceof GameLib.D3.API.Texture) {
this.bumpMap = new GameLib.D3.Texture(
this.graphics,
this.bumpMap
);
}
}
if (this.diffuseMap) {
if (this.diffuseMap instanceof GameLib.D3.API.Texture) {
this.diffuseMap = new GameLib.D3.Texture(
this.graphics,
this.diffuseMap
);
}
}
if (this.displacementMap) {
if (this.displacementMap instanceof GameLib.D3.API.Texture) {
this.displacementMap = new GameLib.D3.Texture(
this.graphics,
this.displacementMap
);
}
}
if (this.emissiveMap) {
if (this.emissiveMap instanceof GameLib.D3.API.Texture) {
this.emissiveMap = new GameLib.D3.Texture(
this.graphics,
this.emissiveMap
);
}
}
if (this.environmentMap) {
if (this.environmentMap instanceof GameLib.D3.API.Texture) {
this.environmentMap = new GameLib.D3.Texture(
this.graphics,
this.environmentMap
);
}
}
if (this.lightMap) {
if (this.lightMap instanceof GameLib.D3.API.Texture) {
this.lightMap = new GameLib.D3.Texture(
this.graphics,
this.lightMap
);
}
}
if (this.metalnessMap) {
if (this.metalnessMap instanceof GameLib.D3.API.Texture) {
this.metalnessMap = new GameLib.D3.Texture(
this.graphics,
this.metalnessMap
);
}
}
if (this.normalMap) {
if (this.normalMap instanceof GameLib.D3.API.Texture) {
this.normalMap = new GameLib.D3.Texture(
this.graphics,
this.normalMap
);
}
}
if (this.roughnessMap) {
if (this.roughnessMap instanceof GameLib.D3.API.Texture) {
this.roughnessMap = new GameLib.D3.Texture(
this.graphics,
this.roughnessMap
);
}
}
if (this.specularMap) {
if (this.specularMap instanceof GameLib.D3.API.Texture) {
this.specularMap = new GameLib.D3.Texture(
this.graphics,
this.specularMap
);
}
}
GameLib.Component.call(
this,
GameLib.Component.COMPONENT_MATERIAL,
{
'alphaMap' : GameLib.D3.Texture,
'aoMap' : GameLib.D3.Texture,
'bumpMap' : GameLib.D3.Texture,
'diffuseMap' : GameLib.D3.Texture,
'displacementMap' : GameLib.D3.Texture,
'emissiveMap' : GameLib.D3.Texture,
'environmentMap' : GameLib.D3.Texture,
'lightMap' : GameLib.D3.Texture,
'metalnessMap' : GameLib.D3.Texture,
'normalMap' : GameLib.D3.Texture,
'roughnessMap' : GameLib.D3.Texture,
'specularMap' : GameLib.D3.Texture
}
);
};
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 = 0x1;
GameLib.D3.Material.MATERIAL_TYPE_LINE_DASHED = 0x2;
GameLib.D3.Material.MATERIAL_TYPE_BASIC = 0x3;
GameLib.D3.Material.MATERIAL_TYPE_DEPTH = 0x4;
GameLib.D3.Material.MATERIAL_TYPE_LAMBERT = 0x5;
GameLib.D3.Material.MATERIAL_TYPE_NORMAL = 0x6;
GameLib.D3.Material.MATERIAL_TYPE_PHONG = 0x7;
GameLib.D3.Material.MATERIAL_TYPE_STANDARD = 0x8;
GameLib.D3.Material.MATERIAL_TYPE_POINTS = 0x9;
GameLib.D3.Material.MATERIAL_TYPE_SPRITE = 0xa;
GameLib.D3.Material.prototype.createStandardMaterialInstance = function() {
return 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
});
};
GameLib.D3.Material.prototype.createPointsMaterialInstance = function() {
return new THREE.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
});
};
GameLib.D3.Material.prototype.createPhongMaterialInstance = function() {
return 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
});
};
GameLib.D3.Material.prototype.createMeshBasicMaterialInstance = function() {
return 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
});
};
/**
* updates textures
*/
GameLib.D3.Material.prototype.updateTextures = function() {
if (this.alphaMap && this.alphaMap.instance) {
this.instance.alphaMap = this.alphaMap.instance;
} else {
this.instance.alphaMap = null;
}
if (this.aoMap && this.aoMap.instance) {
this.instance.aoMap = this.aoMap.instance;
} else {
this.instance.aoMap = null;
}
if (this.bumpMap && this.bumpMap.instance) {
this.instance.bumpMap = this.bumpMap.instance;
} else {
this.instance.bumpMap = null;
}
if (this.diffuseMap && this.diffuseMap.instance) {
this.instance.map = this.diffuseMap.instance;
} else {
this.instance.map = null;
}
if (this.displacementMap && this.displacementMap.instance) {
this.instance.displacementMap = this.displacementMap.instance;
} else {
this.instance.displacementMap = null;
}
if (this.emissiveMap && this.emissiveMap.instance) {
this.instance.emissiveMap = this.emissiveMap.instance;
} else {
this.instance.emissiveMap = null;
}
if (this.environmentMap && this.environmentMap.instance) {
this.instance.envMap = this.environmentMap.instance;
} else {
this.instance.envMap = null;
}
if (this.lightMap && this.lightMap.instance) {
this.instance.lightMap = this.lightMap.instance;
} else {
this.instance.lightMap = null;
}
if (this.metalnessMap && this.metalnessMap.instance) {
this.instance.metalnessMap = this.metalnessMap.instance;
} else {
this.instance.metalnessMap = null;
}
if (this.normalMap && this.normalMap.instance) {
this.instance.normalMap = this.normalMap.instance;
} else {
this.instance.normalMap = null;
}
if (this.roughnessMap && this.roughnessMap.instance) {
this.instance.roughnessMap = this.roughnessMap.instance;
} else {
this.instance.roughnessMap = null;
}
if (this.specularMap && this.specularMap.instance) {
this.instance.specularMap = this.specularMap.instance;
} else {
this.instance.specularMap = null;
}
};
GameLib.D3.Material.prototype.updateStandardMaterialInstance = function() {
this.instance.name = this.name;
this.instance.opacity = this.opacity;
this.instance.transparent = this.transparent;
this.instance.blending = this.blending;
this.instance.blendSrc = this.blendSrc;
this.instance.blendDst = this.blendDst;
this.instance.blendEquation = this.blendEquation;
this.instance.depthTest = this.depthTest;
this.instance.depthFunc = this.depthFunc;
this.instance.depthWrite = this.depthWrite;
this.instance.polygonOffset = this.polygonOffset;
this.instance.polygonOffsetFactor = this.polygonOffsetFactor;
this.instance.polygonOffsetUnits = this.polygonOffsetUnits;
this.instance.alphaTest = this.alphaTest;
this.instance.clippingPlanes = this.clippingPlanes;
this.instance.clipShadows = this.clipShadows;
this.instance.overdraw = this.overdraw;
this.instance.visible = this.visible;
this.instance.side = this.side;
this.instance.color = this.color.instance;
this.instance.envMapIntensity = this.envMapIntensity; //standard material doesnt have specular color
this.instance.roughness = this.roughness;
this.instance.metalness = this.metalness;
this.instance.lightMapIntensity = this.lightMapIntensity;
this.instance.aoMapIntensity = this.aoMapIntensity;
this.instance.emissive = this.emissive.instance;
this.instance.emissiveIntensity = this.emissiveIntensity;
this.instance.bumpScale = this.bumpScale;
this.instance.normalScale = this.normalScale;
this.instance.displacementScale = this.displacementScale;
this.instance.refractionRatio = this.refractionRatio;
this.instance.fog = this.fog;
this.instance.shading = this.shading;
this.instance.wireframe = this.wireframe;
this.instance.wireframeLinewidth = this.wireframeLineWidth;
this.instance.wireframeLinecap = this.wireframeLineCap;
this.instance.wireframeLinejoin = this.wireframeLineJoin;
this.instance.vertexColors = this.vertexColors;
this.instance.skinning = this.skinning;
this.instance.morphTargets = this.morphTargets;
this.instance.morphNormals = this.morphNormals;
};
GameLib.D3.Material.prototype.updatePointsMaterialInstance = function() {
this.instance.name = this.name;
this.instance.opacity = this.opacity;
this.instance.transparent = this.transparent;
this.instance.blending = this.blending;
this.instance.blendSrc = this.blendSrc;
this.instance.blendDst = this.blendDst;
this.instance.blendEquation = this.blendEquation;
this.instance.depthTest = this.depthTest;
this.instance.depthFunc = this.depthFunc;
this.instance.depthWrite = this.depthWrite;
this.instance.polygonOffset = this.polygonOffset;
this.instance.polygonOffsetFactor = this.polygonOffsetFactor;
this.instance.polygonOffsetUnits = this.polygonOffsetUnits;
this.instance.alphaTest = this.alphaTest;
this.instance.clippingPlanes = this.clippingPlanes;
this.instance.clipShadows = this.clipShadows;
this.instance.overdraw = this.overdraw;
this.instance.visible = this.visible;
this.instance.side = this.side;
this.instance.color = this.color.instance;
this.instance.size = this.pointSize;
this.instance.sizeAttenuation = this.pointSizeAttenuation;
this.instance.vertexColors = GameLib.D3.Material.TYPE_NO_COLORS;
this.instance.fog = this.fog;
};
GameLib.D3.Material.prototype.updatePhongMaterialInstance = function() {
this.instance.name = this.name;
this.instance.opacity = this.opacity;
this.instance.transparent = this.transparent;
this.instance.blending = this.blending;
this.instance.blendSrc = this.blendSrc;
this.instance.blendDst = this.blendDst;
this.instance.blendEquation = this.blendEquation;
this.instance.depthTest = this.depthTest;
this.instance.depthFunc = this.depthFunc;
this.instance.depthWrite = this.depthWrite;
this.instance.polygonOffset = this.polygonOffset;
this.instance.polygonOffsetFactor = this.polygonOffsetFactor;
this.instance.polygonOffsetUnits = this.polygonOffsetUnits;
this.instance.alphaTest = this.alphaTest;
this.instance.clippingPlanes = this.clippingPlanes;
this.instance.clipShadows = this.clipShadows;
this.instance.overdraw = this.overdraw;
this.instance.visible = this.visible;
this.instance.side = this.side;
this.instance.color = this.color.instance;
this.instance.specular = this.specular.instance;
this.instance.shininess = this.shininess;
this.instance.lightMapIntensity = this.lightMapIntensity;
this.instance.aoMapIntensity = this.aoMapIntensity;
this.instance.emissive = this.emissive.instance;
this.instance.emissiveIntensity = this.emissiveIntensity;
this.instance.envMapIntensity = this.envMapIntensity;
this.instance.bumpScale = this.bumpScale;
this.instance.normalScale = this.normalScale;
this.instance.displacementScale = this.displacementScale;
this.instance.combine = this.combine;
this.instance.refractionRatio = this.refractionRatio;
this.instance.fog = this.fog;
this.instance.shading = this.shading;
this.instance.wireframe = this.wireframe;
this.instance.wireframeLinewidth = this.wireframeLineWidth;
this.instance.wireframeLinecap = this.wireframeLineCap;
this.instance.wireframeLinejoin = this.wireframeLineJoin;
this.instance.vertexColors = this.vertexColors;
this.instance.skinning = this.skinning;
this.instance.morphTargets = this.morphTargets;
this.instance.morphNormals = this.morphNormals;
};
GameLib.D3.Material.prototype.updateMeshBasicMaterialInstance = function() {
this.instance.name = this.name;
this.instance.opacity = this.opacity;
this.instance.transparent = this.transparent;
this.instance.blending = this.blending;
this.instance.blendSrc = this.blendSrc;
this.instance.blendDst = this.blendDst;
this.instance.blendEquation = this.blendEquation;
this.instance.depthTest = this.depthTest;
this.instance.depthFunc = this.depthFunc;
this.instance.depthWrite = this.depthWrite;
this.instance.polygonOffset = this.polygonOffset;
this.instance.polygonOffsetFactor = this.polygonOffsetFactor;
this.instance.polygonOffsetUnits = this.polygonOffsetUnits;
this.instance.alphaTest = this.alphaTest;
this.instance.clippingPlanes = this.clippingPlanes;
this.instance.clipShadows = this.clipShadows;
this.instance.overdraw = this.overdraw;
this.instance.visible = this.visible;
this.instance.side = this.side;
this.instance.color = this.color.instance;
this.instance.vertexColors = GameLib.D3.Material.TYPE_NO_COLORS;
this.instance.fog = this.fog;
};
/**
* Material instance
* @returns {*}
*/
GameLib.D3.Material.prototype.createInstance = function(update) {
if (!this.loaded) {
console.log('Attempted to create an instance but the runtime object is not fully loaded : ' + this.name);
return null;
}
if (update) {
var typeChange = false;
if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_STANDARD) {
if (!(this.instance instanceof THREE.MeshStandardMaterial)) {
this.instance = this.createStandardMaterialInstance();
typeChange = true;
} else {
this.updateStandardMaterialInstance();
}
} else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_POINTS) {
if (!(this.instance instanceof THREE.PointsMaterial)) {
this.instance = this.createPointsMaterialInstance();
typeChange = true;
} else {
this.updatePointsMaterialInstance();
}
} else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_PHONG) {
if (!(this.instance instanceof THREE.MeshPhongMaterial)) {
this.instance = this.createPhongMaterialInstance();
typeChange = true;
} else {
this.updatePhongMaterialInstance();
}
} else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_BASIC) {
if (!(this.instance instanceof THREE.MeshBasicMaterial)) {
this.instance = this.createMeshBasicMaterialInstance();
typeChange = true;
} else {
this.updateMeshBasicMaterialInstance();
}
} else {
console.warn('not yet implemented (material type = ' + this.materialType + ')');
}
this.updateTextures();
if (typeChange) {
this.publish(
GameLib.Event.MATERIAL_TYPE_CHANGED,
{
material: this
}
);
}
this.instance.needsUpdate = true;
} else {
var instance = null;
if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_STANDARD) {
instance = this.createStandardMaterialInstance();
} else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_POINTS) {
instance = this.createPointsMaterialInstance();
} else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_PHONG) {
instance = this.createPhongMaterialInstance();
} else if (this.materialType === GameLib.D3.Material.MATERIAL_TYPE_BASIC) {
instance = this.createMeshBasicMaterialInstance();
} else {
console.warn("material type is not implemented yet: " + this.materialType);
}
instance.needsUpdate = true;
this.subscribe(
GameLib.Event.TEXTURE_LOADED,
function (data) {
var modified = false;
/**
* We also need to check if the image of the texture is assigned -
* if not we should disable the map
*/
if (this.alphaMap === data.texture) {
if (data.texture.image) {
this.instance.alphaMap = data.texture.instance;
} else {
this.instance.alphaMap = null;
}
modified = true;
}
if (this.aoMap === data.texture) {
if (data.texture.image) {
this.instance.aoMap = data.texture.instance;
} else {
this.instance.aoMap = null;
}
modified = true;
}
if (this.bumpMap === data.texture) {
if (data.texture.image) {
this.instance.bumpMap = data.texture.instance;
} else {
this.instance.bumpMap = null;
}
modified = true;
}
if (this.diffuseMap === data.texture) {
if (data.texture.image) {
this.instance.map = data.texture.instance;
} else {
this.instance.map = null;
}
modified = true;
}
if (this.displacementMap === data.texture) {
if (data.texture.image) {
this.instance.displacementMap = data.texture.instance;
} else {
this.instance.displacementMap = null;
}
modified = true;
}
if (this.emissiveMap === data.texture) {
if (data.texture.image) {
this.instance.emissiveMap = data.texture.instance;
} else {
this.instance.emissiveMap = null;
}
modified = true;
}
if (this.environmentMap === data.texture) {
if (data.texture.image) {
this.instance.envMap = data.texture.instance;
} else {
this.instance.envMap = null;
}
modified = true;
}
if (this.lightMap === data.texture) {
if (data.texture.image) {
this.instance.lightMap = data.texture.instance;
} else {
this.instance.lightMap = null;
}
modified = true;
}
if (this.metalnessMap === data.texture) {
if (data.texture.image) {
this.instance.metalnessMap = data.texture.instance;
} else {
this.instance.metalnessMap = null;
}
modified = true;
}
if (this.normalMap === data.texture) {
if (data.texture.image) {
this.instance.normalMap = data.texture.instance;
} else {
this.instance.normalMap = null;
}
modified = true;
}
if (this.roughnessMap === data.texture) {
if (data.texture.image) {
this.instance.roughnessMap = data.texture.instance;
} else {
this.instance.roughnessMap = null;
}
modified = true;
}
if (this.specularMap === data.texture) {
if (data.texture.image) {
this.instance.specularMap = data.texture.instance;
} else {
this.instance.specularMap = null;
}
modified = true;
}
if (modified) {
this.publish(
GameLib.Event.MATERIAL_LOADED,
{
material: this
}
);
this.instance.needsUpdate = true;
}
}
);
this.instance = instance;
this.updateTextures();
return instance;
}
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Material.prototype.updateInstance = function() {
this.createInstance(true);
};
/**
* Converts a GameLib.D3.Material to a GameLib.D3.API.Material
* @returns {GameLib.D3.API.Material}
*/
GameLib.D3.Material.prototype.toApiObject = function(save) {
if (GameLib.Utils.UndefinedOrNull(save)) {
save = false;
}
var apiAlphaMap = null;
if (this.alphaMap) {
if (save) {
this.alphaMap.save();
}
apiAlphaMap = this.alphaMap.id;
}
var apiAoMap = null;
if (this.aoMap) {
if (save) {
this.aoMap.save();
}
apiAoMap = this.aoMap.id;
}
var apiBumpMap = null;
if (this.bumpMap) {
if (save) {
this.bumpMap.save();
}
apiBumpMap = this.bumpMap.id;
}
var apiDiffuseMap = null;
if (this.diffuseMap) {
if (save) {
this.diffuseMap.save();
}
apiDiffuseMap = this.diffuseMap.id;
}
var apiDisplacementMap = null;
if (this.displacementMap) {
if (save) {
this.displacementMap.save();
}
apiDisplacementMap = this.displacementMap.id;
}
var apiEmissiveMap = null;
if (this.emissiveMap) {
if (save) {
this.emissiveMap.save();
}
apiEmissiveMap = this.emissiveMap.id;
}
var apiEnvironmentMap = null;
if (this.environmentMap) {
if (save) {
this.environmentMap.save();
}
apiEnvironmentMap = this.environmentMap.id;
}
var apiLightMap = null;
if (this.lightMap) {
if (save) {
this.lightMap.save();
}
apiLightMap = this.lightMap.id;
}
var apiMetalnessMap = null;
if (this.metalnessMap) {
if (save) {
this.metalnessMap.save();
}
apiMetalnessMap = this.metalnessMap.id;
}
var apiNormalMap = null;
if (this.normalMap) {
this.normalMap.save();
apiNormalMap = this.normalMap.id;
}
var apiRoughnessMap = null;
if (this.roughnessMap) {
if (save) {
this.roughnessMap.save();
}
apiRoughnessMap = this.roughnessMap.id;
}
var apiSpecularMap = null;
if (this.specularMap) {
if (save) {
this.specularMap.save();
}
apiSpecularMap = this.specularMap.id;
}
var apiMaterial = new GameLib.D3.API.Material(
this.id,
this.materialType,
this.name,
this.opacity,
this.side,
this.transparent,
this.specular.toApiObject(),
this.lightMapIntensity,
this.aoMapIntensity,
this.color.toApiObject(),
this.emissive.toApiObject(),
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)
);
return apiMaterial;
};
/**
* Creates a GameLib.D3.Material from a material Object
* @param graphics GameLib.D3.Graphics
* @param objectMaterial Object
* @constructor
*/
GameLib.D3.Material.FromObject = function(graphics, objectMaterial) {
var gameLibMaterial = new GameLib.D3.Material(
graphics,
GameLib.D3.API.Material.FromObject(objectMaterial)
);
return gameLibMaterial;
};