243 lines
6.4 KiB
JavaScript
243 lines
6.4 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.API.Light
|
||
|
* @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.color = new GameLib.D3.Color(
|
||
|
graphics,
|
||
|
this,
|
||
|
this.color,
|
||
|
0.01
|
||
|
);
|
||
|
|
||
|
this.position = new GameLib.Vector3(
|
||
|
graphics,
|
||
|
this,
|
||
|
this.position
|
||
|
);
|
||
|
|
||
|
this.targetPosition = new GameLib.Vector3(
|
||
|
graphics,
|
||
|
this,
|
||
|
this.targetPosition
|
||
|
);
|
||
|
|
||
|
this.scale = new GameLib.Vector3(
|
||
|
graphics,
|
||
|
this,
|
||
|
this.scale
|
||
|
);
|
||
|
|
||
|
this.quaternion = new GameLib.Quaternion(
|
||
|
graphics,
|
||
|
this,
|
||
|
this.quaternion
|
||
|
);
|
||
|
|
||
|
this.instance = this.createInstance();
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* 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) {
|
||
|
|
||
|
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_AMBIENT) {
|
||
|
instance = new this.graphics.instance.AmbientLight(
|
||
|
this.color.instance,
|
||
|
this.intensity
|
||
|
);
|
||
|
}
|
||
|
|
||
|
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) {
|
||
|
instance = new this.graphics.instance.DirectionalLight(
|
||
|
this.color.instance,
|
||
|
this.intensity
|
||
|
);
|
||
|
}
|
||
|
|
||
|
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT) {
|
||
|
instance = new this.graphics.instance.PointLight(
|
||
|
this.color.instance,
|
||
|
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(
|
||
|
this.color.instance,
|
||
|
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.scale.x = this.scale.x;
|
||
|
instance.scale.y = this.scale.y;
|
||
|
instance.scale.z = this.scale.z;
|
||
|
|
||
|
if (instance.target) {
|
||
|
instance.target.position.x = this.targetPosition.x;
|
||
|
instance.target.position.y = this.targetPosition.y;
|
||
|
instance.target.position.z = this.targetPosition.z;
|
||
|
}
|
||
|
|
||
|
instance.quaternion.x = this.quaternion.x;
|
||
|
instance.quaternion.y = this.quaternion.y;
|
||
|
instance.quaternion.z = this.quaternion.z;
|
||
|
instance.quaternion.w = this.quaternion.w;
|
||
|
|
||
|
instance.intensity = this.intensity;
|
||
|
|
||
|
instance.color.set(this.color.toHex());
|
||
|
|
||
|
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);
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Converts a GameLib.D3.Light to a GameLib.D3.API.Light
|
||
|
* @returns {GameLib.D3.API.Light}
|
||
|
*/
|
||
|
GameLib.D3.Light.prototype.toApiLight = function() {
|
||
|
return new GameLib.D3.API.Light(
|
||
|
this.id,
|
||
|
this.lightType,
|
||
|
this.name,
|
||
|
this.color.toApiColor(),
|
||
|
this.intensity,
|
||
|
this.position.toApiVector(),
|
||
|
this.targetPosition.toApiVector(),
|
||
|
this.quaternion.toApiQuaternion(),
|
||
|
this.rotation,
|
||
|
this.scale.toApiVector(),
|
||
|
this.distance,
|
||
|
this.decay,
|
||
|
this.power,
|
||
|
this.angle,
|
||
|
this.penumbra
|
||
|
);
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Returns a new GameLib.D3.Light from a GameLib.D3.API.Light
|
||
|
* @param graphics GameLib.D3.Graphics
|
||
|
* @param apiLight GameLib.D3.API.Light
|
||
|
* @returns {GameLib.D3.API.Light}
|
||
|
*/
|
||
|
GameLib.D3.Light.FromObjectLight = function(graphics, apiLight) {
|
||
|
|
||
|
return new GameLib.D3.Light(
|
||
|
graphics,
|
||
|
new GameLib.D3.API.Light(
|
||
|
apiLight.id,
|
||
|
apiLight.lightType,
|
||
|
apiLight.name,
|
||
|
new GameLib.D3.API.Color(
|
||
|
apiLight.color.r,
|
||
|
apiLight.color.g,
|
||
|
apiLight.color.b,
|
||
|
apiLight.color.a
|
||
|
),
|
||
|
apiLight.intensity,
|
||
|
new GameLib.API.Vector3(
|
||
|
apiLight.position.x,
|
||
|
apiLight.position.y,
|
||
|
apiLight.position.z
|
||
|
),
|
||
|
new GameLib.API.Vector3(
|
||
|
apiLight.targetPosition.x,
|
||
|
apiLight.targetPosition.y,
|
||
|
apiLight.targetPosition.z
|
||
|
),
|
||
|
new GameLib.API.Quaternion(
|
||
|
apiLight.quaternion.x,
|
||
|
apiLight.quaternion.y,
|
||
|
apiLight.quaternion.z,
|
||
|
apiLight.quaternion.w,
|
||
|
new GameLib.API.Vector3(
|
||
|
apiLight.quaternion.axis.x,
|
||
|
apiLight.quaternion.axis.y,
|
||
|
apiLight.quaternion.axis.z
|
||
|
),
|
||
|
apiLight.quaternion.angle
|
||
|
),
|
||
|
new GameLib.API.Vector3(
|
||
|
apiLight.rotation.x,
|
||
|
apiLight.rotation.y,
|
||
|
apiLight.rotation.z
|
||
|
),
|
||
|
new GameLib.API.Vector3(
|
||
|
apiLight.scale.x,
|
||
|
apiLight.scale.y,
|
||
|
apiLight.scale.z
|
||
|
),
|
||
|
apiLight.distance,
|
||
|
apiLight.decay,
|
||
|
apiLight.power,
|
||
|
apiLight.angle,
|
||
|
apiLight.penumbra
|
||
|
)
|
||
|
);
|
||
|
|
||
|
};
|