Search Unity

Dithering Transparency of opaque object, dithering received shadow and rendering shadows "behind" it

Discussion in 'Shaders' started by Smitzo, May 25, 2020.

  1. Smitzo

    Smitzo

    Joined:
    Mar 23, 2019
    Posts:
    11
    I have a dithering transparency shader that will make a screen-door effect on an opaque object.
    The reason is that I want to create a fading cealing when the player enters a building.
    However, the shader will not allow shadows behind the object to be rendered (picture attached). Besides that, I'd like to dither the "received shadows" on the object aswell.
    What solution do you think would be most appropriate for this?

    I'm using Unity 2018.4.14f1 standard 3D.



    The shader:
    Code (CSharp):
    1. Shader "TEST/Dithering (Simple)"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _Transparency ("Transparency", Range(0,1)) = 1.0
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.         Tags
    12.         {
    13.             "RenderType"="Opaque"
    14.         }
    15.  
    16.         CGPROGRAM
    17.         #pragma surface surf Lambert noforwardadd
    18.  
    19.         sampler2D _MainTex;
    20.  
    21.         struct Input
    22.         {
    23.             float2 uv_MainTex;
    24.             float4 screenPos;
    25.         };
    26.  
    27.         half _Transparency;
    28.  
    29.         void surf (Input IN, inout SurfaceOutput o)
    30.         {
    31.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    32.             o.Albedo = c.rgb;
    33.             o.Alpha = c.a;
    34.             // Screen-door transparency: Discard pixel if below threshold.
    35.             float4x4 thresholdMatrix =
    36.             {  1.0 / 17.0,  9.0 / 17.0,  3.0 / 17.0, 11.0 / 17.0,
    37.             13.0 / 17.0,  5.0 / 17.0, 15.0 / 17.0,  7.0 / 17.0,
    38.             4.0 / 17.0, 12.0 / 17.0,  2.0 / 17.0, 10.0 / 17.0,
    39.             16.0 / 17.0,  8.0 / 17.0, 14.0 / 17.0,  6.0 / 17.0
    40.             };
    41.             float2 pos = IN.screenPos.xy / IN.screenPos.w;
    42.             pos *= _ScreenParams.xy; // pixel position
    43.             clip(_Transparency - thresholdMatrix[pos.x % 4] [pos.y % 4]);
    44.         }
    45.         ENDCG
    46.     }
    47.     Fallback "VertexLit"
    48. }
    This is the effect I'm looking for without Shader Graph:
    - Dithering Transparency
    - Dithering received shadow
    - Rendering shadows behind the object