Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

What is the easiest eqvivalent of 1bit size stencil, which works in deferred rendering path?

Discussion in 'Shaders' started by Kusras, Dec 7, 2019.

  1. Kusras

    Kusras

    Joined:
    Jul 9, 2015
    Posts:
    134
    Hello, I hit the common issue... Stencil and deferred rendering path. I wrote shader it worked, but switching scene to the main with deferred threw my work on that shader into garbage.

    What is the most weird thing, is that 5th bit in stencil is unused by unity, so it could somehow be usable with my shaderrs. sadly it seems like, it just ignore the pass at all in deferred rendering path. even if I use comp Always... so if there is a way with stencil. What I am doing wrong?

    Code (CSharp):
    1. Shader "StandardBoostedXray"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color", Color) = (1,1,1,1)
    6.         _MainTex("Albedo", 2D) = "white" {}
    7.     _ColorXray("Color Xray", Color) = (1,0.792,0,1)
    8.     [NoScaleOffset]_MetallicGlossMap("MetallicMap", 2D) = "white" {}
    9.     _Glossiness("Smoothness", float) = 0.5
    10.         _Metallic("Metallic", float) = 0.0
    11.         [NoScaleOffset]_BumpMap("Normal Map", 2D) = "bump" {}
    12.     _BumpScale("Scale NormalMap", Float) = 1.0
    13.         [NoScaleOffset]_OcclusionMap("Occlusion", 2D) = "white" {}
    14.     _OcclusionStrength("Occlusion Strength",float) = 1.0
    15.     }
    16.  
    17.         SubShader{
    18.         Tags{ "RenderType" = "Opaque"  }//
    19.         LOD 200
    20.         Stencil
    21.     {
    22.         Ref 32
    23.         //IncrSat 32
    24.         Comp always
    25.         Pass replace// IncrSat//
    26.         ZFail keep
    27.     }
    28.         CGPROGRAM
    29.         // Physically based Standard lighting model, and enable shadows on all light types
    30. #pragma surface surf Standard fullforwardshadows
    31.  
    32.         // Use shader model 3.0 target, to get nicer looking lighting
    33.     #pragma target 3.0
    34.  
    35.         sampler2D _MainTex,_MetallicGlossMap,_BumpMap,_OcclusionMap;//,_EmissionMap;
    36.     half _Glossiness, _Metallic, _OcclusionStrength, _BumpScale;//,_EmissionColor
    37.     fixed4 _Color, _ColorXray;
    38.     struct Input {
    39.         float2 uv_MainTex;
    40.     };
    41.  
    42.  
    43.  
    44.     void surf(Input IN, inout SurfaceOutputStandard o) {
    45.         fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    46.         fixed4 m = tex2D(_MetallicGlossMap, IN.uv_MainTex);
    47.         o.Albedo = c.rgb;
    48.         o.Occlusion = tex2D(_OcclusionMap, IN.uv_MainTex)*_OcclusionStrength;
    49.         o.Normal = UnpackScaleNormal(tex2D(_BumpMap, IN.uv_MainTex),_BumpScale);
    50.         o.Metallic = m.a*_Metallic;
    51.         o.Smoothness = _Glossiness*m.rgb;
    52.      
    53.     }
    54.     ENDCG
    55. //    }
    56.  
    57.         Pass
    58.     {
    59.         Tags
    60.     {
    61.         "Queue" = "AlphaTest"//AlphaTest +1
    62.     }
    63.         // Won't draw where it sees ref value 32
    64.         Cull Back // draw front faces
    65.         ZWrite OFF
    66.         ZTest Always
    67.         Stencil
    68.     {
    69.         Ref 32
    70.         Comp NotEqual
    71.         Pass keep
    72.     }
    73.         Blend SrcAlpha OneMinusSrcAlpha
    74.  
    75.         CGPROGRAM
    76. #pragma vertex vert
    77. #pragma fragment frag
    78. #include "UnityCG.cginc"
    79.         // Properties
    80.         uniform float4 _ColorXray;
    81. //    sampler2D _BumpMap;
    82.     struct vertexInput
    83.     {
    84.         float4 vertex : POSITION;
    85.         float3 normal : NORMAL;
    86.     };
    87.  
    88.     struct vertexOutput
    89.     {
    90.         float4 pos : SV_POSITION;
    91.         float3 normal : TEXCOORD0;
    92.         float3 viewDirection : POSITION2;
    93.  
    94.     };
    95.  
    96.     vertexOutput vert(vertexInput input)
    97.     {
    98.         vertexOutput output;
    99.         output.pos = UnityObjectToClipPos(input.vertex);
    100.         output.normal = normalize(mul(float4(input.normal,0), unity_WorldToObject).xyz);
    101.         output.viewDirection = normalize(_WorldSpaceCameraPos - mul(unity_ObjectToWorld, input.vertex).xyz);
    102.         return output;
    103.     }
    104.  
    105.     float4 frag(vertexOutput input) : COLOR
    106.     {
    107.     //    float3 norm = UnpackScaleNormal(tex2D(_BumpMap, IN.uv_MainTex),_BumpScale);
    108.         float newOpacity =1.25 -abs(dot(input.viewDirection,  input.normal));
    109.     newOpacity = newOpacity*(newOpacity+0.3)*_ColorXray.a+0.1;
    110.     float4 output = float4(_ColorXray.rgb, newOpacity);
    111.         return output;
    112.     }
    113.  
    114.         ENDCG
    115.     }
    116.     }
    117.         FallBack "Diffuse"
    118. }
    119.  
    And if there is no way to use stencil in deferred at all (regardless it says there: https://docs.unity3d.com/Manual/SL-Stencil.html about that 5th bit is unused) What would be the easiest way to throw one bit into GBuffer? I know that RT0.a should not be used... but how I pass that alpha to 0 from surface program, or how I read it in pass I am not aware of... I just need to make some X-ray shader, for not-visible units. Any adwice welcome. Thanks
     
    Last edited: Dec 7, 2019
  2. Kusras

    Kusras

    Joined:
    Jul 9, 2015
    Posts:
    134
    Oh, wait a second I made it!!!! I just had to make unity think that this object is forward, then made second material which remains deferred and it works :O Don't ask... pure magic...