Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using Alphas in a Stencil Mask

Discussion in 'Shaders' started by Camronas, Sep 11, 2016.

  1. Camronas

    Camronas

    Joined:
    Apr 20, 2012
    Posts:
    154
    Hi everyone, I am only really new to shaders but I have been trying to use alphas with a stencil.

    Mask Script:
    Code (CSharp):
    1. Shader "Custom/Stencil_Mask" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType"="Opaque" "Queue" = "Geometry-100" }
    10.         ColorMask 0
    11.         ZWrite off
    12.         LOD 200
    13.  
    14.         Stencil
    15.         {
    16.             Ref 1
    17.             Pass replace
    18.         }
    19.  
    20.         CGPROGRAM
    21.         // Physically based Standard lighting model, and enable shadows on all light types
    22.         #pragma surface surf Standard fullforwardshadows
    23.  
    24.         // Use shader model 3.0 target, to get nicer looking lighting
    25.         #pragma target 3.0
    26.  
    27.         sampler2D _MainTex;
    28.  
    29.         struct Input {
    30.             float2 uv_MainTex;
    31.         };
    32.  
    33.         half _Glossiness;
    34.         half _Metallic;
    35.         fixed4 _Color;
    36.  
    37.         void surf (Input IN, inout SurfaceOutputStandard o) {
    38.             // Albedo comes from a texture tinted by color
    39.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    40.             o.Albedo = c.rgb;
    41.             // Metallic and smoothness come from slider variables
    42.             o.Metallic = _Metallic;
    43.             o.Smoothness = _Glossiness;
    44.             o.Alpha = c.a;
    45.         }
    46.         ENDCG
    47.     }
    48.     FallBack "Diffuse"
    49. }
    50.  
    Object Shader:
    Code (CSharp):
    1. Shader "Custom/Stencil_Object" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 200
    11.  
    12.         Stencil
    13.         {
    14.         Ref 1
    15.         Comp equal
    16.         }
    17.  
    18.         CGPROGRAM
    19.         // Physically based Standard lighting model, and enable shadows on all light types
    20.         #pragma surface surf Standard fullforwardshadows
    21.  
    22.         // Use shader model 3.0 target, to get nicer looking lighting
    23.         #pragma target 3.0
    24.  
    25.         sampler2D _MainTex;
    26.  
    27.         struct Input {
    28.             float2 uv_MainTex;
    29.         };
    30.  
    31.         half _Glossiness;
    32.         half _Metallic;
    33.         fixed4 _Color;
    34.  
    35.         void surf (Input IN, inout SurfaceOutputStandard o) {
    36.             // Albedo comes from a texture tinted by color
    37.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    38.             o.Albedo = c.rgb;
    39.             // Metallic and smoothness come from slider variables
    40.             o.Metallic = _Metallic;
    41.             o.Smoothness = _Glossiness;
    42.             o.Alpha = c.a;
    43.         }
    44.         ENDCG
    45.     }
    46.     FallBack "Diffuse"
    47. }
    48.  
    An object is given the mask shader which others are given the stencil. When the Mask object is above the Stencil objects they will become visible, otherwise they are invisible. This works well, however I want to give this a bit more customisation. At the moment the world that the mask is not covering is pitch black and the objects the mask is over are 100% visible. Is it possible to add an alpha value would allow me to find a middle ground for this?

    Something like, the objects not under the mask are darker but still slightly visible rather than pitch black and completely hidden.

    Any help on this would be fantastic! Cheers.
     
  2. etopsirhc

    etopsirhc

    Joined:
    Jan 20, 2015
    Posts:
    5
    i'm also a bit of a newb with this, but from all the research i've done on a similar topic, i dont think you can alpha the stencil, or at least directly. you may be able to add a second pass with something similar to an outline shader to do what you are looking for.