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

642 lines
19 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();
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
this.specular = new GameLib.D3.Color(
2016-12-02 13:00:56 +01:00
graphics,
this,
this.specular
);
2016-12-02 16:03:03 +01:00
this.color = new GameLib.D3.Color(
2016-12-02 13:00:56 +01:00
graphics,
this,
this.color
);
2016-12-02 16:03:03 +01:00
this.emissive = new GameLib.D3.Color(
2016-12-02 13:00:56 +01:00
graphics,
this,
this.emissive
);
2017-01-05 19:34:28 +01:00
this.alphaMap = new GameLib.D3.Texture(
this.graphics,
this.alphaMap,
this,
'alphaMap',
imageFactory
);
this.aoMap = new GameLib.D3.Texture(
this.graphics,
this.aoMap,
this,
'aoMap',
imageFactory
);
this.bumpMap = new GameLib.D3.Texture(
this.graphics,
this.bumpMap,
this,
'bumpMap',
imageFactory
);
this.diffuseMap = new GameLib.D3.Texture(
this.graphics,
this.diffuseMap,
this,
'map',
imageFactory
);
this.displacementMap = new GameLib.D3.Texture(
this.graphics,
this.displacementMap,
this,
'displacementMap',
imageFactory
);
this.emissiveMap = new GameLib.D3.Texture(
this.graphics,
this.emissiveMap,
this,
'emissiveMap',
imageFactory
);
this.environmentMap = new GameLib.D3.Texture(
this.graphics,
this.environmentMap,
this,
'envMap',
imageFactory
);
this.lightMap = new GameLib.D3.Texture(
this.graphics,
this.lightMap,
this,
'lightMap',
imageFactory
);
this.metalnessMap = new GameLib.D3.Texture(
this.graphics,
this.metalnessMap,
this,
'metalnessMap',
imageFactory
);
this.normalMap = new GameLib.D3.Texture(
this.graphics,
this.normalMap,
this,
'normalMap',
imageFactory
);
this.roughnessMap = new GameLib.D3.Texture(
this.graphics,
this.roughnessMap,
this,
'roughnessMap',
imageFactory
);
this.specularMap = new GameLib.D3.Texture(
this.graphics,
this.specularMap,
this,
'specularMap',
imageFactory
);
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') {
instance.alphaMap = this.alphaMap.instance;
2016-12-05 16:40:26 +01:00
}
2017-01-05 19:34:28 +01:00
else if (property == 'aoMap') {
instance.aoMap = this.aoMap.instance;
}
else if (property == 'bumpMap') {
instance.bumpMap = this.bumpMap.instance;
}
else if (property == 'map') {
instance.map = this.diffuseMap.instance;
}
else if (property == 'displacementMap') {
instance.displacementMap = this.displacementMap.instance;
}
else if (property == 'emissiveMap') {
instance.emissiveMap = this.emissiveMap.instance;
}
else if (property == 'envMap') {
instance.envMap = this.environmentMap.instance;
}
else if (property == 'lightMap') {
instance.lightMap = this.lightMap.instance;
}
else if (property == 'metalnessMap') {
instance.metalnessMap = this.metalnessMap.instance;
}
else if (property == 'normalMap') {
instance.normalMap = this.normalMap.instance;
}
else if (property == 'roughnessMap') {
instance.roughnessMap = this.roughnessMap.instance;
}
else if (property == '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) {
2016-12-02 16:03:03 +01:00
instance[property].copy(this[property])
} else {
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() {
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,
this.alphaMap.toApiTexture(),
this.aoMap.toApiTexture(),
this.bumpMap.toApiTexture(),
this.diffuseMap.toApiTexture(),
this.displacementMap.toApiTexture(),
this.emissiveMap.toApiTexture(),
this.environmentMap.toApiTexture(),
this.lightMap.toApiTexture(),
this.metalnessMap.toApiTexture(),
this.normalMap.toApiTexture(),
this.roughnessMap.toApiTexture(),
this.specularMap.toApiTexture(),
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;
};