Home

Awesome

moonshine

Chainable post-processing shaders for LÖVE.

Overview

<a name="getting-started"></a>

Getting started

Clone this repository into your game folder:

git clone https://github.com/vrld/moonshine.git

This will create the folder moonshine.

In your main.lua, or wherever you load your libraries, add the following:

local moonshine = require 'moonshine'

Create and parametrize the post-processing effect in love.load(), for example:

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

Lastly, wrap the things you want to be drawn with the effect inside a function:

function love.draw()
    effect(function()
      love.graphics.rectangle("fill", 300,200, 200,200)
    end)
end

When you package your game for release, you might want consider deleting the (hidden) .git folder in the moonshine directory.

<a name="general-usage"></a>

General usage

The main concept behind moonshine are chains. A chain consists of one or more effects. Effects that come later in the chain will be applied to the result of the effects that come before. In the example above, the vignette is drawn on top of the filmgrain.

Chains

Chains are created using the moonshine.chain function:

chain = moonshine.chain(effect)

For convenience, moonshine(effect) is an alias to moonshine.chain(effect). You can add new effects to a chain using

chain = chain.chain(another_effect)

or using chain.next(), which is an alias to chain.chain(). As the function returns the chain, you can specify your whole chain in one go, as shown in the example above.

Effects and effect parameters

The effects that come bundled with moonshine (see List of effects) are accessed by chain.effects.<effect-name>, e.g.,

moonshine.effects.glow

Most effects are parametrized to change how they look. In the example above, the size of the grains was set to 2 pixels (the default is 1 pixel). Effect parameters are set by first specifying the name of the effect and then the name of the parameter:

chain.<effect>.<parameter> = <value>

For example, if chain contained the glow and crt effects, you can set the glow strength parameter and crt distortionFactor parameter as such:

chain.glow.strength = 10
chain.crt.distortionFactor = {1.06, 1.065}

Because you likely initialize a bunch of parameters at once, you can set all parameters with the special key parameters (or params or settings). This is equivalent to the above:

chain.parameters = {
  glow = {strength = 10},
  crt = {distortionFactor = {1.06, 1.065}},
}

Note that this will only set the parameters specified in the table. The crt parameter feather, for example, will be left untouched.

Drawing effects

Creating effects and setting parameters is fine, but not very useful on its own. You also need to apply it to something. This is done using chain.draw():

chain.draw(func, ...)

This will apply the effect to everything that is drawn inside func(...). Everything that is drawn outside of func(...) will not be affected. For example,

love.graphics.draw(img1, 0,0)
chain.draw(function()
  love.graphics.draw(img2, 200,0)
end)
love.graphics.draw(img3, 400,0)

will apply the effect to img2, but not to img1 and img3. Note that some effects (like filmgrain) draw on the whole screen. So if in this example chain would consist of a gaussianblur and filmgrain effect, img1 will be covered with grain, but will not be blurred, img2 will get both effects, and img3 will be left untouched.

Similar to chain creation, chain(func, ...) is an alias to the more verbose chain.draw(func, ...).

Temporarily disabling effects

You can disable effects in a chain by using chain.disable(names...) and chain.enable(names...). For example,

effect = moonshine(moonshine.effects.boxblur)
                  .chain(moonshine.effects.filmgrain)
                  .chain(moonshine.effects.vignette)
effect.disable("boxblur", "filmgrain")
effect.enable("filmgrain")

would first disable the boxblur and filmgrain effect, and then enable the filmgrain again. Note that the effects are still in the chain, they are only not drawn.

Canvas size

You can change the size of the internal canvas, for example when the window was resized, by calling chain.resize(width, height). Do this anytime you want, but best not during chain.draw().

You can also specify the initial canvas size by starting the chain like this:

effect = moonshine(400,300, moonshine.effects.vignette)

That is, you specify the width and height before the first effect in the chain.

Is this efficient?

Of course, using moonshine is not as efficient as writing your own shader that does all the effects you want in the least amount of passes, but moonshine tries to minimize the overhead.

On the other hand, you don't waste time writing the same shader over and over again when using moonshine: You're trading a small amount of computation time for a large amount of development time.

<a name="list-of-effects"></a>

List of effects

Currently, moonshine contains the following effects (in alphabetical order):

<a name="effect-boxblur"></a>

boxblur

moonshine.effects.boxblur

Parameters:

NameTypeDefault
radiusnumber or table of numbers{3,3}
radius_xnumber3
radius_ynumber3

<a name="effect-chromasep"></a>

chromasep

moonshine.effects.chromasep

Parameters:

NameTypeDefault
anglenumber (in radians)0
radiusnumber0

<a name="effect-colorgradesimple"></a>

colorgradesimple

moonshine.effects.colorgradesimple

Parameters:

NameTypeDefault
factorstable of numbers{1,1,1}

