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

379 lines
10 KiB
JavaScript

/**
* GameLib.D3.Material.Phong
* @param graphics
* @param apiMaterialPhong
* @constructor
*/
GameLib.D3.Material.Phong = function(
graphics,
apiMaterialPhong
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiMaterialPhong)) {
apiMaterialPhong = {
materialType : GameLib.D3.API.Material.MATERIAL_TYPE_PHONG
};
}
GameLib.D3.API.Material.Phong.call(
this,
apiMaterialPhong,
apiMaterialPhong.alphaMap,
apiMaterialPhong.aoMap,
apiMaterialPhong.aoMapIntensity,
apiMaterialPhong.bumpMap,
apiMaterialPhong.bumpScale,
apiMaterialPhong.color,
apiMaterialPhong.combine,
apiMaterialPhong.displacementMap,
apiMaterialPhong.displacementScale,
apiMaterialPhong.displacementBias,
apiMaterialPhong.emissive,
apiMaterialPhong.emissiveMap,
apiMaterialPhong.emissiveIntensity,
apiMaterialPhong.envMap,
apiMaterialPhong.lightMap,
apiMaterialPhong.lightMapIntensity,
apiMaterialPhong.diffuseMap,
apiMaterialPhong.morphNormals,
apiMaterialPhong.morphTargets,
apiMaterialPhong.normalMap,
apiMaterialPhong.normalScale,
apiMaterialPhong.reflectivity,
apiMaterialPhong.refractionRatio,
apiMaterialPhong.shininess,
apiMaterialPhong.skinning,
apiMaterialPhong.specular,
apiMaterialPhong.specularMap,
apiMaterialPhong.wireframe,
apiMaterialPhong.wireframeLinecap,
apiMaterialPhong.wireframeLinejoin,
apiMaterialPhong.wireframeLinewidth
);
if (this.alphaMap instanceof GameLib.D3.API.Texture) {
this.alphaMap = GameLib.Component.ConstructFromObject(this.alphaMap);
}
if (this.aoMap instanceof GameLib.D3.API.Texture) {
this.aoMap = GameLib.Component.ConstructFromObject(this.aoMap);
}
if (this.bumpMap instanceof GameLib.D3.API.Texture) {
this.bumpMap = GameLib.Component.ConstructFromObject(this.bumpMap);
}
this.color = new GameLib.Color(
this.graphics,
this.color,
this
);
if (this.displacementMap instanceof GameLib.D3.API.Texture) {
this.displacementMap = GameLib.Component.ConstructFromObject(this.displacementMap);
}
this.emissive = new GameLib.Color(
this.graphics,
this.emissive,
this
);
if (this.emissiveMap instanceof GameLib.D3.API.Texture) {
this.emissiveMap = GameLib.Component.ConstructFromObject(this.emissiveMap);
}
if (this.envMap instanceof GameLib.D3.API.Texture) {
this.envMap = GameLib.Component.ConstructFromObject(this.envMap);
}
if (this.lightMap instanceof GameLib.D3.API.Texture) {
this.lightMap = GameLib.Component.ConstructFromObject(this.lightMap);
}
if (this.diffuseMap instanceof GameLib.D3.API.Texture) {
this.diffuseMap = GameLib.Component.ConstructFromObject(this.diffuseMap);
}
if (this.normalMap instanceof GameLib.D3.API.Texture) {
this.normalMap = GameLib.Component.ConstructFromObject(this.normalMap);
}
this.specular = new GameLib.Color(
this.graphics,
this.specular,
this
);
if (this.specularMap instanceof GameLib.D3.API.Texture) {
this.specularMap = GameLib.Component.ConstructFromObject(this.specularMap);
}
GameLib.D3.Material.call(
this,
this.graphics,
this
);
};
GameLib.D3.Material.Phong.prototype = Object.create(GameLib.D3.Material.prototype);
GameLib.D3.Material.Phong.prototype.constructor = GameLib.D3.Material.Phong;
/**
* Creates an instance of our texture object
* @returns {*}
*/
GameLib.D3.Material.Phong.prototype.createInstance = function() {
this.instance = new THREE.MeshPhongMaterial(
{
alphaMap : this.alphaMap ? this.alphaMap.instance : null,
aoMap : this.aoMap ? this.aoMap.instance : null,
aoMapIntensity : this.aoMapIntensity,
bumpMap : this.bumpMap ? this.bumpMap.instance : null,
bumpScale : this.bumpScale,
color : this.color.instance,
combine : this.combine,
displacementMap : this.displacementMap ? this.displacementMap.instance : null,
displacementScale : this.displacementScale,
displacementBias : this.displacementBias,
emissive : this.emissive.instance,
emissiveMap : this.emissiveMap ? this.emissiveMap.instance : null,
emissiveIntensity : this.emissiveIntensity,
envMap : this.envMap ? this.envMap.instance : null,
lightMap : this.lightMap ? this.lightMap.instance : null,
lightMapIntensity : this.lightMapIntensity,
map : this.diffuseMap ? this.diffuseMap.instance : null,
morphNormals : this.morphNormals,
morphTargets : this.morphTargets,
normalMap : this.normalMap ? this.normalMap.instance : null,
normalScale : this.normalScale,
reflectivity : this.reflectivity,
refractionRatio : this.refractionRatio,
shininess : this.shininess,
skinning : this.skinning,
specular : this.specular.instance,
specularMap : this.specularMap ? this.specularMap.instance : null,
wireframe : this.wireframe,
wireframeLinecap : this.wireframeLinecap,
wireframeLinejoin : this.wireframeLinejoin,
wireframeLinewidth : this.wireframeLinewidth
}
);
GameLib.D3.Material.prototype.createInstance.call(this);
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Material.Phong.prototype.updateInstance = function(property) {
if (property === 'alphaMap') {
this.assignTexture('alphaMap', property);
return;
}
if (property === 'aoMap') {
this.assignTexture('aoMap', property);
return;
}
if (property === 'aoMapIntensity') {
this.instance.aoMapIntensity = this.aoMapIntensity;
return;
}
if (property === 'bumpMap') {
this.assignTexture('bumpMap', property);
return;
}
if (property === 'bumpScale') {
this.instance.bumpScale = this.bumpScale;
return;
}
if (property === 'color') {
this.instance.color = this.color.instance;
return;
}
if (property === 'combine') {
this.instance.combine = this.combine;
this.instance.needsUpdate = true;
return;
}
if (property === 'displacementMap') {
this.assignTexture('displacementMap', property);
return;
}
if (property === 'displacementScale') {
this.instance.displacementScale = this.displacementScale;
return;
}
if (property === 'displacementBias') {
this.instance.displacementBias = this.displacementBias;
return;
}
if (property === 'emissive') {
this.instance.emissive = this.emissive.instance;
return;
}
if (property === 'emissiveMap') {
this.assignTexture('emissiveMap', property);
return;
}
if (property === 'emissiveIntensity') {
this.instance.emissiveIntensity = this.emissiveIntensity;
return;
}
if (property === 'envMap') {
this.assignTexture('envMap', property);
return;
}
if (property === 'lightMap') {
this.assignTexture('lightMap', property);
return;
}
if (property === 'lightMapIntensity') {
this.instance.lightMapIntensity = this.lightMapIntensity;
return;
}
if (property === 'diffuseMap') {
this.assignTexture('map', property);
return;
}
if (property === 'morphNormals') {
this.instance.morphNormals = this.morphNormals;
return;
}
if (property === 'morphTargets') {
this.instance.morphTargets = this.morphTargets;
return;
}
if (property === 'normalMap') {
this.assignTexture('normalMap', property);
return;
}
if (property === 'normalScale') {
this.instance.normalScale = this.normalScale;
return;
}
if (property === 'reflectivity') {
this.instance.reflectivity = this.reflectivity;
return;
}
if (property === 'refractionRatio') {
this.instance.refractionRatio = this.refractionRatio;
return;
}
if (property === 'shininess') {
this.instance.shininess = this.shininess;
return;
}
if (property === 'skinning') {
this.instance.skinning = this.skinning;
return;
}
if (property === 'specular') {
this.instance.specular = this.specular.instance;
return;
}
if (property === 'specularMap') {
this.assignTexture('specularMap', property);
return;
}
if (property === 'wireframe') {
this.instance.wireframe = this.wireframe;
return;
}
if (property === 'wireframeLinecap') {
this.instance.wireframeLinecap = this.wireframeLinecap;
return;
}
if (property === 'wireframeLinejoin') {
this.instance.wireframeLinejoin = this.wireframeLinejoin;
return;
}
if (property === 'wireframeLinewidth') {
this.instance.wireframeLinewidth = this.wireframeLinewidth;
return;
}
GameLib.D3.Material.prototype.updateInstance.call(this, property);
};
/**
* Converts a GameLib.D3.Material.Phong to a GameLib.D3.API.Material.Phong
* @returns {GameLib.D3.API.Material.Phong}
*/
GameLib.D3.Material.Phong.prototype.toApiObject = function() {
var apiMaterial = GameLib.D3.Material.prototype.toApiObject.call(this);
var apiMaterialPhong = new GameLib.D3.API.Material.Phong(
apiMaterial,
GameLib.Utils.IdOrNull(this.alphaMap),
GameLib.Utils.IdOrNull(this.aoMap),
this.aoMapIntensity,
GameLib.Utils.IdOrNull(this.bumpMap),
this.bumpScale,
this.color.toApiObject(),
this.combine,
GameLib.Utils.IdOrNull(this.displacementMap),
this.displacementScale,
this.displacementBias,
this.emissive.toApiObject(),
GameLib.Utils.IdOrNull(this.emissiveMap),
this.emissiveIntensity,
GameLib.Utils.IdOrNull(this.envMap),
GameLib.Utils.IdOrNull(this.lightMap),
this.lightMapIntensity,
GameLib.Utils.IdOrNull(this.diffuseMap),
this.morphNormals,
this.morphTargets,
GameLib.Utils.IdOrNull(this.normalMap),
this.normalScale,
this.reflectivity,
this.refractionRatio,
this.shininess,
this.skinning,
this.specular.toApiObject(),
GameLib.Utils.IdOrNull(this.specularMap),
this.wireframe,
this.wireframeLinecap,
this.wireframeLinejoin,
this.wireframeLinewidth
);
return apiMaterialPhong;
};