some visible checking optimizations

This commit is contained in:
Tim Anema 2014-12-13 17:19:44 -05:00
parent 8ddb4138b1
commit 33fec6bfd4
4 changed files with 53 additions and 70 deletions

View File

@ -30,6 +30,7 @@ function love.load()
-- create shadow bodys -- create shadow bodys
circleTest = lightWorld:newCircle(256, 256, 16) circleTest = lightWorld:newCircle(256, 256, 16)
cx, cy = circleTest:getPosition()
rectangleTest = lightWorld:newRectangle(512, 512, 64, 64) rectangleTest = lightWorld:newRectangle(512, 512, 64, 64)
local px, py, pw, ph = 100, 200, 20, 50 local px, py, pw, ph = 100, 200, 20, 50
polygonTest = lightWorld:newPolygon( polygonTest = lightWorld:newPolygon(
@ -156,7 +157,6 @@ function love.draw()
love.graphics.setColor(255, 255, 255) love.graphics.setColor(255, 255, 255)
love.graphics.rectangle("fill", -x/scale, -y/scale, love.graphics.getWidth()/scale, love.graphics.getHeight()/scale) love.graphics.rectangle("fill", -x/scale, -y/scale, love.graphics.getWidth()/scale, love.graphics.getHeight()/scale)
love.graphics.setColor(63, 255, 127) love.graphics.setColor(63, 255, 127)
local cx, cy = circleTest:getPosition()
love.graphics.circle("fill", cx, cy, circleTest:getRadius()) love.graphics.circle("fill", cx, cy, circleTest:getRadius())
love.graphics.polygon("fill", rectangleTest:getPoints()) love.graphics.polygon("fill", rectangleTest:getPoints())
love.graphics.polygon("fill", polygonTest:getPoints()) love.graphics.polygon("fill", polygonTest:getPoints())

View File

@ -28,6 +28,7 @@ function body:init(id, type, ...)
self.castsNoShadow = false self.castsNoShadow = false
self.visible = true self.visible = true
self.is_on_screen = true
if self.type == "circle" then if self.type == "circle" then
self.x = args[1] or 0 self.x = args[1] or 0
@ -85,44 +86,27 @@ function body:init(id, type, ...)
self:generateNormalMapFlat("top") self:generateNormalMapFlat("top")
self.reflective = true self.reflective = true
elseif self.type == "refraction" then elseif self.type == "refraction" then
self:initNormal(...) self.x = args[2] or 0
self.y = args[3] or 0
self:setNormalMap(args[1], args[4], args[5])
self.width = args[4] or self.normalWidth
self.height = args[5] or self.normalHeight
self.ox = self.width * 0.5
self.oy = self.height * 0.5
self.refraction = true self.refraction = true
elseif self.type == "reflection" then elseif self.type == "reflection" then
self:initNormal(...) self.x = args[2] or 0
self.y = args[3] or 0
self:setNormalMap(args[1], args[4], args[5])
self.width = args[4] or self.normalWidth
self.height = args[5] or self.normalHeight
self.ox = self.width * 0.5
self.oy = self.height * 0.5
self.reflection = true self.reflection = true
end end
self.old_x, self.old_y = self.x, self.y self.old_x, self.old_y = self.x, self.y
end end
--use for refraction and reflection because they are both just a normal map
function body:initNormal(...)
local args = {...}
self.normal = args[1]
self.x = args[2] or 0
self.y = args[3] or 0
if self.normal then
self.normalWidth = self.normal:getWidth()
self.normalHeight = self.normal:getHeight()
self.width = args[4] or self.normalWidth
self.height = args[5] or self.normalHeight
self.nx = self.normalWidth * 0.5
self.ny = self.normalHeight * 0.5
self.normal:setWrap("repeat", "repeat")
self.normalVert = {
{0.0, 0.0, 0.0, 0.0},
{self.width, 0.0, 1.0, 0.0},
{self.width, self.height, 1.0, 1.0},
{0.0, self.height, 0.0, 1.0}
}
self.normalMesh = love.graphics.newMesh(self.normalVert, self.normal, "fan")
else
self.width = args[4] or 64
self.height = args[5] or 64
end
self.ox = self.width * 0.5
self.oy = self.height * 0.5
end
-- refresh -- refresh
function body:refresh() function body:refresh()
if self.shadowType == "rectangle" then if self.shadowType == "rectangle" then
@ -496,6 +480,10 @@ function body:setShadowType(type, ...)
end end
end end
function body:isVisible()
return self.visible and self.is_on_screen
end
function body:isInLightRange(light) function body:isInLightRange(light)
local l, t, w = light.x - light.range, light.y - light.range, light.range*2 local l, t, w = light.x - light.range, light.y - light.range, light.range*2
return self:isInRange(l,t,w,w,1) return self:isInRange(l,t,w,w,1)

View File

@ -39,7 +39,7 @@ light_world.reflectionShader = love.graphics.newShader(_PACKAGE.."shaders/refl
function light_world:init(options) function light_world:init(options)
self.lights = {} self.lights = {}
self.body = {} self.bodies = {}
self.post_shader = PostShader() self.post_shader = PostShader()
self.l, self.t, self.s = 0, 0, 1 self.l, self.t, self.s = 0, 0, 1
@ -84,10 +84,11 @@ function light_world:refreshScreenSize(w, h)
end end
function light_world:update(dt) function light_world:update(dt)
for i = 1, #self.body do for i = 1, #self.bodies do
if self.body[i]:isInRange(-self.l,-self.t,self.w,self.h,self.s) and if self.bodies[i]:isInRange(-self.l,-self.t,self.w,self.h,self.s) then
self.body[i].type == 'animation' then self.bodies[i]:update(dt)
self.body[i]:update(dt) else
self.bodies[i].is_on_screen = false
end end
end end
end end
@ -120,9 +121,9 @@ function light_world:drawNormalShading(l,t,w,h,s)
-- create normal map -- create normal map
self.normalMap:clear() self.normalMap:clear()
util.drawto(self.normalMap, l, t, s, function() util.drawto(self.normalMap, l, t, s, function()
for i = 1, #self.body do for i = 1, #self.bodies do
if self.body[i]:isInRange(-l,-t,w,h,s) then if self.bodies[i]:isVisible() then
self.body[i]:drawNormal() self.bodies[i]:drawNormal()
end end
end end
end) end)
@ -134,9 +135,9 @@ function light_world:drawNormalShading(l,t,w,h,s)
-- create shadow map for this light -- create shadow map for this light
self.shadowMap:clear() self.shadowMap:clear()
util.drawto(self.shadowMap, l, t, s, function() util.drawto(self.shadowMap, l, t, s, function()
for k = 1, #self.body do for k = 1, #self.bodies do
if self.body[k]:isInLightRange(light) and self.body[k]:isInRange(-l,-t,w,h,s) then if self.bodies[k]:isInLightRange(light) and self.bodies[k]:isVisible() then
self.body[k]:drawShadow(light) self.bodies[k]:drawShadow(light)
end end
end end
end) end)
@ -172,9 +173,9 @@ end
-- draw material -- draw material
function light_world:drawMaterial(l,t,w,h,s) function light_world:drawMaterial(l,t,w,h,s)
for i = 1, #self.body do for i = 1, #self.bodies do
if self.body[i]:isInRange(-l,-t,w,h,s) then if self.bodies[i]:isVisible() then
self.body[i]:drawMaterial() self.bodies[i]:drawMaterial()
end end
end end
end end
@ -195,10 +196,10 @@ function light_world:drawGlow(l,t,w,h,s)
-- create glow map -- create glow map
self.glowMap:clear(0, 0, 0) self.glowMap:clear(0, 0, 0)
util.drawto(self.glowMap, l, t, s, function() util.drawto(self.glowMap, l, t, s, function()
for i = 1, #self.body do for i = 1, #self.bodies do
if self.body[i]:isInRange(-l,-t,w,h,s) and self.body[i].glowStrength > 0.0 then if self.bodies[i]:isVisible() and self.bodies[i].glowStrength > 0.0 then
has_glow = true has_glow = true
self.body[i]:drawGlow() self.bodies[i]:drawGlow()
end end
end end
end) end)
@ -213,9 +214,9 @@ function light_world:drawRefraction(l,t,w,h,s)
-- create refraction map -- create refraction map
self.refractionMap:clear() self.refractionMap:clear()
util.drawto(self.refractionMap, l, t, s, function() util.drawto(self.refractionMap, l, t, s, function()
for i = 1, #self.body do for i = 1, #self.bodies do
if self.body[i]:isInRange(-l,-t,w,h,s) then if self.bodies[i]:isVisible() then
self.body[i]:drawRefraction() self.bodies[i]:drawRefraction()
end end
end end
end) end)
@ -231,9 +232,9 @@ function light_world:drawReflection(l,t,w,h,s)
-- create reflection map -- create reflection map
self.reflectionMap:clear(0, 0, 0) self.reflectionMap:clear(0, 0, 0)
util.drawto(self.reflectionMap, l, t, s, function() util.drawto(self.reflectionMap, l, t, s, function()
for i = 1, #self.body do for i = 1, #self.bodies do
if self.body[i]:isInRange(-l,-t,w,h,s) then if self.bodies[i]:isVisible() then
self.body[i]:drawReflection() self.bodies[i]:drawReflection()
end end
end end
end) end)
@ -262,15 +263,15 @@ end
function light_world:setScale(s) self.s = s end function light_world:setScale(s) self.s = s end
function light_world:clearLights() self.lights = {} end function light_world:clearLights() self.lights = {} end
function light_world:clearBodies() self.body = {} end function light_world:clearBodies() self.bodies = {} end
function light_world:setAmbientColor(red, green, blue) self.ambient = {red, green, blue} end function light_world:setAmbientColor(red, green, blue) self.ambient = {red, green, blue} end
function light_world:setShadowBlur(blur) self.shadowBlur = blur end function light_world:setShadowBlur(blur) self.shadowBlur = blur end
function light_world:setGlowStrength(strength) self.glowBlur = strength end function light_world:setGlowStrength(strength) self.glowBlur = strength end
function light_world:setRefractionStrength(strength) self.refractionStrength = strength end function light_world:setRefractionStrength(strength) self.refractionStrength = strength end
function light_world:setReflectionStrength(strength) self.reflectionStrength = strength end function light_world:setReflectionStrength(strength) self.reflectionStrength = strength end
function light_world:setReflectionVisibility(visibility) self.reflectionVisibility = visibility end function light_world:setReflectionVisibility(visibility) self.reflectionVisibility = visibility end
function light_world:getBodyCount() return #self.body end function light_world:getBodyCount() return #self.bodies end
function light_world:getBody(n) return self.body[n] end function light_world:getBody(n) return self.bodies[n] end
function light_world:getLightCount() return #self.lights end function light_world:getLightCount() return #self.lights end
function light_world:getLight(n) return self.lights[n] end function light_world:getLight(n) return self.lights[n] end
function light_world:newRectangle(...) return self:newBody("rectangle", ...) end function light_world:newRectangle(...) return self:newBody("rectangle", ...) end
@ -290,16 +291,16 @@ end
-- new body -- new body
function light_world:newBody(type, ...) function light_world:newBody(type, ...)
local id = #self.body + 1 local id = #self.bodies + 1
self.body[id] = Body(id, type, ...) self.bodies[id] = Body(id, type, ...)
return self.body[#self.body] return self.bodies[#self.bodies]
end end
function light_world:remove(to_kill) function light_world:remove(to_kill)
if to_kill:is_a(Body) then if to_kill:is_a(Body) then
for i = 1, #self.body do for i = 1, #self.bodies do
if self.body[i] == to_kill then if self.bodies[i] == to_kill then
table.remove(self.body, i) table.remove(self.bodies, i)
return true return true
end end
end end

View File

@ -146,12 +146,6 @@ function post_shader:drawShader(shaderName, canvas, args)
current_arg = current_arg + 3 current_arg = current_arg + 3
elseif def == "imgBuffer" then elseif def == "imgBuffer" then
effect[1]:send("imgBuffer", canvas) effect[1]:send("imgBuffer", canvas)
elseif def ~= "screen" and def ~= "textureSize" and def ~= "inputSize" and def ~= "outputSize" then
local value = args[current_arg]
if value ~= nil then
effect[1]:send(def, value)
end
current_arg = current_arg + 1
end end
end end