Available since LÖVE 0.10.0
Together with love.graphics.setStencilTest, it has replaced love.graphics.setStencil.
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 - love.graphics.setStencilTest 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].
love.graphics.stencil( stencilfunction, action, value, keepvalues )
function stencilfunction
StencilAction action ("replace")
number value (1)
boolean keepvalues (false)
Nothing.
It is possible to draw to the screen and to the stencil values of pixels at the same time, by using love.graphics.setColorMask inside the stencil function to enable drawing to all color components.
local function myStencilFunction() love.graphics.rectangle("fill", 225, 200, 350, 300) end 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. love.graphics.stencil(myStencilFunction, "replace", 1) -- Only allow rendering on pixels which have a stencil value greater than 0. love.graphics.setStencilTest("greater", 0) love.graphics.setColor(255, 0, 0, 120) love.graphics.circle("fill", 300, 300, 150, 50) love.graphics.setColor(0, 255, 0, 120) love.graphics.circle("fill", 500, 300, 150, 50) love.graphics.setColor(0, 0, 255, 120) love.graphics.circle("fill", 400, 400, 150, 50) love.graphics.setStencilTest() end
-- a black/white mask image: black pixels will mask, white pixels will pass. local mask = love.graphics.newImage("mymask.png") local mask_shader = love.graphics.newShader[[ 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. discard; } return vec4(1.0); } ]] local function myStencilFunction() love.graphics.setShader(mask_shader) love.graphics.draw(mask, 0, 0) love.graphics.setShader() end function love.draw() love.graphics.stencil(myStencilFunction, "replace", 1) love.graphics.setStencilTest("greater", 0) love.graphics.rectangle("fill", 0, 0, 256, 256) love.graphics.setStencilTest() end
local function myStencilFunction() -- Draw four overlapping circles as a stencil. love.graphics.circle("fill", 200, 250, 100) love.graphics.circle("fill", 300, 250, 100) love.graphics.circle("fill", 250, 200, 100) love.graphics.circle("fill", 250, 300, 100) end 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. love.graphics.stencil(myStencilFunction, "increment") -- Only allow drawing in areas which have stencil values that are less than 2. love.graphics.setStencilTest("less", 2) -- Draw a big rectangle. love.graphics.rectangle("fill", 0, 0, 500, 500) love.graphics.setStencilTest() end
The love.graphics.setStencilTest wiki page includes more examples.
© 2006–2016 LÖVE Development Team
Licensed under the GNU Free Documentation License, Version 1.3.
https://love2d.org/wiki/love.graphics.stencil