Skip to content

Commit 143957d

Browse files
committed
You can now set the maxLights value in the Game Config, which controls the total number of lights the Light2D shader can render in a single pass. The default is 10. Be careful about pushing this too far. More lights = less performance. Close phaserjs#4081
1 parent 5bdf9aa commit 143957d

5 files changed

Lines changed: 31 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### New Features
66

7+
* You can now set the `maxLights` value in the Game Config, which controls the total number of lights the Light2D shader can render in a single pass. The default is 10. Be careful about pushing this too far. More lights = less performance. Close #4081 (thanks @FrancescoNegri)
8+
79
### Updates
810

911
* `WebGLRenderer.deleteTexture` will check to see if the texture it is being asked to delete is the currently bound texture or not. If it is, it'll set the blank texture to be bound after deletion. This should stop `RENDER WARNING: there is no texture bound to the unit 0` errors if you destroy a Game Object, such as Text or TileSprite, from an async or timed process (thanks jamespierce)

src/boot/Config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ var ValueToColor = require('../display/color/ValueToColor');
9595
* @property {boolean} [failIfMajorPerformanceCaveat=false] - Let the browser abort creating a WebGL context if it judges performance would be unacceptable.
9696
* @property {string} [powerPreference='default'] - "high-performance", "low-power" or "default". A hint to the browser on how much device power the game might use.
9797
* @property {integer} [batchSize=2000] - The default WebGL batch size.
98+
* @property {integer} [maxLights=10] - The maximum number of lights allowed to be visible within range of a single Camera in the LightManager.
9899
*/
99100

100101
/**
@@ -529,6 +530,11 @@ var Config = new Class({
529530
*/
530531
this.batchSize = GetValue(renderConfig, 'batchSize', 2000);
531532

533+
/**
534+
* @const {integer} Phaser.Boot.Config#maxLights - The maximum number of lights allowed to be visible within range of a single Camera in the LightManager.
535+
*/
536+
this.maxLights = GetValue(renderConfig, 'maxLights', 10);
537+
532538
var bgc = GetValue(config, 'backgroundColor', 0);
533539

534540
/**

src/gameobjects/lights/LightsManager.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
var Class = require('../../utils/Class');
88
var Light = require('./Light');
9-
var LightPipeline = require('../../renderer/webgl/pipelines/ForwardDiffuseLightPipeline');
109
var Utils = require('../../renderer/webgl/Utils');
1110

1211
/**
@@ -84,6 +83,17 @@ var LightsManager = new Class({
8483
* @since 3.0.0
8584
*/
8685
this.active = false;
86+
87+
/**
88+
* The maximum number of lights that a single Camera and the lights shader can process.
89+
* Change this via the `maxLights` property in your game config, as it cannot be changed at runtime.
90+
*
91+
* @name Phaser.GameObjects.LightsManager#maxLights
92+
* @type {integer}
93+
* @readOnly
94+
* @since 3.15.0
95+
*/
96+
this.maxLights = -1;
8797
},
8898

8999
/**
@@ -96,6 +106,11 @@ var LightsManager = new Class({
96106
*/
97107
enable: function ()
98108
{
109+
if (this.maxLights === -1)
110+
{
111+
this.maxLights = this.scene.sys.game.renderer.config.maxLights;
112+
}
113+
99114
this.active = true;
100115

101116
return this;
@@ -142,14 +157,13 @@ var LightsManager = new Class({
142157

143158
culledLights.length = 0;
144159

145-
for (var index = 0; index < length && culledLights.length < LightPipeline.LIGHT_COUNT; ++index)
160+
for (var index = 0; index < length && culledLights.length < this.maxLights; index++)
146161
{
147162
var light = lights[index];
148163

149164
cameraMatrix.transformPoint(light.x, light.y, point);
150165

151-
// We'll just use bounding spheres to test
152-
// if lights should be rendered
166+
// We'll just use bounding spheres to test if lights should be rendered
153167
var dx = cameraCenterX - (point.x - (camera.scrollX * light.scrollFactorX * camera.zoom));
154168
var dy = cameraCenterY - (viewportHeight - (point.y - (camera.scrollY * light.scrollFactorY) * camera.zoom));
155169
var distance = Math.sqrt(dx * dx + dy * dy);

src/renderer/webgl/WebGLRenderer.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ var WebGLRenderer = new Class({
8989
roundPixels: gameConfig.roundPixels,
9090
maxTextures: gameConfig.maxTextures,
9191
maxTextureSize: gameConfig.maxTextureSize,
92-
batchSize: gameConfig.batchSize
92+
batchSize: gameConfig.batchSize,
93+
maxLights: gameConfig.maxLights
9394
};
9495

9596
/**
@@ -562,7 +563,7 @@ var WebGLRenderer = new Class({
562563

563564
this.addPipeline('TextureTintPipeline', new TextureTintPipeline({ game: this.game, renderer: this }));
564565
this.addPipeline('BitmapMaskPipeline', new BitmapMaskPipeline({ game: this.game, renderer: this }));
565-
this.addPipeline('Light2D', new ForwardDiffuseLightPipeline({ game: this.game, renderer: this }));
566+
this.addPipeline('Light2D', new ForwardDiffuseLightPipeline({ game: this.game, renderer: this, maxLights: config.maxLights }));
566567

567568
this.setBlendMode(CONST.BlendModes.NORMAL);
568569

src/renderer/webgl/pipelines/ForwardDiffuseLightPipeline.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ var ForwardDiffuseLightPipeline = new Class({
3333

3434
function ForwardDiffuseLightPipeline (config)
3535
{
36+
LIGHT_COUNT = config.maxLights;
37+
3638
config.fragShader = ShaderSourceFS.replace('%LIGHT_COUNT%', LIGHT_COUNT.toString());
3739

3840
TextureTintPipeline.call(this, config);

0 commit comments

Comments
 (0)