Available since LÖVE 0.10.0
Together with, it has replaced

Draws geometry as a stencil.

The geometry drawn by the supplied function sets invisible stencil values of pixels, instead of setting pixel colors. The stencil values of pixels can act like a mask / stencil - can be used afterward to determine how further rendering is affected by the stencil values in each pixel.

Each Canvas has its own per-pixel stencil values. Stencil values are within the range of [0, 255].


Synopsis stencilfunction, action, value, keepvalues )


function stencilfunction
Function which draws geometry. The stencil values of pixels, rather than the color of each pixel, will be affected by the geometry.
StencilAction action ("replace")
How to modify any stencil values of pixels that are touched by what's drawn in the stencil function.
number value (1)
The new stencil value to use for pixels if the "replace" stencil action is used. Has no effect with other stencil actions. Must be between 0 and 255.
boolean keepvalues (false)
True to preserve old stencil values of pixels, false to re-set every pixel's stencil value to 0 before executing the stencil function. will also re-set all stencil values.




It is possible to draw to the screen and to the stencil values of pixels at the same time, by using inside the stencil function to enable drawing to all color components.


Drawing circles masked by a rectangle

local function myStencilFunction()"fill", 225, 200, 350, 300)
function love.draw()
    -- draw a rectangle as a stencil. Each pixel touched by the rectangle will have its stencil value set to 1. The rest will be 0., "replace", 1)
   -- Only allow rendering on pixels which have a stencil value greater than 0."greater", 0), 0, 0, 120)"fill", 300, 300, 150, 50), 255, 0, 120)"fill", 500, 300, 150, 50), 0, 255, 120)"fill", 400, 400, 150, 50)

Using an Image as a stencil mask

-- a black/white mask image: black pixels will mask, white pixels will pass.
local mask ="mymask.png")
local mask_shader =[[
   vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {
      if (Texel(texture, texture_coords).rgb == vec3(0.0)) {
         // a discarded pixel wont be applied as the stencil.
      return vec4(1.0);
local function myStencilFunction(), 0, 0)
function love.draw(), "replace", 1)"greater", 0)"fill", 0, 0, 256, 256)

Allow drawing everywhere except where multiple circles intersect

local function myStencilFunction()
   -- Draw four overlapping circles as a stencil."fill", 200, 250, 100)"fill", 300, 250, 100)"fill", 250, 200, 100)"fill", 250, 300, 100)
function love.draw()
   -- Each pixel touched by each circle will have its stencil value incremented by 1.
   -- The stencil values for pixels where the circles overlap will be at least 2., "increment")
   -- Only allow drawing in areas which have stencil values that are less than 2."less", 2)
   -- Draw a big rectangle."fill", 0, 0, 500, 500)

The wiki page includes more examples.

See Also

Other Languages