<a name="effect-crt"></a>

crt

moonshine.effects.crt

Parameters:

NameTypeDefault
distortionFactortable of numbers{1.06, 1.065}
xnumber1.06
ynumber1.065
scaleFactornumber or table of numbers{1,1}
feathernumber0.02

<a name="effect-desaturate"></a>

desaturate

moonshine.effects.desaturate

Parameters:

NameTypeDefault
tintcolor / table of numbers{255,255,255}
strengthnumber between 0 and 10.5

<a name="effect-dmg"></a>

dmg

moonshine.effects.dmg
NameTypeDefault
palettenumber or string or table of table of numbers"default"

DMG ships with 7 palettes:

  1. default
  2. dark_yellow
  3. light_yellow
  4. green
  5. greyscale
  6. stark_bw
  7. pocket

Custom palettes must be in the format {{R,G,B}, {R,G,B}, {R,G,B}, {R,G,B}}, where R, G, and B are numbers between 0 and 255.

<a name="effect-fastgaussianblur"></a>

fastgaussianblur

moonshine.effects.fastgaussianblur

Parameters:

NameTypeDefault
tapsodd number >= 37
offsetnumber1
sigmanumber-1

<a name="effect-filmgrain"></a>

filmgrain

moonshine.effects.filmgrain

Parameters:

NameTypeDefault
opacitynumber0.3
sizenumber1

<a name="effect-gaussianblur"></a>

gaussianblur

moonshine.effects.gaussianblur

Parameters:

NameTypeDefault
sigmanumber1

<a name="effect-glow"></a>

glow

moonshine.effects.glow

Parameters:

NameTypeDefault
min_lumanumber between 0 and 10.7
strengthnumber >= 05

<a name="effect-godsray"></a>

godsray

moonshine.effects.godsray

Parameters:

NameTypeDefault
exposirenumber between 0 and 10.5
decaynumber between 0 and 10.95
densitynumber between 0 and 10.05
weightnumber between 0 and 10.5
light_positiontable of two numbers{0.5, 0.5}
light_xnumber0.5
light_ynumber0.5
samplesnumber >= 170

<a name="effect-pixelate"></a>

pixelate

moonshine.effects.pixelate

Parameters:

NameTypeDefault
sizenumber or table of two numbers{5,5}
feedbacknumber between 0 and 10

<a name="effect-posterize"></a>

posterize

moonshine.effects.posterize

Parameters:

NameTypeDefault
num_bandsnumber >= 13

<a name="effect-scanlines"></a>

scanlines

moonshine.effects.scanlines

Parameters:

NameTypeDefault
widthnumber2
frequencynumberscreen-height
phasenumber0
thicknessnumber1
opacitynumber1
colorcolor / table of numbers{0,0,0}

<a name="effect-sketch"></a>

sketch

moonshine.effects.sketch

Parameters:

NameTypeDefault
ampnumber0.0007
centertable of numbers{0,0}

<a name="effect-vignette"></a>

vignette

moonshine.effects.vignette

Parameters:

NameTypeDefault
radiusnumber > 00.8
softnessnumber > 00.5
opacitynumber > 00.5
colorcolor / table of numbers{0,0,0}

<a name="effect-fog"></a>

fog

moonshine.effects.fog

Parameters:

NameTypeDefault
fog_colorcolor/table of numbers{0.35, 0.48, 0.95}
octavesnumber > 04
speedvec2/table of numbers{0.5, 0.5}

<a name="writing-effects"></a>

Writing effects

An effect is essentially a function that returns a moonshine.Effect{}, which must specify at least a name and a shader or a draw function.

It may also specify a setters table that contains functions that set the effect parameters and a defaults table with the corresponding default values. The default values will be set when the effect is instantiated.

A good starting point to see how to write effects is the colorgradesimple effect, which uses the shader, setters and defaults fields.

Moonshine uses double buffering to draw the effects. A function to swap and access the buffers is provided to the draw(buffer) function of your effect:

front, back = buffer() -- swaps front and back buffer and returns both

You don't have to care about canvases or restoring defaults, moonshine handles all that for you.

If you only need a custom draw function because your effect needs multiple shader passes, moonshine provides the draw_shader(buffer, shader) function. As you might have guessed, this function uses shader to draw the front buffer to the back buffer. The boxblur effect gives a simple example how to use this function.

If for some reason you need more than two buffer, you are more or less on your own. You can do everything, but make sure that the blend mode and the order of back and front buffer is the same before and after your custom draw function. The glow effect gives an example of a more complicated draw function.

<a name="license"></a>

License

See here for a list of contributors.

The main library can freely be used under the following conditions:

The MIT License (MIT)

Copyright (c) 2017 Matthias Richter

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.

Most of the effects are public domain (see comments inside the files):

These effects are MIT-licensed with multiple authors: