Problem With Stencil Buffering

Fixed the first issue.

Now i have a problem, i cant get the script to not draw through walls.

[lua]local MaterialBlurX = Material( “pp/blurx” )
local MaterialBlurY = Material( “pp/blury” )
local MaterialWhite = CreateMaterial( “WhiteMaterial”, “VertexLitGeneric”, {
["$basetexture"] = “color/white”,
["$vertexalpha"] = “1”,
["$model"] = “1”,
} )
local MaterialComposite = CreateMaterial( “CompositeMaterial”, “UnlitGeneric”, {
["$basetexture"] = “_rt_FullFrameFB”,
["$additive"] = “1”,
} )

// we need two render targets, if you don’t want to create them yourself, you can
local RT1 = render.GetBloomTex0()
local RT2 = render.GetBloomTex1()

/------------------------------------
RenderGlow()
------------------------------------
/
function RenderGlow(entity, color)
local def = Color(255,255,255)
col = color or Color(255,0,0)

render.SetStencilEnable( true )
render.SetStencilFailOperation( STENCILOPERATION_ZERO )
render.SetStencilZFailOperation( STENCILOPERATION_ZERO )
render.SetStencilPassOperation( STENCILOPERATION_REPLACE )
render.SetStencilCompareFunction( STENCILCOMPARISONFUNCTION_ALWAYS )
render.SetStencilWriteMask(1)
render.SetStencilReferenceValue( 1 )

cam.IgnoreZ( true )
    render.SetBlend( 0 )
        SetMaterialOverride( MaterialWhite )
            entity:DrawModel()
        SetMaterialOverride()
    render.SetBlend( 1 )
cam.IgnoreZ( false )
 
local w, h = ScrW(), ScrH()
 
// draw into the white texture
local oldRT = render.GetRenderTarget()
 
render.SetRenderTarget( RT1 )
 
    render.SetViewPort( 0, 0, RT1:GetActualWidth(), RT1:GetActualHeight() )
     
    cam.IgnoreZ( true )
     
        render.SuppressEngineLighting( true )
         
         render.SetColorModulation(col.r/255,col.g/255,col.b/255)  
                        
            SetMaterialOverride( MaterialWhite )
                entity:DrawModel()
            SetMaterialOverride()
             
        render.SetColorModulation( 1, 1, 1 )
        render.SuppressEngineLighting(false)
         
	cam.IgnoreZ( false )
	render.SetViewPort( 0, 0, w, h )
render.SetRenderTarget( oldRT )
render.SetStencilEnable(false)

end

/------------------------------------
RenderScene()
------------------------------------
/
hook.Add( “RenderScene”, “ResetGlow”, function( Origin, Angles )

local oldRT = render.GetRenderTarget()
render.SetRenderTarget( RT1 )
    render.Clear( 0, 0, 0, 255, true )
render.SetRenderTarget( oldRT )

end )

/------------------------------------
RenderScreenspaceEffects()
------------------------------------
/
hook.Add( “RenderScreenspaceEffects”, “CompositeGlow”, function()

MaterialBlurX:SetMaterialTexture( "$basetexture", RT1 )
MaterialBlurY:SetMaterialTexture( "$basetexture", RT2 )
MaterialBlurX:SetMaterialFloat( "$size", 2 )
MaterialBlurY:SetMaterialFloat( "$size", 2 )
     
local oldRT = render.GetRenderTarget()
 
for i = 1, 3 do
 
    // blur horizontally
    render.SetRenderTarget( RT2 )
    render.SetMaterial( MaterialBlurX )
    render.DrawScreenQuad()

    // blur vertically
    render.SetRenderTarget( RT1 )
    render.SetMaterial( MaterialBlurY )
    render.DrawScreenQuad()
     
end

render.SetRenderTarget( oldRT )
 
// tell the stencil buffer we're only going to draw
// where the player models are not.
render.SetStencilEnable( true )
render.SetStencilReferenceValue( 0 )
render.SetStencilTestMask( 1 )
render.SetStencilCompareFunction( STENCILCOMPARISONFUNCTION_EQUAL )
render.SetStencilPassOperation( STENCILOPERATION_ZERO )
 
// composite the scene
MaterialComposite:SetMaterialTexture( "$basetexture", RT1 )
render.SetMaterial( MaterialComposite )
render.DrawScreenQuad()

// don't need this anymore
render.SetStencilEnable( false )

end )

local playerheldweap = nil

–[[hook.Add( “PostPlayerDraw”, “RenderEntityGlow”, function(ply)
if( ScrW() == ScrH() ) then return end
if OUTLINING_ENTITY then return end
OUTLINING_ENTITY = true
RenderGlow(ply,Color(0,255,0))
playerheldweap = ply:GetActiveWeapon()
if IsValid(playerheldweap) then
RenderGlow(playerheldweap,Color(0,255,0))
end
OUTLINING_ENTITY = false
end)]]–[/lua]

Don’t ignore Z?

Kuro asked the same, tried for 2 days, not possible.

Great, If Kuro couldnt do it, i have no chance.

render.SetStencilZFailOperation( STENCILOPERATION_KEEP )?

Won’t work.

Why not only draw it when you can see it? It should eliminate most of this problem, but will leave the issue when its partially visible.

Have you actually tried it?

I got it working, use the code on http://wiki.garrysmod.com/?title=Stencil and change render.SetStencilZFailOperation to STENCILOPERATION_ZERO, it wont work with the other material

Read the op, ive got that, still nothing.

This is something different, it also does not use render targets, rt’s don’t have their own stencil buffer hence all this won’t work.

Could you not just run a trace from the local players eye position, to the other player, and if it hits the world then don’t draw it?

It would be a bit shitty seeing as they could have an arm or a leg sticking out though.

This won’t work, not only because collision meshes aren’t the same as the model meshes.

That shouldn’t matter, the physics meshes are made to encompass the entire model mesh. If they don’t, you need a new model.

You didn’t get my point.