Skip to content

Commit

Permalink
added shader library and license
Browse files Browse the repository at this point in the history
  • Loading branch information
Retrisma committed Jul 24, 2023
1 parent 811bcb4 commit c653a20
Show file tree
Hide file tree
Showing 21 changed files with 1,535 additions and 4 deletions.
19 changes: 19 additions & 0 deletions license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

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.
13 changes: 9 additions & 4 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ require "sprite"
require "ui"
require "actor"
require "tools"
sti = require("sti")
sti = require "sti"
moonshine = require "moonshine"

showdebug = true
debug = 0
Expand All @@ -31,9 +32,11 @@ function love.load()

map = loadmap("sample")

debug = gettile("Template", -1, -1)

Button:add(180, 40, "button", function() showdebug = not showdebug end)

effect = moonshine(moonshine.effects.filmgrain)
.chain(moonshine.effects.vignette)
effect.filmgrain.size = 2
end

function love.update(dt)
Expand Down Expand Up @@ -68,7 +71,9 @@ function love.draw()
--set drawing offset to camera position
love.graphics.translate(camera.x * window.scale, camera.y * window.scale)
--draw tiled map
map:draw(camera.x, camera.y, window.scale)
effect(function()
map:draw(camera.x, camera.y, window.scale)
end)

for _,v in pairs(p) do
v:draw()
Expand Down
62 changes: 62 additions & 0 deletions moonshine/boxblur.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
--[[
Public domain:
Copyright (C) 2017 by Matthias Richter <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
]]--

return function(moonshine)
local radius_x, radius_y = 3, 3
local shader = love.graphics.newShader[[
extern vec2 direction;
extern number radius;
vec4 effect(vec4 color, Image texture, vec2 tc, vec2 _) {
vec4 c = vec4(0.0);
for (float i = -radius; i <= radius; i += 1.0)
{
c += Texel(texture, tc + i * direction);
}
return c / (2.0 * radius + 1.0) * color;
}]]

local setters = {}
setters.radius = function(v)
if type(v) == "number" then
radius_x, radius_y = v, v
elseif type(v) == "table" and #v >= 2 then
radius_x, radius_y = tonumber(v[1] or v.h or v.x), tonumber(v[2] or v.v or v.y)
else
error("Invalid argument `radius'")
end
end
setters.radius_x = function(v) radius_x = tonumber(v) end
setters.radius_y = function(v) radius_y = tonumber(v) end

local draw = function(buffer)
shader:send('direction', {1 / love.graphics.getWidth(), 0})
shader:send('radius', math.floor(radius_x + .5))
moonshine.draw_shader(buffer, shader)

shader:send('direction', {0, 1 / love.graphics.getHeight()})
shader:send('radius', math.floor(radius_y + .5))
moonshine.draw_shader(buffer, shader)
end

return moonshine.Effect{
name = "boxblur",
draw = draw,
setters = setters,
defaults = {radius = 3}
}
end
49 changes: 49 additions & 0 deletions moonshine/chromasep.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--[[
Public domain:
Copyright (C) 2017 by Matthias Richter <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
]]--

return function(moonshine)
local shader = love.graphics.newShader[[
extern vec2 direction;
vec4 effect(vec4 color, Image texture, vec2 tc, vec2 _)
{
return color * vec4(
Texel(texture, tc - direction).r,
Texel(texture, tc).g,
Texel(texture, tc + direction).b,
1.0);
}]]

local angle, radius = 0, 0
local setters = {
angle = function(v) angle = tonumber(v) or 0 end,
radius = function(v) radius = tonumber(v) or 0 end
}

local draw = function(buffer, effect)
local dx = math.cos(angle) * radius / love.graphics.getWidth()
local dy = math.sin(angle) * radius / love.graphics.getHeight()
shader:send("direction", {dx,dy})
moonshine.draw_shader(buffer, shader)
end

