SweptForums
How To Fix Stuff Drawing In Front of Your 3D2D - Printable Version

+- SweptForums (https://forums.sweptthr.one)
+-- Forum: Garry's Mod Zone (https://forums.sweptthr.one/forum-7.html)
+--- Forum: Help and Bugs (https://forums.sweptthr.one/forum-11.html)
+--- Thread: How To Fix Stuff Drawing In Front of Your 3D2D (/thread-102.html)



How To Fix Stuff Drawing In Front of Your 3D2D - SweptThrone - 21-Jun-2024

Set ENT.RenderGroup = RENDERGROUP_BOTH in your entity's shared file.
Create a new ENT:DrawTranslucent() hook and move all of your 3D2D code to it.  Keep self:DrawModel() in ENT:Draw().  These should both be clientside only.

Pretty simple fix for a common problem.  It makes sense when you think about it, but if you're like me, you never even knew there was a "DrawTranslucent" hook for entities.  This is the case even if your 3D2D isn't translucent, as you can see below.  Both the opaque text and the translucent background are drawn behind the door before the fix, and in front of it after.

[Image: fixdumb.png]

Shoutout to Fesiug for helping me fix this on STRP.

Here's an example from STFood Neu STRP Edition's stove:

Bad Example
-- BAD, DO NOT USE THIS
-- st_food_neu_stove.lua (shared)

AddCSLuaFile()

ENT.Type = "anim"
ENT.Base = "base_anim"

ENT.PrintName = "Stove"
ENT.Author = "SweptThrone"
ENT.Contact = "sweptthr.one/contact"
ENT.Spawnable = true
ENT.AdminSpawnable = false
ENT.Category = "STFood Neu"

if CLIENT then
    function ENT:Draw()
        self:DrawModel()

        local Pos = self:GetPos()
        local Ang = self:GetAngles()
       
        Ang:RotateAroundAxis(Ang:Forward(), 90)
        Ang:RotateAroundAxis(Ang:Right(), 270)
       
        local txt = "Serve Food!"
       
        surface.SetFont( "DermaLarge" )
        local TextWidth = surface.GetTextSize( txt )
        cam.Start3D2D( Pos + Ang:Up() * -15 + Ang:Right() * ( 5 + TimedSin( 0.25, 0, 2, 0 ) ) - Ang:Forward() * 1.5, Ang, 0.16 )
            draw.WordBox( 4, -TextWidth*0.5, -260, txt, "DermaLarge", Color( 0, 0, 0, 200 ), Color( 255, 255, 255, 255 ) )
        cam.End3D2D()
       
    end
end

Good Example
-- GOOD, USE THIS EXAMPLE
-- st_food_neu_stove.lua (shared)

AddCSLuaFile()

ENT.Type = "anim"
ENT.Base = "base_anim"

ENT.PrintName = "Stove"
ENT.Author = "SweptThrone"
ENT.Contact = "sweptthr.one/contact"
ENT.Purpose = ""
ENT.Instructions = ""
ENT.Spawnable = true
ENT.AdminSpawnable = false
ENT.Category = "STFood Neu"

ENT.RenderGroup = RENDERGROUP_BOTH

if CLIENT then
function ENT:Draw()
self:DrawModel()
end

    function ENT:DrawTranslucent()
        local Pos = self:GetPos()
        local Ang = self:GetAngles()
       
        Ang:RotateAroundAxis(Ang:Forward(), 90)
        Ang:RotateAroundAxis(Ang:Right(), 270)
       
        local txt = "Serve Food!"
       
        surface.SetFont( "DermaLarge" )
        local TextWidth = surface.GetTextSize( txt )
        cam.Start3D2D( Pos + Ang:Up() * -15 + Ang:Right() * ( 5 + TimedSin( 0.25, 0, 2, 0 ) ) - Ang:Forward() * 1.5, Ang, 0.16 )
            draw.WordBox( 4, -TextWidth*0.5, -260, txt, "DermaLarge", Color( 0, 0, 0, 200 ), Color( 255, 255, 255, 255 ) )
        cam.End3D2D()
       
    end
end