2014-04-08 17:45:21 +00:00
|
|
|
--[[
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
|
|
|
Copyright (c) 2014 Marcus Ihde
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
]]
|
2014-09-29 14:03:15 +00:00
|
|
|
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
|
|
|
local class = require(_PACKAGE..'/class')
|
2014-10-22 12:37:19 +00:00
|
|
|
local util = require(_PACKAGE..'/util')
|
2014-04-08 17:45:21 +00:00
|
|
|
|
2014-10-06 13:31:14 +00:00
|
|
|
local post_shader = class()
|
2014-10-06 21:27:41 +00:00
|
|
|
post_shader.blurv = love.graphics.newShader(_PACKAGE.."/shaders/blurv.glsl")
|
|
|
|
post_shader.blurh = love.graphics.newShader(_PACKAGE.."/shaders/blurh.glsl")
|
2014-10-08 12:55:05 +00:00
|
|
|
post_shader.contrast = love.graphics.newShader(_PACKAGE.."/shaders/postshaders/contrast.glsl")
|
|
|
|
post_shader.tilt_shift = love.graphics.newShader(_PACKAGE.."/shaders/postshaders/tilt_shift.glsl")
|
2014-10-06 21:27:41 +00:00
|
|
|
|
2014-10-24 01:35:35 +00:00
|
|
|
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
|
|
|
|
|
2014-10-06 21:27:41 +00:00
|
|
|
function post_shader:init()
|
|
|
|
self:refreshScreenSize()
|
|
|
|
self.effects = {}
|
|
|
|
end
|
|
|
|
|
2014-10-08 12:55:05 +00:00
|
|
|
function post_shader:refreshScreenSize(w, h)
|
|
|
|
w, h = w or love.window.getWidth(), h or love.window.getHeight()
|
2014-10-06 21:27:41 +00:00
|
|
|
|
2014-10-08 12:55:05 +00:00
|
|
|
self.render_buffer = love.graphics.newCanvas(w, h)
|
|
|
|
self.back_buffer = love.graphics.newCanvas(w, h)
|
|
|
|
|
|
|
|
self.w = w
|
|
|
|
self.h = h
|
2014-10-06 21:27:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
self:addEffect(shaderName, ...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-22 12:37:19 +00:00
|
|
|
function post_shader:drawWith(canvas)
|
2014-10-06 21:27:41 +00:00
|
|
|
for shader, args in pairs(self.effects) do
|
|
|
|
if shader == "bloom" then
|
|
|
|
self:drawBloom(canvas, args)
|
|
|
|
elseif shader == "blur" then
|
|
|
|
self:drawBlur(canvas, args)
|
2014-10-24 01:35:35 +00:00
|
|
|
elseif shader == "tilt_shift" then
|
|
|
|
self:drawTiltShift(canvas, args)
|
|
|
|
else
|
|
|
|
self:drawShader(shader, canvas, args)
|
2014-10-06 21:27:41 +00:00
|
|
|
end
|
|
|
|
end
|
2014-10-22 12:37:19 +00:00
|
|
|
util.drawCanvasToCanvas(canvas)
|
2014-10-06 21:27:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function post_shader:drawBloom(canvas, args)
|
|
|
|
post_shader.blurv:send("steps", args[1] or 2.0)
|
|
|
|
post_shader.blurh:send("steps", args[1] or 2.0)
|
2014-10-22 12:37:19 +00:00
|
|
|
util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = post_shader.blurv})
|
|
|
|
util.drawCanvasToCanvas(self.back_buffer, self.back_buffer, {shader = post_shader.blurh})
|
|
|
|
util.drawCanvasToCanvas(self.back_buffer, self.back_buffer, {shader = post_shader.contrast})
|
|
|
|
util.drawCanvasToCanvas(canvas, canvas, {shader = post_shader.contrast})
|
|
|
|
util.drawCanvasToCanvas(self.back_buffer, canvas, {blendmode = "additive", color = {255, 255, 255, (args[2] or 0.25) * 255}})
|
2014-10-06 21:27:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function post_shader:drawBlur(canvas, args)
|
|
|
|
post_shader.blurv:send("steps", args[1] or 2.0)
|
|
|
|
post_shader.blurh:send("steps", args[2] or 2.0)
|
2014-10-22 12:37:19 +00:00
|
|
|
util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = post_shader.blurv})
|
|
|
|
util.drawCanvasToCanvas(self.back_buffer, self.back_buffer, {shader = post_shader.blurh})
|
|
|
|
util.drawCanvasToCanvas(self.back_buffer, canvas)
|
2014-10-06 21:27:41 +00:00
|
|
|
end
|
|
|
|
|
2014-10-24 01:35:35 +00:00
|
|
|
function post_shader:drawTiltShift(canvas, args)
|
|
|
|
post_shader.blurv:send("steps", args[1] or 2.0)
|
|
|
|
post_shader.blurh:send("steps", args[2] or 2.0)
|
|
|
|
util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = post_shader.blurv})
|
|
|
|
util.drawCanvasToCanvas(self.back_buffer, self.back_buffer, {shader = post_shader.blurh})
|
|
|
|
post_shader.tilt_shift:send("imgBuffer", canvas)
|
|
|
|
util.drawCanvasToCanvas(self.back_buffer, canvas, {shader = post_shader.tilt_shift})
|
2014-10-06 21:27:41 +00:00
|
|
|
end
|
|
|
|
|
2014-10-24 01:35:35 +00:00
|
|
|
function post_shader:drawShader(shaderName, canvas, args)
|
|
|
|
local w, h = love.graphics.getWidth(), love.graphics.getHeight()
|
|
|
|
local current_arg = 1
|
2014-03-04 13:52:51 +00:00
|
|
|
|
2014-10-24 01:35:35 +00:00
|
|
|
local effect = shaders[shaderName]
|
|
|
|
if effect == nil then
|
|
|
|
print("no shader called "..shaderName)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
for def in pairs(effect[2]) do
|
|
|
|
if def == "time" then
|
|
|
|
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
|
2014-10-06 21:27:41 +00:00
|
|
|
end
|
|
|
|
end
|
2014-03-19 22:22:46 +00:00
|
|
|
|
2014-10-24 01:35:35 +00:00
|
|
|
util.drawCanvasToCanvas(canvas, self.back_buffer, {shader = effect[1]})
|
2014-10-22 12:37:19 +00:00
|
|
|
util.drawCanvasToCanvas(self.back_buffer, canvas)
|
2014-04-08 17:45:21 +00:00
|
|
|
end
|
|
|
|
|
2014-10-24 01:35:35 +00:00
|
|
|
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)
|
2014-09-26 17:38:55 +00:00
|
|
|
end
|
2014-10-06 21:27:41 +00:00
|
|
|
|
2014-10-24 01:35:35 +00:00
|
|
|
function process_palette(palette)
|
|
|
|
for i = 1, #palette do
|
|
|
|
palette[i] = {process_tint(unpack(palette[i]))}
|
2014-10-08 12:55:05 +00:00
|
|
|
end
|
2014-10-24 01:35:35 +00:00
|
|
|
return palette
|
2014-10-08 12:55:05 +00:00
|
|
|
end
|
|
|
|
|
2014-10-06 21:27:41 +00:00
|
|
|
return post_shader
|