January 22, 2019
Field of View
Tutorial – E03
Youtube -Field of view visualisation (E03)
By: Sebastian Lague
This tutorial ties the Field of View (FoV) series in with shaders. These shaders block everything from rendering except what is viewed by the player character. The shader scripts actually had barely anything added to them, and I’m not completely sure what they did.
The shaders were both Standard Surface Shaders. A Stencil method was added into the SubShader section of both. In StencilMask, this method was just:Ref1Pass replaceIn StencilObject, it was:Ref 1Comp equalThe only other additional code was in StencilMask, which was adding “Queue” = “Geometry-100” to the Tags, then ColorMask 0, and ZWrite off.
SHADER PROGRAMMING TERMS
-
”Queue” = “Geometry-100”
So these tags are terms that represent integer values that determine what is rendered first. Geometry has an integer value of 2000, and is the base for most opaque objects, so choosing “Geometry-100” will make whatever this shader is applied to render before anything labeled as just Geometry (like most opaque objects) as it will now have a value of 1900.
-
ColorMask 0
ColorMask sets color channel writing mask. Writing ColorMask 0 turns off rendering to all color channels. Not sure exactly what this ends up doing.
-
Stencil
The Stencil Buffer is a general purpose per pixel mask for saving or discarding pixels.
-
Ref 1
This is the value each pixel is compared against and/or value to be written to the buffer.
-
Comp equal
Comp compares the reference value to the current contents of the buffer.
-
Pass replace
A Pass block renders geometry of a GameObject once. This determines what to do with the contents of the buffer if the stencil test passes (which might go with the Ref). Replace writes the value into the buffer.
ATTEMPTED SUMMARY
StencilObject is placed on the materials to all of the GameObjects, like the obstacles, the ground and the targets. This makes them render completely black. Then the StencilMask is applied to the ViewVisualization material of the player’s vision, and this “uncovers” the objects in vision by allowing them to render normally (removing the internal mask the objects are placing on themselves).
StencilObject creates a reference value of 1, then compares to that value. So it should only render pixels whose value equals 1, the reference value in the buffer. StencilMask has a Pass replace command, so this apparently writes the Ref value directly to the buffer. So this is writing the value 1 to the buffer. For some reason, this renders just before standard Geometry queue objects, and there is a ColorMask 0. This will require more shader knowledge in the future for me to understand fully.
PROBLEMS
For some reason, my ground plane was fading out on the edges. The outter edges would actually fade so much they completely blended into the background as the exact same color. I know the ground was still there as I could see it in the editor and the player did not fall when walking on the invisible ground area, but it just did not appear. Changing the Directional Light color to black fixed this for some reason, so there appears to be some strange interactions with these shaders and the lighting in the scene.