Difference between revisions of "love.graphics.setStencil"

m (added image stencil example)
m
Line 103: Line 103:
 
{{#set:Description=Defines or releases a stencil.}}
 
{{#set:Description=Defines or releases a stencil.}}
 
{{#set:Since=080}}
 
{{#set:Since=080}}
 +
{{#set:Sub-Category=State}}
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|love.graphics.setStencil}}
 
{{i18n|love.graphics.setStencil}}

Revision as of 01:05, 29 March 2013

Available since LÖVE 0.8.0
This function is not supported in earlier versions.

Defines or releases a stencil for the drawing operations.

The passed function draws to the stencil instead of the screen, creating an image with transparent and opaque pixels. While active, it is used to test where pixels will be drawn or discarded. Image contents do not directly affect the stencil, but see below for a workaround.

Calling the function without arguments releases the active stencil.

Function

Synopsis

love.graphics.setStencil( stencilFunction )

Arguments

function stencilFunction
Function that draws the stencil.

Returns

Nothing.

Function

Synopsis

love.graphics.setStencil( )

Arguments

None.

Returns

Nothing.

Notes

Releases the active stencil.

Examples

Drawing circles masked by a rectangle

myStencilFunction = function()
   love.graphics.rectangle("fill", 225, 200, 350, 300)
end

myStencil = love.graphics.newStencil(myStencilFunction)

love.graphics.setStencil(myStencil)

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)

Drawing a circle with a hole

myStencilFunction = function()
   love.graphics.circle("fill", 400, 300, 50)
end

myStencil = love.graphics.newStencil(myStencilFunction)

love.graphics.setInvertedStencil(myStencil)
love.graphics.circle("fill", 400, 300, 150)

Drawing two masked triangles with different colors

myStencilFunction = function()
   love.graphics.circle("fill", 400, 300, 60, 25)
end

myStencil = love.graphics.newStencil(myStencilFunction)

love.graphics.setStencil(myStencil)
love.graphics.setColor(155, 0, 128)
love.graphics.triangle("fill", 400, 200, 486, 350, 314, 350)


love.graphics.setInvertedStencil(myStencil)
love.graphics.setColor(144, 214, 128)
love.graphics.triangle("fill", 400, 200, 486, 350, 314, 350)

Using an Image as a mask

-- a black/white mask image: black pixels will mask, white pixels will pass.
local mask = love.graphics.newImage("mymask.png")

local mask_effect = love.graphics.newPixelEffect [[
   vec4 effect ( vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords ) {
      // a discarded fragment will fail the stencil test.
      if (Texel(texture, texture_coords).rgb == vec3(0.0))
         discard;
      return vec4(1.0);
   }
]]

myStencilFunction = function ()
   love.graphics.setPixelEffect(mask_effect)
   love.graphics.draw(mask, 0, 0)
   love.graphics.setPixelEffect()
end

myStencil = love.graphics.newStencil(myStencilFunction)

love.graphics.setStencil(myStencil)
love.graphics.rectangle("fill", 0, 0, 256, 256)

See Also


Other Languages