phaser/src/gameobjects/lights/PointLight.js

193 lines
4.3 KiB
JavaScript
Raw Normal View History

2020-10-13 17:17:30 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../../utils/Class');
var Components = require('../components');
var GameObject = require('../GameObject');
2020-12-01 17:23:42 +00:00
var GetCalcMatrix = require('../GetCalcMatrix');
2020-10-13 17:17:30 +00:00
var IntegerToRGB = require('../../display/color/IntegerToRGB');
var RGB = require('../../display/RGB');
/**
* @classdesc
*
2020-12-01 17:23:42 +00:00
* TODO
2020-10-13 17:17:30 +00:00
*
* @class PointLight
* @extends Phaser.GameObjects.GameObject
* @memberof Phaser.GameObjects
* @constructor
* @since 3.50.0
*
* @extends Phaser.GameObjects.Components.AlphaSingle
* @extends Phaser.GameObjects.Components.BlendMode
* @extends Phaser.GameObjects.Components.Depth
* @extends Phaser.GameObjects.Components.GetBounds
* @extends Phaser.GameObjects.Components.Mask
* @extends Phaser.GameObjects.Components.Pipeline
* @extends Phaser.GameObjects.Components.ScrollFactor
* @extends Phaser.GameObjects.Components.Transform
* @extends Phaser.GameObjects.Components.Visible
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object belongs. A Game Object can only belong to one Scene at a time.
* @param {number} x - The horizontal position of this Game Object in the world.
* @param {number} y - The vertical position of this Game Object in the world.
*/
var PointLight = new Class({
Extends: GameObject,
Mixins: [
Components.AlphaSingle,
Components.BlendMode,
Components.Depth,
Components.GetBounds,
Components.Mask,
Components.Pipeline,
Components.ScrollFactor,
Components.Transform,
Components.Visible
],
initialize:
2020-12-01 17:23:42 +00:00
function PointLight (scene, x, y, color, radius, intensity, falloff)
2020-10-13 17:17:30 +00:00
{
if (color === undefined) { color = 0xffffff; }
if (radius === undefined) { radius = 128; }
2020-12-01 17:23:42 +00:00
if (intensity === undefined) { intensity = 1; }
if (falloff === undefined) { falloff = radius * 2; }
2020-10-13 17:17:30 +00:00
GameObject.call(this, scene, 'PointLight');
2020-12-01 17:23:42 +00:00
this.initPipeline('PointLightPipeline');
2020-10-13 17:17:30 +00:00
this.setPosition(x, y);
var rgb = IntegerToRGB(color);
this.color = new RGB(
rgb.r / 255,
rgb.g / 255,
rgb.b / 255
);
this.intensity = intensity;
2020-12-01 17:23:42 +00:00
this.attenuation = 0.1;
2020-10-13 17:17:30 +00:00
// read only:
2020-12-01 17:23:42 +00:00
this.width = falloff * 2;
this.height = falloff * 2;
2020-10-13 17:17:30 +00:00
// private
2020-12-01 17:23:42 +00:00
this.radius = radius;
this._falloff = falloff;
2020-10-13 17:17:30 +00:00
},
2020-12-01 17:23:42 +00:00
// radius: {
// get: function ()
// {
// return this._radius;
// },
// set: function (value)
// {
// this._radius = value;
// }
// },
falloff: {
2020-10-13 17:17:30 +00:00
get: function ()
{
2020-12-01 17:23:42 +00:00
return this._falloff;
2020-10-13 17:17:30 +00:00
},
set: function (value)
{
2020-12-01 17:23:42 +00:00
this._falloff = value;
2020-10-13 17:17:30 +00:00
this.width = value * 2;
this.height = value * 2;
}
},
originX: {
get: function ()
{
return 0.5;
}
},
originY: {
get: function ()
{
return 0.5;
}
},
displayOriginX: {
get: function ()
{
return this._radius;
}
},
displayOriginY: {
get: function ()
{
return this._radius;
}
},
2020-12-01 17:23:42 +00:00
renderWebGL: function (renderer, src, camera, parentMatrix)
2020-10-13 17:17:30 +00:00
{
2020-11-20 13:05:32 +00:00
var pipeline = renderer.pipelines.set(src.pipeline);
2020-10-13 17:17:30 +00:00
2020-12-01 17:23:42 +00:00
var calcMatrix = GetCalcMatrix(src, camera, parentMatrix).calc;
2020-10-13 17:17:30 +00:00
var width = src.width;
var height = src.height;
2020-12-01 17:23:42 +00:00
var x = -src._falloff;
var y = -src._falloff;
2020-10-13 17:17:30 +00:00
var xw = x + width;
var yh = y + height;
var lightX = calcMatrix.getX(0, 0);
var lightY = calcMatrix.getY(0, 0);
var tx0 = calcMatrix.getX(x, y);
var ty0 = calcMatrix.getY(x, y);
var tx1 = calcMatrix.getX(x, yh);
var ty1 = calcMatrix.getY(x, yh);
var tx2 = calcMatrix.getX(xw, yh);
var ty2 = calcMatrix.getY(xw, yh);
var tx3 = calcMatrix.getX(xw, y);
var ty3 = calcMatrix.getY(xw, y);
2020-12-01 17:23:42 +00:00
pipeline.batchPointLight(src, camera, tx0, ty0, tx1, ty1, tx2, ty2, tx3, ty3, lightX, lightY);
2020-10-13 17:17:30 +00:00
}
});
module.exports = PointLight;