Seeing player through wall using stencils.

I’m trying to make it so when you’re behind a wall you’ll be able to see a silhouette of yourself, but I’m not really sure how the stencil buffer works. I’ve been looking at the iVision addon but I can’t really figure it out. The gamemode is third person.

[lua]local matBlack = CreateMaterial( “ZBlack”, “UnlitGeneric”, {
[ “$basetexture” ] = “vgui/black”
} )

function GM:PostPlayerDraw(ply)

if not ply == LocalPlayer() then return end

--Zfail player draw
cam.Start3D(EyePos(),EyeAngles())
render.ClearStencil()
render.SetStencilEnable(true)

--what goes here?

render.SetStencilEnable(false)
cam.End3D()
--End Zfail player draw

end[/lua]

First of all, I think you need to set your material to ignorez. Then you need to set the pixels value in the stencil buffer to one if it Z fails and then you can draw a screenquad with the reference value as 1.

[lua]local matBlack = CreateMaterial( “ZBlack”, “UnlitGeneric”, {
[ “$basetexture” ] = “vgui/black”,
[ “$ignorez” ] = “1”
} )

function GM:PostPlayerDraw(ply)

if not ply == LocalPlayer() then return end

cam.Start3D(EyePos(),EyeAngles())
	render.ClearStencil()
	render.SetStencilEnable(true)

	render.SetStencilFailOperation(STENCILOPERATION_KEEP)
	render.SetStencilZFailOperation(STENCILOPERATION_REPLACE)
	render.SetStencilPassOperation(STENCILOPERATION_KEEP)
	render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_ALWAYS)
	render.SetStencilReferenceValue(1)
			
	ply:DrawModel()
	
	render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_EQUAL)
	render.SetStencilPassOperation(STENCILOPERATION_REPLACE)
	
	render.SetMaterial(matBlack)
	render.DrawScreenQuad()

	render.SetStencilEnable(false)
cam.End3D()

end[/lua]

This is untested and probably won’t work as I have never used the stencil buffer.

[editline]10:08PM[/editline]

I have just tried it and I am getting a C stack overflow. I know why, but I don’t know how to solve it.

Thank you, but yeah I’m getting the C stack overflow with it too.

[lua]local matBlack = CreateMaterial( “ZBlack”, “UnlitGeneric”, {
[ “$basetexture” ] = “vgui/black”,
[ “$ignorez” ] = “1”
} )

local exception

function GM:PostPlayerDraw(ply)

if ply != LocalPlayer() or exception then return end

cam.Start3D(EyePos(),EyeAngles())
	render.ClearStencil()
	render.SetStencilEnable(true)

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

            exception = true
			
	ply:DrawModel()
	
	exception = nil

            render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_EQUAL)
	render.SetStencilPassOperation(STENCILOPERATION_REPLACE)
	
	render.SetMaterial(matBlack)
	render.DrawScreenQuad()

	render.SetStencilEnable(false)
cam.End3D()

end[/lua]

Fixed MakeR’s code.

[editline]01:28AM[/editline]

Also $ignorez isn’t needed but keeping it there won’t break anything.

Thank you, but there are a few errors, black stuff appears on the player.
[img_thumb]https://dl.dropbox.com/u/3609329/sb_error1.jpg[/img_thumb]

and, The little Fretta circle gets stenciled too.
[img_thumb]https://dl.dropbox.com/u/3609329/sb_error2.jpg[/img_thumb]

[lua]
function GM:PrePlayerDraw(ply)

if ply == LocalPlayer() then

    ply:SetNoDraw(true)
end

end
[/lua]

This should fix the first problem, not sure about the second one though.

That made me not draw at all.
However doing this fixed the second problem:
[lua]local matBlack = CreateMaterial( “ZBlack”, “UnlitGeneric”, {
[ “$basetexture” ] = “vgui/black”,
[ “$ignorez” ] = “1”
} )

local exception

function GM:PrePlayerDraw(ply)

if ply != LocalPlayer() or exception then return end

cam.Start3D(EyePos(),EyeAngles())
	render.ClearStencil()
	render.SetStencilEnable(true)

	render.SetStencilFailOperation(STENCILOPERATION_KEEP)
	render.SetStencilZFailOperation(STENCILOPERATION_REPLACE)
	render.SetStencilPassOperation(STENCILOPERATION_KEEP)
	render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_ALWAYS)
	render.SetStencilReferenceValue(1)
	
	exception = true

end

function GM:PostPlayerDraw(ply)

	exception = nil
	
	render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_EQUAL)
	render.SetStencilPassOperation(STENCILOPERATION_REPLACE)
	
	render.SetMaterial(matBlack)
	render.DrawScreenQuad()

	render.SetStencilEnable(false)
cam.End3D()

end[/lua]

That should fix the first problem too. And you don’t need the exception variable when you do that (it’s only needed if you call ply:DrawModel())

[editline]02:23AM[/editline]

Dayum that is good code you got there:3: