Search Unity

Matte Shadow

Discussion in 'Shaders' started by ern, Oct 29, 2008.

  1. animMaDeR

    animMaDeR

    Joined:
    Nov 19, 2017
    Posts:
    1
    Thank you so much joaobsneto! Your are awesome. Your shader\material worked perfectly in Unity 5 and with Tango on Android. Super easy, and exactly what we are all looking for. They should add this to the Unity SDK!
     
  2. joaobsneto

    joaobsneto

    Joined:
    Dec 10, 2009
    Posts:
    152
    It's not mine, I just found it on the internet. But thanks for the awesome.

    :):):)
     
  3. pwalters

    pwalters

    Joined:
    Oct 1, 2016
    Posts:
    3
  4. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Wont work on Unity 2018

    Shader error in 'Custom/ShadowDrawer': syntax error: unexpected token ';' at line 33 (on d3d11)
    Compiling Vertex program with UNITY_PASS_FORWARDADD POINT
    Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_LIGHTMAP_RGBM_ENCODING
     
  5. S-Miyahon

    S-Miyahon

    Joined:
    Jun 30, 2016
    Posts:
    12
    I'm getting the same error on Unity 2018. There seems to be something wrong with the color lerp.
    Code (CSharp):
    1. return half4(_Color.rgb, lerp(_Color.a, 0, atten));
    Could anyone with a bit of shader code knowdledge have a look at this?
    Full shader is here:
    https://raw.githubusercontent.com/keijiro/ShadowDrawer/master/Assets/ShadowDrawer.shader
     
  6. grobonom

    grobonom

    Joined:
    Jun 23, 2018
    Posts:
    335
    the https://github.com/keijiro/ShadowDrawer shader works like a charm in 2018 !!!!

    Tho it needs a drawn invisible mesh, it's just perfect !!!

    i wish unity includes this shader ( mebe optimized for the engine ) in standard shaders !!!!!
    It would be damn convenient for solving unity engine limitations....
    errr....
    For beein sure.... here is the code that works with unity 2018 ( just in case it disappears )
    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. Shader "Custom/ShadowDrawer"
    4. {
    5.     Properties
    6.     {
    7.         _Color ("Shadow Color", Color) = (0, 0, 0, 0.6)
    8.     }
    9.  
    10.     CGINCLUDE
    11.  
    12.     #include "UnityCG.cginc"
    13.     #include "AutoLight.cginc"
    14.  
    15.     struct v2f_shadow {
    16.         float4 pos : SV_POSITION;
    17.         LIGHTING_COORDS(0, 1)
    18.     };
    19.  
    20.     half4 _Color;
    21.  
    22.     v2f_shadow vert_shadow(appdata_full v)
    23.     {
    24.         v2f_shadow o;
    25.         o.pos = UnityObjectToClipPos(v.vertex);
    26.         TRANSFER_VERTEX_TO_FRAGMENT(o);
    27.         return o;
    28.     }
    29.  
    30.     half4 frag_shadow(v2f_shadow IN) : SV_Target
    31.     {
    32.         half atten = LIGHT_ATTENUATION(IN);
    33.         return half4(_Color.rgb, lerp(_Color.a, 0, atten));
    34.     }
    35.  
    36.     ENDCG
    37.  
    38.     SubShader
    39.     {
    40.         Tags { "Queue"="AlphaTest+49" }
    41.  
    42.         // Depth fill pass
    43.         Pass
    44.         {
    45.             ColorMask 0
    46.  
    47.             CGPROGRAM
    48.  
    49.             #pragma vertex vert
    50.             #pragma fragment frag
    51.  
    52.             struct v2f {
    53.                 float4 pos : SV_POSITION;
    54.             };
    55.  
    56.             v2f vert(appdata_full v)
    57.             {
    58.                 v2f o;
    59.                 o.pos = UnityObjectToClipPos (v.vertex);
    60.                 return o;
    61.             }
    62.  
    63.             half4 frag(v2f IN) : SV_Target
    64.             {
    65.                 return (half4)0;
    66.             }
    67.  
    68.             ENDCG
    69.         }
    70.  
    71.         // Forward base pass
    72.         Pass
    73.         {
    74.             Tags { "LightMode" = "ForwardBase" }
    75.             Blend SrcAlpha OneMinusSrcAlpha
    76.             CGPROGRAM
    77.             #pragma vertex vert_shadow
    78.             #pragma fragment frag_shadow
    79.             #pragma multi_compile_fwdbase
    80.             ENDCG
    81.         }
    82.  
    83.         // Forward add pass
    84.         Pass
    85.         {
    86.             Tags { "LightMode" = "ForwardAdd" }
    87.             Blend SrcAlpha OneMinusSrcAlpha
    88.             CGPROGRAM
    89.             #pragma vertex vert_shadow
    90.             #pragma fragment frag_shadow
    91.             #pragma multi_compile_fwdadd_fullshadows
    92.             ENDCG
    93.         }
    94.     }
    95.     FallBack "Mobile/VertexLit"
    96. }
     
  7. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    requires forward, a bit limiting, but if that meets your needs its a blast. There is a dude that made a camera casting shadows, cna´t find the link now
     
    grobonom likes this.
  8. amoraleite

    amoraleite

    Joined:
    Oct 16, 2014
    Posts:
    41
    Shadow works for me!
    Thanks a lot!
     
  9. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Here the dr Frankenstein monster, it's works with point and spot lights sources, but I have no clue how, and I pretty sure "wpos" can be gotten from built in vars, but I also no clue XD

    Code (CSharp):
    1. Shader "Custom/ShadowDrawer"
    2. {
    3.     Properties
    4.     {
    5.         _Color ("Shadow Color", Color) = (0, 0, 0, 0.6)
    6.         _ShadowBoost("sShadow Boost", float) = 1.0
    7.     }
    8.  
    9.     CGINCLUDE
    10.  
    11.     #include "UnityCG.cginc"
    12.     #include "AutoLight.cginc"
    13.  
    14.     struct v2f_shadow {
    15.         float4 pos : SV_POSITION;
    16.         LIGHTING_COORDS(0, 1)
    17.     };
    18.  
    19.     half4 _Color;
    20.  
    21.     v2f_shadow vert_shadow(appdata_full v)
    22.     {
    23.         v2f_shadow o;
    24.         o.pos = UnityObjectToClipPos(v.vertex);
    25.         TRANSFER_VERTEX_TO_FRAGMENT(o);
    26.         return o;
    27.     }
    28.  
    29.     half4 frag_shadow(v2f_shadow IN) : SV_Target
    30.     {
    31.         half atten = LIGHT_ATTENUATION(IN);
    32.         return half4(_Color.rgb, lerp(_Color.a, 0, atten));
    33.     }
    34.  
    35.     ENDCG
    36.  
    37.     SubShader
    38.     {
    39.         Tags { "Queue"="AlphaTest+49" }
    40.  
    41.         // Depth fill pass
    42.         Pass
    43.         {
    44.             ColorMask 0
    45.  
    46.             CGPROGRAM
    47.  
    48.             #pragma vertex vert
    49.             #pragma fragment frag
    50.  
    51.             struct v2f {
    52.                 float4 pos : SV_POSITION;
    53.             };
    54.  
    55.             v2f vert(appdata_full v)
    56.             {
    57.                 v2f o;
    58.                 o.pos = UnityObjectToClipPos (v.vertex);
    59.                 return o;
    60.             }
    61.  
    62.             half4 frag(v2f IN) : SV_Target
    63.             {
    64.                 return (half4)0;
    65.             }
    66.  
    67.             ENDCG
    68.         }
    69.  
    70.         // Forward base pass
    71.         Pass
    72.         {
    73.             Tags { "LightMode" = "ForwardBase" }
    74.             Blend SrcAlpha OneMinusSrcAlpha
    75.             CGPROGRAM
    76.             #pragma vertex vert_shadow
    77.             #pragma fragment frag_shadow
    78.             #pragma multi_compile_fwdbase
    79.             ENDCG
    80.         }
    81.  
    82.        
    83.         Pass
    84.         {
    85.        
    86.             Tags { "LightMode" = "ForwardAdd" }
    87.             Blend SrcAlpha OneMinusSrcAlpha
    88.             CGPROGRAM
    89.             #pragma vertex vert
    90.             #pragma fragment frag
    91.             #pragma multi_compile_fwdadd_fullshadows
    92.             #include "Lighting.cginc"
    93.             #include "AutoLight.cginc"
    94.            
    95.             float _ShadowBoost;
    96.            
    97.             struct v2f {
    98.                 float4 pos : SV_POSITION;
    99.                 float4 wpos : TEXCOORD2;
    100.                 LIGHTING_COORDS(0,1)
    101.             };
    102.             v2f vert (appdata_full v) {
    103.                 v2f o;
    104.                 o.pos = UnityObjectToClipPos(v.vertex);
    105.                 o.wpos = mul(unity_ObjectToWorld, v.vertex);
    106.                 TRANSFER_VERTEX_TO_FRAGMENT(o);
    107.                 return o;
    108.             }
    109.            
    110.             float4 frag (v2f i) : COLOR
    111.             {      
    112.                 fixed shadow = UNITY_SHADOW_ATTENUATION(1, i.wpos);
    113.                 float atten = 1;
    114.                 #if defined (POINT)
    115.                 unityShadowCoord3 lightCoord = mul(unity_WorldToLight, unityShadowCoord4(i.wpos.xyz, 1)).xyz;
    116.                 atten = tex2D(_LightTexture0, dot(lightCoord, lightCoord).rr).r.UNITY_ATTEN_CHANNEL;
    117.                 atten*=_ShadowBoost;
    118.                
    119.                 #elif defined (SPOT)
    120.                     float4 lightCoord = mul(unity_WorldToLight, float4(i.wpos.xyz, 1));
    121.                     atten = (lightCoord.z > 0) * tex2D(_LightTexture0, lightCoord.xy / lightCoord.w + 0.5).w * tex2D(_LightTextureB0, dot(lightCoord, lightCoord).rr).UNITY_ATTEN_CHANNEL;
    122.                 #endif
    123.                
    124.                 return float4(_Color.xyz, _Color.a*(1-shadow)*atten);
    125.             }
    126.            
    127.             ENDCG
    128.         }
    129.     }
    130.     FallBack "Mobile/VertexLit"
    131. }
     
    salex1, id0, Fangh and 3 others like this.
  10. JJunior

    JJunior

    Joined:
    May 22, 2019
    Posts:
    53
    Thats great! That is just what I am looking for, especially this last version that works with point light! You guys rock!
     
  11. Fangh

    Fangh

    Joined:
    Apr 19, 2013
    Posts:
    274
    Sadly this doesn't work if there is a videoPlayer playing in the far clip of the camera