Clearing rendertarget with render.Clear(0,0,0,0) doesn't work as expected

I doubt that anyone here can answer this but I’ll just post it here.
I’m trying to render a 3D object in cam.Start3D2D with alpha background.
With help of amazing Wizard of Ass I could get it to this point where the 3d object is being rendered but with background.

http://puu.sh/3aukk.jpg

Alright here’s the tricky part: If I clear the rendertarget with alpha 0 (render.Clear(255,255,255,0)) it doesn’t render the model with
alpha 255, but instead with alpha 0.
So instead of just turning the background invisible it also makes everything else invisible that is being drawn on the rendertarget.

TL;DR; I have no idea how to render 3d objects on rendertarget without a background and according to Wizzy this code used to work before.

Here’s the code if you want to try it out:



local TEXTURE_FLAGS_CLAMP_S = 0x0004
local TEXTURE_FLAGS_CLAMP_T = 0x0008


local rt = GetRenderTargetEx("testRT",
    512,
    512,
    RT_SIZE_HDR,
    MATERIAL_RT_DEPTH_SEPARATE,
    bit.bor(TEXTURE_FLAGS_CLAMP_S,TEXTURE_FLAGS_CLAMP_T),
    CREATERENDERTARGETFLAGS_HDR,
    IMAGE_FORMAT_RGBA8888
)


local drawMaterial = CreateMaterial("TestRT","UnlitGeneric",{
    ["$ignorez"] = 1,
    ["$vertexcolor"] = 1,
    ["$vertexalpha"] = 1,
    ["$nolod"] = 1,
    ["$basetexture"] = rt:GetName()
})
 
local ent = ClientsideModel("models/props_junk/PlasticCrate01a.mdl")
ent:SetNoDraw(true)
ent:SetPos(Vector(0,0,0))
 
local lookat = Vector(0,0,10)
local campos = Vector(40,40,0)
local anglus =((ent:GetPos() + ent:OBBCenter())-campos):Angle()

hook.Add("PostDrawTranslucentRenderables","DrawStuff",function()

    ent:SetAngles(Angle(0,CurTime()*15,0))
   
    local oldRT = render.GetRenderTarget()
    render.SetRenderTarget(rt)
        render.Clear(255,255,255,255)
        render.ClearDepth()
        local w,h = ScrW(),ScrH()
        render.SetViewPort(0,0,512,512)
            cam.Start3D(campos,anglus,50,0,0,512,512)
           
                    render.SuppressEngineLighting(true)
                   
                    render.SetLightingOrigin(ent:GetPos())
                    render.ResetModelLighting(1,1,1)
                    render.SetColorModulation(1,1,1)
                    render.SetBlend(1)
                   
                        for i=0, 6 do
                            render.SetModelLighting( i, 1,1,1 )
                        end
                   
                       ent:DrawModel()
                   
                    render.SuppressEngineLighting(false)
               
            cam.End3D()
        render.SetViewPort(0,0,w,h)
    render.SetRenderTarget(oldRT)
 
    local pos = Vector(-391.968750,844.689941,572.749512)
   
    cam.Start3D2D(pos,Angle(0,90,90),1)
   
        surface.SetMaterial(drawMaterial)
        surface.SetDrawColor(Color(255,255,255,255))
        surface.DrawTexturedRect(0,0,100,100)
   
    cam.End3D2D()
 
end)



To be more precise, rather than updating the alpha value, the renderer seems to use the previous alpha value(in that case rather than setting the alpha to 255 where the prop is is simply uses 0 from the buffer).

It might have something to do with additive alpha rendering but I am not too sure.

The workaround I had to come up with for this was to clear the render target with black, then write to the stencil buffer while drawing onto the render target and then clear the areas that were not marked on the stencil buffer with 0 alpha using render.ClearBuffersObeyStencil. If you decide to do this, make sure the render target has its own depth buffer.

That sounds quite expensive method, I need update the rendertarget like pretty much every frame.
How much fps did you lose before- and after using the stencil method?

In my case, I just needed to render the render targets once and then never again, so there wasn’t any framerate impact. I don’t think the stencil buffer adds any significant overhead (roughly the same cost as using the depth buffer I imagine), so just try it out.

I am wondering why this happens in the first place, would prefer if he wouldn’t have to use these hacks.