return moonshine.Effect{
name = "chromasep",
draw = draw,
setters = setters,
defaults = {angle = 0, radius = 0}
}
end
33 changes: 33 additions & 0 deletions moonshine/colorgradesimple.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--[[
Public domain:
Copyright (C) 2017 by Matthias Richter <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
]]--

return function(moonshine)
local shader = love.graphics.newShader[[
extern vec3 factors;
vec4 effect(vec4 color, Image texture, vec2 tc, vec2 _) {
return vec4(factors, 1.0) * Texel(texture, tc) * color;
}]]

local setters = {}

return moonshine.Effect{
name = "colorgradesimple",
shader = shader,
setters = {factors = function(v) shader:send("factors", v) end},
defaults = {factors = {1,1,1}}
}
end
79 changes: 79 additions & 0 deletions moonshine/crt.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
--[[
Public domain:
Copyright (C) 2017 by Matthias Richter <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
]]--

return function(moonshine)
-- Barrel distortion adapted from Daniel Oaks (see commit cef01b67fd)
-- Added feather to mask out outside of distorted texture
local distortionFactor
local shader = love.graphics.newShader[[
extern vec2 distortionFactor;
extern vec2 scaleFactor;
extern number feather;
vec4 effect(vec4 color, Image tex, vec2 uv, vec2 px) {
// to barrel coordinates
uv = uv * 2.0 - vec2(1.0);
// distort
uv *= scaleFactor;
uv += (uv.yx*uv.yx) * uv * (distortionFactor - 1.0);
number mask = (1.0 - smoothstep(1.0-feather,1.0,abs(uv.x)))
* (1.0 - smoothstep(1.0-feather,1.0,abs(uv.y)));
// to cartesian coordinates
uv = (uv + vec2(1.0)) / 2.0;
return color * Texel(tex, uv) * mask;
}
]]

local setters = {}

setters.distortionFactor = function(v)
assert(type(v) == "table" and #v == 2, "Invalid value for `distortionFactor'")
distortionFactor = {unpack(v)}
shader:send("distortionFactor", v)
end

setters.x = function(v) setters.distortionFactor{v, distortionFactor[2]} end
setters.y = function(v) setters.distortionFactor{distortionFactor[1], v} end

setters.scaleFactor = function(v)
if type(v) == "table" and #v == 2 then
shader:send("scaleFactor", v)
elseif type(v) == "number" then
shader:send("scaleFactor", {v,v})
else
error("Invalid value for `scaleFactor'")
end
end

setters.feather = function(v) shader:send("feather", v) end

local defaults = {
distortionFactor = {1.06, 1.065},
feather = 0.02,
scaleFactor = 1,
}

return moonshine.Effect{
name = "crt",
shader = shader,
setters = setters,
defaults = defaults
}
end
52 changes: 52 additions & 0 deletions moonshine/desaturate.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--[[
Public domain:
Copyright (C) 2017 by Matthias Richter <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
]]--

return function(moonshine)
local shader = love.graphics.newShader[[
extern vec4 tint;
extern number strength;
vec4 effect(vec4 color, Image texture, vec2 tc, vec2 _) {
color = Texel(texture, tc);
number luma = dot(vec3(0.299, 0.587, 0.114), color.rgb);
return mix(color, tint * luma, strength);
}]]

local setters = {}

setters.tint = function(c)
assert(type(c) == "table" and #c == 3, "Invalid value for `tint'")
shader:send("tint", {
(tonumber(c[1]) or 0) / 255,
(tonumber(c[2]) or 0) / 255,
(tonumber(c[3]) or 0) / 255,
1
})
end

setters.strength = function(v)
shader:send("strength", math.max(0, math.min(1, tonumber(v) or 0)))
end

local defaults = {tint = {255,255,255}, strength = 0.5}

return moonshine.Effect{
name = "desaturate",
shader = shader,
setters = setters,
defaults = defaults
}
end
Loading

0 comments on commit c653a20

Please sign in to comment.