made the postshader be more easily extendable

This commit is contained in:
Tim Anema 2014-10-23 21:35:35 -04:00
parent 5e8ef63d6d
commit e9a98dbcee
15 changed files with 121 additions and 106 deletions

View File

@ -112,7 +112,7 @@ function love.load()
lightOn = true lightOn = true
gravityOn = 1 gravityOn = 1
shadowBlur = 2.0 shadowBlur = 2.0
bloomOn = 0.25 bloomOn = 0.0
textureOn = true textureOn = true
normalOn = false normalOn = false
glowBlur = 1.0 glowBlur = 1.0
@ -217,10 +217,10 @@ function love.update(dt)
if colorAberration > 0.0 then if colorAberration > 0.0 then
-- vert / horz blur -- vert / horz blur
lightWorld.post_shader:addEffect("blur", 2.0, 2.0) 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) lightWorld.post_shader:addEffect("chromatic_aberration", 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 else
lightWorld.post_shader:removeEffect("blur") lightWorld.post_shader:removeEffect("blur")
lightWorld.post_shader:removeEffect("chromatic") lightWorld.post_shader:removeEffect("chromatic_aberration")
end end
if bloomOn > 0.0 then if bloomOn > 0.0 then
@ -472,10 +472,10 @@ function love.keypressed(k, u)
end end
if effectOn == 1.0 then 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("four_colors", {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}) --lightWorld.post_shader:addEffect("4colors", {108, 108, 78}, {142, 139, 87}, {195, 196, 165}, {227, 230, 201})
else else
lightWorld.post_shader:removeEffect("4colors") lightWorld.post_shader:removeEffect("four_colors")
end end
if effectOn == 2.0 then if effectOn == 2.0 then
@ -491,9 +491,9 @@ function love.keypressed(k, u)
end end
if effectOn == 4.0 then if effectOn == 4.0 then
lightWorld.post_shader:addEffect("tiltshift", 4.0) lightWorld.post_shader:addEffect("tilt_shift", 4.0)
else else
lightWorld.post_shader:removeEffect("tiltshift") lightWorld.post_shader:removeEffect("tilt_shift")
end end
elseif k == "f11" then elseif k == "f11" then

View File

@ -6,6 +6,7 @@ function love.load()
x = 0 x = 0
y = 0 y = 0
scale = 1 scale = 1
colorAberration = 0.0
-- 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")
@ -41,21 +42,41 @@ end
function love.keypressed(k) function love.keypressed(k)
if k == "1" then if k == "1" then
lightWorld.post_shader:toggleEffect("4colors", {15, 56, 15}, {48, 98, 48}, {139, 172, 15}, {155, 188, 15}) lightWorld.post_shader:toggleEffect("four_colors", {15, 56, 15}, {48, 98, 48}, {139, 172, 15}, {155, 188, 15})
elseif k == "2" then elseif k == "2" then
lightWorld.post_shader:toggleEffect("monochrome") lightWorld.post_shader:toggleEffect("monochrome")
elseif k == "3" then elseif k == "3" then
lightWorld.post_shader:toggleEffect("scanlines") lightWorld.post_shader:toggleEffect("scanlines")
elseif k == "4" then elseif k == "4" then
lightWorld.post_shader:toggleEffect("tiltshift", 4.0) lightWorld.post_shader:toggleEffect("tilt_shift", 4.0)
elseif k == "5" then elseif k == "5" then
lightWorld.post_shader:toggleEffect("bloom", 2.0, 0.25) lightWorld.post_shader:toggleEffect("bloom", 2.0, 0.25)
elseif k == "6" then elseif k == "6" then
lightWorld.post_shader:toggleEffect("blur", 2.0, 2.0) lightWorld.post_shader:toggleEffect("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)
elseif k == "7" then elseif k == "7" then
testShader = testShader + 1 lightWorld.post_shader:toggleEffect("black_and_white")
lightWorld.post_shader:addEffect("test", testShader) elseif k == "8" then
lightWorld.post_shader:toggleEffect("curvature")
elseif k == "9" then
lightWorld.post_shader:toggleEffect("edges")
elseif k == "0" then
lightWorld.post_shader:toggleEffect("hdr_tv")
elseif k == "q" then
lightWorld.post_shader:toggleEffect("phosphor")
elseif k == "w" then
lightWorld.post_shader:toggleEffect("phosphorish")
elseif k == "e" then
lightWorld.post_shader:toggleEffect("pip")
elseif k == "r" then
lightWorld.post_shader:toggleEffect("pixellate")
elseif k == "t" then
lightWorld.post_shader:toggleEffect("radialblur")
elseif k == "y" then
lightWorld.post_shader:toggleEffect("waterpaint")
elseif k == "c" then
if colorAberration == 0.0 then
colorAberration = 3.0
end
end end
end end
@ -79,6 +100,15 @@ function love.update(dt)
elseif love.keyboard.isDown("=") then elseif love.keyboard.isDown("=") then
scale = scale + 0.01 scale = scale + 0.01
end end
colorAberration = math.max(0.0, colorAberration - dt * 10.0)
if colorAberration > 0.0 then
lightWorld.post_shader:addEffect("blur", 2.0, 2.0)
lightWorld.post_shader:addEffect("chromatic_aberration")
else
lightWorld.post_shader:removeEffect("blur")
lightWorld.post_shader:removeEffect("chromatic_aberration")
end
lightMouse:setPosition(love.mouse.getX()/scale, love.mouse.getY()/scale) lightMouse:setPosition(love.mouse.getX()/scale, love.mouse.getY()/scale)
end end

View File

@ -173,7 +173,7 @@ function light_world:drawPixelShadow(l,t,w,h,s)
self.normalMap:clear() self.normalMap:clear()
util.drawto(self.normalMap, l, t, s, function() util.drawto(self.normalMap, l, t, s, function()
for i = 1, #self.body do for i = 1, #self.body do
self.body[i]:drawPixelShadow(l,t,w,h) self.body[i]:drawPixelShadow()
end end
end) end)
@ -196,7 +196,7 @@ end
-- draw material -- draw material
function light_world:drawMaterial(l,t,w,h,s) function light_world:drawMaterial(l,t,w,h,s)
for i = 1, #self.body do for i = 1, #self.body do
self.body[i]:drawMaterial(l,t,w,h) self.body[i]:drawMaterial()
end end
end end
@ -222,7 +222,7 @@ function light_world:drawGlow(l,t,w,h,s)
end end
for i = 1, #self.body do for i = 1, #self.body do
self.body[i]:drawGlow(l,t,w,h) self.body[i]:drawGlow()
end end
end) end)
@ -239,7 +239,7 @@ function light_world:drawRefraction(l,t,w,h,s)
self.refractionMap:clear() self.refractionMap:clear()
util.drawto(self.refractionMap, l, t, s, function() util.drawto(self.refractionMap, l, t, s, function()
for i = 1, #self.body do for i = 1, #self.body do
self.body[i]:drawRefraction(l,t,w,h) self.body[i]:drawRefraction()
end end
end) end)
@ -259,7 +259,7 @@ function light_world:drawReflection(l,t,w,h,s)
self.reflectionMap:clear(0, 0, 0) self.reflectionMap:clear(0, 0, 0)
util.drawto(self.reflectionMap, l, t, s, function() util.drawto(self.reflectionMap, l, t, s, function()
for i = 1, #self.body do for i = 1, #self.body do
self.body[i]:drawReflection(l,t,w,h) self.body[i]:drawReflection()
end end
end) end)

