Skip to content

Commit 0648161

Browse files
committed
Added simple light culling
1 parent d9b04ef commit 0648161

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

src/gameobjects/LightsManager.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Class = require('../utils/Class');
22
var Utils = require('../renderer/webgl/Utils');
3+
var LightPipeline = require('../renderer/webgl/pipelines/ForwardDiffuseLightPipeline');
34

45
var Light = new Class({
56

@@ -76,6 +77,21 @@ var LightsManager = new Class({
7677
this.lights = [];
7778
this.culledLights = [];
7879
this.ambientColor = { r: 0.1, g: 0.1, b: 0.1 };
80+
this.active = false;
81+
},
82+
83+
enable: function ()
84+
{
85+
this.active = true;
86+
87+
return this;
88+
},
89+
90+
disable: function ()
91+
{
92+
this.active = false;
93+
94+
return this;
7995
},
8096

8197
shutdown: function ()
@@ -102,11 +118,27 @@ var LightsManager = new Class({
102118
var lights = this.lights;
103119
var culledLights = this.culledLights;
104120
var length = lights.length;
121+
var cameraCenterX = camera.x + camera.width / 2.0;
122+
var cameraCenterY = camera.y + camera.height / 2.0;
123+
var cameraRadius = (camera.width + camera.height) / 2.0;
124+
105125
culledLights.length = 0;
106126

107-
for (var index = 0; index < length; ++index)
127+
128+
for (var index = 0; index < length && culledLights.length < LightPipeline.LIGHT_COUNT; ++index)
108129
{
109-
culledLights.push(lights[index]);
130+
var light = lights[index];
131+
132+
// We'll just use bounding spheres to test
133+
// if lights should be rendered
134+
var dx = cameraCenterX - light.x;
135+
var dy = cameraCenterY - light.y;
136+
var distance = Math.sqrt(dx * dx + dy * dy);
137+
138+
if (distance < light.radius + cameraRadius)
139+
{
140+
culledLights.push(lights[index]);
141+
}
110142
}
111143

112144
return culledLights;

src/renderer/webgl/pipelines/ForwardDiffuseLightPipeline.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,29 @@ var ForwardDiffuseLightPipeline = new Class({
3333

3434
onRender: function (scene, camera)
3535
{
36+
var lightManager = scene.lights;
37+
38+
lightManager.culledLights.length = 0;
39+
40+
if (lightManager.lights.length <= 0 || !lightManager.active)
41+
{
42+
return this; // If not visible lights just passthrough
43+
}
44+
3645
var renderer = this.renderer;
3746
var program = this.program;
38-
var lightManager = scene.lights;
3947
var lights = scene.lights.cull(camera);
4048
var lightCount = Math.min(lights.length, LIGHT_COUNT);
4149
var cameraMatrix = camera.matrix;
4250
var point = {x: 0, y: 0};
4351
var height = renderer.height;
4452

45-
if (lightCount <= 0) return; // If not visible lights just passthrough
53+
for (var index = 0; index < LIGHT_COUNT; ++index)
54+
{
55+
renderer.setFloat1(program, 'uLights[' + index + '].radius', 0); // reset lights
56+
}
57+
58+
if (lightCount <= 0) return this;
4659

4760
renderer.setFloat4(program, 'uCamera', camera.x, camera.y, camera.rotation, camera.zoom);
4861
renderer.setFloat3(program, 'uAmbientLightColor', lightManager.ambientColor.r, lightManager.ambientColor.g, lightManager.ambientColor.b);
@@ -57,7 +70,7 @@ var ForwardDiffuseLightPipeline = new Class({
5770
renderer.setFloat1(program, lightName + 'intensity', light.intensity);
5871
renderer.setFloat1(program, lightName + 'radius', light.radius);
5972
}
60-
73+
6174
return this;
6275
},
6376

@@ -216,4 +229,6 @@ var ForwardDiffuseLightPipeline = new Class({
216229

217230
});
218231

232+
ForwardDiffuseLightPipeline.LIGHT_COUNT = LIGHT_COUNT;
233+
219234
module.exports = ForwardDiffuseLightPipeline;

0 commit comments

Comments
 (0)