mirror of
https://github.com/tanema/light_world.lua.git
synced 2024-12-24 20:24:19 +00:00
finally coming up with a proper strategy to handle translation and zooming of canvases
This commit is contained in:
parent
22c45f7bac
commit
0f882da300
@ -1,11 +1,11 @@
|
|||||||
-- Example: Short Example
|
-- Example: Short Example
|
||||||
local gamera = require "vendor/gamera"
|
|
||||||
local LightWorld = require "lib/light_world"
|
local LightWorld = require "lib/light_world"
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
testShader = 0
|
testShader = 0
|
||||||
|
x = 0
|
||||||
|
y = 0
|
||||||
scale = 1
|
scale = 1
|
||||||
camera = gamera.new(0,0,2000,2000)
|
|
||||||
-- load images
|
-- load images
|
||||||
image = love.graphics.newImage("gfx/machine2.png")
|
image = love.graphics.newImage("gfx/machine2.png")
|
||||||
image_normal = love.graphics.newImage("gfx/cone_normal.png")
|
image_normal = love.graphics.newImage("gfx/cone_normal.png")
|
||||||
@ -62,7 +62,6 @@ end
|
|||||||
function love.update(dt)
|
function love.update(dt)
|
||||||
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
|
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
|
||||||
|
|
||||||
local x, y = camera:getPosition()
|
|
||||||
if love.keyboard.isDown("up") then
|
if love.keyboard.isDown("up") then
|
||||||
y = y - dt * 200
|
y = y - dt * 200
|
||||||
elseif love.keyboard.isDown("down") then
|
elseif love.keyboard.isDown("down") then
|
||||||
@ -81,20 +80,23 @@ function love.update(dt)
|
|||||||
scale = scale + 0.01
|
scale = scale + 0.01
|
||||||
end
|
end
|
||||||
|
|
||||||
camera:setPosition(x, y)
|
lightMouse:setPosition(love.mouse.getX(), love.mouse.getY())
|
||||||
camera:setScale(scale)
|
|
||||||
lightMouse:setPosition(camera:toWorld(love.mouse.getX(), love.mouse.getY()))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
camera:draw(function(l,t,w,h)
|
love.graphics.push()
|
||||||
lightWorld:draw(l,t,w,h,scale)
|
love.graphics.translate(x, y)
|
||||||
end)
|
love.graphics.scale(scale)
|
||||||
|
lightWorld:draw(x,y,scale)
|
||||||
|
love.graphics.pop()
|
||||||
end
|
end
|
||||||
|
|
||||||
function drawBackground(l,t,w,h)
|
function drawBackground(l,t,w,h)
|
||||||
love.graphics.setColor(255, 255, 255)
|
love.graphics.push()
|
||||||
love.graphics.rectangle("fill", 0, 0, 2000, 2000)
|
love.graphics.origin()
|
||||||
|
love.graphics.setColor(255, 255, 255)
|
||||||
|
love.graphics.rectangle("fill", 0, 0, w, h)
|
||||||
|
love.graphics.pop()
|
||||||
end
|
end
|
||||||
|
|
||||||
function drawForground(l,t,w,h)
|
function drawForground(l,t,w,h)
|
||||||
|
17
lib/body.lua
17
lib/body.lua
@ -363,7 +363,6 @@ function body:setShadowType(type, ...)
|
|||||||
self:refresh()
|
self:refresh()
|
||||||
elseif self.shadowType == "polygon" then
|
elseif self.shadowType == "polygon" then
|
||||||
self.data = args or {0, 0, 0, 0, 0, 0}
|
self.data = args or {0, 0, 0, 0, 0, 0}
|
||||||
print(self.data)
|
|
||||||
elseif self.shadowType == "image" then
|
elseif self.shadowType == "image" then
|
||||||
if self.img then
|
if self.img then
|
||||||
self.width = self.imgWidth
|
self.width = self.imgWidth
|
||||||
@ -388,7 +387,19 @@ function body:setShadowType(type, ...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function body:drawShadow(light, l,t,w,h)
|
function body:shadowStencil()
|
||||||
|
if self.shadowType == "circle" then
|
||||||
|
love.graphics.circle("fill", self.x - self.ox, self.y - self.oy, self.radius)
|
||||||
|
elseif self.shadowType == "rectangle" then
|
||||||
|
love.graphics.rectangle("fill", self.x - self.ox, self.y - self.oy, self.width, self.height)
|
||||||
|
elseif self.shadowType == "polygon" then
|
||||||
|
love.graphics.polygon("fill", unpack(self.data))
|
||||||
|
elseif self.shadowType == "image" then
|
||||||
|
--love.graphics.rectangle("fill", self.x - self.ox, self.y - self.oy, self.width, self.height)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function body:drawShadow(light, l,t,w,h,s)
|
||||||
if self.alpha < 1.0 then
|
if self.alpha < 1.0 then
|
||||||
love.graphics.setBlendMode("multiplicative")
|
love.graphics.setBlendMode("multiplicative")
|
||||||
love.graphics.setColor(self.red, self.green, self.blue)
|
love.graphics.setColor(self.red, self.green, self.blue)
|
||||||
@ -446,7 +457,7 @@ function body:drawShadow(light, l,t,w,h)
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.shadowMesh:setVertices(self.shadowVert)
|
self.shadowMesh:setVertices(self.shadowVert)
|
||||||
love.graphics.draw(self.shadowMesh, self.x - self.ox + l, self.y - self.oy + t)
|
love.graphics.draw(self.shadowMesh, self.x - self.ox + l, self.y - self.oy + t, 0, s, s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
||||||
local class = require(_PACKAGE.."/class")
|
local class = require(_PACKAGE.."/class")
|
||||||
local stencils = require(_PACKAGE..'/stencils')
|
local stencils = require(_PACKAGE..'/stencils')
|
||||||
|
local util = require(_PACKAGE..'/util')
|
||||||
|
|
||||||
local light = class()
|
local light = class()
|
||||||
|
|
||||||
@ -127,20 +128,8 @@ function light:inRange(l,t,w,h)
|
|||||||
return self.x + self.range > l and self.x - self.range < (l+w) and self.y + self.range > t and self.y - self.range < (t+h)
|
return self.x + self.range > l and self.x - self.range < (l+w) and self.y + self.range > t and self.y - self.range < (t+h)
|
||||||
end
|
end
|
||||||
|
|
||||||
function light:updateShadow(l,t,w,h, bodies)
|
function light:drawShadow(l,t,w,h,s,bodies, canvas)
|
||||||
love.graphics.setShader(self.shader)
|
|
||||||
if self.visible and self:inRange(l,t,w,h) then
|
if self.visible and self:inRange(l,t,w,h) then
|
||||||
|
|
||||||
self.shader:send("lightPosition", {self.x - l, h - (self.y - t), self.z})
|
|
||||||
self.shader:send("lightRange", self.range)
|
|
||||||
self.shader:send("lightColor", {self.red / 255.0, self.green / 255.0, self.blue / 255.0})
|
|
||||||
self.shader:send("lightSmooth", self.smooth)
|
|
||||||
self.shader:send("lightGlow", {1.0 - self.glowSize, self.glowStrength})
|
|
||||||
self.shader:send("lightAngle", math.pi - self.angle / 2.0)
|
|
||||||
self.shader:send("lightDirection", self.direction)
|
|
||||||
self.shadow:clear()
|
|
||||||
love.graphics.setCanvas(self.shadow)
|
|
||||||
|
|
||||||
-- calculate shadows
|
-- calculate shadows
|
||||||
local shadow_geometry = {}
|
local shadow_geometry = {}
|
||||||
for i = 1, #bodies do
|
for i = 1, #bodies do
|
||||||
@ -150,46 +139,54 @@ function light:updateShadow(l,t,w,h, bodies)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
self.shadow:clear()
|
||||||
|
self.shader:send("lightPosition", {self.x - l, h - (self.y - t), self.z})
|
||||||
|
self.shader:send("lightRange", self.range)
|
||||||
|
self.shader:send("lightColor", {self.red / 255.0, self.green / 255.0, self.blue / 255.0})
|
||||||
|
self.shader:send("lightSmooth", self.smooth)
|
||||||
|
self.shader:send("lightGlow", {1.0 - self.glowSize, self.glowStrength})
|
||||||
|
self.shader:send("lightAngle", math.pi - self.angle / 2.0)
|
||||||
|
self.shader:send("lightDirection", self.direction)
|
||||||
|
|
||||||
-- draw shadow
|
-- draw shadow
|
||||||
love.graphics.setInvertedStencil(stencils.shadow(shadow_geometry, bodies))
|
util.drawto(self.shadow, l, t, s, function()
|
||||||
love.graphics.setBlendMode("additive")
|
love.graphics.setShader(self.shader)
|
||||||
love.graphics.rectangle("fill", l,t,w,h)
|
love.graphics.setInvertedStencil(stencils.shadow(shadow_geometry, bodies))
|
||||||
|
love.graphics.setBlendMode("additive")
|
||||||
|
love.graphics.rectangle("fill", l,t,w,h)
|
||||||
|
|
||||||
-- draw color shadows
|
-- draw color shadows
|
||||||
love.graphics.setBlendMode("multiplicative")
|
love.graphics.setBlendMode("multiplicative")
|
||||||
love.graphics.setShader()
|
love.graphics.setShader()
|
||||||
for k = 1,#shadow_geometry do
|
for k = 1,#shadow_geometry do
|
||||||
if shadow_geometry[k].alpha < 1.0 then
|
if shadow_geometry[k].alpha < 1.0 then
|
||||||
love.graphics.setColor(
|
love.graphics.setColor(
|
||||||
shadow_geometry[k].red * (1.0 - shadow_geometry[k].alpha),
|
shadow_geometry[k].red * (1.0 - shadow_geometry[k].alpha),
|
||||||
shadow_geometry[k].green * (1.0 - shadow_geometry[k].alpha),
|
shadow_geometry[k].green * (1.0 - shadow_geometry[k].alpha),
|
||||||
shadow_geometry[k].blue * (1.0 - shadow_geometry[k].alpha)
|
shadow_geometry[k].blue * (1.0 - shadow_geometry[k].alpha)
|
||||||
)
|
)
|
||||||
love.graphics.polygon("fill", unpack(shadow_geometry[k]))
|
love.graphics.polygon("fill", unpack(shadow_geometry[k]))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
for k = 1, #bodies do
|
for k = 1, #bodies do
|
||||||
bodies[k]:drawShadow(self, l,t,w,h)
|
bodies[k]:drawShadow(self,l,t,w,h,s)
|
||||||
end
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
-- draw shine
|
-- draw shine
|
||||||
love.graphics.setShader(self.shader)
|
util.drawto(self.shine, l, t, s, function()
|
||||||
love.graphics.setCanvas(self.shine)
|
love.graphics.setShader(self.shader)
|
||||||
self.shine:clear(255, 255, 255)
|
self.shine:clear(255, 255, 255)
|
||||||
love.graphics.setBlendMode("alpha")
|
love.graphics.setBlendMode("alpha")
|
||||||
love.graphics.setStencil(stencils.poly(bodies))
|
love.graphics.setStencil(stencils.colorShadow(bodies))
|
||||||
love.graphics.rectangle("fill", l,t,w,h)
|
love.graphics.rectangle("fill", 0,0,w,h)
|
||||||
love.graphics.setStencil()
|
end)
|
||||||
end
|
|
||||||
love.graphics.setShader()
|
|
||||||
end
|
|
||||||
|
|
||||||
function light:drawShadow(l,t,w,h)
|
love.graphics.setStencil()
|
||||||
if self.visible then
|
love.graphics.setShader()
|
||||||
love.graphics.setColor(255, 255, 255)
|
|
||||||
love.graphics.setBlendMode("additive")
|
util.drawCanvasToCanvas(self.shadow, canvas, {blendmode = "additive"})
|
||||||
love.graphics.draw(self.shadow, l, t)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
|||||||
local class = require(_PACKAGE..'/class')
|
local class = require(_PACKAGE..'/class')
|
||||||
local Light = require(_PACKAGE..'/light')
|
local Light = require(_PACKAGE..'/light')
|
||||||
local Body = require(_PACKAGE..'/body')
|
local Body = require(_PACKAGE..'/body')
|
||||||
|
local util = require(_PACKAGE..'/util')
|
||||||
local normal_map = require(_PACKAGE..'/normal_map')
|
local normal_map = require(_PACKAGE..'/normal_map')
|
||||||
local PostShader = require(_PACKAGE..'/postshader')
|
local PostShader = require(_PACKAGE..'/postshader')
|
||||||
require(_PACKAGE..'/postshader')
|
require(_PACKAGE..'/postshader')
|
||||||
@ -62,44 +63,19 @@ function light_world:init(options)
|
|||||||
self:refreshScreenSize()
|
self:refreshScreenSize()
|
||||||
end
|
end
|
||||||
|
|
||||||
function light_world:drawBlur(blendmode, blur, canvas, canvas2, l, t, w, h)
|
function light_world:drawBlur(blendmode, blur, canvas, canvas2, l, t, w, h, s)
|
||||||
if blur <= 0 then
|
if blur <= 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
love.graphics.setColor(255, 255, 255)
|
canvas2:clear()
|
||||||
self.blurv:send("steps", blur)
|
self.blurv:send("steps", blur)
|
||||||
self.blurh:send("steps", blur)
|
self.blurh:send("steps", blur)
|
||||||
love.graphics.setBlendMode(blendmode)
|
util.drawCanvasToCanvas(canvas, canvas2, {shader = self.blurv, blendmode = blendmode})
|
||||||
canvas2:clear()
|
util.drawCanvasToCanvas(canvas2, canvas, {shader = self.blurh, blendmode = blendmode})
|
||||||
love.graphics.setCanvas(canvas2)
|
|
||||||
love.graphics.setShader(self.blurv)
|
|
||||||
love.graphics.draw(canvas, l, t)
|
|
||||||
love.graphics.setCanvas(canvas)
|
|
||||||
love.graphics.setShader(self.blurh)
|
|
||||||
love.graphics.draw(canvas2, l, t)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function light_world:updateShadows(l,t,w,h)
|
function light_world:updateShine(l,t,w,h,s)
|
||||||
for i = 1, #self.lights do
|
|
||||||
self.lights[i]:updateShadow(l,t,w,h, self.body)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- update shadow
|
|
||||||
love.graphics.setCanvas(self.shadow)
|
|
||||||
love.graphics.setColor(unpack(self.ambient))
|
|
||||||
love.graphics.setBlendMode("alpha")
|
|
||||||
love.graphics.rectangle("fill", l, t, w, h)
|
|
||||||
|
|
||||||
for i = 1, #self.lights do
|
|
||||||
self.lights[i]:drawShadow(l,t,w,h)
|
|
||||||
end
|
|
||||||
|
|
||||||
light_world:drawBlur("alpha", self.blur, self.shadow, self.shadow2, l, t, w, h)
|
|
||||||
love.graphics.setCanvas(self.render_buffer)
|
|
||||||
end
|
|
||||||
|
|
||||||
function light_world:updateShine(l,t,w,h)
|
|
||||||
-- update shine
|
-- update shine
|
||||||
love.graphics.setCanvas(self.shine)
|
love.graphics.setCanvas(self.shine)
|
||||||
love.graphics.setColor(unpack(self.ambient))
|
love.graphics.setColor(unpack(self.ambient))
|
||||||
@ -112,8 +88,7 @@ function light_world:updateShine(l,t,w,h)
|
|||||||
self.lights[i]:drawShine(l,t,w,h)
|
self.lights[i]:drawShine(l,t,w,h)
|
||||||
end
|
end
|
||||||
|
|
||||||
--light_world:drawBlur("additive", self.blur, self.shine, self.shine2, l, t, w, h)
|
--light_world:drawBlur("additive", self.blur, self.shine, self.shine2, l, t, w, h, s)
|
||||||
love.graphics.setCanvas(self.render_buffer)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function light_world:updatePixelShadows(l,t,w,h)
|
function light_world:updatePixelShadows(l,t,w,h)
|
||||||
@ -148,11 +123,9 @@ function light_world:updatePixelShadows(l,t,w,h)
|
|||||||
love.graphics.setBlendMode("additive")
|
love.graphics.setBlendMode("additive")
|
||||||
love.graphics.setColor({self.ambient[1], self.ambient[2], self.ambient[3]})
|
love.graphics.setColor({self.ambient[1], self.ambient[2], self.ambient[3]})
|
||||||
love.graphics.rectangle("fill", l,t,w,h)
|
love.graphics.rectangle("fill", l,t,w,h)
|
||||||
love.graphics.setBlendMode("alpha")
|
|
||||||
love.graphics.setCanvas(self.render_buffer)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function light_world:updateGlow(l,t,w,h)
|
function light_world:updateGlow(l,t,w,h,s)
|
||||||
-- create glow map
|
-- create glow map
|
||||||
self.glowMap:clear(0, 0, 0)
|
self.glowMap:clear(0, 0, 0)
|
||||||
love.graphics.setCanvas(self.glowMap)
|
love.graphics.setCanvas(self.glowMap)
|
||||||
@ -173,8 +146,7 @@ function light_world:updateGlow(l,t,w,h)
|
|||||||
self.body[i]:drawGlow(l,t,w,h)
|
self.body[i]:drawGlow(l,t,w,h)
|
||||||
end
|
end
|
||||||
|
|
||||||
light_world:drawBlur("alpha", self.glowBlur, self.glowMap, self.glowMap2, l, t, w, h)
|
light_world:drawBlur("alpha", self.glowBlur, self.glowMap, self.glowMap2, l, t, w, h, s)
|
||||||
love.graphics.setCanvas(self.render_buffer)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function light_world:updateRefraction(l,t,w,h)
|
function light_world:updateRefraction(l,t,w,h)
|
||||||
@ -189,8 +161,6 @@ function light_world:updateRefraction(l,t,w,h)
|
|||||||
love.graphics.setBlendMode("alpha")
|
love.graphics.setBlendMode("alpha")
|
||||||
love.graphics.setCanvas(self.refractionMap2)
|
love.graphics.setCanvas(self.refractionMap2)
|
||||||
love.graphics.draw(self.render_buffer, l, t)
|
love.graphics.draw(self.render_buffer, l, t)
|
||||||
love.graphics.setShader()
|
|
||||||
love.graphics.setCanvas(self.render_buffer)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function light_world:updateRelfection(l,t,w,h)
|
function light_world:updateRelfection(l,t,w,h)
|
||||||
@ -205,8 +175,6 @@ function light_world:updateRelfection(l,t,w,h)
|
|||||||
love.graphics.setBlendMode("alpha")
|
love.graphics.setBlendMode("alpha")
|
||||||
love.graphics.setCanvas(self.reflectionMap2)
|
love.graphics.setCanvas(self.reflectionMap2)
|
||||||
love.graphics.draw(self.render_buffer, l, t)
|
love.graphics.draw(self.render_buffer, l, t)
|
||||||
love.graphics.setShader()
|
|
||||||
love.graphics.setCanvas(self.render_buffer)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function light_world:refreshScreenSize(w, h)
|
function light_world:refreshScreenSize(w, h)
|
||||||
@ -239,32 +207,24 @@ function light_world:refreshScreenSize(w, h)
|
|||||||
self.post_shader:refreshScreenSize(w, h)
|
self.post_shader:refreshScreenSize(w, h)
|
||||||
end
|
end
|
||||||
|
|
||||||
function light_world:draw(l,t,w,h,s)
|
function light_world:draw(l,t,s)
|
||||||
l,t,w,h,s = (l or 0), (t or 0), (w or love.graphics.getWidth()), (h or love.graphics.getHeight()), s or 1
|
l,t,s = (l or 0), (t or 0), s or 1
|
||||||
|
local w, h = (love.graphics.getWidth() / s), (love.graphics.getHeight() / s)
|
||||||
|
|
||||||
if s ~= self.scale then
|
util.drawto(self.render_buffer, l, t, s, function()
|
||||||
--self:refreshScreenSize(w, h)
|
self.drawBackground( l,t,w,h,s)
|
||||||
self.scale = scale
|
self:drawShadow( l,t,w,h,s)
|
||||||
end
|
self.drawForground( l,t,w,h,s)
|
||||||
|
--self:drawMaterial( l,t,w,h,s)
|
||||||
|
--self:drawShine( l,t,w,h,s)
|
||||||
|
--self:drawPixelShadow(l,t,w,h,s)
|
||||||
|
--self:drawGlow( l,t,w,h,s)
|
||||||
|
--self:drawRefraction( l,t,w,h,s)
|
||||||
|
--self:drawReflection( l,t,w,h,s)
|
||||||
|
end)
|
||||||
|
|
||||||
local last_buffer = love.graphics.getCanvas()
|
util.drawCanvasToCanvas(self.render_buffer)
|
||||||
love.graphics.setCanvas(self.render_buffer)
|
--self.post_shader:drawWith(self.render_buffer, l, t, s)
|
||||||
|
|
||||||
love.graphics.push()
|
|
||||||
love.graphics.scale(1/s)
|
|
||||||
local sl, st, sw, sh = (l*s), (t*s), (w*s), (h*s)
|
|
||||||
self.drawBackground( sl,st,sw,sh,s)
|
|
||||||
self:drawShadow( sl,st,sw,sh,s)
|
|
||||||
self.drawForground( sl,st,sw,sh,s)
|
|
||||||
self:drawMaterial( sl,st,sw,sh,s)
|
|
||||||
self:drawShine( sl,st,sw,sh,s)
|
|
||||||
self:drawPixelShadow( sl,st,sw,sh,s)
|
|
||||||
self:drawGlow( sl,st,sw,sh,s)
|
|
||||||
self:drawRefraction( sl,st,sw,sh,s)
|
|
||||||
self:drawReflection( sl,st,sw,sh,s)
|
|
||||||
love.graphics.pop()
|
|
||||||
|
|
||||||
self.post_shader:drawWith(self.render_buffer, l, t, s)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- draw shadow
|
-- draw shadow
|
||||||
@ -272,12 +232,19 @@ function light_world:drawShadow(l,t,w,h,s)
|
|||||||
if not self.isShadows and not self.isLight then
|
if not self.isShadows and not self.isLight then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
self:updateShadows(l,t,w,h)
|
|
||||||
love.graphics.setColor(255, 255, 255)
|
-- draw ambient
|
||||||
love.graphics.setBlendMode("multiplicative")
|
util.drawto(self.shadow, l, t, s, function()
|
||||||
love.graphics.setShader()
|
love.graphics.setColor(unpack(self.ambient))
|
||||||
love.graphics.draw(self.shadow, l, t)
|
love.graphics.setBlendMode("alpha")
|
||||||
love.graphics.setBlendMode("alpha")
|
love.graphics.rectangle("fill", l, t, w, h)
|
||||||
|
for i = 1, #self.lights do
|
||||||
|
self.lights[i]:drawShadow(l,t,w,h,s,self.body, self.shadow)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
light_world:drawBlur("alpha", self.blur, self.shadow, self.shadow2, l, t, w, h, s)
|
||||||
|
util.drawCanvasToCanvas(self.shadow, self.render_buffer, {blendmode = "multiplicative"})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- draw shine
|
-- draw shine
|
||||||
@ -286,6 +253,7 @@ function light_world:drawShine(l,t,w,h,s)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
self:updateShine(l,t,w,h)
|
self:updateShine(l,t,w,h)
|
||||||
|
love.graphics.setCanvas(self.render_buffer)
|
||||||
love.graphics.setColor(255, 255, 255)
|
love.graphics.setColor(255, 255, 255)
|
||||||
love.graphics.setBlendMode("multiplicative")
|
love.graphics.setBlendMode("multiplicative")
|
||||||
love.graphics.setShader()
|
love.graphics.setShader()
|
||||||
@ -299,6 +267,7 @@ function light_world:drawPixelShadow(l,t,w,h,s)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
self:updatePixelShadows(l,t,w,h)
|
self:updatePixelShadows(l,t,w,h)
|
||||||
|
love.graphics.setCanvas(self.render_buffer)
|
||||||
love.graphics.setColor(255, 255, 255)
|
love.graphics.setColor(255, 255, 255)
|
||||||
love.graphics.setBlendMode("multiplicative")
|
love.graphics.setBlendMode("multiplicative")
|
||||||
love.graphics.setShader()
|
love.graphics.setShader()
|
||||||
@ -319,6 +288,7 @@ function light_world:drawGlow(l,t,w,h,s)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
self:updateGlow(l,t,w,h)
|
self:updateGlow(l,t,w,h)
|
||||||
|
love.graphics.setCanvas(self.render_buffer)
|
||||||
love.graphics.setColor(255, 255, 255)
|
love.graphics.setColor(255, 255, 255)
|
||||||
love.graphics.setBlendMode("additive")
|
love.graphics.setBlendMode("additive")
|
||||||
love.graphics.setShader()
|
love.graphics.setShader()
|
||||||
@ -331,6 +301,7 @@ function light_world:drawRefraction(l,t,w,h,s)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
self:updateRefraction(l,t,w,h)
|
self:updateRefraction(l,t,w,h)
|
||||||
|
love.graphics.setCanvas(self.render_buffer)
|
||||||
self.refractionShader:send("backBuffer", self.refractionMap2)
|
self.refractionShader:send("backBuffer", self.refractionMap2)
|
||||||
self.refractionShader:send("refractionStrength", self.refractionStrength)
|
self.refractionShader:send("refractionStrength", self.refractionStrength)
|
||||||
love.graphics.setShader(self.refractionShader)
|
love.graphics.setShader(self.refractionShader)
|
||||||
@ -344,6 +315,7 @@ function light_world:drawReflection(l,t,w,h,s)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
self:updateRelfection(l,t,w,h)
|
self:updateRelfection(l,t,w,h)
|
||||||
|
love.graphics.setCanvas(self.render_buffer)
|
||||||
self.reflectionShader:send("backBuffer", self.reflectionMap2)
|
self.reflectionShader:send("backBuffer", self.reflectionMap2)
|
||||||
self.reflectionShader:send("reflectionStrength", self.reflectionStrength)
|
self.reflectionShader:send("reflectionStrength", self.reflectionStrength)
|
||||||
self.reflectionShader:send("reflectionVisibility", self.reflectionVisibility)
|
self.reflectionShader:send("reflectionVisibility", self.reflectionVisibility)
|
||||||
|
@ -69,7 +69,7 @@ function post_shader:toggleEffect(shaderName, ...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function post_shader:drawWith(canvas, l, t)
|
function post_shader:drawWith(canvas, l, t, scale)
|
||||||
for shader, args in pairs(self.effects) do
|
for shader, args in pairs(self.effects) do
|
||||||
if shader == "bloom" then
|
if shader == "bloom" then
|
||||||
self:drawBloom(canvas, args)
|
self:drawBloom(canvas, args)
|
||||||
@ -274,7 +274,6 @@ function post_shader:drawTest(canvas, args)
|
|||||||
effect[1]:send(def, defaults[def])
|
effect[1]:send(def, defaults[def])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
print(effect[3])
|
|
||||||
love.graphics.setShader(effect[1])
|
love.graphics.setShader(effect[1])
|
||||||
love.graphics.draw(canvas)
|
love.graphics.draw(canvas)
|
||||||
|
|
||||||
|
@ -2,40 +2,29 @@ local stencils = {}
|
|||||||
|
|
||||||
function stencils.shadow(geometry, bodies)
|
function stencils.shadow(geometry, bodies)
|
||||||
return function()
|
return function()
|
||||||
|
--cast shadows
|
||||||
for i = 1,#geometry do
|
for i = 1,#geometry do
|
||||||
if geometry[i].alpha == 1.0 then
|
if geometry[i].alpha == 1.0 then
|
||||||
love.graphics.polygon("fill", unpack(geometry[i]))
|
love.graphics.polygon("fill", unpack(geometry[i]))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
-- underneath shadows
|
||||||
for i = 1, #bodies do
|
for i = 1, #bodies do
|
||||||
if not bodies[i].castsNoShadow then
|
if not bodies[i].castsNoShadow then
|
||||||
if bodies[i].shadowType == "circle" then
|
bodies[i]:shadowStencil()
|
||||||
love.graphics.circle("fill", bodies[i].x - bodies[i].ox, bodies[i].y - bodies[i].oy, bodies[i].radius)
|
|
||||||
elseif bodies[i].shadowType == "rectangle" then
|
|
||||||
love.graphics.rectangle("fill", bodies[i].x - bodies[i].ox, bodies[i].y - bodies[i].oy, bodies[i].width, bodies[i].height)
|
|
||||||
elseif bodies[i].shadowType == "polygon" then
|
|
||||||
love.graphics.polygon("fill", unpack(bodies[i].data))
|
|
||||||
elseif bodies[i].shadowType == "image" then
|
|
||||||
--love.graphics.rectangle("fill", bodies[i].x - bodies[i].ox, bodies[i].y - bodies[i].oy, bodies[i].width, bodies[i].height)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function stencils.poly(bodies)
|
function stencils.colorShadow(bodies)
|
||||||
return function()
|
return function()
|
||||||
for i = 1, #bodies do
|
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
|
if bodies[i].shine and
|
||||||
if bodies[i].shadowType == "circle" then
|
(bodies[i].glowStrength == 0.0 or
|
||||||
love.graphics.circle("fill", bodies[i].x - bodies[i].ox, bodies[i].y - bodies[i].oy, bodies[i].radius)
|
(bodies[i].type == "image" and not bodies[i].normal))
|
||||||
elseif bodies[i].shadowType == "rectangle" then
|
then
|
||||||
love.graphics.rectangle("fill", bodies[i].x - bodies[i].ox, bodies[i].y - bodies[i].oy, bodies[i].width, bodies[i].height)
|
bodies[i]:shadowStencil()
|
||||||
elseif bodies[i].shadowType == "polygon" then
|
|
||||||
love.graphics.polygon("fill", unpack(bodies[i].data))
|
|
||||||
elseif bodies[i].shadowType == "image" then
|
|
||||||
--love.graphics.rectangle("fill", bodies[i].x - bodies[i].ox, bodies[i].y - bodies[i].oy, bodies[i].width, bodies[i].height)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
40
lib/util.lua
Normal file
40
lib/util.lua
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
local util = {}
|
||||||
|
|
||||||
|
function util.drawCanvasToCanvas(canvas, other_canvas, options)
|
||||||
|
options = options or {}
|
||||||
|
|
||||||
|
local last_buffer = love.graphics.getCanvas()
|
||||||
|
love.graphics.push()
|
||||||
|
love.graphics.origin()
|
||||||
|
love.graphics.setCanvas(other_canvas)
|
||||||
|
if options["blendmode"] then
|
||||||
|
love.graphics.setBlendMode(options["blendmode"])
|
||||||
|
end
|
||||||
|
if options["shader"] then
|
||||||
|
love.graphics.setShader(options["shader"])
|
||||||
|
end
|
||||||
|
love.graphics.setColor(255,255,255)
|
||||||
|
love.graphics.draw(canvas,0,0)
|
||||||
|
love.graphics.setCanvas(last_buffer)
|
||||||
|
if options["blendmode"] then
|
||||||
|
love.graphics.setBlendMode("alpha")
|
||||||
|
end
|
||||||
|
if options["shader"] then
|
||||||
|
love.graphics.setShader()
|
||||||
|
end
|
||||||
|
love.graphics.pop()
|
||||||
|
end
|
||||||
|
|
||||||
|
function util.drawto(canvas, x, y, scale, cb)
|
||||||
|
local last_buffer = love.graphics.getCanvas()
|
||||||
|
love.graphics.push()
|
||||||
|
love.graphics.origin()
|
||||||
|
love.graphics.setCanvas(canvas)
|
||||||
|
love.graphics.translate(x, y)
|
||||||
|
love.graphics.scale(scale)
|
||||||
|
cb()
|
||||||
|
love.graphics.setCanvas(last_buffer)
|
||||||
|
love.graphics.pop()
|
||||||
|
end
|
||||||
|
|
||||||
|
return util
|
Loading…
Reference in New Issue
Block a user