render/PushRenderTarget

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(Simply not true)
Line 39: Line 39:
 
|desc=Height of the viewport
 
|desc=Height of the viewport
 
|default=texture:Height()
 
|default=texture:Height()
 +
}}
 +
{{Example
 +
|Description=Shows how to create a material which uses a custom created Render Target texture.
 +
|Code=-- Create render target
 +
local exampleRT = GetRenderTarget( "example_rt", 1024, 1024 )
 +
 +
-- Draw to the render target
 +
render.PushRenderTarget( exampleRT )
 +
cam.Start2D()
 +
-- Draw background
 +
surface.SetDrawColor( 0, 0, 0, 255 )
 +
surface.DrawRect( 0, 0, 1024, 1024 )
 +
 +
-- Draw some foreground stuff
 +
surface.SetDrawColor( 255, 0, 0, 255 )
 +
surface.DrawRect( 0, 0, 256, 256 )
 +
cam.End2D()
 +
render.PopRenderTarget()
 +
 +
local customMaterial = CreateMaterial( "example_rt_mat", "UnlitGeneric", {
 +
["$basetexture"] = "example_rt",
 +
["$translucent"] = 1,
 +
["$vertexcolor"] = 1
 +
} )
 +
 +
hook.Add( "HUDPaint", "ExampleDraw", function()
 +
surface.SetDrawColor( 255, 255, 255, 255 )
 +
surface.SetMaterial( customMaterial )
 +
surface.DrawTexturedRect( 0, 0, customMaterial:GetTexture( "$basetexture" ):Width(), customMaterial:GetTexture( "$basetexture" ):Height() )
 +
end )
 +
|Output=A black 1024x1024 render target with a 256x256 red square in top left corner drawn in your top left corner.
 
}}
 
}}
 
{{Example
 
{{Example

Revision as of 16:13, 17 October 2019

 render.PushRenderTarget( )

Contents

Description

Pushes the current render target and viewport to the RT stack then sets a new current render target and viewport. If the viewport is not specified, the dimensions of the render target are used instead.

This is similar to a call to render.SetRenderTarget and render.SetViewPort where the current render target and viewport have been saved beforehand, except the viewport isn't clipped to screen bounds.

See also render.PopRenderTarget.

NOTE

If you want to render to the render target in 2d mode and it is not the same size as the screen, use cam.Start2D and cam.End2D.

NOTE

If the render target is bigger than the screen, rendering done with the surface library will be clipped to the screen bounds unless you call surface.DisableClipping

Arguments

ITexture texture

The new render target to be used.

Arguments

number x=0

X origin of the viewport.

Arguments

number y=0

Y origin of the viewport.

Arguments

number w=texture:Width()

Width of the viewport.

Arguments

number h=texture:Height()

Height of the viewport

Examples

Example

Shows how to create a material which uses a custom created Render Target texture.

-- Create render target
local exampleRT = GetRenderTarget( "example_rt", 1024, 1024 )

-- Draw to the render target
render.PushRenderTarget( exampleRT )
	cam.Start2D()
		-- Draw background
		surface.SetDrawColor( 0, 0, 0, 255 )
		surface.DrawRect( 0, 0, 1024, 1024 )

		-- Draw some foreground stuff
		surface.SetDrawColor( 255, 0, 0, 255 )
		surface.DrawRect( 0, 0, 256, 256 )
	cam.End2D()
render.PopRenderTarget()

local customMaterial = CreateMaterial( "example_rt_mat", "UnlitGeneric", {
	["$basetexture"] = "example_rt",
	["$translucent"] = 1,
	["$vertexcolor"] = 1
} )

hook.Add( "HUDPaint", "ExampleDraw", function()
	surface.SetDrawColor( 255, 255, 255, 255 )
	surface.SetMaterial( customMaterial )
	surface.DrawTexturedRect( 0, 0, customMaterial:GetTexture( "$basetexture" ):Width(), customMaterial:GetTexture( "$basetexture" ):Height() )
end )

Output:

A black 1024x1024 render target with a 256x256 red square in top left corner drawn in your top left corner.

Examples

Example

Shows how you can use alpha channel with render targets.

render.PushRenderTarget( texture )
render.OverrideAlphaWriteEnable( true, true )

render.ClearDepth()
render.Clear( 0, 0, 0, 0 )

-- Draw stuff here

render.OverrideAlphaWriteEnable( false )
render.PopRenderTarget()


Personal tools
Navigation