/** * 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 ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); 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 ); 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(); }; GameLib.D3.Light.prototype = Object.create(GameLib.D3.API.Light.prototype); GameLib.D3.Light.prototype.constructor = GameLib.D3.Light; /** * 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, GameLib.Utils.IdOrNull(this.parentEntity) ); }; /** * Returns a new GameLib.D3.Light from a GameLib.D3.API.Light * @param graphics GameLib.D3.Graphics * @param objectLight GameLib.D3.API.Light * @returns {GameLib.D3.Light} */ GameLib.D3.Light.FromObjectLight = function(graphics, objectLight) { return new GameLib.D3.Light( graphics, GameLib.D3.API.Light.FromObjectLight(objectLight) ); };