Screenspace RenderTarget Overlay

I don’t quite understand how rendertargets work.


local rt1 = GetRenderTarget("construct_rt1", ScrW(), ScrH())
local mat1 = CreateMaterial("construct_mat1", "gmodscreenspace", {["$basetexture"] = rt1})

local oldw,oldh, oldrt = ScrW(), ScrH(), render.GetRenderTarget()
render.SetRenderTarget(rt1)
render.SetViewPort(0, 0, oldw, oldh)
cam.Start2D()
	for y = 1, oldh / 5 do
		for x = 1, oldw / 5 do
			surface.DrawRect(x * 5, y * 5, 2, 2)
		end
	end
cam.End2D()
render.SetRenderTarget(oldrt)
render.SetViewPort(0, 0, oldw, oldh)


Here’s what I end up with:

What am I doing wrong?

$basetexture should be set to “construct_rt1”, not the rt itself

No effect.

try setting the draw color before drawing your rects, other than that it might be something with the gmodscreenspace shader cause what you have looks fine to me

Side note, I want the material to be transparent except for where the rects are.

[editline]17th March 2015[/editline]

I’m drawing this is PostDrawOpaqueRenderables, inside a stencil:


render.SetMaterial(mat1)
render.DrawScreenQuad()

[editline]17th March 2015[/editline]

Mesmerizing.

Fixed it with mat1:SetTexture("$basetexture", rt1), but it’s not transparent.

It should work the same but you can avoid storing the old render target, width and height by using

render.PushRenderTarget and PopRenderTarget