Difference between revisions of "love.graphics.setStencilTest"

(Created page)
 
(Drawing circles masked by rectangle, polygon and circle)
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{newin|[[0.10.0]]|100|type=function|text=Together with [[love.graphics.stencil]], it has replaced [[love.graphics.setStencil]]}}
 
{{newin|[[0.10.0]]|100|type=function|text=Together with [[love.graphics.stencil]], it has replaced [[love.graphics.setStencil]]}}
Enables or disables stencil testing.
+
Configures or disables stencil testing.
  
When stencil testing is enabled, the geometry of everything that is drawn will be clipped / stencilled out based on whether it intersects with what has been previously drawn to the [[love.graphics.stencil|stencil buffer.]]
+
When stencil testing is enabled, the geometry of everything that is drawn afterward will be clipped / stencilled out based on a comparison between the arguments of this function and the stencil value of each pixel that the geometry touches. The stencil values of pixels are affected via [[love.graphics.stencil]].
 +
 
 +
 
 +
{{notice|1=Starting with version [[11.0]], a stencil buffer must be set or requested in [[love.graphics.setCanvas]] when using stencils with a Canvas. <code>love.graphics.setCanvas{canvas, stencil=true}</code> is an easy way to use an automatically provided temporary stencil buffer in that case.}}
 +
== Function ==
 +
=== Synopsis ===
 +
<source lang="lua">
 +
love.graphics.setStencilTest( comparemode, comparevalue )
 +
</source>
 +
=== Arguments ===
 +
{{param|CompareMode|comparemode|The type of comparison to make for each pixel.}}
 +
{{param|number|comparevalue|The value to use when comparing with the stencil value of each pixel. Must be between 0 and 255.}}
 +
=== Returns ===
 +
Nothing.
  
Each [[Canvas]] has its own stencil buffer.
 
 
== Function ==
 
== Function ==
 +
Disables stencil testing.
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
love.graphics.setStencilTest( enable, invert )
+
love.graphics.setStencilTest( )
 
</source>
 
</source>
 
=== Arguments ===
 
=== Arguments ===
{{param|boolean|enable (false)|Whether to enable stencil testing.}}
+
None.
{{param|boolean|invert (false)|Whether to invert the stencil test. If true, the parts of drawn geometry that touch what has been previously drawn to the stencil buffer will be clipped. Otherwise everything else will be clipped.}}
 
 
=== Returns ===
 
=== Returns ===
 
Nothing.
 
Nothing.
 +
 
== Examples ==
 
== Examples ==
=== Drawing circles masked by a rectangle ===
+
=== Drawing circles masked by rectangle, polygon and circle ===
 
<source lang="lua">
 
<source lang="lua">
local function myStencilFunction()
+
local function myStencilFunction1()
  love.graphics.rectangle("fill", 225, 200, 350, 300)
+
love.graphics.rectangle("fill", 225, 200, 350, 300)
 +
end
 +
 
 +
local function myStencilFunction2()
 +
love.graphics.polygon ("fill", 225, 200, 400, 100, 575, 200)
 +
end
 +
 
 +
local function myStencilFunction3()
 +
love.graphics.circle ("fill", 400, 200, 60)
 
end
 
end
  
 
function love.draw()
 
function love.draw()
     -- draw a rectangle to the stencil buffer
+
     love.graphics.stencil(myStencilFunction)
+
     -- 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 (myStencilFunction1, "replace", 1)
 +
 +
-- draw polygon as a stencil. Each pixel touched by the polygon will have its stencil value set to 1. The rest will be not changed.
 +
    love.graphics.stencil (myStencilFunction2, "replace", 2, true)
 +
 +
-- remove circle from stencil. Each pixel touched by the circle will have its stencil value decrement to 1. The rest will be not changed.
 +
     love.graphics.stencil (myStencilFunction3, "decrement", 1, true)
  
     -- enable testing against the contents of the stencil buffer
+
     -- Only allow rendering on pixels whose stencil value is greater than 0.
     love.graphics.setStencilTest(true)
+
     love.graphics.setStencilTest ("greater", 0)
  
     love.graphics.setColor(255, 0, 0, 120)
+
     love.graphics.setColor(1, 0, 0, 0.8)
     love.graphics.circle("fill", 300, 300, 150, 50)
+
     love.graphics.circle("fill", 300, 300, 150)
  
     love.graphics.setColor(0, 255, 0, 120)
+
     love.graphics.setColor(0, 1, 0, 0.6)
     love.graphics.circle("fill", 500, 300, 150, 50)
+
     love.graphics.circle("fill", 500, 300, 150)
  
     love.graphics.setColor(0, 0, 255, 120)
+
     love.graphics.setColor(0, 0, 1, 0.6)
     love.graphics.circle("fill", 400, 400, 150, 50)
+
     love.graphics.circle("fill", 400, 400, 150)
 +
 +
    love.graphics.setColor(1, 1, 1, 1)
 +
    love.graphics.circle("fill", 400, 100, 117)
  
     love.graphics.setStencilTest(false)
+
     love.graphics.setStencilTest()
 
end
 
end
 
</source>
 
</source>
 +
 
=== Drawing a circle with a hole ===
 
=== Drawing a circle with a hole ===
 
<source lang="lua">
 
<source lang="lua">
 
local function myStencilFunction()
 
local function myStencilFunction()
   -- Draw a small circle to the stencil buffer. This will be the hole.
+
   -- Draw a small circle as a stencil. This will be the hole.
 
   love.graphics.circle("fill", 400, 300, 50)
 
   love.graphics.circle("fill", 400, 300, 50)
 
end
 
end
  
 
function love.draw()
 
function love.draw()
   love.graphics.stencil(myStencilFunction)
+
  -- Each pixel touched by the circle will have its stencil value set to 1. The rest will be 0.
 +
   love.graphics.stencil(myStencilFunction, "replace", 1)
  
   -- invert the stencil test so anything touching the geometry drawn to the stencil buffer is not rendered.
+
   -- Configure the stencil test to only allow rendering on pixels whose stencil value is equal to 0.
   love.graphics.setStencilTest(true, true)
+
  -- This will end up being every pixel *except* ones that were touched by the circle drawn as a stencil.
 +
   love.graphics.setStencilTest("equal", 0)
 
   love.graphics.circle("fill", 400, 300, 150)
 
   love.graphics.circle("fill", 400, 300, 150)
   love.graphics.setStencilTest(false)
+
   love.graphics.setStencilTest()
 
end
 
end
 
</source>
 
</source>
 +
 
=== Drawing two masked triangles with different colors ===
 
=== Drawing two masked triangles with different colors ===
 
<source lang="lua">
 
<source lang="lua">
Line 64: Line 99:
  
 
function love.draw()
 
function love.draw()
    love.graphics.stencil(myStencilFunction)
+
  -- Each pixel touched by the circle will have its stencil value set to 1. The rest will be 0.
 +
  love.graphics.stencil(myStencilFunction, "replace", 1)
  
   love.graphics.setStencilTest(true, false)
+
  -- Only allow rendering on pixels whose stencil value is greater than 0.
   love.graphics.setColor(155, 0, 128)
+
   love.graphics.setStencilTest("greater", 0)
 +
   love.graphics.setColor(0.6, 0, 0.5)
 
   love.graphics.polygon("fill", 400, 200, 486, 350, 314, 350)
 
   love.graphics.polygon("fill", 400, 200, 486, 350, 314, 350)
 
   
 
   
   love.graphics.setStencilTest(true, true)
+
  -- Now only allow rendering on pixels whose stencil value is equal to 0.
   love.graphics.setColor(144, 214, 128)
+
   love.graphics.setStencilTest("equal", 0)
 +
   love.graphics.setColor(0.55, 0.85, 0.5)
 
   love.graphics.polygon("fill", 400, 200, 486, 350, 314, 350)
 
   love.graphics.polygon("fill", 400, 200, 486, 350, 314, 350)
  
   love.graphics.setStencilTest(false)
+
   love.graphics.setStencilTest()
 
end
 
end
 
</source>
 
</source>
  
 
The [[love.graphics.stencil]] wiki page includes more examples.
 
The [[love.graphics.stencil]] wiki page includes more examples.
 +
 
== See Also ==
 
== See Also ==
 
* [[parent::love.graphics]]
 
* [[parent::love.graphics]]
 +
* [[love.graphics.getStencilTest]]
 
* [[love.graphics.stencil]]
 
* [[love.graphics.stencil]]
 
[[Category:Functions]]
 
[[Category:Functions]]
{{#set:Description=Enables or disables stencil testing.}}
+
{{#set:Description=Configures or disables stencil testing.}}
 
{{#set:Sub-Category=State}}
 
{{#set:Sub-Category=State}}
 
== Other Languages ==
 
== Other Languages ==
 
{{i18n|love.graphics.setStencilTest}}
 
{{i18n|love.graphics.setStencilTest}}

Latest revision as of 11:16, 14 February 2022

Available since LÖVE 0.10.0
Together with love.graphics.stencil, it has replaced love.graphics.setStencil.

Configures or disables stencil testing.

When stencil testing is enabled, the geometry of everything that is drawn afterward will be clipped / stencilled out based on a comparison between the arguments of this function and the stencil value of each pixel that the geometry touches. The stencil values of pixels are affected via love.graphics.stencil.


O.png Starting with version 11.0, a stencil buffer must be set or requested in love.graphics.setCanvas when using stencils with a Canvas. love.graphics.setCanvas{canvas, stencil=true} is an easy way to use an automatically provided temporary stencil buffer in that case.  


Function

Synopsis

love.graphics.setStencilTest( comparemode, comparevalue )

Arguments

CompareMode comparemode
The type of comparison to make for each pixel.
number comparevalue
The value to use when comparing with the stencil value of each pixel. Must be between 0 and 255.

Returns

Nothing.

Function

Disables stencil testing.

Synopsis

love.graphics.setStencilTest( )

Arguments

None.

Returns

Nothing.

Examples

Drawing circles masked by rectangle, polygon and circle

local function myStencilFunction1()
	love.graphics.rectangle("fill", 225, 200, 350, 300)
end

local function myStencilFunction2()
	love.graphics.polygon ("fill", 225, 200, 400, 100, 575, 200)
end

local function myStencilFunction3()
	love.graphics.circle ("fill", 400, 200, 60)
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 (myStencilFunction1, "replace", 1)
	
	-- draw polygon as a stencil. Each pixel touched by the polygon will have its stencil value set to 1. The rest will be not changed.
    love.graphics.stencil (myStencilFunction2, "replace", 2, true)
	
	-- remove circle from stencil. Each pixel touched by the circle will have its stencil value decrement to 1. The rest will be not changed.
    love.graphics.stencil (myStencilFunction3, "decrement", 1, true)

    -- Only allow rendering on pixels whose stencil value is greater than 0.
    love.graphics.setStencilTest ("greater", 0)

    love.graphics.setColor(1, 0, 0, 0.8)
    love.graphics.circle("fill", 300, 300, 150)

    love.graphics.setColor(0, 1, 0, 0.6)
    love.graphics.circle("fill", 500, 300, 150)

    love.graphics.setColor(0, 0, 1, 0.6)
    love.graphics.circle("fill", 400, 400, 150)
	
    love.graphics.setColor(1, 1, 1, 1)
    love.graphics.circle("fill", 400, 100, 117)

    love.graphics.setStencilTest()
end

Drawing a circle with a hole

local function myStencilFunction()
   -- Draw a small circle as a stencil. This will be the hole.
   love.graphics.circle("fill", 400, 300, 50)
end

function love.draw()
   -- Each pixel touched by the circle will have its stencil value set to 1. The rest will be 0.
   love.graphics.stencil(myStencilFunction, "replace", 1)

   -- Configure the stencil test to only allow rendering on pixels whose stencil value is equal to 0.
   -- This will end up being every pixel *except* ones that were touched by the circle drawn as a stencil.
   love.graphics.setStencilTest("equal", 0)
   love.graphics.circle("fill", 400, 300, 150)
   love.graphics.setStencilTest()
end

Drawing two masked triangles with different colors

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

function love.draw()
   -- Each pixel touched by the circle will have its stencil value set to 1. The rest will be 0.
   love.graphics.stencil(myStencilFunction, "replace", 1)

   -- Only allow rendering on pixels whose stencil value is greater than 0.
   love.graphics.setStencilTest("greater", 0)
   love.graphics.setColor(0.6, 0, 0.5)
   love.graphics.polygon("fill", 400, 200, 486, 350, 314, 350)
 
   -- Now only allow rendering on pixels whose stencil value is equal to 0.
   love.graphics.setStencilTest("equal", 0)
   love.graphics.setColor(0.55, 0.85, 0.5)
   love.graphics.polygon("fill", 400, 200, 486, 350, 314, 350)

   love.graphics.setStencilTest()
end

The love.graphics.stencil wiki page includes more examples.

See Also


Other Languages