Hello. I have been messing around with stencils, and there are still some things that just aren’t clear to me. I made a thread because people might have similar questions, which I couldn’t find the answer to which searching through other old threads. I read through this thread, and admittedly, it raised more questions than it had answered. So I wrote some code based off the tutorials I saw and what I read through. It doesn’t work, but that doesn’t really matter because I don’t know what each line is doing anyways lol. The purpose of the code is just to help me(and hopefully other people?) better understand whats going on. So, I really have more of a theory that I picked up when I went through the tutorials. It’s in the code. If someone could just confirm or deny that I’m doing it right or wrong OR better yet correct me, it would much appreciated. =D
Please be advised: Code below might contain a lot of retardery lol
hook.Add("PostDrawOpaqueRenderables","PlayerBorders",function()
--[[ What I was trying to do is draw the prop when it dissapears from the players view through the wall. When the prop
goes behind the wall, it should turn red or something. ]]
-- Again, everything below is my interpretaion of what is going on, not what is actually going on.
for k, v in pairs( ents.FindByClass("prop_physics")) do
render.ClearStencil() -- clears stencil data(which I assume is clearing any previously set settings by other addons or even my own stencil render)
render.SetStencilEnable( true ) -- starts stencil ( in my mind, essentially like declaring a new function )
render.SetStencilWriteMask( 0-255 ) -- I have no idea what this does.
render.SetStencilTestMask( 0-255 ) -- or this
render.SetStencilReferenceValue(1) -- sets a value that you use to compare to other pixel values? I assume this is for drawing, like, layers of stencils?
-- That sounds retarded, but currently what I think it is.
render.SetStencilFailOperation(STENCILOPERATION_KEEP) --[[ if the stencil fails for any reason, we keep the current value. What value this is, I have no idea. Is it
pixel value or the reference value? ]]
render.SetStencilZFailOperation(STENCILOPERATION_INCR) --[[ If the pixel value is behind something and blocking the players sight then we increase the current value. ]]
render.SetStencilPassOperation(STENCILOPERATION_KEEP) --[[ If the stencil passes then we keep the current value. Again, don't know what value we are even changing
with this. ]]
--[[ Okay. While my understanding is bad, I think(SetStencilCompareFunction) these work like regular if-statements. The only way to end these
'if statements' is SetStencilEnable(false) or just calling a different SetStencilCompareFunction. Also, before you call these, you have to have a
previously set Reference Value and all SetStencilPassOperation set. ]]
render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_ALWAYS) -- this one runs always, so here is where you would update stuff every frame if you needed to.
// Code that runs as a result of the above SetStencilCompareFunction passing.
-- Only the code directly under this check doesn't run if this check fails. BUT other check still run below this one.
render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_GREATER)
// code that runs as a result of the above SetStencilCompareFunction passing.
-- this next check isn't needed for what I want(I think), but here just for the sake of questions.
render.SetStencilCompareFunction(STENCILCOMPARISONFUNCTION_NOTEQUAL) -- This check would still run even if above checks failed.
// code that runs as a result of the above SetStencilCompareFunction passing.
render.SetStencilEnable(false) -- stops stencil (the end of our 'function')
end
end)
So, main questions -
- What does a reference value do/What does it represent? I know it’s used for comparisons, but can someone maybe provide an example of how it’s used? Confusing.
- What is the pixel value?
- Why is, whatever the ‘value’ is, reset to 0 when it goes above 255? A better question is why stop at 255? What does 255 represent?
Sorry for the long post. If anyone has any past experience with stencils, please, drop some knowledge below.