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

516 lines
17 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
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,
apiMaterial
) {
for (var property in apiMaterial) {
if (apiMaterial.hasOwnProperty(property)) {
this[property] = apiMaterial[property];
}
}
2016-12-02 16:03:03 +01:00
this.graphics = graphics;
this.graphics.isNotThreeThrow();
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
);
2016-11-17 18:31:41 +01:00
this.instance = this.createInstance();
2016-11-18 16:00:13 +01:00
this.needsUpdate = false;
2016-11-17 18:31:41 +01:00
};
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) {
instance = new this.graphics.instance.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,
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) {
instance = new this.graphics.instance.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,
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
}
if (update) {
for (var property in instance) {
if (
2016-12-05 16:40:26 +01:00
instance.hasOwnProperty(property) &&
this.hasOwnProperty(property)
) {
2016-12-05 16:40:26 +01:00
if (property == 'size') {
instance[property] = this.pointSize;
}
if (property == 'sizeAttenuation') {
instance[property] = this.pointSizeAttenuation;
}
2016-12-02 16:03:03 +01:00
if (instance[property] instanceof THREE.Color) {
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.maps.toApiTextureMaps(),
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,
this.envMapIntensity
);
};
/**
* 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) {
var gameLibMaterial = new GameLib.D3.Material(
graphics,
new GameLib.D3.API.Material(
objectMaterial.id,
objectMaterial.materialType,
objectMaterial.name,
objectMaterial.opacity,
objectMaterial.side,
objectMaterial.transparent,
GameLib.D3.API.TextureMapTemplate(),
new GameLib.D3.API.Color(
objectMaterial.specular.r,
objectMaterial.specular.g,
objectMaterial.specular.b,
objectMaterial.specular.a
),
objectMaterial.lightMapIntensity,
objectMaterial.aoMapIntensity,
new GameLib.D3.API.Color(
objectMaterial.color.r,
objectMaterial.color.g,
objectMaterial.color.b,
objectMaterial.color.a
),
new GameLib.D3.API.Color(
objectMaterial.emissive.r,
objectMaterial.emissive.g,
objectMaterial.emissive.b,
objectMaterial.emissive.a
),
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
)
);
var objectMaps = objectMaterial.maps;
var gameLibTextureMap = new GameLib.D3.TextureMapTemplate(graphics);
for (var map in gameLibTextureMap) {
if (gameLibTextureMap.hasOwnProperty(map) && objectMaps[map] && objectMaps[map].texture && objectMaps[map].texture.imagePath) {
gameLibTextureMap[map].texture = GameLib.D3.Texture.FromObjectTexture(graphics, objectMaps[map].texture, gameLibMaterial, map, gameLibTextureMap, imageFactory);
}
}
gameLibMaterial.maps = gameLibTextureMap;
return gameLibMaterial;
};