mirror of
https://github.com/tanema/light_world.lua.git
synced 2024-12-24 20:24:19 +00:00
some visible checking optimizations
This commit is contained in:
parent
8ddb4138b1
commit
33fec6bfd4
@ -30,6 +30,7 @@ function love.load()
|
||||
|
||||
-- create shadow bodys
|
||||
circleTest = lightWorld:newCircle(256, 256, 16)
|
||||
cx, cy = circleTest:getPosition()
|
||||
rectangleTest = lightWorld:newRectangle(512, 512, 64, 64)
|
||||
local px, py, pw, ph = 100, 200, 20, 50
|
||||
polygonTest = lightWorld:newPolygon(
|
||||
@ -156,7 +157,6 @@ function love.draw()
|
||||
love.graphics.setColor(255, 255, 255)
|
||||
love.graphics.rectangle("fill", -x/scale, -y/scale, love.graphics.getWidth()/scale, love.graphics.getHeight()/scale)
|
||||
love.graphics.setColor(63, 255, 127)
|
||||
local cx, cy = circleTest:getPosition()
|
||||
love.graphics.circle("fill", cx, cy, circleTest:getRadius())
|
||||
love.graphics.polygon("fill", rectangleTest:getPoints())
|
||||
love.graphics.polygon("fill", polygonTest:getPoints())
|
||||
|
50
lib/body.lua
50
lib/body.lua
@ -28,6 +28,7 @@ function body:init(id, type, ...)
|
||||
|
||||
self.castsNoShadow = false
|
||||
self.visible = true
|
||||
self.is_on_screen = true
|
||||
|
||||
if self.type == "circle" then
|
||||
self.x = args[1] or 0
|
||||
@ -85,44 +86,27 @@ function body:init(id, type, ...)
|
||||
self:generateNormalMapFlat("top")
|
||||
self.reflective = true
|
||||
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
|
||||
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
|
||||
end
|
||||
self.old_x, self.old_y = self.x, self.y
|
||||
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
|
||||
function body:refresh()
|
||||
if self.shadowType == "rectangle" then
|
||||
@ -496,6 +480,10 @@ function body:setShadowType(type, ...)
|
||||
end
|
||||
end
|
||||
|
||||
function body:isVisible()
|
||||
return self.visible and self.is_on_screen
|
||||
end
|
||||
|
||||
function body:isInLightRange(light)
|
||||
local l, t, w = light.x - light.range, light.y - light.range, light.range*2
|
||||
return self:isInRange(l,t,w,w,1)
|
||||
|
65
lib/init.lua
65
lib/init.lua
@ -39,7 +39,7 @@ light_world.reflectionShader = love.graphics.newShader(_PACKAGE.."shaders/refl
|
||||
|
||||
function light_world:init(options)
|
||||
self.lights = {}
|
||||
self.body = {}
|
||||
self.bodies = {}
|
||||
self.post_shader = PostShader()
|
||||
|
||||
self.l, self.t, self.s = 0, 0, 1
|
||||
@ -84,10 +84,11 @@ function light_world:refreshScreenSize(w, h)
|
||||
end
|
||||
|
||||
function light_world:update(dt)
|
||||
for i = 1, #self.body do
|
||||
if self.body[i]:isInRange(-self.l,-self.t,self.w,self.h,self.s) and
|
||||
self.body[i].type == 'animation' then
|
||||
self.body[i]:update(dt)
|
||||
for i = 1, #self.bodies do
|
||||
if self.bodies[i]:isInRange(-self.l,-self.t,self.w,self.h,self.s) then
|
||||
self.bodies[i]:update(dt)
|
||||
else
|
||||
self.bodies[i].is_on_screen = false
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -120,9 +121,9 @@ function light_world:drawNormalShading(l,t,w,h,s)
|
||||
-- create normal map
|
||||
self.normalMap:clear()
|
||||
util.drawto(self.normalMap, l, t, s, function()
|
||||
for i = 1, #self.body do
|
||||
if self.body[i]:isInRange(-l,-t,w,h,s) then
|
||||
self.body[i]:drawNormal()
|
||||
for i = 1, #self.bodies do
|
||||
if self.bodies[i]:isVisible() then
|
||||
self.bodies[i]:drawNormal()
|
||||
end
|
||||
end
|
||||
end)
|
||||
@ -134,9 +135,9 @@ function light_world:drawNormalShading(l,t,w,h,s)
|
||||
-- create shadow map for this light
|
||||
self.shadowMap:clear()
|
||||
util.drawto(self.shadowMap, l, t, s, function()
|
||||
for k = 1, #self.body do
|
||||
if self.body[k]:isInLightRange(light) and self.body[k]:isInRange(-l,-t,w,h,s) then
|
||||
self.body[k]:drawShadow(light)
|
||||
for k = 1, #self.bodies do
|
||||
if self.bodies[k]:isInLightRange(light) and self.bodies[k]:isVisible() then
|
||||
self.bodies[k]:drawShadow(light)
|
||||
end
|
||||
end
|
||||
end)
|
||||
@ -172,9 +173,9 @@ end
|
||||
|
||||
-- draw material
|
||||
function light_world:drawMaterial(l,t,w,h,s)
|
||||
for i = 1, #self.body do
|
||||
if self.body[i]:isInRange(-l,-t,w,h,s) then
|
||||
self.body[i]:drawMaterial()
|
||||
for i = 1, #self.bodies do
|
||||
if self.bodies[i]:isVisible() then
|
||||
self.bodies[i]:drawMaterial()
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -195,10 +196,10 @@ function light_world:drawGlow(l,t,w,h,s)
|
||||
-- create glow map
|
||||
self.glowMap:clear(0, 0, 0)
|
||||
util.drawto(self.glowMap, l, t, s, function()
|
||||
for i = 1, #self.body do
|
||||
if self.body[i]:isInRange(-l,-t,w,h,s) and self.body[i].glowStrength > 0.0 then
|
||||
for i = 1, #self.bodies do
|
||||
if self.bodies[i]:isVisible() and self.bodies[i].glowStrength > 0.0 then
|
||||
has_glow = true
|
||||
self.body[i]:drawGlow()
|
||||
self.bodies[i]:drawGlow()
|
||||
end
|
||||
end
|
||||
end)
|
||||
@ -213,9 +214,9 @@ function light_world:drawRefraction(l,t,w,h,s)
|
||||
-- create refraction map
|
||||
self.refractionMap:clear()
|
||||
util.drawto(self.refractionMap, l, t, s, function()
|
||||
for i = 1, #self.body do
|
||||
if self.body[i]:isInRange(-l,-t,w,h,s) then
|
||||
self.body[i]:drawRefraction()
|
||||
for i = 1, #self.bodies do
|
||||
if self.bodies[i]:isVisible() then
|
||||
self.bodies[i]:drawRefraction()
|
||||
end
|
||||
end
|
||||
end)
|
||||
@ -231,9 +232,9 @@ function light_world:drawReflection(l,t,w,h,s)
|
||||
-- create reflection map
|
||||
self.reflectionMap:clear(0, 0, 0)
|
||||
util.drawto(self.reflectionMap, l, t, s, function()
|
||||
for i = 1, #self.body do
|
||||
if self.body[i]:isInRange(-l,-t,w,h,s) then
|
||||
self.body[i]:drawReflection()
|
||||
for i = 1, #self.bodies do
|
||||
if self.bodies[i]:isVisible() then
|
||||
self.bodies[i]:drawReflection()
|
||||
end
|
||||
end
|
||||
end)
|
||||
@ -262,15 +263,15 @@ end
|
||||
|
||||
function light_world:setScale(s) self.s = s 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:setShadowBlur(blur) self.shadowBlur = blur end
|
||||
function light_world:setGlowStrength(strength) self.glowBlur = strength end
|
||||
function light_world:setRefractionStrength(strength) self.refractionStrength = strength end
|
||||
function light_world:setReflectionStrength(strength) self.reflectionStrength = strength end
|
||||
function light_world:setReflectionVisibility(visibility) self.reflectionVisibility = visibility end
|
||||
function light_world:getBodyCount() return #self.body end
|
||||
function light_world:getBody(n) return self.body[n] end
|
||||
function light_world:getBodyCount() return #self.bodies end
|
||||
function light_world:getBody(n) return self.bodies[n] end
|
||||
function light_world:getLightCount() return #self.lights end
|
||||
function light_world:getLight(n) return self.lights[n] end
|
||||
function light_world:newRectangle(...) return self:newBody("rectangle", ...) end
|
||||
@ -290,16 +291,16 @@ end
|
||||
|
||||
-- new body
|
||||
function light_world:newBody(type, ...)
|
||||
local id = #self.body + 1
|
||||
self.body[id] = Body(id, type, ...)
|
||||
return self.body[#self.body]
|
||||
local id = #self.bodies + 1
|
||||
self.bodies[id] = Body(id, type, ...)
|
||||
return self.bodies[#self.bodies]
|
||||
end
|
||||
|
||||
function light_world:remove(to_kill)
|
||||
if to_kill:is_a(Body) then
|
||||
for i = 1, #self.body do
|
||||
if self.body[i] == to_kill then
|
||||
table.remove(self.body, i)
|
||||
for i = 1, #self.bodies do
|
||||
if self.bodies[i] == to_kill then
|
||||
table.remove(self.bodies, i)
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
@ -146,12 +146,6 @@ function post_shader:drawShader(shaderName, canvas, args)
|
||||
current_arg = current_arg + 3
|
||||
elseif def == "imgBuffer" then
|
||||
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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user