Revision Difference
render.ClearStencilBufferRectangle#561547
<function name="ClearStencilBufferRectangle" parent="render" type="libraryfunc">
<description>
Sets the stencil value in a specified rect.
Sets the Stencil Buffer value for every pixel in a given rectangle to a given value.
This is **not** affected by <page>render.SetStencilWriteMask</page>
⤶
For more detailed information on the Stencil system, including usage examples, see the <page text="Stencils Render Reference">render_stencils</page> page⤶
</description>
<realm>Client and Menu</realm>
<args>
<arg name="originX" type="number">X origin of the rectangle.</arg>
<arg name="originY" type="number">Y origin of the rectangle.</arg>
<arg name="endX" type="number">The end X coordinate of the rectangle.</arg>⤶
<arg name="endY" type="number">The end Y coordinate of the rectangle.</arg>⤶
<arg name="stencilValue" type="number">Value to set cleared stencil buffer to.</arg>⤶
<arg name="startX" type="number">The X coordinate of the top left corner of the rectangle to be cleared.</arg>
<arg name="startY" type="number">The Y coordinate of the top left corner of the rectangle to be cleared.</arg>
<arg name="endX" type="number">⤶
The X coordinate of the bottom right corner of the rectangle to be cleared. ⤶
**Note:** Unlike some other rectangle-based functions, this is **not** the width of the rectangle.⤶
</arg>⤶
<arg name="endY" type="number">⤶
The Y coordinate of the bottom right corner of the rectangle to be cleared. ⤶
**Note:** Unlike some other rectangle-based functions, this is **not** the height of the rectangle.⤶
</arg>⤶
<arg name="stencilValue" type="number">The Stencil Buffer value that all pixels within the rectangle will be set to.</arg>⤶
</args>
</function>
⤶
<example>⤶
<description>A basic stencil operation that limits rendering to the centre of the screen (from ⤶
[Lex's Stencil Tutorial](https://github.com/Lexicality/stencil-tutorial)).</description>⤶
⤶
<example name="Horizontal Wipe">⤶
<description>⤶
This example demonstrates using this function to perform a "wipe" transition between two drawn elements. ⤶
</description>⤶
<code>
hook.Add( "PostDrawOpaqueRenderables", "Stencil Tutorial Example", function()
-- Reset everything to known good⤶
render.SetStencilWriteMask( 0xFF )
render.SetStencilTestMask( 0xFF )
render.SetStencilReferenceValue( 0 )
render.SetStencilCompareFunction( STENCIL_ALWAYS )
render.SetStencilPassOperation( STENCIL_KEEP )
render.SetStencilFailOperation( STENCIL_KEEP )
render.SetStencilZFailOperation( STENCIL_KEEP )
render.ClearStencil()
-- Enable stencils⤶
render.SetStencilEnable( true )
-- Set the reference value to 1. This is what the compare function tests against⤶
render.SetStencilReferenceValue( 1 )
-- Refuse to write things to the screen unless that pixel's value is 1⤶
render.SetStencilCompareFunction( STENCIL_EQUAL )⤶
-- Write a 1 to the centre third of the screen. Because we cleared it earlier, everything is currently 0⤶
local w, h = ScrW() / 3, ScrH() / 3⤶
local x_start, y_start = w, h⤶
local x_end, y_end = x_start + w, y_start + h⤶
render.ClearStencilBufferRectangle( x_start, y_start, x_end, y_end, 1 )
-- Draw our entities. They will only draw in the area cleared above⤶
for _, ent in ipairs( ents.FindByClass( "sent_stencil_test" ) ) do⤶
ent:DrawModel()
end⤶
-- Let everything render normally again⤶
render.SetStencilEnable( false )
end )
</code>⤶
<output><image src="stencil_basic_clipping_result.jpg" alt="left|400px"/></output>⤶
local COLOR_BLUE = Color( 65, 98, 214 )
local COLOR_RED = Color( 214, 44, 100 )⤶
⤶
local colorMaterial = Material( "color" )
⤶
hook.Add( "PostDrawTranslucentRenderables", "StencilExampleClearStencilBufferRectangle", function()
-- Reset the Stencil system's settings⤶
render.SetStencilTestMask( 255 )
render.SetStencilWriteMask( 255 )
render.SetStencilFailOperation( STENCILOPERATION_KEEP )
render.SetStencilPassOperation( STENCILOPERATION_KEEP )
render.SetStencilZFailOperation( STENCILOPERATION_KEEP )
-- Reset all Stencil Buffer values to 0⤶
render.ClearStencil()
⤶
-- Only pass pixels whose Stencil Buffer value equals the Stencil Reference value⤶
render.SetStencilCompareFunction( STENCILCOMPARISONFUNCTION_EQUAL )
⤶
-- We're going to mask everything to the left and right of this X coordinate⤶
-- It starts at the middle of the screen and moves back and forth thanks to math.sin⤶
local horizontalSplitX = ScrW()/2 + math.sin( CurTime() ) * ScrW()/8⤶
⤶
-- Set the Stencil Buffer value for all pixels on the left side of the horizontal split to 1⤶
render.ClearStencilBufferRectangle( 0, 0, horizontalSplitX, ScrH(), 1 )⤶
⤶
-- Set the Stencil Buffer value for all pixels on the right side of the horizontal split to 2⤶
render.ClearStencilBufferRectangle( horizontalSplitX, 0, ScrW(), ScrH(), 2 )
cam.Start2D()⤶
render.SetStencilEnable( true )⤶
⤶
-- Only render on pixels whose Stencil Buffer value equals 1 (The left side)
render.SetStencilReferenceValue( 1 )⤶
-- Draw a rectangle in the center of the screen, which will be masked to only the left half of the screen⤶
surface.SetDrawColor( COLOR_BLUE )
surface.SetMaterial( colorMaterial )
surface.DrawTexturedRectRotated( ScrW()/2, ScrH()/2, ScrW()/8, ScrW()/8, math.sin( CurTime() ) * 360 )⤶
⤶
-- Only render on pixels whose Stencil Buffer value equals 2 (The right side)⤶
render.SetStencilReferenceValue( 2 )⤶
⤶
-- Draw a rectangle in the center of the screen, which will be masked to only the right half of the screen⤶
surface.SetDrawColor( COLOR_RED )⤶
surface.DrawTexturedRectRotated( ScrW()/2, ScrH()/2, ScrW()/8, ScrW()/8, math.cos( CurTime() ) * 360 )⤶
⤶
render.SetStencilEnable( false )⤶
cam.End2D()⤶
end ) </code>⤶
<output>⤶
⤶
</output>⤶
</example>