516 lines
17 KiB
JavaScript
516 lines
17 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 Material(
|
||
|
graphics,
|
||
|
apiMaterial
|
||
|
) {
|
||
|
for (var property in apiMaterial) {
|
||
|
if (apiMaterial.hasOwnProperty(property)) {
|
||
|
this[property] = apiMaterial[property];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
this.graphics = graphics;
|
||
|
this.graphics.isNotThreeThrow();
|
||
|
|
||
|
this.specular = new GameLib.D3.Color(
|
||
|
graphics,
|
||
|
this,
|
||
|
this.specular
|
||
|
);
|
||
|
|
||
|
this.color = new GameLib.D3.Color(
|
||
|
graphics,
|
||
|
this,
|
||
|
this.color
|
||
|
);
|
||
|
|
||
|
this.emissive = new GameLib.D3.Color(
|
||
|
graphics,
|
||
|
this,
|
||
|
this.emissive
|
||
|
);
|
||
|
|
||
|
this.instance = this.createInstance();
|
||
|
|
||
|
this.needsUpdate = false;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* 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 = "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";
|
||
|
|
||
|
/**
|
||
|
* Material instance
|
||
|
* @returns {*}
|
||
|
*/
|
||
|
GameLib.D3.Material.prototype.createInstance = function(update) {
|
||
|
|
||
|
var instance = null;
|
||
|
|
||
|
if (update) {
|
||
|
instance = this.instance;
|
||
|
}
|
||
|
|
||
|
if (!instance) {
|
||
|
|
||
|
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,
|
||
|
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
|
||
|
});
|
||
|
|
||
|
} 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,
|
||
|
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
|
||
|
});
|
||
|
} 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,
|
||
|
color: this.color.instance,
|
||
|
size: this.pointSize,
|
||
|
sizeAttenuation: this.pointSizeAttenuation,
|
||
|
vertexColors: GameLib.D3.Material.TYPE_NO_COLORS,
|
||
|
fog: this.fog
|
||
|
});
|
||
|
|
||
|
} 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
|
||
|
});
|
||
|
|
||
|
} else {
|
||
|
console.log("material type is not implemented yet: " + this.materialType + " - material indexes could be screwed up");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (update) {
|
||
|
|
||
|
for (var property in instance) {
|
||
|
if (
|
||
|
instance.hasOwnProperty(property) &&
|
||
|
this.hasOwnProperty(property)
|
||
|
) {
|
||
|
if (property == 'size') {
|
||
|
instance[property] = this.pointSize;
|
||
|
}
|
||
|
|
||
|
if (property == 'sizeAttenuation') {
|
||
|
instance[property] = this.pointSizeAttenuation;
|
||
|
}
|
||
|
|
||
|
if (instance[property] instanceof THREE.Color) {
|
||
|
instance[property].copy(this[property])
|
||
|
} else {
|
||
|
instance[property] = this[property];
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
instance.needsUpdate = true;
|
||
|
}
|
||
|
|
||
|
return instance;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Updates the instance with the current state
|
||
|
*/
|
||
|
GameLib.D3.Material.prototype.updateInstance = function() {
|
||
|
this.instance = this.createInstance(true);
|
||
|
};
|
||
|
|
||
|
GameLib.D3.Material.prototype.clone = function() {
|
||
|
return _.cloneDeep(this);
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* 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;
|
||
|
};
|