forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLightsManager.js
More file actions
363 lines (314 loc) · 9.45 KB
/
Copy pathLightsManager.js
File metadata and controls
363 lines (314 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/**
* @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 Light = require('./Light');
var Utils = require('../../renderer/webgl/Utils');
/**
* @callback LightForEach
*
* @param {Phaser.GameObjects.Light} light - The Light.
*/
/**
* @classdesc
* Manages Lights for a Scene.
*
* Affects the rendering of Game Objects using the `Light2D` pipeline.
*
* @class LightsManager
* @memberof Phaser.GameObjects
* @constructor
* @since 3.0.0
*/
var LightsManager = new Class({
initialize:
function LightsManager ()
{
/**
* The pool of Lights.
*
* Used to recycle removed Lights for a more efficient use of memory.
*
* @name Phaser.GameObjects.LightsManager#lightPool
* @type {Phaser.GameObjects.Light[]}
* @default []
* @since 3.0.0
*/
this.lightPool = [];
/**
* The Lights in the Scene.
*
* @name Phaser.GameObjects.LightsManager#lights
* @type {Phaser.GameObjects.Light[]}
* @default []
* @since 3.0.0
*/
this.lights = [];
/**
* Lights that have been culled from a Camera's viewport.
*
* Lights in this list will not be rendered.
*
* @name Phaser.GameObjects.LightsManager#culledLights
* @type {Phaser.GameObjects.Light[]}
* @default []
* @since 3.0.0
*/
this.culledLights = [];
/**
* The ambient color.
*
* @name Phaser.GameObjects.LightsManager#ambientColor
* @type {{ r: number, g: number, b: number }}
* @since 3.0.0
*/
this.ambientColor = { r: 0.1, g: 0.1, b: 0.1 };
/**
* Whether the Lights Manager is enabled.
*
* @name Phaser.GameObjects.LightsManager#active
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.active = false;
/**
* The maximum number of lights that a single Camera and the lights shader can process.
* Change this via the `maxLights` property in your game config, as it cannot be changed at runtime.
*
* @name Phaser.GameObjects.LightsManager#maxLights
* @type {integer}
* @readonly
* @since 3.15.0
*/
this.maxLights = -1;
},
/**
* Enable the Lights Manager.
*
* @method Phaser.GameObjects.LightsManager#enable
* @since 3.0.0
*
* @return {Phaser.GameObjects.LightsManager} This Lights Manager object.
*/
enable: function ()
{
if (this.maxLights === -1)
{
this.maxLights = this.scene.sys.game.renderer.config.maxLights;
}
this.active = true;
return this;
},
/**
* Disable the Lights Manager.
*
* @method Phaser.GameObjects.LightsManager#disable
* @since 3.0.0
*
* @return {Phaser.GameObjects.LightsManager} This Lights Manager object.
*/
disable: function ()
{
this.active = false;
return this;
},
/**
* Cull any Lights that aren't visible to the given Camera.
*
* Culling Lights improves performance by ensuring that only Lights within a Camera's viewport are rendered.
*
* @method Phaser.GameObjects.LightsManager#cull
* @since 3.0.0
*
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera to cull Lights for.
*
* @return {Phaser.GameObjects.Light[]} The culled Lights.
*/
cull: function (camera)
{
var lights = this.lights;
var culledLights = this.culledLights;
var length = lights.length;
var cameraCenterX = camera.x + camera.width / 2.0;
var cameraCenterY = camera.y + camera.height / 2.0;
var cameraRadius = (camera.width + camera.height) / 2.0;
var point = { x: 0, y: 0 };
var cameraMatrix = camera.matrix;
var viewportHeight = this.systems.game.renderer.height;
culledLights.length = 0;
for (var i = 0; i < length && culledLights.length < this.maxLights; i++)
{
var light = lights[i];
cameraMatrix.transformPoint(light.x, light.y, point);
// We'll just use bounding spheres to test if lights should be rendered
var dx = cameraCenterX - (point.x - (camera.scrollX * light.scrollFactorX * camera.zoom));
var dy = cameraCenterY - (viewportHeight - (point.y - (camera.scrollY * light.scrollFactorY) * camera.zoom));
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < light.radius + cameraRadius)
{
culledLights.push(lights[i]);
}
}
return culledLights;
},
/**
* Iterate over each Light with a callback.
*
* @method Phaser.GameObjects.LightsManager#forEachLight
* @since 3.0.0
*
* @param {LightForEach} callback - The callback that is called with each Light.
*
* @return {Phaser.GameObjects.LightsManager} This Lights Manager object.
*/
forEachLight: function (callback)
{
if (!callback)
{
return;
}
var lights = this.lights;
var length = lights.length;
for (var i = 0; i < length; i++)
{
callback(lights[i]);
}
return this;
},
/**
* Set the ambient light color.
*
* @method Phaser.GameObjects.LightsManager#setAmbientColor
* @since 3.0.0
*
* @param {number} rgb - The integer RGB color of the ambient light.
*
* @return {Phaser.GameObjects.LightsManager} This Lights Manager object.
*/
setAmbientColor: function (rgb)
{
var color = Utils.getFloatsFromUintRGB(rgb);
this.ambientColor.r = color[0];
this.ambientColor.g = color[1];
this.ambientColor.b = color[2];
return this;
},
/**
* Returns the maximum number of Lights allowed to appear at once.
*
* @method Phaser.GameObjects.LightsManager#getMaxVisibleLights
* @since 3.0.0
*
* @return {integer} The maximum number of Lights allowed to appear at once.
*/
getMaxVisibleLights: function ()
{
return 10;
},
/**
* Get the number of Lights managed by this Lights Manager.
*
* @method Phaser.GameObjects.LightsManager#getLightCount
* @since 3.0.0
*
* @return {integer} The number of Lights managed by this Lights Manager.
*/
getLightCount: function ()
{
return this.lights.length;
},
/**
* Add a Light.
*
* @method Phaser.GameObjects.LightsManager#addLight
* @since 3.0.0
*
* @param {number} [x=0] - The horizontal position of the Light.
* @param {number} [y=0] - The vertical position of the Light.
* @param {number} [radius=100] - The radius of the Light.
* @param {number} [rgb=0xffffff] - The integer RGB color of the light.
* @param {number} [intensity=1] - The intensity of the Light.
*
* @return {Phaser.GameObjects.Light} The Light that was added.
*/
addLight: function (x, y, radius, rgb, intensity)
{
if (x === undefined) { x = 0; }
if (y === undefined) { y = 0; }
if (radius === undefined) { radius = 100; }
if (rgb === undefined) { rgb = 0xffffff; }
if (intensity === undefined) { intensity = 1; }
var color = null;
var light = null;
color = Utils.getFloatsFromUintRGB(rgb);
light = null;
var pool = this.lightPool;
if (pool.length > 0)
{
light = pool.pop();
light.set(x, y, radius, color[0], color[1], color[2], intensity);
}
else
{
light = new Light(x, y, radius, color[0], color[1], color[2], intensity);
}
this.lights.push(light);
return light;
},
/**
* Remove a Light.
*
* @method Phaser.GameObjects.LightsManager#removeLight
* @since 3.0.0
*
* @param {Phaser.GameObjects.Light} light - The Light to remove.
*
* @return {Phaser.GameObjects.LightsManager} This Lights Manager object.
*/
removeLight: function (light)
{
var index = this.lights.indexOf(light);
if (index >= 0)
{
this.lightPool.push(light);
this.lights.splice(index, 1);
}
return this;
},
/**
* Shut down the Lights Manager.
*
* Recycles all active Lights into the Light pool, resets ambient light color and clears the lists of Lights and
* culled Lights.
*
* @method Phaser.GameObjects.LightsManager#shutdown
* @since 3.0.0
*/
shutdown: function ()
{
while (this.lights.length > 0)
{
this.lightPool.push(this.lights.pop());
}
this.ambientColor = { r: 0.1, g: 0.1, b: 0.1 };
this.culledLights.length = 0;
this.lights.length = 0;
},
/**
* Destroy the Lights Manager.
*
* Cleans up all references by calling {@link Phaser.GameObjects.LightsManager#shutdown}.
*
* @method Phaser.GameObjects.LightsManager#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.shutdown();
this.lightPool.length = 0;
}
});
module.exports = LightsManager;