View File

@ -29,12 +29,25 @@ local post_shader = class()
post_shader.blurv = love.graphics.newShader(_PACKAGE.."/shaders/blurv.glsl") post_shader.blurv = love.graphics.newShader(_PACKAGE.."/shaders/blurv.glsl")
post_shader.blurh = love.graphics.newShader(_PACKAGE.."/shaders/blurh.glsl") post_shader.blurh = love.graphics.newShader(_PACKAGE.."/shaders/blurh.glsl")
post_shader.contrast = love.graphics.newShader(_PACKAGE.."/shaders/postshaders/contrast.glsl") post_shader.contrast = love.graphics.newShader(_PACKAGE.."/shaders/postshaders/contrast.glsl")
post_shader.chromatic_aberration = love.graphics.newShader(_PACKAGE.."/shaders/postshaders/chromatic_aberration.glsl")
post_shader.four_color = love.graphics.newShader(_PACKAGE.."/shaders/postshaders/four_colors.glsl")
post_shader.monochrome = love.graphics.newShader(_PACKAGE.."/shaders/postshaders/monochrome.glsl")
post_shader.scanlines = love.graphics.newShader(_PACKAGE.."/shaders/postshaders/scanlines.glsl")
post_shader.tilt_shift = love.graphics.newShader(_PACKAGE.."/shaders/postshaders/tilt_shift.glsl") post_shader.tilt_shift = love.graphics.newShader(_PACKAGE.."/shaders/postshaders/tilt_shift.glsl")
local files = love.filesystem.getDirectoryItems(_PACKAGE.."/shaders/postshaders")
local shaders = {}
for i,v in ipairs(files) do
local name = _PACKAGE.."/shaders/postshaders".."/"..v
if love.filesystem.isFile(name) then
local str = love.filesystem.read(name)
local effect = love.graphics.newShader(name)
local defs = {}
for vtype, extern in str:gmatch("extern (%w+) (%w+)") do
defs[extern] = true
end
local shaderName = name:match(".-([^\\|/]-[^%.]+)$"):gsub("%.glsl", "")
shaders[shaderName] = {effect, defs}
end
end
function post_shader:init() function post_shader:init()
self:refreshScreenSize() self:refreshScreenSize()
self.effects = {} self.effects = {}
@ -48,7 +61,13 @@ function post_shader:refreshScreenSize(w, h)
post_shader.blurv:send("screen", {w, h}) post_shader.blurv:send("screen", {w, h})
post_shader.blurh:send("screen", {w, h}) post_shader.blurh:send("screen", {w, h})
post_shader.scanlines:send("screen", {w, h}) for shaderName, v in pairs(shaders) do
for def in pairs(v[2]) do
if def == "screen" or def == "textureSize" or def == "inputSize" or def == "outputSize" then
v[1]:send(def, {w, h})
end
end
end
self.w = w self.w = w
self.h = h self.h = h
@ -76,18 +95,10 @@ function post_shader:drawWith(canvas)
self:drawBloom(canvas, args) self:drawBloom(canvas, args)
elseif shader == "blur" then elseif shader == "blur" then
self:drawBlur(canvas, args) self:drawBlur(canvas, args)
elseif shader == "chromatic" then elseif shader == "tilt_shift" then
self:drawChromatic(canvas, args) self:drawTiltShift(canvas, args)
elseif shader == "4colors" then else
self:draw4Color(canvas, args) self:drawShader(shader, canvas, args)
elseif shader == "monochrome" then
self:drawMonochome(canvas, args)
elseif shader == "scanlines" then
self:drawScanlines(canvas, args)
elseif shader == "tiltshift" then
self:drawTiltshift(canvas, args)
elseif shader == "test" then
self:drawTest(canvas, args)
end end
end end
util.drawCanvasToCanvas(canvas) util.drawCanvasToCanvas(canvas)
@ -111,83 +122,46 @@ function post_shader:drawBlur(canvas, args)
util.drawCanvasToCanvas(self.back_buffer, canvas) util.drawCanvasToCanvas(self.back_buffer, canvas)
end end
function post_shader:drawChromatic(canvas, args) function post_shader:drawTiltShift(canvas, args)
post_shader.chromatic_aberration:send("redStrength", {args[1] or 0.0, args[2] or 0.0}) post_shader.blurv:send("steps", args[1] or 2.0)
post_shader.chromatic_aberration:send("greenStrength", {args[3] or 0.0, args[4] or 0.0}) post_shader.blurh:send("steps", args[2] or 2.0)
post_shader.chromatic_aberration:send("blueStrength", {args[5] or 0.0, args[6] or 0.0}) util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = post_shader.blurv})
util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = post_shader.chromatic_aberration}) util.drawCanvasToCanvas(self.back_buffer, self.back_buffer, {shader = post_shader.blurh})
util.drawCanvasToCanvas(self.back_buffer, canvas)
end
function post_shader:draw4Color(canvas, args)
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])
util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = post_shader.four_color})
util.drawCanvasToCanvas(self.back_buffer, canvas)
end
function post_shader:drawMonochome(canvas, args)
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())
util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = post_shader.monochrome})
util.drawCanvasToCanvas(self.back_buffer, canvas)
end
function post_shader:drawScanlines(canvas, args)
post_shader.scanlines:send("strength", args[1] or 2.0)
post_shader.scanlines:send("time", args[2] or love.timer.getTime())
util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = post_shader.scanlines})
util.drawCanvasToCanvas(self.back_buffer, canvas)
end
function post_shader:drawTiltshift(canvas, args)
post_shader.tilt_shift:send("imgBuffer", canvas) post_shader.tilt_shift:send("imgBuffer", canvas)
util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = post_shader.tilt_shift}) util.drawCanvasToCanvas(self.back_buffer, canvas, {shader = post_shader.tilt_shift})
util.drawCanvasToCanvas(self.back_buffer, canvas)
end end
local files = love.filesystem.getDirectoryItems(_PACKAGE.."/shaders/postshaders/test") function post_shader:drawShader(shaderName, canvas, args)
local testShaders = {}
for i,v in ipairs(files) do
local name = _PACKAGE.."/shaders/postshaders/test".."/"..v
if love.filesystem.isFile(name) then
local str = love.filesystem.read(name)
local effect = love.graphics.newShader(name)
local defs = {}
for vtype, extern in str:gmatch("extern (%w+) (%w+)") do
defs[extern] = true
end
testShaders[#testShaders+1] = {effect, defs, name}
end
end
function post_shader:drawTest(canvas, args)
local w, h = love.graphics.getWidth(), love.graphics.getHeight() local w, h = love.graphics.getWidth(), love.graphics.getHeight()
local scale = 1 local current_arg = 1
local defaults = {
textureSize = {w, h},
inputSize = {w, h},
outputSize = {w, h},
time = love.timer.getTime()
}
local effect = testShaders[args[1]] local effect = shaders[shaderName]
if effect == nil then
print("no shader called "..shaderName)
return
end
for def in pairs(effect[2]) do for def in pairs(effect[2]) do
if defaults[def] then if def == "time" then
effect[1]:send(def, defaults[def]) effect[1]:send("time", love.timer.getTime())
elseif def == "palette" then
effect[1]:send("palette", unpack(process_palette({
args[current_arg],
args[current_arg + 1],
args[current_arg + 2],
args[current_arg + 3]
})))
current_arg = current_arg + 4
elseif def == "tint" then
effect[1]:send("tint", {process_tint(args[1], args[2], args[3])})
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
end end
@ -195,4 +169,15 @@ function post_shader:drawTest(canvas, args)
util.drawCanvasToCanvas(self.back_buffer, canvas) util.drawCanvasToCanvas(self.back_buffer, canvas)
end end
function process_tint(r, g, b)
return (r and r/255.0 or 1.0), (g and g/255.0 or 1.0), (b and b/255.0 or 1.0)
end
function process_palette(palette)
for i = 1, #palette do
palette[i] = {process_tint(unpack(palette[i]))}
end
return palette
end
return post_shader return post_shader

View File

@ -9,4 +9,4 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords){
} else { } else {
return vec4(pixel.rgb * (0.5 - texture_coords.y) * 2.0 + pixelBuffer.rgb * texture_coords.y * 2.0, 1.0); return vec4(pixel.rgb * (0.5 - texture_coords.y) * 2.0 + pixelBuffer.rgb * texture_coords.y * 2.0, 1.0);
} }
} }