mirror of
https://github.com/tanema/light_world.lua.git
synced 2024-12-11 01:14:26 +00:00
added in checking to see if the shadow bodies should be drawn, if they are within the screen and within range of the light
This commit is contained in:
parent
64bcbd1e94
commit
d02bc54b05
@ -1,5 +1,6 @@
|
||||
:todoing
|
||||
-make sure all draw calls check if the object is within range
|
||||
-add body animations
|
||||
-optimize shadow body calculations and drawing methods
|
||||
|
||||
# light_world.lua
|
||||
|
@ -165,7 +165,6 @@ function love.draw()
|
||||
end)
|
||||
love.graphics.pop()
|
||||
|
||||
|
||||
love.graphics.setBlendMode("alpha")
|
||||
love.graphics.setColor(0, 0, 0, 191)
|
||||
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), 24)
|
||||
|
28
lib/body.lua
28
lib/body.lua
@ -25,6 +25,9 @@ function body:init(id, type, ...)
|
||||
self.tileY = 0
|
||||
self.zheight = 1
|
||||
|
||||
self.castsNoShadow = false
|
||||
self.visible = true
|
||||
|
||||
if self.type == "circle" then
|
||||
self.x = args[1] or 0
|
||||
self.y = args[2] or 0
|
||||
@ -218,6 +221,10 @@ function body:setGlowStrength(strength)
|
||||
self.glowStrength = strength
|
||||
end
|
||||
|
||||
function body:setVisible(visible)
|
||||
self.visible = visible
|
||||
end
|
||||
|
||||
-- get radius
|
||||
function body:getRadius()
|
||||
return self.radius
|
||||
@ -434,6 +441,27 @@ function body:setShadowType(type, ...)
|
||||
end
|
||||
end
|
||||
|
||||
function body:isInLightRange(light, l, t, w, h, s)
|
||||
local distance
|
||||
if self.type == 'circle' then
|
||||
return light.range > math.sqrt(math.pow(light.x - self.x, 2) + math.pow(light.y - self.y, 2))
|
||||
else
|
||||
local cx, cy = self.x + (self.width * 0.5), self.y + (self.height * 0.5)
|
||||
distance = math.sqrt(math.pow(light.x - cx, 2) + math.pow(light.y - cy, 2))
|
||||
return distance <= light.range + (self.width > self.height and self.width or self.height)
|
||||
end
|
||||
end
|
||||
|
||||
function body:isInRange(l, t, w, h, s)
|
||||
local bx, by, bw, bh
|
||||
if self.type == 'circle' then
|
||||
bx, by, bw, bh = (self.x + l/s) * s, (self.y + t/s) * s, self.radius/s, self.radius/s
|
||||
else
|
||||
bx, by, bw, bh = (self.x + l/s) * s, (self.y + t/s) * s, self.width/s, self.height/s
|
||||
end
|
||||
return self.visible and (bx + bw) > 0 and (bx - bw) < w/s and (by + bh) > 0 and (by - bh) < h/s
|
||||
end
|
||||
|
||||
function body:drawNormal()
|
||||
if not self.refraction and not self.reflection and self.normalMesh then
|
||||
love.graphics.setColor(255, 255, 255)
|
||||
|
12
lib/init.lua
12
lib/init.lua
@ -103,22 +103,28 @@ function light_world:drawNormalShading(l,t,w,h,s)
|
||||
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()
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
self.normal2:clear()
|
||||
for i = 1, #self.lights do
|
||||
if self.lights[i]:inRange(l,t,w,h,s) then
|
||||
-- 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(self.lights[i]) and self.body[k]:isInRange(l,t,w,h,s) then
|
||||
self.body[k]:drawShadow(self.lights[i])
|
||||
end
|
||||
end
|
||||
end)
|
||||
-- draw scene for this light using normals and shadowmap
|
||||
self.lights[i]:drawNormalShading(l,t,w,h,s, self.normalMap, self.shadowMap, self.normal2)
|
||||
end
|
||||
end
|
||||
|
||||
-- add in ambient color
|
||||
self.normal:clear(255, 255, 255)
|
||||
@ -135,9 +141,11 @@ 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()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- draw glow
|
||||
function light_world:drawGlow(l,t,w,h,s)
|
||||
@ -155,8 +163,10 @@ function light_world:drawGlow(l,t,w,h,s)
|
||||
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) then
|
||||
self.body[i]:drawGlow()
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
self.glowMap2:clear()
|
||||
@ -188,8 +198,10 @@ function light_world:drawReflection(l,t,w,h,s)
|
||||
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()
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
util.drawCanvasToCanvas(self.render_buffer, self.reflectionMap2)
|
||||
|
@ -114,11 +114,10 @@ end
|
||||
|
||||
function light:inRange(l,t,w,h,s)
|
||||
local lx, ly, rs = (self.x + l/s) * s, (self.y + t/s) * s, self.range * s
|
||||
return (lx + rs) > 0 and (lx - rs) < w/s and (ly + rs) > 0 and (ly - rs) < h/s
|
||||
return self.visible and (lx + rs) > 0 and (lx - rs) < w/s and (ly + rs) > 0 and (ly - rs) < h/s
|
||||
end
|
||||
|
||||
function light:drawNormalShading(l,t,w,h,s, normalMap, shadowMap, canvas)
|
||||
if self.visible and self:inRange(l,t,w,h,s) then
|
||||
self.shadowShader:send('shadowMap', shadowMap)
|
||||
self.shadowShader:send('lightColor', {self.red / 255.0, self.green / 255.0, self.blue / 255.0})
|
||||
self.shadowShader:send("lightPosition", {(self.x + l/s) * s, (h/s - (self.y + t/s)) * s, (self.z * 10) / 255.0})
|
||||
@ -133,7 +132,6 @@ function light:drawNormalShading(l,t,w,h,s, normalMap, shadowMap, canvas)
|
||||
shader = self.shadowShader
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
function light:setVisible(visible)
|
||||
self.visible = visible
|
||||
|
Loading…
Reference in New Issue
Block a user