1

I've recently started working with the stencil Shader on Unity 5 (Version 2017.1.1) I'm trying to create a line of sight mask in a top down game(i.e. the player can only see what doesn't block their character's view) I was following a YouTube tutorial on the matter (https://www.youtube.com/watch?v=xkcCWqifT9M&t=185s) however despite using the exact same code as him I could not get my project to work.

I've tried altering various values including ZTest, ColorMask and many of the stencil commands and None of the statements inside the Stencil part of the Shader seem to have any effect on how the Shader appears on screen

I have 2 Shaders: One applied to a mesh which represents what the player can see and the second is applied to objects which are to be hidden until covered by the players line of sight mesh This is the first shader which is applied to the line of sight mesh

Shader "Custom/StencilMask" {
Properties {
    _Color ("Color", Color) = (1,1,1,0.2)
    _MainTex ("Albedo (RGB)", 2D) = "white" {}
    _Glossiness ("Smoothness", Range(0,1)) = 0.5
    _Metallic ("Metallic", Range(0,1)) = 0.0

}
SubShader {
    Tags {"Queue"="Geometry-100" "RenderType"="Opaque"}

    LOD 200
    ColorMask 0
    ZWrite off

    Stencil {
        Ref 1
        Pass replace



    }

// Everything after this point in this shader is the just the standard unity shader, I haven't edited it at all

// The second shader is applied to objects that are to be revealed by the first shader:

Shader "Custom/StencilObject" {
Properties {
    _Color ("Color", Color) = (1,1,1,1)
    _MainTex ("Albedo (RGB)", 2D) = "white" {}
    _Glossiness ("Smoothness", Range(0,1)) = 0.5
    _Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
    Tags { "RenderType"="Opaque" "Queue"="Geometry+10" }
    LOD 200

    Stencil{
        Ref 1
        Comp equal

    }

I'm brand new to Shaders so any help would be very much appreciated, I'm also using unity personal but I don't know if stencil Shaders are a Unity Pro feature or not

1 Answer 1

1

One potential culprit is that stencils don't work properly in deferred rendering.

https://docs.unity3d.com/Manual/SL-Stencil.html

Stencil functionality for objects rendered in the deferred rendering path is somewhat limited, as during the base pass and lighting pass the stencil buffer is used for other purposes. During those two stages stencil state defined in the shader will be ignored and only taken into account during the final pass. Because of that it’s not possible to mask out these objects based on a stencil test [...]

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the help mate! do you know how I can force it to stencil in a non deferred rendering path?
I used your code (copy paste into some base Standard and Unlit shaders) and it works as expected. However pay attention to these details: ZWrite Off in StencilMask.shader : has a capital O #pragma surface surf Standard fullforwardshadows addshadow in StencilObject.shader : I had to add addshadow for shadows to behave properly
Also, it stops working in Deferred rendering mode as expected :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.