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

243 lines
6.4 KiB
JavaScript
Raw Normal View History

2016-10-14 12:32:53 +02:00
/**
2016-11-17 18:31:41 +01:00
* Light Superset - The apiLight properties get moved into the Light object itself, and then the instance is created
* @param graphics GameLib.D3.Graphics
2016-11-29 12:54:25 +01:00
* @param apiLight GameLib.D3.API.Light
2016-11-17 18:31:41 +01:00
* @constructor
*/
2016-11-21 16:08:39 +01:00
GameLib.D3.Light = function Light(
2016-11-17 18:31:41 +01:00
graphics,
apiLight
) {
for (var property in apiLight) {
if (apiLight.hasOwnProperty(property)) {
this[property] = apiLight[property];
}
}
2016-12-02 16:03:03 +01:00
this.graphics = graphics;
this.graphics.isNotThreeThrow();
this.color = new GameLib.D3.Color(
2016-12-02 13:00:56 +01:00
graphics,
this,
this.color,
0.01
);
2016-12-15 14:53:39 +01:00
this.position = new GameLib.Vector3(
2016-12-02 13:00:56 +01:00
graphics,
this,
this.position
);
2016-12-15 14:53:39 +01:00
this.targetPosition = new GameLib.Vector3(
2016-12-02 13:00:56 +01:00
graphics,
this,
this.targetPosition
);
2016-12-15 14:53:39 +01:00
this.scale = new GameLib.Vector3(
2016-12-02 13:00:56 +01:00
graphics,
this,
this.scale
);
2016-12-15 14:53:39 +01:00
this.quaternion = new GameLib.Quaternion(
2016-12-02 13:00:56 +01:00
graphics,
this,
this.quaternion
);
2016-11-17 18:31:41 +01:00
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 {*}
*/
2016-11-18 16:00:13 +01:00
GameLib.D3.Light.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-11-17 18:31:41 +01:00
}
2016-12-02 13:00:56 +01:00
if (!instance) {
2016-11-17 18:31:41 +01:00
2016-11-18 16:00:13 +01:00
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_AMBIENT) {
instance = new this.graphics.instance.AmbientLight(
2016-12-02 16:03:03 +01:00
this.color.instance,
2016-11-18 16:00:13 +01:00
this.intensity
);
}
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL) {
instance = new this.graphics.instance.DirectionalLight(
2016-12-02 16:03:03 +01:00
this.color.instance,
2016-11-18 16:00:13 +01:00
this.intensity
);
}
2016-11-17 18:31:41 +01:00
2016-11-18 16:00:13 +01:00
if (this.lightType == GameLib.D3.Light.LIGHT_TYPE_POINT) {
instance = new this.graphics.instance.PointLight(
2016-12-02 16:03:03 +01:00
this.color.instance,
2016-11-18 16:00:13 +01:00
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(
2016-12-02 16:03:03 +01:00
this.color.instance,
2016-11-18 16:00:13 +01:00
this.intensity
);
instance.distance = this.distance;
instance.angle = this.angle;
instance.penumbra = this.penumbra;
instance.decay = this.decay;
}
2016-11-17 18:31:41 +01:00
}
2016-11-21 16:08:39 +01:00
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?');
}
2016-11-17 18:31:41 +01:00
instance.gameLibObject = this;
2016-11-18 16:00:13 +01:00
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;
2016-12-02 13:00:56 +01:00
instance.intensity = this.intensity;
instance.color.set(this.color.toHex());
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.Light.prototype.updateInstance = function() {
this.instance = this.createInstance(true);
};
2016-11-21 16:08:39 +01:00
GameLib.D3.Light.prototype.clone = function() {
return _.cloneDeep(this);
};
2016-12-09 20:32:09 +01:00
/**
* 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,
2016-12-15 14:53:39 +01:00
new GameLib.API.Vector3(
2016-12-09 20:32:09 +01:00
apiLight.position.x,
apiLight.position.y,
apiLight.position.z
),
2016-12-15 14:53:39 +01:00
new GameLib.API.Vector3(
2016-12-09 20:32:09 +01:00
apiLight.targetPosition.x,
apiLight.targetPosition.y,
apiLight.targetPosition.z
),
2016-12-15 14:53:39 +01:00
new GameLib.API.Quaternion(
2016-12-09 20:32:09 +01:00
apiLight.quaternion.x,
apiLight.quaternion.y,
apiLight.quaternion.z,
apiLight.quaternion.w,
2016-12-15 14:53:39 +01:00
new GameLib.API.Vector3(
2016-12-09 20:32:09 +01:00
apiLight.quaternion.axis.x,
apiLight.quaternion.axis.y,
apiLight.quaternion.axis.z
),
apiLight.quaternion.angle
),
2016-12-15 14:53:39 +01:00
new GameLib.API.Vector3(
2016-12-09 20:32:09 +01:00
apiLight.rotation.x,
apiLight.rotation.y,
apiLight.rotation.z
),
2016-12-15 14:53:39 +01:00
new GameLib.API.Vector3(
2016-12-09 20:32:09 +01:00
apiLight.scale.x,
apiLight.scale.y,
apiLight.scale.z
),
apiLight.distance,
apiLight.decay,
apiLight.power,
apiLight.angle,
apiLight.penumbra
)
);
};