render.ClearStencilBufferRectangle

From Garry's Mod
Jump to: navigation, search
 render.ClearStencilBufferRectangle( )

Contents

Description

Sets the stencil value in a specified rect.

This is not affected by render.SetStencilWriteMask

Arguments

number originX

X origin of the rectangle.

Arguments

number originY

Y origin of the rectangle.

Arguments

number endX

The end X coordinate of the rectangle.

Arguments

number endY

The end Y coordinate of the rectangle.

Arguments

number stencilValue

Value to set cleared stencil buffer to.

Examples

Example

A basic stencil operation that limits rendering to the centre of the screen

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 pairs( ents.FindByClass( "sent_stencil_test" ) ) do
		ent:DrawModel()
	end

	-- Let everything render normally again
	render.SetStencilEnable( false )
end )

Output:

stencil basic clipping result.jpg
Personal tools
Navigation