Skip to content

Commit 84968f5

Browse files
committed
Update ForwardDiffuseLightPipeline.js
* The pipeline will no longer look-up and set all of the light uniforms unless the `Light` is dirty. * The pipeline will no longer reset all of the lights unless the quantity of lights has changed.
1 parent 3b9d115 commit 84968f5

1 file changed

Lines changed: 39 additions & 13 deletions

File tree

src/renderer/webgl/pipelines/ForwardDiffuseLightPipeline.js

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ var ForwardDiffuseLightPipeline = new Class({
5353
0, 1, 0,
5454
0, 0, 1
5555
]);
56+
57+
/**
58+
* Stores the previous number of lights rendered.
59+
*
60+
* @name Phaser.Renderer.WebGL.Pipelines.ForwardDiffuseLightPipeline#lightCount
61+
* @type {number}
62+
* @since 3.25.0
63+
*/
64+
this.lightCount = 0;
5665
},
5766

5867
/**
@@ -117,28 +126,45 @@ var ForwardDiffuseLightPipeline = new Class({
117126
var cameraMatrix = camera.matrix;
118127
var point = {x: 0, y: 0};
119128
var height = renderer.height;
120-
var index;
129+
var i;
130+
131+
if (lightCount !== this.lightCount)
132+
{
133+
for (i = 0; i < LIGHT_COUNT; i++)
134+
{
135+
// Reset lights
136+
renderer.setFloat1(program, 'uLights[' + i + '].radius', 0);
137+
}
138+
139+
this.lightCount = lightCount;
140+
}
121141

122-
for (index = 0; index < LIGHT_COUNT; ++index)
142+
if (camera.dirty)
123143
{
124-
// Reset lights
125-
renderer.setFloat1(program, 'uLights[' + index + '].radius', 0);
144+
renderer.setFloat4(program, 'uCamera', camera.x, camera.y, camera.rotation, camera.zoom);
126145
}
127146

128-
renderer.setFloat4(program, 'uCamera', camera.x, camera.y, camera.rotation, camera.zoom);
147+
// TODO - Only if dirty! and cache the location
129148
renderer.setFloat3(program, 'uAmbientLightColor', lightManager.ambientColor.r, lightManager.ambientColor.g, lightManager.ambientColor.b);
130149

131-
for (index = 0; index < lightCount; ++index)
150+
for (i = 0; i < lightCount; i++)
132151
{
133-
var light = lights[index];
134-
var lightName = 'uLights[' + index + '].';
152+
var light = lights[i];
135153

136-
cameraMatrix.transformPoint(light.x, light.y, point);
154+
if (light.dirty)
155+
{
156+
var lightName = 'uLights[' + i + '].';
137157

138-
renderer.setFloat2(program, lightName + 'position', point.x - (camera.scrollX * light.scrollFactorX * camera.zoom), height - (point.y - (camera.scrollY * light.scrollFactorY) * camera.zoom));
139-
renderer.setFloat3(program, lightName + 'color', light.r, light.g, light.b);
140-
renderer.setFloat1(program, lightName + 'intensity', light.intensity);
141-
renderer.setFloat1(program, lightName + 'radius', light.radius);
158+
cameraMatrix.transformPoint(light.x, light.y, point);
159+
160+
// TODO - Cache the uniform locations!!!
161+
renderer.setFloat2(program, lightName + 'position', point.x - (camera.scrollX * light.scrollFactorX * camera.zoom), height - (point.y - (camera.scrollY * light.scrollFactorY) * camera.zoom));
162+
renderer.setFloat3(program, lightName + 'color', light.r, light.g, light.b);
163+
renderer.setFloat1(program, lightName + 'intensity', light.intensity);
164+
renderer.setFloat1(program, lightName + 'radius', light.radius);
165+
166+
light.dirty = false;
167+
}
142168
}
143169

144170
this.currentNormalMapRotation = null;

0 commit comments

Comments
 (0)