Stencil with custom entities

Hey guys, I’ve been working on some code recently and I have an irritating issue.

I’ve been using the stencil functions to reproduce the left 4 dead glow (not originally my code but it’s been edited quite a bit) and I want to apply it to a number of different objects. The problem is, while this code works perfectly with players, random props, etc, it does not work at all when I apply it to a custom-coded entity.

For example, a wooden crate would work just fine with this script. But trying to apply the effect to a custom entity with the wooden crate’s model will not.

Any idea what the issue could be?

Code added. I use the “glowents_add” command while looking at an entity to test it. Only works on “default” entities. Nothing custom.

[lua]
local GLOWENTS = {}

concommand.Add( “glowents”, function() PrintTable( GLOWENTS ) end )
concommand.Add( “glowents_add”, function() table.insert( GLOWENTS, LocalPlayer():GetEyeTrace().Entity ) end )

local white = CreateMaterial( “whitepower”, “UnlitGeneric”, {["$basetexture"] = “models/debug/debugwhite”} )
local total = CreateMaterial( “totalimage”, “UnlitGeneric”, { ["$basetexture"] = “_rt_FullFrameFB”, ["$additive"] = “1” } )
local BloomX = render.GetBloomTex0()
local BloomY = render.GetBloomTex1()
local BlurX = Material( “pp/blurx” )
local BlurY = Material( “pp/blury” )

hook.Add( “PostDrawOpaqueRenderables”, “Glow!”, function()

cam.Start3D( EyePos(), EyeAngles() )
local DefRT = render.GetRenderTarget()
render.SetRenderTarget( BloomX )
render.Clear( 0, 0, 0, 255, true )
render.SetRenderTarget( DefRT )

BlurX:SetMaterialTexture( "$basetexture", BloomX )
BlurY:SetMaterialTexture( "$basetexture", BloomY )

render.ClearStencil()
render.SetStencilEnable( true )

render.SetStencilCompareFunction( STENCILCOMPARISONFUNCTION_ALWAYS )
render.SetStencilPassOperation( STENCILOPERATION_REPLACE )
render.SetStencilFailOperation( STENCILOPERATION_KEEP )
render.SetStencilZFailOperation( STENCILOPERATION_KEEP )
render.SetStencilWriteMask( 1 )
render.SetStencilReferenceValue( 1 )

local Colors = { r=204, g=0, b=0 }
cam.IgnoreZ( true )
SetMaterialOverride( white )
render.SetBlend( 0 )
local tab = ents.FindByClass( "npc_turret_floor" )
table.Add( tab, ents.FindByClass( "player" ) )
if GLOWENTS then table.Add( tab, GLOWENTS ) end
local tab2 = {}
for _,v in pairs( tab ) do
	if ValidEntity(v) then
		if ( v:IsPlayer() and ( v:Team() < 666 or LocalPlayer():Team() >= 666 ) ) or not v:IsPlayer() then
			table.insert( tab2, v )
			v:DrawModel()
		end
	end
end

cam.IgnoreZ(false)
render.SetBlend( 1 )
render.SetStencilEnable( false )

render.SetRenderTarget( BloomX )
render.SuppressEngineLighting( true )
render.SetColorModulation( Colors.r/255, Colors.b/255, Colors.g/255 )
for _,v in pairs( tab2 ) do
	local t = {}
	t.start = LocalPlayer():GetShootPos()
	t.endpos = v:GetPos() + Vector ( 0, 0, 30 )
	t.filter = LocalPlayer()
	local trace = util.TraceLine( t )
	if tostring(trace.Entity) != "[NULL Entity]" and trace.Entity != v then
		v:DrawModel()
	end
end

render.SetColorModulation( 1, 1, 1 )
render.SuppressEngineLighting( false )
render.SetRenderTarget( DefRT )
SetMaterialOverride()
cam.IgnoreZ( false )

render.SetRenderTarget( BloomY )
render.SetMaterial( BlurX )
render.DrawScreenQuad()

render.SetRenderTarget( BloomX )
render.SetMaterial( BlurY )
render.DrawScreenQuad()

render.SetRenderTarget( DefRT )

render.SetStencilEnable( true )
render.SetStencilReferenceValue( 0 )
render.SetStencilTestMask( 1 )
render.SetStencilCompareFunction( STENCILCOMPARISONFUNCTION_EQUAL )
render.SetStencilPassOperation( STENCILOPERATION_ZERO )

total:SetMaterialTexture( "$basetexture", BloomX )
render.SetMaterial( total )
render.DrawScreenQuad()

render.SetStencilEnable( false )
cam.End3D()

end )

[/lua]

Nothing? D: