refactored post shader to be part of the light world but still able to use it on its own, took out globals, and made adding and removing effects independant from the draw calls

This commit is contained in:
Tim Anema 2014-10-06 17:27:41 -04:00
parent 5e0320c4bf
commit 702de1389a
4 changed files with 259 additions and 227 deletions

View File

@ -1,5 +1,4 @@
-- Example: Complex Example
require "lib/postshader"
local LightWorld = require "lib/light_world"
function initScene()
@ -226,14 +225,27 @@ function love.update(dt)
offsetOldX = offsetX
offsetOldY = offsetY
-- draw shader
if colorAberration > 0.0 then
-- vert / horz blur
lightWorld.post_shader:addEffect("blur", 2.0, 2.0)
lightWorld.post_shader:addEffect("chromatic", math.sin(lightDirection * 10.0) * colorAberration, math.cos(lightDirection * 10.0) * colorAberration, math.cos(lightDirection * 10.0) * colorAberration, math.sin(lightDirection * 10.0) * -colorAberration, math.sin(lightDirection * 10.0) * colorAberration, math.cos(lightDirection * 10.0) * -colorAberration)
else
lightWorld.post_shader:removeEffect("blur")
lightWorld.post_shader:removeEffect("chromatic")
end
if bloomOn > 0.0 then
-- blur, strength
lightWorld.post_shader:addEffect("bloom", 2.0, bloomOn)
else
lightWorld.post_shader:removeEffect("bloom")
end
end
function love.draw()
-- set shader buffer
if bloomOn then
love.postshader.setBuffer("render")
end
lightWorld:draw()
love.graphics.draw(imgLight, mx - 5, (my - 5) - (16.0 + (math.sin(lightDirection) + 1.0) * 64.0))
@ -327,31 +339,6 @@ function love.draw()
love.graphics.setColor(255, 255, 255, 191)
love.graphics.print("F1: Help", 4, 4)
end
-- draw shader
if colorAberration > 0.0 then
-- vert / horz blur
love.postshader.addEffect("blur", 2.0, 2.0)
love.postshader.addEffect("chromatic", math.sin(lightDirection * 10.0) * colorAberration, math.cos(lightDirection * 10.0) * colorAberration, math.cos(lightDirection * 10.0) * colorAberration, math.sin(lightDirection * 10.0) * -colorAberration, math.sin(lightDirection * 10.0) * colorAberration, math.cos(lightDirection * 10.0) * -colorAberration)
end
if bloomOn > 0.0 then
-- blur, strength
love.postshader.addEffect("bloom", 2.0, bloomOn)
end
if effectOn == 1.0 then
love.postshader.addEffect("4colors", {15, 56, 15}, {48, 98, 48}, {139, 172, 15}, {155, 188, 15})
--love.postshader.addEffect("4colors", {108, 108, 78}, {142, 139, 87}, {195, 196, 165}, {227, 230, 201})
elseif effectOn == 2.0 then
love.postshader.addEffect("monochrom")
elseif effectOn == 3.0 then
love.postshader.addEffect("scanlines")
elseif effectOn == 4.0 then
love.postshader.addEffect("tiltshift", 4.0)
end
love.postshader.draw()
end
function drawBackground(l,t,w,h)
@ -493,6 +480,32 @@ function love.keypressed(k, u)
if effectOn > 4.0 then
effectOn = 0.0
end
if effectOn == 1.0 then
lightWorld.post_shader:addEffect("4colors", {15, 56, 15}, {48, 98, 48}, {139, 172, 15}, {155, 188, 15})
--lightWorld.post_shader:addEffect("4colors", {108, 108, 78}, {142, 139, 87}, {195, 196, 165}, {227, 230, 201})
else
lightWorld.post_shader:removeEffect("4colors")
end
if effectOn == 2.0 then
lightWorld.post_shader:addEffect("monochrome")
else
lightWorld.post_shader:removeEffect("monochrome")
end
if effectOn == 3.0 then
lightWorld.post_shader:addEffect("scanlines")
else
lightWorld.post_shader:removeEffect("scanlines")
end
if effectOn == 4.0 then
lightWorld.post_shader:addEffect("tiltshift", 4.0)
else
lightWorld.post_shader:removeEffect("tiltshift")
end
elseif k == "f11" then
physicWorld:destroy()
lightWorld:clearBodys()

