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
|
|
|
|
) {
|
2016-12-02 16:03:03 +01:00
|
|
|
this.graphics = graphics;
|
|
|
|
this.graphics.isNotThreeThrow();
|
|
|
|
|
2017-01-17 13:24:45 +01:00
|
|
|
if (GameLib.Utils.UndefinedOrNull(apiLight)) {
|
|
|
|
apiLight = {};
|
|
|
|
}
|
|
|
|
|
2017-05-11 04:48:02 +02:00
|
|
|
if (apiLight instanceof GameLib.D3.Light) {
|
|
|
|
return apiLight;
|
|
|
|
}
|
|
|
|
|
2017-01-02 17:05:40 +01:00
|
|
|
GameLib.D3.API.Light.call(
|
|
|
|
this,
|
|
|
|
apiLight.id,
|
|
|
|
apiLight.lightType,
|
|
|
|
apiLight.name,
|
|
|
|
apiLight.color,
|
|
|
|
apiLight.intensity,
|
|
|
|
apiLight.position,
|
|
|
|
apiLight.targetPosition,
|
|
|
|
apiLight.quaternion,
|
|
|
|
apiLight.rotation,
|
|
|
|
apiLight.scale,
|
|
|
|
apiLight.distance,
|
|
|
|
apiLight.decay,
|
|
|
|
apiLight.power,
|
|
|
|
apiLight.angle,
|
|
|
|
apiLight.penumbra,
|
|
|
|
apiLight.parentEntity
|
|
|
|
);
|
|
|
|
|
2017-01-06 16:53:53 +01:00
|
|
|
this.color = new GameLib.Color(
|
2016-12-02 13:00:56 +01:00
|
|
|
graphics,
|
|
|
|
this.color,
|
2017-01-17 13:24:45 +01:00
|
|
|
this
|
2016-12-02 13:00:56 +01:00
|
|
|
);
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
this.position = new GameLib.Vector3(
|
2016-12-02 13:00:56 +01:00
|
|
|
graphics,
|
2017-01-17 13:24:45 +01:00
|
|
|
this.position,
|
|
|
|
this
|
2016-12-02 13:00:56 +01:00
|
|
|
);
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
this.targetPosition = new GameLib.Vector3(
|
2016-12-02 13:00:56 +01:00
|
|
|
graphics,
|
2017-01-17 13:24:45 +01:00
|
|
|
this.targetPosition,
|
|
|
|
this
|
2016-12-02 13:00:56 +01:00
|
|
|
);
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
this.scale = new GameLib.Vector3(
|
2016-12-02 13:00:56 +01:00
|
|
|
graphics,
|
2017-01-17 13:24:45 +01:00
|
|
|
this.scale,
|
|
|
|
this
|
2016-12-02 13:00:56 +01:00
|
|
|
);
|
|
|
|
|
2016-12-15 14:53:39 +01:00
|
|
|
this.quaternion = new GameLib.Quaternion(
|
2016-12-02 13:00:56 +01:00
|
|
|
graphics,
|
2017-01-17 13:24:45 +01:00
|
|
|
this.quaternion,
|
|
|
|
this
|
2016-12-02 13:00:56 +01:00
|
|
|
);
|
|
|
|
|
2016-11-17 18:31:41 +01:00
|
|
|
this.instance = this.createInstance();
|
|
|
|
};
|
|
|
|
|
2017-01-02 17:05:40 +01:00
|
|
|
GameLib.D3.Light.prototype = Object.create(GameLib.D3.API.Light.prototype);
|
|
|
|
GameLib.D3.Light.prototype.constructor = GameLib.D3.Light;
|
|
|
|
|
2016-11-17 18:31:41 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
2017-01-31 11:38:47 +01:00
|
|
|
|
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
|
|
|
|
2017-01-31 11:38:47 +01:00
|
|
|
if (
|
2017-05-11 04:48:02 +02:00
|
|
|
this.lightType === GameLib.D3.Light.LIGHT_TYPE_AMBIENT ||
|
|
|
|
this.lightType === 'AmbientLight'
|
2017-01-31 11:38:47 +01:00
|
|
|
) {
|
|
|
|
instance = new THREE.AmbientLight(
|
2016-12-02 16:03:03 +01:00
|
|
|
this.color.instance,
|
2016-11-18 16:00:13 +01:00
|
|
|
this.intensity
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-31 11:38:47 +01:00
|
|
|
if (
|
2017-05-11 04:48:02 +02:00
|
|
|
this.lightType === GameLib.D3.Light.LIGHT_TYPE_DIRECTIONAL ||
|
|
|
|
this.lightType === 'DirectionalLight'
|
2017-01-31 11:38:47 +01:00
|
|
|
) {
|
|
|
|
instance = new THREE.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
|
|
|
|
2017-01-31 11:38:47 +01:00
|
|
|
if (
|
2017-05-11 04:48:02 +02:00
|
|
|
this.lightType === GameLib.D3.Light.LIGHT_TYPE_POINT ||
|
|
|
|
this.lightType === 'PointLight'
|
2017-01-31 11:38:47 +01:00
|
|
|
) {
|
|
|
|
instance = new THREE.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;
|
|
|
|
}
|
|
|
|
|
2017-01-31 11:38:47 +01:00
|
|
|
if (
|
2017-05-11 04:48:02 +02:00
|
|
|
this.lightType === GameLib.D3.Light.LIGHT_TYPE_SPOT ||
|
|
|
|
this.lightType === 'SpotLight'
|
2017-01-31 11:38:47 +01:00
|
|
|
) {
|
|
|
|
instance = new THREE.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;
|
|
|
|
|
2016-12-06 19:34:22 +01:00
|
|
|
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;
|
|
|
|
|
2017-01-31 11:38:47 +01:00
|
|
|
if (instance.target) {
|
|
|
|
instance.target.position.x = this.targetPosition.x;
|
|
|
|
instance.target.position.y = this.targetPosition.y;
|
|
|
|
instance.target.position.z = this.targetPosition.z;
|
|
|
|
}
|
2016-12-06 19:34:22 +01:00
|
|
|
|
|
|
|
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,
|
2017-01-02 17:05:40 +01:00
|
|
|
this.penumbra,
|
|
|
|
GameLib.Utils.IdOrNull(this.parentEntity)
|
2016-12-09 20:32:09 +01:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a new GameLib.D3.Light from a GameLib.D3.API.Light
|
|
|
|
* @param graphics GameLib.D3.Graphics
|
2017-01-02 17:05:40 +01:00
|
|
|
* @param objectLight GameLib.D3.API.Light
|
|
|
|
* @returns {GameLib.D3.Light}
|
2016-12-09 20:32:09 +01:00
|
|
|
*/
|
2017-01-02 17:05:40 +01:00
|
|
|
GameLib.D3.Light.FromObjectLight = function(graphics, objectLight) {
|
2016-12-09 20:32:09 +01:00
|
|
|
|
|
|
|
return new GameLib.D3.Light(
|
|
|
|
graphics,
|
2017-01-05 19:34:28 +01:00
|
|
|
GameLib.D3.API.Light.FromObjectLight(objectLight)
|
2016-12-09 20:32:09 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
};
|