Render texture to prop

I’m trying to make a portal entity, and i can’t seem to figure out what’s causing a few things.
When looking straight at the prop it works fine:

http://puu.sh/39xEN.jpg[/t]

But when i pick up a prop (with physgun grab halo enabled, without it it doesnt happen), the render goes black:

[t]http://puu.sh/39xFv.jpg[/t]

And for some reason i can see the render through the prop its on, but not through different props:

[t]http://puu.sh/39xGh.jpg[/t]
[t]http://puu.sh/39xGJ.jpg

And also the crosshair goes invisible as soon as i spawn one. Here’s the rendering code:



include("shared.lua")

ENT.RenderTarget = render.GetScreenEffectTexture()
local matView = CreateMaterial(
	"UnlitGeneric",
	"GMODScreenspace",
	{
		["$basetexture"] = ENT.RenderTarget,
		["$basetexturetransform"] = "center .5 .5 scale -1 -1 rotate 0 translate 0 0",
		["$texturealpha"] = "0",
		["$vertexalpha"] = "1",
	}
)

hook.Add("RenderScene", "WorldPortalRenderScene", function(origin, angles, fov)
	for _,ent in ipairs(ents.FindByClass("worldportal")) do
		ent:RenderScene(origin, angles, fov)
	end
end)

hook.Add("ShouldDrawLocalPlayer", "WorldPortalShouldDrawLocalPlayer", function(ply)
	if WorldPortalRender then
		return true
	end
end)

function ENT:Initialize()
	self:SetModelScale(1.2, 0)
end

function ENT:RenderScene(origin, angles, fov)
	local oldRT = render.GetRenderTarget()
	render.SetRenderTarget(self.RenderTarget)

	render.Clear(0, 0, 0, 255)
	render.ClearDepth()
	render.ClearStencil()

	WorldPortalRender = true
	render.RenderView({
		x = 0,
		y = 0,
		w = ScrW(),
		h = ScrH(),
		origin = self:GetPos() + Vector(0,0,500),
		angles = LocalPlayer():EyeAngles(),
		drawviewmodel = false,
		drawhud = false
	})
	WorldPortalRender = false
	render.UpdateScreenEffectTexture()

	render.SetRenderTarget(oldRT)
end

function ENT:Draw()
	self:DrawModel()

	if self.RenderTarget then
		matView:SetTexture("$basetexture", self.RenderTarget)
		render.SetMaterial(matView)
		mesh.Begin(MATERIAL_QUADS, 1)
			mesh.QuadEasy(self:GetPos() + Vector(-0.2, -23.2, 0), Vector(0, 1, 0), 40.5, -72)
		mesh.End()
	end
end


You are better off getting your own render target rather than using the screen effect texture, be sure to also allocate a depth buffer.

Great, i changed it to



ENT.RenderTarget = GetRenderTarget("WorldPortalRT", ScrW(), ScrH(), false)


And now the crosshair doesn’t disappear anymore, and it works when holding props. You can still see it through the box though, can you elaborate on the depth buffer?

GetRenderTargetEx is what you want to use.

With which values for the flags exactly?

Old code of mine which basically does render a view to a texture.

[lua]
local TEXTURE_FLAGS_CLAMP_S = 0x0004
local TEXTURE_FLAGS_CLAMP_T = 0x0008

local rt = GetRenderTargetEx(“testRT”,
512,
512,
RT_SIZE_NO_CHANGE,
MATERIAL_RT_DEPTH_SEPERATE,
TEXTURE_FLAGS_CLAMP_S | TEXTURE_FLAGS_CLAMP_T,
CREATERENDERTARGETFLAGS_UNFILTERABLE_OK
)

local drawMaterial = CreateMaterial(“TestRT”,“UnlitGeneric”,{
["$ignorez"] = 1,
["$vertexcolor"] = 1,
["$vertexalpha"] = 1,
["$nolod"] = 1,
["$basetexture"] = rt:GetName()
})

local view = {
angles = Angle(),
origin = Vector(),
x = 0,
y = 0,
w = 512,
h = 512,
drawhud = false,
drawviewmodel = false,
}

local inDraw = false
function ENT:Draw()
self:DrawModel()
if(inDraw) then
return
end
inDraw = true

local pos = self:GetPos()
local ang = self:GetAngles()

ang:RotateAroundAxis(ang:Forward(), 90)
ang:RotateAroundAxis(ang:Right(), 90)

local oldRT = render.GetRenderTarget()
render.SetRenderTarget(rt)
	render.Clear(0, 0, 0, 255)
	render.ClearDepth()
	local w, h = ScrW(), ScrH()
	render.SetViewPort(0, 0, 512, 512)
		cam.Start2D()
			render.RenderView(view)
			surface.SetDrawColor((math.cos(math.rad(RealTime()*120))+1)*127.5)
			surface.DrawRect(0, 0, 4, 4)
		cam.End2D()
	render.SetViewPort(0, 0, w, h)
render.SetRenderTarget(oldRT)

cam.Start3D2D(pos, ang, 1)
	surface.SetMaterial(drawMaterial)
	surface.SetDrawColor(255, 255, 255, 255)
	surface.DrawTexturedRect(0, 0, 512, 512)
cam.End3D2D()

inDraw = false

end
[/lua]