Merge pull request #46 from tanema/optimizations

Adding optimizations done by other developers
This commit is contained in:
Tim Anema 2021-11-05 12:33:52 -04:00 committed by GitHub
commit eb964de9f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 71 deletions

View File

@ -40,6 +40,8 @@ local function new(options)
local obj = {} local obj = {}
obj.lights = {} obj.lights = {}
obj.bodies = {} obj.bodies = {}
obj.visibleLights = {}
obj.visibleBodies = {}
obj.post_shader = PostShader() obj.post_shader = PostShader()
obj.l, obj.t, obj.s = 0, 0, 1 obj.l, obj.t, obj.s = 0, 0, 1
@ -83,14 +85,22 @@ function light_world:refreshScreenSize(w, h)
end end
function light_world:update(dt) function light_world:update(dt)
self.visibleBodies = {}
self.visibleLights = {}
for i = 1, #self.bodies do for i = 1, #self.bodies do
self.bodies[i].is_on_screen = self.bodies[i]:inRange(-self.l,-self.t,self.w,self.h,self.s) local body = self.bodies[i]
if self.bodies[i]:isVisible() then body.is_on_screen = body:inRange(-self.l,-self.t,self.w,self.h,self.s)
self.bodies[i]:update(dt) if body:isVisible() then
body:update(dt)
table.insert(self.visibleBodies, body)
end end
end end
for i = 1, #self.lights do for i = 1, #self.lights do
self.lights[i].is_on_screen = self.lights[i]:inRange(self.l,self.t,self.w,self.h,self.s) local light = self.lights[i]
light.is_on_screen = light:inRange(self.l,self.t,self.w,self.h,self.s)
if light.is_on_screen then
table.insert(self.visibleLights, light)
end
end end
end end
@ -112,10 +122,8 @@ function light_world:drawShadows(l,t,w,h,s)
love.graphics.clear() love.graphics.clear()
love.graphics.setCanvas() love.graphics.setCanvas()
util.drawto(self.normalMap, l, t, s, false, function() util.drawto(self.normalMap, l, t, s, false, function()
for i = 1, #self.bodies do for i = 1, #self.visibleBodies do
if self.bodies[i]:isVisible() then self.visibleBodies[i]:drawNormal()
self.bodies[i]:drawNormal()
end
end end
end) end)
@ -125,9 +133,8 @@ function light_world:drawShadows(l,t,w,h,s)
love.graphics.setCanvas( self.shadow_buffer ) love.graphics.setCanvas( self.shadow_buffer )
love.graphics.clear() love.graphics.clear()
love.graphics.setCanvas() love.graphics.setCanvas()
for i = 1, #self.lights do for i = 1, #self.visibleLights do
local light = self.lights[i] local light = self.visibleLights[i]
if light:isVisible() then
-- create shadow map for this light -- create shadow map for this light
love.graphics.setCanvas( self.shadowMap ) love.graphics.setCanvas( self.shadowMap )
love.graphics.clear() love.graphics.clear()
@ -174,7 +181,6 @@ function light_world:drawShadows(l,t,w,h,s)
end end
}) })
end end
end
-- add in ambient color -- add in ambient color
util.drawto(self.shadow_buffer, l, t, s, false, function() util.drawto(self.shadow_buffer, l, t, s, false, function()

View File

@ -1,11 +1,14 @@
local util = {} local util = {}
local tempCanvas
--TODO: the whole stencil/canvas system should be reviewed since it has been changed in a naive way --TODO: the whole stencil/canvas system should be reviewed since it has been changed in a naive way
function util.process(canvas, options) function util.process(canvas, options)
--TODO: now you cannot draw a canvas to itself --TODO: now you cannot draw a canvas to itself
temp = love.graphics.newCanvas() if not tempCanvas then
util.drawCanvasToCanvas(canvas, temp, options) tempCanvas = love.graphics.newCanvas()
util.drawCanvasToCanvas(temp, canvas, options) end
util.drawCanvasToCanvas(canvas, tempCanvas, options)
util.drawCanvasToCanvas(tempCanvas, canvas, options)
end end
function util.drawCanvasToCanvas(canvas, other_canvas, options) function util.drawCanvasToCanvas(canvas, other_canvas, options)