Revision Difference
Global.GetRenderTarget#524864
<function name="GetRenderTarget" parent="Global" type="libraryfunc">
<description>
Creates or gets the rendertarget with the given name.
See <page>Global.GetRenderTargetEx</page> for an advanced version of this function with more options.
<bug issue="2885">This crashes when used on a cubemap texture.</bug>
</description>
<realm>Client</realm>
<args>
<arg name="name" type="string">The internal name of the render target.</arg>
<arg name="width" type="number">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.</arg>
<arg name="height" type="number">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.</arg>
<arg name="additive" type="boolean" default="false">Sets whenever the rt should be additive.</arg>
</args>
<rets>
<ret name="" type="ITexture">The render target</ret>
</rets>
</function>
<example>
<description>Example usage of a render target</description>⤶
<code>
-- 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 )⤶
</code>⤶
</example>⤶
⤶
<example>⤶
<description>Example usage of a render target with transparency/alpha channel</description>⤶
<code>⤶
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 )
</code>
</example>