From 5474b11c50721fcc24bf01378724b7023f341c56 Mon Sep 17 00:00:00 2001 From: Tim Anema Date: Thu, 23 Oct 2014 22:41:52 -0400 Subject: [PATCH] some proper refactoring so shine and shadows code are separate --- examples/short.lua | 2 +- lib/body.lua | 2 +- lib/light.lua | 24 ++++++++++++------------ lib/light_world.lua | 4 +--- lib/stencils.lua | 6 +++--- 5 files changed, 18 insertions(+), 20 deletions(-) diff --git a/examples/short.lua b/examples/short.lua index 3058afa..a74286b 100644 --- a/examples/short.lua +++ b/examples/short.lua @@ -17,7 +17,7 @@ function love.load() lightWorld = LightWorld({ drawBackground = drawBackground, drawForground = drawForground, - ambient = {15,15,15}, + ambient = {55,55,55}, refractionStrength = 32.0, reflectionVisibility = 0.75, }) diff --git a/lib/body.lua b/lib/body.lua index e8bf383..9cb52f5 100644 --- a/lib/body.lua +++ b/lib/body.lua @@ -387,7 +387,7 @@ function body:setShadowType(type, ...) end end -function body:shadowStencil() +function body:stencil() if self.shadowType == "circle" then love.graphics.circle("fill", self.x - self.ox, self.y - self.oy, self.radius) elseif self.shadowType == "rectangle" then diff --git a/lib/light.lua b/lib/light.lua index f1babd9..6681633 100644 --- a/lib/light.lua +++ b/lib/light.lua @@ -176,25 +176,25 @@ function light:drawShadow(l,t,w,h,s,bodies, canvas) end end) - -- update shine - self.shine:clear(255, 255, 255) - util.drawto(self.shine, l, t, s, function() - love.graphics.setShader(self.shader) - love.graphics.setBlendMode("alpha") - love.graphics.setStencil(stencils.colorShadow(bodies)) - love.graphics.rectangle("fill", -l/s,-t/s,w/s,h/s) - end) - love.graphics.setStencil() love.graphics.setShader() - util.drawCanvasToCanvas(self.shadow, canvas, {blendmode = "additive"}) end end -function light:drawShine(canvas) +function light:drawShine(l,t,w,h,s,bodies,canvas) if self.visible then - util.drawCanvasToCanvas(self.shine, canvas) + --update shine + self.shine:clear(255, 255, 255) + util.drawto(self.shine, l, t, s, function() + love.graphics.setShader(self.shader) + love.graphics.setBlendMode("alpha") + love.graphics.setStencil(stencils.shine(bodies)) + love.graphics.rectangle("fill", -l/s,-t/s,w/s,h/s) + end) + love.graphics.setStencil() + love.graphics.setShader() + util.drawCanvasToCanvas(self.shine, canvas, {blendmode = "additive"}) end end diff --git a/lib/light_world.lua b/lib/light_world.lua index 54f8d12..a804b11 100644 --- a/lib/light_world.lua +++ b/lib/light_world.lua @@ -152,10 +152,8 @@ function light_world:drawShine(l,t,w,h,s) love.graphics.setColor(unpack(self.ambient)) love.graphics.setBlendMode("alpha") love.graphics.rectangle("fill", -l/s, -t/s, w/s, h/s) - love.graphics.setColor(255, 255, 255) - love.graphics.setBlendMode("additive") for i = 1, #self.lights do - self.lights[i]:drawShine(self.shine) + self.lights[i]:drawShine(l,t,w,h,s,self.body,self.shine) end end) diff --git a/lib/stencils.lua b/lib/stencils.lua index c5561cb..de293c6 100644 --- a/lib/stencils.lua +++ b/lib/stencils.lua @@ -11,20 +11,20 @@ function stencils.shadow(geometry, bodies) -- underneath shadows for i = 1, #bodies do if not bodies[i].castsNoShadow then - bodies[i]:shadowStencil() + bodies[i]:stencil() end end end end -function stencils.colorShadow(bodies) +function stencils.shine(bodies) return function() for i = 1, #bodies do if bodies[i].shine and (bodies[i].glowStrength == 0.0 or (bodies[i].type == "image" and not bodies[i].normal)) then - bodies[i]:shadowStencil() + bodies[i]:stencil() end end end