Added simple light culling

This commit is contained in:
Felipe Alfonso 2018-01-30 22:11:51 -03:00
parent d9b04ef2e9
commit 0648161ca5
2 changed files with 52 additions and 5 deletions

View file

@ -1,5 +1,6 @@
var Class = require('../utils/Class');
var Utils = require('../renderer/webgl/Utils');
var LightPipeline = require('../renderer/webgl/pipelines/ForwardDiffuseLightPipeline');
var Light = new Class({
@ -76,6 +77,21 @@ var LightsManager = new Class({
this.lights = [];
this.culledLights = [];
this.ambientColor = { r: 0.1, g: 0.1, b: 0.1 };
this.active = false;
},
enable: function ()
{
this.active = true;
return this;
},
disable: function ()
{
this.active = false;
return this;
},
shutdown: function ()
@ -102,11 +118,27 @@ var LightsManager = new Class({
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;
culledLights.length = 0;
for (var index = 0; index < length; ++index)
for (var index = 0; index < length && culledLights.length < LightPipeline.LIGHT_COUNT; ++index)
{
culledLights.push(lights[index]);
var light = lights[index];
// We'll just use bounding spheres to test
// if lights should be rendered
var dx = cameraCenterX - light.x;
var dy = cameraCenterY - light.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < light.radius + cameraRadius)
{
culledLights.push(lights[index]);
}
}
return culledLights;

View file

@ -33,16 +33,29 @@ var ForwardDiffuseLightPipeline = new Class({
onRender: function (scene, camera)
{
var lightManager = scene.lights;
lightManager.culledLights.length = 0;
if (lightManager.lights.length <= 0 || !lightManager.active)
{
return this; // If not visible lights just passthrough
}
var renderer = this.renderer;
var program = this.program;
var lightManager = scene.lights;
var lights = scene.lights.cull(camera);
var lightCount = Math.min(lights.length, LIGHT_COUNT);
var cameraMatrix = camera.matrix;
var point = {x: 0, y: 0};
var height = renderer.height;
if (lightCount <= 0) return; // If not visible lights just passthrough
for (var index = 0; index < LIGHT_COUNT; ++index)
{
renderer.setFloat1(program, 'uLights[' + index + '].radius', 0); // reset lights
}
if (lightCount <= 0) return this;
renderer.setFloat4(program, 'uCamera', camera.x, camera.y, camera.rotation, camera.zoom);
renderer.setFloat3(program, 'uAmbientLightColor', lightManager.ambientColor.r, lightManager.ambientColor.g, lightManager.ambientColor.b);
@ -57,7 +70,7 @@ var ForwardDiffuseLightPipeline = new Class({
renderer.setFloat1(program, lightName + 'intensity', light.intensity);
renderer.setFloat1(program, lightName + 'radius', light.radius);
}
return this;
},
@ -216,4 +229,6 @@ var ForwardDiffuseLightPipeline = new Class({
});
ForwardDiffuseLightPipeline.LIGHT_COUNT = LIGHT_COUNT;
module.exports = ForwardDiffuseLightPipeline;