View File

@ -86,6 +86,7 @@ end
-- refresh
function body:refresh()
if self.x and self.y and self.width and self.height and self.ox and self.oy then
self.data = {
self.x - self.ox,
self.y - self.oy,
@ -96,6 +97,7 @@ function body:refresh()
self.x - self.ox,
self.y - self.oy + self.height
}
end
end
-- set position
@ -165,10 +167,8 @@ function body:setOffset(ox, oy)
if ox ~= self.ox or oy ~= self.oy then
self.ox = ox
self.oy = oy
if self.shadowType == "rectangle" then
self:refresh()
end
end
end
-- set offset

View File

@ -26,6 +26,7 @@ local class = require(_PACKAGE..'/class')
local Light = require(_PACKAGE..'/light')
local Body = require(_PACKAGE..'/body')
local normal_map = require(_PACKAGE..'/normal_map')
local PostShader = require(_PACKAGE..'/postshader')
require(_PACKAGE..'/postshader')
local light_world = class()
@ -38,6 +39,7 @@ light_world.reflectionShader = love.graphics.newShader(_PACKAGE.."/shaders/ref
function light_world:init(options)
self.lights = {}
self.body = {}
self.post_shader = PostShader()
self.ambient = {0, 0, 0}
self.normalInvert = false
@ -258,12 +260,7 @@ function light_world:draw(l,t,w,h,s)
self:drawReflection( sl,st,sw,sh,s)
love.graphics.pop()
love.graphics.setBackgroundColor(0, 0, 0)
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas(last_buffer)
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(self.render_buffer, l, t)
self.post_shader:drawWith(self.render_buffer)
end
-- draw shadow

View File

@ -25,189 +25,211 @@ local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
local class = require(_PACKAGE..'/class')
local post_shader = class()
post_shader.blurv = love.graphics.newShader(_PACKAGE.."/shaders/blurv.glsl")
post_shader.blurh = love.graphics.newShader(_PACKAGE.."/shaders/blurh.glsl")
post_shader.contrast = love.graphics.newShader(_PACKAGE.."/shaders/contrast.glsl")
post_shader.chromatic_aberration = love.graphics.newShader(_PACKAGE.."/shaders/chromatic_aberration.glsl")
post_shader.four_color = love.graphics.newShader(_PACKAGE.."/shaders/four_colors.glsl")
post_shader.monochrome = love.graphics.newShader(_PACKAGE.."/shaders/monochrome.glsl")
post_shader.scanlines = love.graphics.newShader(_PACKAGE.."/shaders/scanlines.glsl")
post_shader.tilt_shift = love.graphics.newShader(_PACKAGE.."/shaders/tilt_shift.glsl")
LOVE_POSTSHADER_BUFFER_RENDER = love.graphics.newCanvas()
LOVE_POSTSHADER_BUFFER_BACK = love.graphics.newCanvas()
LOVE_POSTSHADER_LAST_BUFFER = nil
function post_shader:init()
self:refreshScreenSize()
self.effects = {}
end
LOVE_POSTSHADER_BLURV = love.graphics.newShader(_PACKAGE.."/shaders/blurv.glsl")
LOVE_POSTSHADER_BLURH = love.graphics.newShader(_PACKAGE.."/shaders/blurh.glsl")
LOVE_POSTSHADER_CONTRAST = love.graphics.newShader(_PACKAGE.."/shaders/contrast.glsl")
LOVE_POSTSHADER_CHROMATIC_ABERRATION = love.graphics.newShader(_PACKAGE.."/shaders/chromatic_aberration.glsl")
LOVE_POSTSHADER_FOUR_COLOR = love.graphics.newShader(_PACKAGE.."/shaders/four_colors.glsl")
LOVE_POSTSHADER_MONOCHROM = love.graphics.newShader(_PACKAGE.."/shaders/monochrome.glsl")
LOVE_POSTSHADER_SCANLINES = love.graphics.newShader(_PACKAGE.."/shaders/scanlines.glsl")
LOVE_POSTSHADER_TILT_SHIFT = love.graphics.newShader(_PACKAGE.."/shaders/tilt_shift.glsl")
function post_shader:refreshScreenSize()
self.render_buffer = love.graphics.newCanvas()
self.back_buffer = love.graphics.newCanvas()
love.postshader = {}
post_shader.blurv:send("screen", {love.window.getWidth(), love.window.getHeight()})
post_shader.blurh:send("screen", {love.window.getWidth(), love.window.getHeight()})
post_shader.scanlines:send("screen", {love.window.getWidth(), love.window.getHeight()})
end
love.postshader.setBuffer = function(path)
if path == "back" then
love.graphics.setCanvas(LOVE_POSTSHADER_BUFFER_BACK)
function post_shader:addEffect(shaderName, ...)
self.effects[shaderName] = {...}
end
function post_shader:removeEffect(shaderName)
self.effects[shaderName] = nil
end
function post_shader:toggleEffect(shaderName, ...)
if self.effects[shaderName] ~= nil then
self:removeEffect(shaderName)
else
love.graphics.setCanvas(LOVE_POSTSHADER_BUFFER_RENDER)
self:addEffect(shaderName, ...)
end
LOVE_POSTSHADER_LAST_BUFFER = love.graphics.getCanvas()
end
love.postshader.addEffect = function(shader, ...)
args = {...}
LOVE_POSTSHADER_LAST_BUFFER = love.graphics.getCanvas()
function post_shader:drawWith(canvas)
for shader, args in pairs(self.effects) do
if shader == "bloom" then
-- Bloom Shader
love.graphics.setCanvas(LOVE_POSTSHADER_BUFFER_BACK)
love.graphics.setBlendMode("alpha")
LOVE_POSTSHADER_BLURV:send("screen", {love.window.getWidth(), love.window.getHeight()})
LOVE_POSTSHADER_BLURH:send("screen", {love.window.getWidth(), love.window.getHeight()})
LOVE_POSTSHADER_BLURV:send("steps", args[1] or 2.0)
LOVE_POSTSHADER_BLURH:send("steps", args[1] or 2.0)
love.graphics.setShader(LOVE_POSTSHADER_BLURV)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER)
love.graphics.setShader(LOVE_POSTSHADER_BLURH)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_BACK)
love.graphics.setShader(LOVE_POSTSHADER_CONTRAST)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_BACK)
love.graphics.setCanvas(LOVE_POSTSHADER_LAST_BUFFER)
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER)
love.graphics.setBlendMode("additive")
love.graphics.setColor(255, 255, 255, (args[2] or 0.25) * 255)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_BACK)
love.graphics.setBlendMode("alpha")
self:drawBloom(canvas, args)
elseif shader == "blur" then
-- Blur Shader
love.graphics.setCanvas(LOVE_POSTSHADER_BUFFER_BACK)
love.graphics.setBlendMode("alpha")
LOVE_POSTSHADER_BLURV:send("screen", {love.window.getWidth(), love.window.getHeight()})
LOVE_POSTSHADER_BLURH:send("screen", {love.window.getWidth(), love.window.getHeight()})
LOVE_POSTSHADER_BLURV:send("steps", args[1] or 2.0)
LOVE_POSTSHADER_BLURH:send("steps", args[2] or 2.0)
love.graphics.setShader(LOVE_POSTSHADER_BLURV)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER)
love.graphics.setShader(LOVE_POSTSHADER_BLURH)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_BACK)
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas(LOVE_POSTSHADER_LAST_BUFFER)
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_BACK)
self:drawBlur(canvas, args)
elseif shader == "chromatic" then
-- Chromatic Shader
love.graphics.setCanvas(LOVE_POSTSHADER_BUFFER_BACK)
love.graphics.setBlendMode("alpha")
LOVE_POSTSHADER_CHROMATIC_ABERRATION:send("redStrength", {args[1] or 0.0, args[2] or 0.0})
LOVE_POSTSHADER_CHROMATIC_ABERRATION:send("greenStrength", {args[3] or 0.0, args[4] or 0.0})
LOVE_POSTSHADER_CHROMATIC_ABERRATION:send("blueStrength", {args[5] or 0.0, args[6] or 0.0})
love.graphics.setCanvas(LOVE_POSTSHADER_BUFFER_BACK)
love.graphics.setShader(LOVE_POSTSHADER_CHROMATIC_ABERRATION)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER)
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas(LOVE_POSTSHADER_LAST_BUFFER)
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_BACK)
self:drawChromatic(canvas, args)
elseif shader == "4colors" then
-- 4 Color Shader
love.graphics.setCanvas(LOVE_POSTSHADER_BUFFER_BACK)
love.graphics.setBlendMode("alpha")
for i = 1, 4 do
for k = 1, 3 do
args[i][k] = args[i][k] / 255.0
end
end
LOVE_POSTSHADER_FOUR_COLOR:send("palette", args[1], args[2], args[3], args[4])
love.graphics.setShader(LOVE_POSTSHADER_FOUR_COLOR)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER)
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas(LOVE_POSTSHADER_LAST_BUFFER)
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_BACK)
elseif shader == "monochrom" then
-- Monochrom Shader
love.graphics.setCanvas(LOVE_POSTSHADER_BUFFER_BACK)
love.graphics.setBlendMode("alpha")
for i = 1, 3 do
if args[i] then
args[i] = args[i] / 255.0
end
end
LOVE_POSTSHADER_MONOCHROM:send("tint", {args[1] or 1.0, args[2] or 1.0, args[3] or 1.0})
LOVE_POSTSHADER_MONOCHROM:send("fudge", args[4] or 0.1)
LOVE_POSTSHADER_MONOCHROM:send("time", args[5] or love.timer.getTime())
love.graphics.setShader(LOVE_POSTSHADER_MONOCHROM)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER)
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas(LOVE_POSTSHADER_LAST_BUFFER)
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_BACK)
self:draw4Color(canvas, args)
elseif shader == "monochrome" then
self:drawMonochome(canvas, args)
elseif shader == "scanlines" then
-- Scanlines Shader
love.graphics.setCanvas(LOVE_POSTSHADER_BUFFER_BACK)
love.graphics.setBlendMode("alpha")
LOVE_POSTSHADER_SCANLINES:send("screen", {love.window.getWidth(), love.window.getHeight()})
LOVE_POSTSHADER_SCANLINES:send("strength", args[1] or 2.0)
LOVE_POSTSHADER_SCANLINES:send("time", args[2] or love.timer.getTime())
love.graphics.setShader(LOVE_POSTSHADER_SCANLINES)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER)
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas(LOVE_POSTSHADER_LAST_BUFFER)
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_BACK)
self:drawScanlines(canvas, args)
elseif shader == "tiltshift" then
-- Blur Shader
love.graphics.setCanvas(LOVE_POSTSHADER_BUFFER_BACK)
love.graphics.setBlendMode("alpha")
LOVE_POSTSHADER_BLURH:send("screen", {love.window.getWidth(), love.window.getHeight()})
LOVE_POSTSHADER_BLURV:send("steps", args[1] or 2.0)
LOVE_POSTSHADER_BLURH:send("steps", args[1] or 2.0)
love.graphics.setShader(LOVE_POSTSHADER_BLURV)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_RENDER)
love.graphics.setShader(LOVE_POSTSHADER_BLURH)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_BACK)
LOVE_POSTSHADER_TILT_SHIFT:send("imgBuffer", LOVE_POSTSHADER_BUFFER_RENDER)
love.graphics.setShader(LOVE_POSTSHADER_TILT_SHIFT)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_BACK)
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas(LOVE_POSTSHADER_LAST_BUFFER)
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(LOVE_POSTSHADER_BUFFER_BACK)
self:drawTiltshift(canvas, args)
end
end
end
love.postshader.draw = function()
if LOVE_POSTSHADER_LAST_BUFFER then
love.graphics.setBackgroundColor(0, 0, 0)
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas()
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(LOVE_POSTSHADER_LAST_BUFFER)
end
love.graphics.draw(canvas)
end
love.postshader.refreshScreenSize = function()
LOVE_POSTSHADER_BUFFER_RENDER = love.graphics.newCanvas()
LOVE_POSTSHADER_BUFFER_BACK = love.graphics.newCanvas()
function post_shader:drawBloom(canvas, args)
love.graphics.setCanvas(self.back_buffer)
love.graphics.setBlendMode("alpha")
post_shader.blurv:send("steps", args[1] or 2.0)
post_shader.blurh:send("steps", args[1] or 2.0)
love.graphics.setShader(post_shader.blurv)
love.graphics.draw(canvas)
love.graphics.setShader(post_shader.blurh)
love.graphics.draw(self.back_buffer)
love.graphics.setShader(post_shader.contrast)
love.graphics.draw(self.back_buffer)
love.graphics.setCanvas(canvas)
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(canvas)
love.graphics.setBlendMode("additive")
love.graphics.setColor(255, 255, 255, (args[2] or 0.25) * 255)
love.graphics.draw(self.back_buffer)
love.graphics.setBlendMode("alpha")
end
function post_shader:drawBlur(canvas, args)
love.graphics.setCanvas(self.back_buffer)
love.graphics.setBlendMode("alpha")
post_shader.blurv:send("steps", args[1] or 2.0)
post_shader.blurh:send("steps", args[2] or 2.0)
love.graphics.setShader(post_shader.blurv)
love.graphics.draw(canvas)
love.graphics.setShader(post_shader.blurh)
love.graphics.draw(self.back_buffer)
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas(canvas)
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(self.back_buffer)
end
function post_shader:drawChromatic(canvas, args)
love.graphics.setCanvas(self.back_buffer)
love.graphics.setBlendMode("alpha")
post_shader.chromatic_aberration:send("redStrength", {args[1] or 0.0, args[2] or 0.0})
post_shader.chromatic_aberration:send("greenStrength", {args[3] or 0.0, args[4] or 0.0})
post_shader.chromatic_aberration:send("blueStrength", {args[5] or 0.0, args[6] or 0.0})
love.graphics.setCanvas(self.back_buffer)
love.graphics.setShader(post_shader.chromatic_aberration)
love.graphics.draw(canvas)
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas(canvas)
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(self.back_buffer)
end
function post_shader:draw4Color(canvas, args)
love.graphics.setCanvas(self.back_buffer)
love.graphics.setBlendMode("alpha")
local palette = {{unpack(args[1])}, {unpack(args[2])}, {unpack(args[3])}, {unpack(args[4])}}
for i = 1, 4 do
for k = 1, 3 do
palette[i][k] = args[i][k] / 255.0
end
end
self.four_color:send("palette", palette[1], palette[2], palette[3], palette[4])
love.graphics.setShader(self.four_color)
love.graphics.draw(canvas)
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas(canvas)
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(self.back_buffer)
end
function post_shader:drawMonochome(canvas, args)
love.graphics.setCanvas(self.back_buffer)
love.graphics.setBlendMode("alpha")
local tint = {args[1], args[2], args[3]}
for i = 1, 3 do
if tint[i] then
tint[i] = tint[i] / 255.0
end
end
post_shader.monochrome:send("tint", {tint[1] or 1.0, tint[2] or 1.0, tint[3] or 1.0})
post_shader.monochrome:send("fudge", args[4] or 0.1)
post_shader.monochrome:send("time", args[5] or love.timer.getTime())
love.graphics.setShader(post_shader.monochrome)
love.graphics.draw(canvas)
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas(canvas)
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(self.back_buffer)
end
function post_shader:drawScanlines(canvas, args)
love.graphics.setCanvas(self.back_buffer)
love.graphics.setBlendMode("alpha")
post_shader.scanlines:send("strength", args[1] or 2.0)
post_shader.scanlines:send("time", args[2] or love.timer.getTime())
love.graphics.setShader(post_shader.scanlines)
love.graphics.draw(canvas)
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas(canvas)
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(self.back_buffer)
end
function post_shader:drawTiltshift(canvas, args)
love.graphics.setCanvas(self.back_buffer)
love.graphics.setBlendMode("alpha")
post_shader.tilt_shift:send("imgBuffer", canvas)
love.graphics.setShader(post_shader.tilt_shift)
love.graphics.draw(self.back_buffer)
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas(canvas)
love.graphics.setShader()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(self.back_buffer)
end
return post_shader