Render Target as Material

Ok so i’m trying to copy a RT and make it into a material.

[lua]
function RTAsMaterial(RT)
local RTMat = Material(“RTMat”)
RTMat:SetMaterialTexture("$basetexture",RT)
local ID = surface.GetTextureID(“RTMat”)
return ID,RTMat
end
[/lua]

From what i understand this should work.
I get no error and RT is a valid RenderTarget.
Yet when i come to draw something using “surface.SetTexture( ID )” i get a blank texture.

Also i have tried using CreateMaterial(“RTMat”) opposed to Material(“RTMat”).
Any help would be appreciated.

[lua]local RT = GetRenderTarget( “SomeRT”, 512, 512, false )
local OldRT = nil
local OldScrW = nil
local OldScrH = nil
local RenderWidth = 512
local RenderHeight = 512

local MaterialFile = “some/blank/material” – Make a texture that is about the size of your render target
local screenMat = Material( MaterialFile )
local screenTex = surface.GetTextureID( MaterialFile )
local OldTex = nil

local CamData = {}

hook.Add( “HUDPaint”, “TestScreen”, function()

OldRT = render.GetRenderTarget()

OldScrW = ScrW()
OldScrH = ScrH()

render.SetViewPort( 0, 0, RenderWidth, RenderHeight )
	render.SetRenderTarget( RT )
		render.Clear( 0, 0, 0, 255 )

		CamData = {
			x = 0,
			y = 0,
			w = RenderWidth,
			h = RenderHeight,
			origin = EyePos(),
			angles = EyeAngles(),
			drawhud = false,
			drawviewmodel = false,
			dopostprocess = false,
		}

		render.RenderView( CamData )
	render.SetRenderTarget( OldRT )
render.SetViewPort( 0, 0, OldScrW, OldScrH )

OldTex = screenMat:GetMaterialTexture( "$basetexture" )
screenMat:SetMaterialTexture( "$basetexture", RT )

surface.SetTexture( screenTex )
surface.SetDrawColor( 255, 255, 255, 255 )
surface.DrawTexturedRect( 32, 32, 256, 256 )

screenMat:SetMaterialTexture( "$basetexture", OldTex )

end )[/lua]

Enjoy.