Skip to content
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ Lastly, wrap the things you want to be drawn with the effect inside a function:

```lua
function love.draw()
local x, y = 0, 0
effect(function()
love.graphics.rectangle("fill", 300,200, 200,200)
end)
end, x, y) -- allows passing of arguments which can be used in effect's draw call
-- for now, check glow.lua how it's used.
end
```

Expand Down Expand Up @@ -202,6 +204,7 @@ Currently, moonshine contains the following effects (in alphabetical order):
* [dmg](#effect-dmg): Gameboy and other four color palettes
* [fastgaussianblur](#effect-fastgaussianblur): faster Gaussian blurring
* [filmgrain](#effect-filmgrain): image noise
* [fog](#effect-fog): Procedural fog
* [gaussianblur](#effect-gaussianblur): Gaussian blurring
* [glow](#effect-glow): aka (light bloom
* [godsray](#effect-godsray): aka light scattering
Expand Down Expand Up @@ -373,7 +376,11 @@ Name | Type | Default
-----|------|--------
min_luma | number between 0 and 1 | 0.7
strength | number >= 0 | 5
size | table{w, h} | 1280, 640

`size` is used for the canvas size. Previously moonshine uses the size of the
whole window, this can lead to performance slowdown especially in very big
window size. Make this smaller accordingly to your game for performance boost.

<a name="effect-godsray"></a>
### godsray
Expand Down
38 changes: 32 additions & 6 deletions glow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,33 @@ return function(moonshine)
return c * step(min_luma, luma) * color;
}]]

local w = 1280/4
local h = 720/4
local scene = love.graphics.newCanvas(w, h)

local dir = {1/w, 0}
local strength = 5

local setters = {}
setters.strength = function(v)
blurshader = make_blur_shader(math.max(0,tonumber(v) or 1))
strength = v
end
setters.min_luma = function(v)
threshold:send("min_luma", math.max(0, math.min(1, tonumber(v) or 0.5)))
end
setters.dir = function(v)
dir[1] = v[1]/w
dir[2] = v[2]/h
blurshader:send("direction", {dir[1], dir[2]})
end
setters.size = function(v)
w = v[1]
h = v[2]
scene = love.graphics.newCanvas(w, h)
end

local scene = love.graphics.newCanvas()
local draw = function(buffer)
local draw = function(buffer, shader, tx, ty)
local front, back = buffer() -- scene so far is in `back'
scene, back = back, scene -- save it for second draw below

Expand All @@ -69,8 +86,8 @@ return function(moonshine)
love.graphics.setShader(threshold)
love.graphics.draw(scene)

-- 2nd pass: apply blur shader in x
blurshader:send('direction', {1 / love.graphics.getWidth(), 0})
-- 2nd pass: apply blur shader in dir
blurshader:send('direction', dir)
love.graphics.setCanvas(back)
love.graphics.clear()
love.graphics.setShader(blurshader)
Expand All @@ -83,12 +100,16 @@ return function(moonshine)
-- original scene without blur shader
love.graphics.setShader()
love.graphics.setBlendMode("add", "premultiplied")
love.graphics.push()
love.graphics.translate(tx or 0, ty or 0)
love.graphics.draw(scene) -- original scene

-- second pass of light blurring
blurshader:send('direction', {0, 1 / love.graphics.getHeight()})
blurshader:send('direction', {0, 1 / h})
love.graphics.setShader(blurshader)

love.graphics.draw(back)
love.graphics.pop()

-- restore things as they were before entering draw()
love.graphics.setBlendMode("alpha", "premultiplied")
Expand All @@ -99,6 +120,11 @@ return function(moonshine)
name = "glow",
draw = draw,
setters = setters,
defaults = {min_luma=.7, strength = 5}
defaults = {
min_luma=.7,
strength = 5,
dir = {1/w, 0},
size = {w, h},
}
}
end
2 changes: 1 addition & 1 deletion init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ moonshine.chain = function(w,h,effect)
love.graphics.setBlendMode("alpha", "premultiplied")
for _,e in ipairs(chain) do
if not disabled[e.name] then
(e.draw or moonshine.draw_shader)(buffer, e.shader)
(e.draw or moonshine.draw_shader)(buffer, e.shader, ...)
end
end

Expand Down