Garry's Mod Wiki

Revision Difference

Global.GetRenderTarget#524472

<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>⤶ <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>