Rendering a render.view onto a 3d2d surface

Hey guys, basically my title post. I was wondering how this could be done. Whenever I try, my game locks up and I have to kill the process. I know I need to do something with a rendertarget but I’m not sure how I would do it.

Here’s the code I have so far:

[LUA]
local function DrawRender()

local ply = LocalPlayer()

local panel_w = 200
local panel_h = 50
local panel_dist = 150

local CamData = {} //just the top down view from the wiki
CamData.angles = Angle(90,LocalPlayer():EyeAngles().yaw,0)
CamData.origin = LocalPlayer():GetPos()+Vector(0,0,500)
CamData.x = 0
CamData.y = 0
CamData.w = ScrW() / 4
CamData.h = ScrH() / 4

local rt = GetRenderTarget(	"Test",512, 128 , true)
local OldRT	= render.GetRenderTarget()
local mat = Material( "materials/color/white.vtf" )

hook.Add( "PostDrawOpaqueRenderables", "DrawThing", function( )

	local ang = ply:EyeAngles()
	local pos = ply:EyePos() + Vector( 0, 0, panel_h/2 ) + (ang:Forward()*panel_dist) + ang:Right()*-panel_w/2
	ang.x = 90 //force the view to upright

	mat:SetTexture( "$basetexture", rt )
	render.SetRenderTarget( rt )
	render.ClearDepth()
	render.RenderView( CamData )
	render.SetRenderTarget( OldRT )
 
	cam.Start3D2D( pos, ang, 1 )
		surface.SetDrawColor( 255, 255, 255, 100 )
		surface.SetMaterial( mat )
		surface.DrawTexturedRect(0, 0, panel_w, panel_h )
	cam.End3D2D()

	 
end )

timer.Simple( 5, function()
	hook.Remove( "PostDrawOpaqueRenderables", "DrawThing" )
end )

end

concommand.Add( “RenderTest”, DrawRender )
[/LUA]

I’m pretty sure it’s an obvious mistake, but I’ve not really messed with rendering at all yet in gmod, so any help would be appreciated.

Note: The intended function of this code is to render a top-down view of yourself on a panel that floats in front of your face.

Rendering a 3D view in a cam that turns 2D stuff into 3D? Don’t think it’s smart.
Never tried it either so I can’t be of any help, but good luck!

It certainly doesn’t have to be done this way, any execution of the concept is fine with me :slight_smile: This is just the way I came up with from looking at other 3d2d camera projects.

I’m probably wrong but, have you tried cam.Start3D?
Also on the documentation for cam.Start3D2D there is

Maybe you should try that out if you haven’t already.

The lockup looks to be caused by infinite recursion. When you call render.RenderView(), the game calls most of the rendering hooks as well, including PostDrawOpaqueRenderables (which proceeds to call render.RenderView() again).

To fix it, you do something like this:

[lua]
local inhook = false

hook.Add( “PostDrawOpaqueRenderables”, “DrawThing”, function()
if inhook then return end
– blah blah code code
inhook = true
render.RenderView(CamData)
inhook = false
– blah blah code code
end)
[/lua]

-snip-

Well the recursion fix did work, thanks a bunch for that :slight_smile:

However, It’s still not rendering my material in 3d2d space, it’s simply drawing it on my screen. Also it breaks the rendering of other items, such as the viewmodel for weapons. I tried looking at some of your code, brandon, but it didn’t seem to help that much. Here’s what I currently have:

[LUA]
local function DrawRender()

local ply = LocalPlayer()

local panel_w = 200
local panel_h = 50
local panel_dist = 150

local CamData = {} //just the top down view from the wiki
CamData.angles = Angle(90,LocalPlayer():EyeAngles().yaw,0)
CamData.origin = LocalPlayer():GetPos()+Vector(0,0,500)
CamData.x = 0
CamData.y = 0
CamData.w = ScrW() / 4
CamData.h = ScrH() / 4

local TEXTURE_FLAGS_CLAMP_S = 0x0004
local TEXTURE_FLAGS_CLAMP_T = 0x0008

local rt = GetRenderTargetEx("tex_icon"..CurTime(),
	2048,
	2048,
	RT_SIZE_NO_CHANGE,
	MATERIAL_RT_DEPTH_SEPARATE,
	bit.bor(TEXTURE_FLAGS_CLAMP_S, TEXTURE_FLAGS_CLAMP_T),
	CREATERENDERTARGETFLAGS_UNFILTERABLE_OK,
	IMAGE_FORMAT_RGBA8888
)

local mat = CreateMaterial("mat_icon"..CurTime(), "UnlitGeneric", {
	["$ignorez"] = 1,
	["$vertexcolor"] = 1,
	["$translucent"] = 1,
	["$nolod"] = 0,
	["$basetexture"] = rt:GetName()
})

local inhook = false

hook.Add( "PostDrawOpaqueRenderables", "DrawThing", function( )
	if inhook then return end

	local ang = ply:EyeAngles()
	local pos = ply:EyePos() + Vector( 0, 0, panel_h/2 ) + (ang:Forward()*panel_dist) + ang:Right()*-panel_w/2
	ang.x = 90 //force the view to upright

	local OldRT = render.GetRenderTarget()

	render.SetRenderTarget( rt )

	inhook = true
	render.RenderView( CamData )
	inhook = false

	render.SetRenderTarget( OldRT )

	cam.Start3D2D( pos, ang, 1 )
		surface.SetMaterial( mat )
		surface.SetDrawColor( 255, 255, 255, 200 )
		surface.DrawTexturedRect(0, 0, panel_w, panel_h )
		draw.RoundedBox( 0, 0, 0, panel_h, panel_w, Color( 255, 255, 255, 100 ) )
	cam.End3D2D()

end )

timer.Simple( 5, function()
	hook.Remove( "PostDrawOpaqueRenderables", "DrawThing" )
end )

end

concommand.Add( “RenderTest”, DrawRender )
[/LUA]