Stencils acting weirdly?

I’m using the following code to attempt to draw stripes over every prop_physics in the world;


local mat = Material("phoenix_storms/stripes")

hook.Add("PostDrawOpaqueRenderables", "Development:Stencil:PostDrawOpaqueRenderables", function()
	local ply = LocalPlayer()

	cam.Start3D(ply:EyePos(), ply:EyeAngles())

	render.ClearStencil()
	render.SetStencilEnable(true)

	render.SetStencilFailOperation(STENCILOPERATION_KEEP)
	render.SetStencilZFailOperation(STENCILOPERATION_KEEP)
	render.SetStencilPassOperation(STENCILOPERATION_REPLACE)

	render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_ALWAYS)
	render.SetStencilReferenceValue(1)

	for _, ent in pairs(ents.FindByClass("prop_physics")) do
		ent:DrawModel()
	end

	render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_EQUAL)
	render.SetStencilPassOperation(STENCILOPERATION_KEEP)
	render.SetStencilReferenceValue(1)

	if #ents.FindByClass("prop_physics") > 0 then
		render.SetMaterial(mat)
		render.DrawScreenQuad()
	end

	render.SetStencilEnable(false)

	cam.End3D()
end)

The problem is, it instead just fills my entire screen with the material and it flickers weirdly (looks like Z fighting almost). It also randomly just goes black, and when I pick the item up with my physgun (running Garry’s halo code) it starts flickering again.

Adding the following two lines of code I found in a similar thread fixes the flickering material, but does not draw the material over prop_physics. What have I done wrong?


render.SetStencilWriteMask(1)
render.SetStencilTestMask(1)

[editline]8th July 2014[/editline]

As it turns out, when the prop spawns and I don’t touch it with my physgun, it is overlaid with the striped material correctly, but when picking it up the screen fills and breaks everything (due to Garry’s halo.lua)…

[editline]8th July 2014[/editline]

Seems to have fixed it by moving around the SetStencilWrite/TestMask.


local mat = Material("phoenix_storms/stripes")

hook.Add("PostDrawOpaqueRenderables", "Development:Stencil:PostDrawOpaqueRenderables", function()
	local ply = LocalPlayer()

	cam.Start3D(ply:EyePos(), ply:EyeAngles())

	render.ClearStencil()
	render.SetStencilEnable(true)

	render.SetStencilFailOperation(STENCILOPERATION_KEEP)
	render.SetStencilZFailOperation(STENCILOPERATION_KEEP)
	render.SetStencilPassOperation(STENCILOPERATION_REPLACE)

	render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_ALWAYS)
	render.SetStencilReferenceValue(1)

	render.SetStencilWriteMask(1)
	render.SetStencilTestMask(1)

	for _, ent in pairs(ents.FindByClass("prop_physics")) do
		ent:DrawModel()
	end

	render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_EQUAL)
	render.SetStencilPassOperation(STENCILOPERATION_KEEP)
	render.SetStencilReferenceValue(1)


	if #ents.FindByClass("prop_physics") > 0 then
		render.SetMaterial(mat)
		render.DrawScreenQuad()
	end

	render.SetStencilEnable(false)

	cam.End3D()
end)

However now, the material has flickering lines going up and down it, kind of like scanlines.

[editline]8th July 2014[/editline]

It also breaks when hitting the props against solid objects, as can be seen here:

http://a.pomf.se/zopeus.webm[/VID]

[editline]8th July 2014[/editline]

Also seems to break when pushing the object. Looks like the EyePos() and EyeAngles() lag behind?
[VID]http://a.pomf.se/pvtizz.webm

Question, how do you record and post these webms?

Use gm_video, upload the file from garrysmod/videos to pomf.se, put the URL inside [VID] tags.

This is because you still draw the old entity, try applying invisible material to it, or make it invisible otherwise.

By drawing the old entity, do you mean that I need to prevent the game from drawing it, and just let me draw it using DrawModel()?