Transparency with render targets.

I’ve run into a certain problem with transparency on render targets when I was doing something like this:

  1. Creating an RT and making a material using the RT as a texture.
  2. Drawing once onto the RT and then displaying the material somewhere in the world (this was made as an alternative to using cam.Start3D2, since that caused a lot of FPS lag when being rendered on every frame)

However, I could not achieve having a transparent background, as using render.Clear() with an alpha value of 255 created a rather horrible looking background, and using it with a lesser alpha value would render the entire RT transparent, even if it was supposed to have opaque parts on it.

Here’s roughly the code that I’ve used, and I’ve omitted some of the variable definitions to get to the core part: (It’s for a ‘rules’ panel displayed on the spawn)
[lua]local rulesrt = GetRenderTarget(“rulesrt”, 1024, 1024, true)
local rulesmat = CreateMaterial(“rulesmat”, “UnlitGeneric”, {
["$basetexture"] = “rulesrt”,
["$translucent"] = “1”
})

local rendered = false

hook.Add(“PostDrawTranslucentRenderables”, “test”, function()
if !rendered then
local oldRT = render.GetRenderTarget()
local oldW, oldH = ScrW(), ScrH()
render.SetRenderTarget(rulesrt)
render.SetViewPort(0, 0, 1024, 1024)
render.Clear(20, 20, 20, 255, true, true) – changing the alpha to anything other than 255 renders the entire RT being transparent
cam.Start2D()
do
– draw stuff
end
cam.End2D()
render.SetRenderTarget(oldRT)
render.SetViewPort(0, 0, oldW, oldH)
rendered = true
end
cam.Start3D(LocalPlayer():EyePos(), LocalPlayer():EyeAngles())
render.SetMaterial(rulesmat)
render.DrawQuadEasy(Vector(1023.9, -122, -146), Vector(-1, 0, 0), 200, 200, Color(255, 255, 255, 255), 180)
cam.End3D()
end)[/lua]

Help would be greatly appreciated.

What are you using in the “draw stuff” area? I couldn’t get anything to draw with your code.
Incidentally have another go at using cam.Start3D2D; I’m pretty sure a bigger scale would raise your FPS a bit, but someone please correct me if I’m wrong.

The lag was due to having a lot of draw operations overall, it appeared.
And like I said, I’ve omitted a lot of everything in there. Lemme throw up an example though.

[lua]
if SERVER then end

local rulesrt = GetRenderTarget(“rulesrt”, 1024, 1024, true)
local rulesmat = CreateMaterial(“rulesmat”, “UnlitGeneric”, {
["$basetexture"] = “rulesrt”,
["$translucent"] = “1”
})

hook.Add(“HUDPaint”, “test”, function()
if !rendered then
local oldRT = render.GetRenderTarget()
local oldW, oldH = ScrW(), ScrH()
render.SetRenderTarget(rulesrt)
render.SetViewPort(0, 0, 1024, 1024)
render.Clear(20, 20, 20, 255, true, true)
cam.Start2D()
surface.DrawCircle(512, 512, 128, Color(255, 0, 0, 255))
cam.End2D()
render.SetRenderTarget(oldRT)
render.SetViewPort(0, 0, oldW, oldH)
rendered = true
end
cam.Start3D(LocalPlayer():EyePos(), LocalPlayer():EyeAngles())
render.SetMaterial(rulesmat)
render.DrawQuadEasy(Vector(1023.9, -100, -40), Vector(-1, 0, 0), 200, 200, Color(255, 255, 255, 255), 180)
cam.End3D()
end)
[/lua]

This should work off the shelf, it puts a big black square on the spawn in gm_construct and draws a red circle on it.

http://gyazo.com/557a1a1fdba84a133d49a8f56271aac4.png

Shameless bamp.

This is a bug currently with Garry’s mod.
I’ve reported it here:
http://forum.facepunch.com/showthread.php?t=1277159
and there
http://forum.facepunch.com/showthread.php?t=1276498

But nobody seems to give a shit about it.

I see, figured it was a bug. Not really surprised that nobody gives a shit about it, rendertargets are fairly obscure in use.