GetRenderTarget
Description
Creates or gets the rendertarget with the given name.
See GetRenderTargetEx for an advanced version of this function with more options.
Calling this function is equivalent to
GetRenderTargetEx(name,
width, height,
RT_SIZE_NO_CHANGE,
MATERIAL_RT_DEPTH_SEPARATE,
bit.bor(2, 256),
0,
IMAGE_FORMAT_BGRA8888
)
Arguments
2 number width
The width of the render target, must be power of 2. If not set to PO2, the size will be automatically converted to the nearest PO2 size.
3 number height
The height of the render target, must be power of 2. If not set to PO2, the size will be automatically converted to the nearest PO2 size.
Returns
Example
Example usage of a render target
-- Give the RT a size
local TEX_SIZE = 512
-- Create the RT
local tex = GetRenderTarget( "ExampleRT", TEX_SIZE, TEX_SIZE )
-- Write something to the RT
-- Note how this is not in a render hook, in this case we only write to the render target once
local txBackground = surface.GetTextureID( "models/weapons/v_toolgun/screen_bg" )
render.PushRenderTarget( tex )
cam.Start2D()
surface.SetDrawColor( color_white )
surface.SetTexture( txBackground )
surface.DrawTexturedRect( 0, 0, TEX_SIZE, TEX_SIZE )
cam.End2D()
render.PopRenderTarget()
-- Create a render-able material for our render target
local myMat = CreateMaterial( "ExampleRTMat", "UnlitGeneric", {
["$basetexture"] = tex:GetName() -- Make the material use our render target texture
} )
-- Draw it on screen
hook.Add( "HUDPaint", "DrawExampleMat", function()
surface.SetDrawColor( color_white )
surface.SetMaterial( myMat )
surface.DrawTexturedRect( 25, 25, TEX_SIZE, TEX_SIZE )
end )
Example
Example usage of a render target with transparency/alpha channel
local textureRT = GetRenderTarget( "ExampleRTwithAlpha", 512, 512 )
local mat = CreateMaterial( "ExampleRTwithAlpha_Mat", "UnlitGeneric", {
['$basetexture'] = textureRT:GetName(),
["$translucent"] = "1" -- This is necessary to render the RT with alpha channel
} );
hook.Add( "HUDPaint", "ExampleRTwithAlpha_Render", function()
render.PushRenderTarget( textureRT )
cam.Start2D()
-- Clear the RT
render.Clear( 0, 0, 0, 0 )
-- Draw some basic animated stuff on it
draw.RoundedBox( 0, 20, 100 + math.sin( CurTime() ) * 50, 50, 50, color_white )
-- Draw with transparency
draw.RoundedBox( 0, 120, 100 + math.sin( CurTime() ) * 50, 50, 50, Color( 255, 0, 0, 100 ) )
cam.End2D()
render.PopRenderTarget()
-- Draw our render target on screen so we can see our result
surface.SetDrawColor( color_white )
surface.SetMaterial( mat )
surface.DrawTexturedRect( 50, 50, 512, 512 )
end )