Garry's Mod Wiki

Revision Difference

render.ClearBuffersObeyStencil#561586

<function name="ClearBuffersObeyStencil" parent="render" type="libraryfunc"> <description> Tests every pixel of the active <page text="Render Target">render_rendertargets</page> against the current Stencil configuration and sets the Color Channel values and, optionally, the Depth Buffer values for every pixel that passes. For more detailed information on the Stencil system, including usage examples, see the <page text="Stencils Render Reference">render_stencils</page> page <note> This function does **not** clear the Stencil Buffer on its own. If you would like to clear the Stencil Buffer, you will need to set the <page text="Pass Operation">render.SetStencilPassOperation</page> to <page text="STENCILOPERATION_ZERO">Enums/STENCILOPERATION</page>⤶ If you would like to clear the Stencil Buffer, you can use <page>render.ClearStencil</page>⤶ </note> </description> <realm>Client and Menu</realm> <args> <arg name="red" type="number"> The red Color Channel value for each pixel that is cleared. Must be an integer value in the range 0-255 (`byte`) </arg> <arg name="green" type="number"> The green Color Channel value for each pixel that is cleared. Must be an integer value in the range 0-255 (`byte`) </arg> <arg name="blue" type="number"> The blue Color Channel value for each pixel that is cleared. Must be an integer value in the range 0-255 (`byte`) </arg> <arg name="alpha" type="number"> The alpha (translucency) Color Channel value for each pixel that is cleared. Must be an integer value in the range 0-255 (`byte`) </arg> <arg name="clearDepth" type="boolean"> If true, reset the Depth Buffer values. </arg> </args> </function> <example> <description>Clearing a section of the screen via the stencil buffer (from [Lex's Stencil Tutorial](https://github.com/Lexicality/stencil-tutorial)).</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 ) -- Tell the render library to clear the screen, but obeying the stencil test function. -- This means it will only clear the centre third. render.ClearBuffersObeyStencil( 0, 148, 133, 255, false ) -- Let everything render normally again render.SetStencilEnable( false ) end ) </code> <output><image src="clearbuffersobeystencil.jpg" alt="800px"/></output> </example>