Skip to content

Commit f45ab89

Browse files
committed
The ForwardDiffuseLightPipeline, used by the Lights system, now sets a flag if the Scene doesn't contain any lights. All of the Game Objects now check this flag and don't even bother adding themselves to the batch if there are no lights in the Scene, as they'd never render anyway. This also avoids the ghost-image problem if you swap Scenes to a new Scene with the Light Manager enabled, but no actual lights defined. Fix phaserjs#3707
1 parent 0fdb7fc commit f45ab89

3 files changed

Lines changed: 71 additions & 9 deletions

File tree

CHANGELOG.md

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

1010
### Updates
1111

12+
* The ForwardDiffuseLightPipeline, used by the Lights system, now sets a flag if the Scene doesn't contain any lights. All of the Game Objects now check this flag and don't even bother adding themselves to the batch if there are no lights in the Scene, as they'd never render anyway. This also avoids the ghost-image problem if you swap Scenes to a new Scene with the Light Manager enabled, but no actual lights defined. Fix #3707 (thanks @samvieten)
13+
1214
### Bug Fixes
1315

1416
* The Canvas RenderTexture drawImage method incorrectly set the values of the frame, causing them to appear wrongly scaled in the canvas renderer. Fix #3710 (thanks @saqsun)

src/renderer/webgl/WebGLPipeline.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,16 @@ var WebGLPipeline = new Class({
226226
* @since 3.1.0
227227
*/
228228
this.flushLocked = false;
229+
230+
/**
231+
* Indicates if the current pipeline is active or not for this frame only.
232+
* Reset in the onRender method.
233+
*
234+
* @name Phaser.Renderer.WebGL.WebGLPipeline#active
235+
* @type {boolean}
236+
* @since 3.10.0
237+
*/
238+
this.active = false;
229239
},
230240

231241
/**

src/renderer/webgl/pipelines/ForwardDiffuseLightPipeline.js

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,44 +75,49 @@ var ForwardDiffuseLightPipeline = new Class({
7575
*/
7676
onRender: function (scene, camera)
7777
{
78+
this.active = false;
79+
7880
var lightManager = scene.sys.lights;
7981

80-
if (!lightManager)
82+
if (!lightManager || lightManager.lights.length <= 0 || !lightManager.active)
8183
{
84+
// Passthru
8285
return this;
8386
}
8487

85-
lightManager.culledLights.length = 0;
88+
var lights = lightManager.cull(camera);
89+
var lightCount = Math.min(lights.length, LIGHT_COUNT);
8690

87-
if (lightManager.lights.length <= 0 || !lightManager.active)
91+
if (lightCount === 0)
8892
{
89-
return this; // If not visible lights just passthrough
93+
return this;
9094
}
9195

96+
this.active = true;
97+
9298
var renderer = this.renderer;
9399
var program = this.program;
94-
var lights = lightManager.cull(camera);
95-
var lightCount = Math.min(lights.length, LIGHT_COUNT);
96100
var cameraMatrix = camera.matrix;
97101
var point = {x: 0, y: 0};
98102
var height = renderer.height;
99103
var index;
100104

101105
for (index = 0; index < LIGHT_COUNT; ++index)
102106
{
103-
renderer.setFloat1(program, 'uLights[' + index + '].radius', 0); // reset lights
107+
// Reset lights
108+
renderer.setFloat1(program, 'uLights[' + index + '].radius', 0);
104109
}
105110

106-
if (lightCount <= 0) { return this; }
107-
108111
renderer.setFloat4(program, 'uCamera', camera.x, camera.y, camera.rotation, camera.zoom);
109112
renderer.setFloat3(program, 'uAmbientLightColor', lightManager.ambientColor.r, lightManager.ambientColor.g, lightManager.ambientColor.b);
110113

111114
for (index = 0; index < lightCount; ++index)
112115
{
113116
var light = lights[index];
114117
var lightName = 'uLights[' + index + '].';
118+
115119
cameraMatrix.transformPoint(light.x, light.y, point);
120+
116121
renderer.setFloat2(program, lightName + 'position', point.x - (camera.scrollX * light.scrollFactorX * camera.zoom), height - (point.y - (camera.scrollY * light.scrollFactorY) * camera.zoom));
117122
renderer.setFloat3(program, lightName + 'color', light.r, light.g, light.b);
118123
renderer.setFloat1(program, lightName + 'intensity', light.intensity);
@@ -135,6 +140,11 @@ var ForwardDiffuseLightPipeline = new Class({
135140
*/
136141
drawStaticTilemapLayer: function (tilemap, camera, parentTransformMatrix)
137142
{
143+
if (!this.active)
144+
{
145+
return;
146+
}
147+
138148
var normalTexture = tilemap.tileset.image.dataSource[0];
139149

140150
if (normalTexture)
@@ -163,6 +173,11 @@ var ForwardDiffuseLightPipeline = new Class({
163173
*/
164174
drawEmitterManager: function (emitterManager, camera, parentTransformMatrix)
165175
{
176+
if (!this.active)
177+
{
178+
return;
179+
}
180+
166181
var normalTexture = emitterManager.texture.dataSource[emitterManager.frame.sourceIndex];
167182

168183
if (normalTexture)
@@ -191,6 +206,11 @@ var ForwardDiffuseLightPipeline = new Class({
191206
*/
192207
drawBlitter: function (blitter, camera, parentTransformMatrix)
193208
{
209+
if (!this.active)
210+
{
211+
return;
212+
}
213+
194214
var normalTexture = blitter.texture.dataSource[blitter.frame.sourceIndex];
195215

196216
if (normalTexture)
@@ -219,6 +239,11 @@ var ForwardDiffuseLightPipeline = new Class({
219239
*/
220240
batchSprite: function (sprite, camera, parentTransformMatrix)
221241
{
242+
if (!this.active)
243+
{
244+
return;
245+
}
246+
222247
var normalTexture = sprite.texture.dataSource[sprite.frame.sourceIndex];
223248

224249
if (normalTexture)
@@ -247,6 +272,11 @@ var ForwardDiffuseLightPipeline = new Class({
247272
*/
248273
batchMesh: function (mesh, camera, parentTransformMatrix)
249274
{
275+
if (!this.active)
276+
{
277+
return;
278+
}
279+
250280
var normalTexture = mesh.texture.dataSource[mesh.frame.sourceIndex];
251281

252282
if (normalTexture)
@@ -276,6 +306,11 @@ var ForwardDiffuseLightPipeline = new Class({
276306
*/
277307
batchBitmapText: function (bitmapText, camera, parentTransformMatrix)
278308
{
309+
if (!this.active)
310+
{
311+
return;
312+
}
313+
279314
var normalTexture = bitmapText.texture.dataSource[bitmapText.frame.sourceIndex];
280315

281316
if (normalTexture)
@@ -332,6 +367,11 @@ var ForwardDiffuseLightPipeline = new Class({
332367
*/
333368
batchText: function (text, camera, parentTransformMatrix)
334369
{
370+
if (!this.active)
371+
{
372+
return;
373+
}
374+
335375
var normalTexture = text.texture.dataSource[text.frame.sourceIndex];
336376

337377
if (normalTexture)
@@ -360,6 +400,11 @@ var ForwardDiffuseLightPipeline = new Class({
360400
*/
361401
batchDynamicTilemapLayer: function (tilemapLayer, camera, parentTransformMatrix)
362402
{
403+
if (!this.active)
404+
{
405+
return;
406+
}
407+
363408
var normalTexture = tilemapLayer.tileset.image.dataSource[0];
364409

365410
if (normalTexture)
@@ -388,6 +433,11 @@ var ForwardDiffuseLightPipeline = new Class({
388433
*/
389434
batchTileSprite: function (tileSprite, camera, parentTransformMatrix)
390435
{
436+
if (!this.active)
437+
{
438+
return;
439+
}
440+
391441
var normalTexture = tileSprite.texture.dataSource[tileSprite.frame.sourceIndex];
392442

393443
if (normalTexture)

0 commit comments

Comments
 (0)