r3-legacy/src/game-lib-light.js

240 lines
5.9 KiB
JavaScript

/**
* Light Superset - The apiLight properties get moved into the Light object itself, and then the instance is created
* @param graphics GameLib.D3.Graphics
* @param apiLight GameLib.D3.Light.API
* @constructor
*/
GameLib.D3.Light = function Light(
graphics,
apiLight
) {
for (var property in apiLight) {
if (apiLight.hasOwnProperty(property)) {
this[property] = apiLight[property];
}
}
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.instance = this.createInstance();
};
/**
* Raw Light API object - should always correspond with the Light Schema
* @param id
* @param lightType
* @param name
* @param color
* @param intensity
* @param position
* @param targetPosition
* @param quaternion
* @param rotation
* @param scale
* @param distance
* @param decay
* @param power
* @param angle
* @param penumbra
* @constructor
*/
GameLib.D3.Light.API = function(
id,
lightType,
name,
color,
intensity,
position,
targetPosition,
quaternion,
rotation,
scale,
distance,
decay,
power,
angle,
penumbra
) {
if (GameLib.D3.Utils.UndefinedOrNull(id)) {
id = GameLib.D3.Tools.RandomId();
}
this.id = id;
if (GameLib.D3.Utils.UndefinedOrNull(lightType)) {
lightType = GameLib.D3.Light.LIGHT_TYPE_AMBIENT;
}
this.lightType = lightType;
if (GameLib.D3.Utils.UndefinedOrNull(name)) {
name = 'Light (' + lightType + ')';
}
this.name = name;
if (GameLib.D3.Utils.UndefinedOrNull(color)) {
color = new GameLib.D3.Color(1,1,1,1);
}
this.color = color;
if (GameLib.D3.Utils.UndefinedOrNull(intensity)) {
intensity = 1;
}
this.intensity = intensity;
if (typeof position == 'undefined') {
position = new GameLib.D3.Vector3(0,10,0);
}
this.position = position;
if (typeof targetPosition == 'undefined') {
targetPosition = new GameLib.D3.Vector3(0,0,0);
}
this.targetPosition = targetPosition;
if (typeof quaternion == 'undefined'){
quaternion = new GameLib.D3.Vector4();
}
this.quaternion = quaternion;
if (typeof rotation == 'undefined'){
rotation = new GameLib.D3.Vector3(0,0,0);
}
this.rotation = rotation;
if (typeof scale == 'undefined'){
scale = new GameLib.D3.Vector3(1,1,1);
}
this.scale = scale;
if (typeof distance == 'undefined'){
distance = 0;
}
this.distance = distance;
if (typeof decay == 'undefined'){
decay = 1;
}
this.decay = decay;
if (typeof power == 'undefined'){
power = 4 * Math.PI;
}
this.power = power;
if (typeof angle == 'undefined'){
angle = Math.PI / 3;
}
this.angle = angle;
if (typeof penumbra == 'undefined'){
penumbra = 0;
}
this.penumbra = penumbra;
};
/**
* Light Types
* @type {number}
*/
GameLib.D3.Light.LIGHT_TYPE_AMBIENT = 0x1;
GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL = 0x2;
GameLib.D3.Light.LIGHT_TYPE_POINT = 0x3;
GameLib.D3.Light.LIGHT_TYPE_SPOT = 0x4;
/**
* Creates a light instance
* @returns {*}
*/
GameLib.D3.Light.prototype.createInstance = function(update) {
var instance = null;
if (update) {
instance = this.instance;
}
if ((!instance) ||
(instance && instance.lightType != this.lightType)) {
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_AMBIENT) {
instance = new this.graphics.instance.AmbientLight(
new this.graphics.instance.Color(
this.color.r,
this.color.g,
this.color.b
),
this.intensity
);
}
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) {
instance = new this.graphics.instance.DirectionalLight(
new this.graphics.instance.Color(
this.color.r,
this.color.g,
this.color.b
),
this.intensity
);
}
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT) {
instance = new this.graphics.instance.PointLight(
new this.graphics.instance.Color(
this.color.r,
this.color.g,
this.color.b
),
this.intensity
);
instance.distance = this.distance;
instance.decay = this.decay;
}
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_SPOT ) {
instance = new this.graphics.instance.SpotLight(
new this.graphics.instance.Color(
this.color.r,
this.color.g,
this.color.b
),
this.intensity
);
instance.distance = this.distance;
instance.angle = this.angle;
instance.penumbra = this.penumbra;
instance.decay = this.decay;
}
}
if (!instance) {
console.warn('Do not support lights of type : ' + this.lightType + ' - is your DB out of date?');
throw new Error('Do not support lights of type : ' + this.lightType + ' - is your DB out of date?');
}
instance.gameLibObject = this;
instance.name = this.name;
instance.position.x = this.position.x;
instance.position.y = this.position.y;
instance.position.z = this.position.z;
instance.rotation.x = this.rotation.x;
instance.rotation.y = this.rotation.y;
instance.rotation.z = this.rotation.z;
return instance;
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Light.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
GameLib.D3.Light.prototype.clone = function() {
return _.cloneDeep(this);
};