Garry's Mod Wiki

Revision Difference

render.SetStencilEnable#561465

<function name="SetStencilEnable" parent="render" type="libraryfunc"> <description> Sets whether stencil tests are carried out for each rendered pixel. ⤶ Only pixels passing the stencil test are written to the render target. </description>⤶ Enables or disables the Stencil system for future draw operations. ⤶ While enabled, all pixels affected by draw operations will have their corresponding values in the active <page text="Render Target's">render_rendertargets</page> Stencil Buffer compared against the current <page text="Reference Value">render.SetStencilReferenceValue</page> and their current Depth Buffer value compared against the depth of the corresponding pixel from the draw operation. Depending on the outcomes of these comparisons, one of either the <page text="Pass">render.SetStencilPassOperation</page>, <page text="Fail">render.SetStencilFailOperation</page>, or <page text="Z-Fail">render.SetStencilZFailOperation</page> operations is performed on the pixel's Stencil Buffer value. ⤶ A pixel will only be updated in the active <page text="Render Target">render_rendertargets</page> if the <page text="Pass Operation">render.SetStencilPassOperation</page> is performed.⤶ ⤶ For more detailed information on the Stencil system, including usage examples, see the <page text="Stencils Render Reference">render_stencils</page> page⤶ ⤶ <note>The Stencil system's configuration does **not** reset automatically. ⤶ To prevent unexpected behavior, always manually ensure that the Stencil system is configured appropriately for your use-case after enabling it.</note>⤶ ⤶ </description>⤶ <realm>Client and Menu</realm> <args> <arg name="newState" type="boolean">The new state.</arg> </args> </function> ⤶ <example>⤶ <description>A basic stencil operation that limits rendering to the centre of the screen.</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>⤶ </example>