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

822 lines
26 KiB
JavaScript
Raw Normal View History

2016-10-14 12:32:53 +02:00
/**
2016-11-17 18:31:41 +01:00
* Material Superset - The apiMaterial properties get moved into the Material object itself, and then the instance is
* created
* @param graphics GameLib.D3.Graphics
2016-11-29 12:54:25 +01:00
* @param apiMaterial GameLib.D3.API.Material
2017-01-05 19:34:28 +01:00
* @param imageFactory GameLib.D3.ImageFactory
2016-11-17 18:31:41 +01:00
* @constructor
2016-11-29 12:54:25 +01:00
* @returns {GameLib.D3.Material | GameLib.D3.API.Material}
2016-11-17 18:31:41 +01:00
*/
2016-11-21 16:08:39 +01:00
GameLib.D3.Material = function Material(
2016-11-17 18:31:41 +01:00
graphics,
2017-01-05 19:34:28 +01:00
apiMaterial,
imageFactory
2016-11-17 18:31:41 +01:00
) {
2016-12-02 16:03:03 +01:00
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiMaterial)) {
apiMaterial = {};
}
if (GameLib.Utils.UndefinedOrNull(imageFactory)) {
2017-01-20 13:40:27 +01:00
console.warn('Cannot create a Material fully without specifying an ImageFactory');
imageFactory = null;
}
2017-01-20 13:40:27 +01:00
this.imageFactory = imageFactory;
2017-01-05 19:34:28 +01:00
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
);
2016-12-02 16:03:03 +01:00
2017-01-06 16:53:53 +01:00
this.specular = new GameLib.Color(
2016-12-02 13:00:56 +01:00
graphics,
this,
this.specular
);
2017-01-06 16:53:53 +01:00
this.color = new GameLib.Color(
2016-12-02 13:00:56 +01:00
graphics,
this,
this.color
);
2017-01-06 16:53:53 +01:00
this.emissive = new GameLib.Color(
2016-12-02 13:00:56 +01:00
graphics,
this,
this.emissive
);
2017-01-06 16:53:53 +01:00
if (this.alphaMap) {
if (this.alphaMap instanceof GameLib.D3.API.Texture) {
this.alphaMap = new GameLib.D3.Texture(
this.graphics,
this.alphaMap,
this.imageFactory
);
} else {
console.warn('this.alphaMap is not an instance of API.Texture');
// throw new Error('this.alphaMap is not an instance of API.Texture');
}
2017-01-06 16:53:53 +01:00
}
if (this.aoMap) {
if (this.aoMap instanceof GameLib.D3.API.Texture) {
this.aoMap = new GameLib.D3.Texture(
this.graphics,
this.aoMap,
this.imageFactory
);
} else {
console.warn('this.aoMap is not an instance of API.Texture');
// throw new Error('this.aoMap is not an instance of API.Texture');
}
2017-01-06 16:53:53 +01:00
}
if (this.bumpMap) {
if (this.bumpMap instanceof GameLib.D3.API.Texture) {
this.bumpMap = new GameLib.D3.Texture(
this.graphics,
this.bumpMap,
this.imageFactory
);
} else {
console.warn('this.bumpMap is not an instance of API.Texture');
// throw new Error('this.bumpMap is not an instance of API.Texture');
}
2017-01-06 16:53:53 +01:00
}
if (this.diffuseMap) {
if (this.diffuseMap instanceof GameLib.D3.API.Texture) {
this.diffuseMap = new GameLib.D3.Texture(
this.graphics,
this.diffuseMap,
this.imageFactory
);
} else {
console.warn('this.diffuseMap is not an instance of API.Texture');
// throw new Error('this.diffuseMap is not an instance of API.Texture');
}
2017-01-06 16:53:53 +01:00
}
if (this.displacementMap) {
if (this.displacementMap instanceof GameLib.D3.API.Texture) {
this.displacementMap = new GameLib.D3.Texture(
this.graphics,
this.displacementMap,
this.imageFactory
);
} else {
console.warn('this.displacementMap is not an instance of API.Texture');
// throw new Error('this.displacementMap is not an instance of API.Texture');
}
2017-01-06 16:53:53 +01:00
}
if (this.emissiveMap) {
if (this.emissiveMap instanceof GameLib.D3.API.Texture) {
this.emissiveMap = new GameLib.D3.Texture(
this.graphics,
this.emissiveMap,
this.imageFactory
);
} else {
console.warn('this.emissiveMap is not an instance of API.Texture');
// throw new Error('this.emissiveMap is not an instance of API.Texture');
}
2017-01-06 16:53:53 +01:00
}
if (this.environmentMap) {
if (this.environmentMap instanceof GameLib.D3.API.Texture) {
this.environmentMap = new GameLib.D3.Texture(
this.graphics,
this.environmentMap,
this.imageFactory
);
} else {
console.warn('this.environmentMap is not an instance of API.Texture');
// throw new Error('this.environmentMap is not an instance of API.Texture');
}
2017-01-06 16:53:53 +01:00
}
if (this.lightMap) {
if (this.lightMap instanceof GameLib.D3.API.Texture) {
this.lightMap = new GameLib.D3.Texture(
this.graphics,
this.lightMap,
this.imageFactory
);
} else {
console.warn('this.lightMap is not an instance of API.Texture');
// throw new Error('this.lightMap is not an instance of API.Texture');
}
2017-01-06 16:53:53 +01:00
}
if (this.metalnessMap) {
if (this.metalnessMap instanceof GameLib.D3.API.Texture) {
this.metalnessMap = new GameLib.D3.Texture(
this.graphics,
this.metalnessMap,
this.imageFactory
);
} else {
console.warn('this.metalnessMap is not an instance of API.Texture');
// throw new Error('this.metalnessMap is not an instance of API.Texture');
}
2017-01-06 16:53:53 +01:00
}
if (this.normalMap) {
if (this.normalMap instanceof GameLib.D3.API.Texture) {
this.normalMap = new GameLib.D3.Texture(
this.graphics,
this.normalMap,
this.imageFactory
);
} else {
console.warn('this.normalMap is not an instance of API.Texture');
// throw new Error('this.normalMap is not an instance of API.Texture');
}
2017-01-06 16:53:53 +01:00
}
if (this.roughnessMap) {
if (this.roughnessMap instanceof GameLib.D3.API.Texture) {
this.roughnessMap = new GameLib.D3.Texture(
this.graphics,
this.roughnessMap,
this.imageFactory
);
} else {
console.warn('this.roughnessMap is not an instance of API.Texture');
// throw new Error('this.roughnessMap is not an instance of API.Texture');
}
2017-01-06 16:53:53 +01:00
}
if (this.specularMap) {
if (this.specularMap instanceof GameLib.D3.API.Texture) {
this.specularMap = new GameLib.D3.Texture(
this.graphics,
this.specularMap,
this.imageFactory
);
} else {
console.warn('this.specularMap is not an instance of API.Texture');
// throw new Error('this.specularMap is not an instance of API.Texture');
}
2017-01-06 16:53:53 +01:00
}
2017-01-19 17:50:11 +01:00
this.buildIdToObject();
2016-11-17 18:31:41 +01:00
this.instance = this.createInstance();
2016-11-18 16:00:13 +01:00
2016-11-17 18:31:41 +01:00
};
2017-01-05 19:34:28 +01:00
GameLib.D3.Material.prototype = Object.create(GameLib.D3.API.Material.prototype);
GameLib.D3.Material.prototype.constructor = GameLib.D3.Material;
2016-10-14 12:32:53 +02:00
/**
* 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}
*/
2016-11-17 18:31:41 +01:00
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";
2016-10-14 12:32:53 +02:00
/**
2016-11-17 18:31:41 +01:00
* Material instance
* @returns {*}
2016-10-14 12:32:53 +02:00
*/
2016-11-18 16:00:13 +01:00
GameLib.D3.Material.prototype.createInstance = function(update) {
2016-11-17 18:31:41 +01:00
var instance = null;
2016-11-18 16:00:13 +01:00
if (update) {
instance = this.instance;
}
2016-12-02 16:03:03 +01:00
if (!instance) {
2016-11-18 16:00:13 +01:00
if (this.materialType == GameLib.D3.Material.MATERIAL_TYPE_STANDARD) {
2017-01-05 19:34:28 +01:00
instance = new THREE.MeshStandardMaterial({
2016-11-18 16:00:13 +01:00
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,
2016-12-02 16:03:03 +01:00
color: this.color.instance,
2016-11-18 16:00:13 +01:00
roughness: this.roughness,
metalness: this.metalness,
lightMapIntensity: this.lightMapIntensity,
aoMapIntensity: this.aoMapIntensity,
2016-12-02 16:03:03 +01:00
emissive: this.emissive.instance,
2016-11-18 16:00:13 +01:00
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) {
2017-01-05 19:34:28 +01:00
instance = new THREE.MeshPhongMaterial({
2016-11-18 16:00:13 +01:00
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,
2016-12-02 16:03:03 +01:00
color: this.color.instance,
specular: this.specular.instance,
2016-11-18 16:00:13 +01:00
shininess: this.shininess,
lightMapIntensity: this.lightMapIntensity,
aoMapIntensity: this.aoMapIntensity,
2016-12-02 16:03:03 +01:00
emissive: this.emissive.instance,
2016-11-18 16:00:13 +01:00
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,
2016-12-02 16:03:03 +01:00
color: this.color.instance,
size: this.pointSize,
sizeAttenuation: this.pointSizeAttenuation,
vertexColors: GameLib.D3.Material.TYPE_NO_COLORS,
fog: this.fog
});
2016-11-18 16:00:13 +01:00
} 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
});
2016-11-18 16:00:13 +01:00
} else {
console.log("material type is not implemented yet: " + this.materialType + " - material indexes could be screwed up");
}
2016-10-14 12:32:53 +02:00
}
2017-01-05 19:34:28 +01:00
if (update) {
for (var property in instance) {
2017-01-05 19:34:28 +01:00
if (instance.hasOwnProperty(property)) {
2016-12-05 16:40:26 +01:00
2017-01-05 19:34:28 +01:00
if (property == 'alphaMap') {
2017-01-06 16:53:53 +01:00
if (this.alphaMap) {
instance.alphaMap = this.alphaMap.instance;
2017-01-17 17:16:10 +01:00
} else {
instance.alphaMap = null;
2017-01-06 16:53:53 +01:00
}
2016-12-05 16:40:26 +01:00
}
2017-01-05 19:34:28 +01:00
else if (property == 'aoMap') {
2017-01-06 16:53:53 +01:00
if (this.aoMap) {
instance.aoMap = this.aoMap.instance;
2017-01-17 17:16:10 +01:00
} else {
instance.aoMap = null;
2017-01-06 16:53:53 +01:00
}
2017-01-05 19:34:28 +01:00
}
else if (property == 'bumpMap') {
2017-01-06 16:53:53 +01:00
if (this.bumpMap) {
instance.bumpMap = this.bumpMap.instance;
2017-01-17 17:16:10 +01:00
} else {
instance.bumpMap = null;
2017-01-06 16:53:53 +01:00
}
2017-01-05 19:34:28 +01:00
}
else if (property == 'map') {
2017-01-06 16:53:53 +01:00
if (this.diffuseMap) {
instance.map = this.diffuseMap.instance;
2017-01-17 17:16:10 +01:00
} else {
instance.map = null;
2017-01-06 16:53:53 +01:00
}
2017-01-05 19:34:28 +01:00
}
else if (property == 'displacementMap') {
2017-01-06 16:53:53 +01:00
if (this.displacementMap) {
instance.displacementMap = this.displacementMap.instance;
2017-01-17 17:16:10 +01:00
} else {
instance.displacementMap = null;
2017-01-06 16:53:53 +01:00
}
2017-01-05 19:34:28 +01:00
}
else if (property == 'emissiveMap') {
2017-01-06 16:53:53 +01:00
if (this.emissiveMap) {
instance.emissiveMap = this.emissiveMap.instance;
2017-01-17 17:16:10 +01:00
} else {
instance.emissiveMap = null;
2017-01-06 16:53:53 +01:00
}
2017-01-05 19:34:28 +01:00
}
else if (property == 'envMap') {
2017-01-06 16:53:53 +01:00
if (this.environmentMap) {
instance.envMap = this.environmentMap.instance;
2017-01-17 17:16:10 +01:00
} else {
instance.envMap = null;
2017-01-06 16:53:53 +01:00
}
2017-01-05 19:34:28 +01:00
}
else if (property == 'lightMap') {
2017-01-06 16:53:53 +01:00
if (this.lightMap) {
instance.lightMap = this.lightMap.instance;
2017-01-17 17:16:10 +01:00
} else {
instance.lightMap = null;
2017-01-06 16:53:53 +01:00
}
2017-01-05 19:34:28 +01:00
}
else if (property == 'metalnessMap') {
2017-01-06 16:53:53 +01:00
if (this.metalnessMap) {
instance.metalnessMap = this.metalnessMap.instance;
2017-01-17 17:16:10 +01:00
} else {
instance.metalnessMap = null;
2017-01-06 16:53:53 +01:00
}
2017-01-05 19:34:28 +01:00
}
else if (property == 'normalMap') {
2017-01-06 16:53:53 +01:00
if (this.normalMap) {
instance.normalMap = this.normalMap.instance;
2017-01-17 17:16:10 +01:00
} else {
instance.normalMap = null;
2017-01-06 16:53:53 +01:00
}
2017-01-05 19:34:28 +01:00
}
else if (property == 'roughnessMap') {
2017-01-06 16:53:53 +01:00
if (this.roughnessMap) {
instance.roughnessMap = this.roughnessMap.instance;
2017-01-17 17:16:10 +01:00
} else {
instance.roughnessMap = null;
2017-01-06 16:53:53 +01:00
}
2017-01-05 19:34:28 +01:00
}
else if (property == 'specularMap') {
2017-01-06 16:53:53 +01:00
if (this.specularMap) {
instance.specularMap = this.specularMap.instance;
2017-01-17 17:16:10 +01:00
} else {
instance.specularMap = null;
2017-01-06 16:53:53 +01:00
}
2017-01-05 19:34:28 +01:00
}
else if (property == 'size') {
instance.size = this.pointSize;
}
else if (property == 'sizeAttenuation') {
instance.sizeAttenuation = this.pointSizeAttenuation;
}
else if (instance[property] instanceof THREE.Color) {
2016-12-02 16:03:03 +01:00
instance[property].copy(this[property])
2017-01-06 16:53:53 +01:00
} else if (this.hasOwnProperty(property)) {
2016-12-02 16:03:03 +01:00
instance[property] = this[property];
}
}
}
instance.needsUpdate = true;
}
2016-11-17 18:31:41 +01:00
return instance;
2016-11-18 16:00:13 +01:00
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Material.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
2016-11-21 16:08:39 +01:00
};
GameLib.D3.Material.prototype.clone = function() {
return _.cloneDeep(this);
2016-12-09 20:32:09 +01:00
};
/**
* Converts a GameLib.D3.Material to a GameLib.D3.API.Material
* @returns {GameLib.D3.API.Material}
*/
GameLib.D3.Material.prototype.toApiMaterial = function() {
2017-01-06 16:53:53 +01:00
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();
}
2016-12-09 20:32:09 +01:00
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,
2017-01-05 19:34:28 +01:00
this.envMapIntensity,
2017-01-06 16:53:53 +01:00
apiAlphaMap,
apiAoMap,
apiBumpMap,
apiDiffuseMap,
apiDisplacementMap,
apiEmissiveMap,
apiEnvironmentMap,
apiLightMap,
apiMetalnessMap,
apiNormalMap,
apiRoughnessMap,
apiSpecularMap,
2017-01-05 19:34:28 +01:00
GameLib.Utils.IdOrNull(this.parentEntity)
2016-12-09 20:32:09 +01:00
);
};
/**
* 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) {
2017-01-05 19:34:28 +01:00
2016-12-09 20:32:09 +01:00
var gameLibMaterial = new GameLib.D3.Material(
graphics,
2017-01-05 19:34:28 +01:00
GameLib.D3.API.Material.FromObjectMaterial(objectMaterial),
imageFactory
2016-12-09 20:32:09 +01:00
);
return gameLibMaterial;
};