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

transparent decal recieve shadows

Discussion in 'Shaders' started by Airship, May 28, 2013.

  1. Airship

    Airship

    Joined:
    Sep 10, 2011
    Posts:
    260
    Is there a way to have a transparent diffuse object receive real time directional shadows? I have searched through the multiple forum threads, all saying it's really not practical/ possible to do. However what I am looking for is a decal shader that will be placed on a wall so it really just needs to allow the shadows underneath to read through. Can this be done more easily perhaps?
     
  2. Airship

    Airship

    Joined:
    Sep 10, 2011
    Posts:
    260
    looking at the shader on the wiki http://wiki.unity3d.com/index.php?title=TransparentShadowReceiver

    I have found that by changing the Queue to Geometry+1 and adding "LightMode" = "ForwardBase" to the tags and fullforwardshadows to the #pragma a transparent decal gets shadows, like I need. However "LightMode" = "ForwardBase" is wrecks the lighting and everything becomes extremely bright and washed out . Any ideas, I just need the shadows to show through my transparent decal.


    Code (csharp):
    1. Shader "AlphaShadow"
    2. {
    3. Properties
    4. {
    5.     _Color ("Main Color", Color) = (1,1,1,1)
    6.     _MainTex ("tex1", 2D) = "white" {}
    7. }
    8.  
    9. SubShader
    10. {
    11.     Tags {
    12.     "Queue"="Geometry+1"
    13.     "IgnoreProjector"="True"
    14.     "RenderType"="Transparent"
    15.     "LightMode" = "ForwardBase"
    16.     }
    17.  
    18.     LOD 300
    19.  
    20. CGPROGRAM
    21. #pragma target 3.0
    22. #pragma surface surf Lambert alpha fullforwardshadows
    23.         sampler2D _MainTex;
    24.         float4 _Color;
    25.        
    26.         struct Input {
    27.             float2 uv_MainTex;
    28.             fixed4 color: Color;
    29.         };
    30.  
    31.         void surf (Input IN, inout SurfaceOutput o) {
    32.             fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
    33.             o.Albedo = tex.rgb ;
    34.             o.Alpha = 1;
    35.        
    36.         }
    37. ENDCG
    38. }
    39.  
    40. FallBack "Transparent/Cutout/VertexLit"
    41. }
    42.  
     
    Last edited: May 29, 2013
  3. Sirmaiji

    Sirmaiji

    Joined:
    Feb 15, 2013
    Posts:
    1
    Try this one (works with Forward rendering type and directional light only):
    Code (csharp):
    1.     Shader "Transptest"
    2.     {
    3.             Properties
    4.             {
    5.     _MainTex("MainTex", 2D) = "black" {}
    6.     _Cutoff("Cutoff", Range (0, 1))= 0.5
    7.     _MainColor("_MainColor", Color) = (1,1,1,1)
    8.      
    9.             }
    10.            
    11.             SubShader
    12.             {
    13.             Pass {
    14.                     Name "Caster"
    15.                     Tags { "LightMode" = "ShadowCaster"  }
    16.                     Offset 1, 1
    17.                    
    18.                     Fog {Mode Off}
    19.                     ZWrite On
    20.                     ZTest LEqual
    21.                     Cull Off
    22.                     Alphatest Greater [_Cutoff]
    23.      
    24.     CGPROGRAM
    25.     #pragma vertex vert
    26.     #pragma fragment frag addshadow
    27.     #pragma multi_compile_shadowcaster
    28.     #pragma fragmentoption ARB_precision_hint_fastest
    29.     #include "UnityCG.cginc"
    30.      
    31.     struct v2f {
    32.             V2F_SHADOW_CASTER;
    33.             float2  uv : TEXCOORD1;
    34.     };
    35.      
    36.     uniform float4 _MainTex_ST;
    37.      
    38.     v2f vert( appdata_base v )
    39.     {
    40.             v2f o;
    41.             TRANSFER_SHADOW_CASTER(o)
    42.             o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    43.             return o;
    44.     }
    45.      
    46.     uniform sampler2D _MainTex;
    47.     uniform fixed _Cutoff;
    48.     uniform fixed4 _MainColor;
    49.      
    50.     float4 frag( v2f i ) : COLOR
    51.     {
    52.             fixed4 texcol = tex2D( _MainTex, i.uv );
    53.             clip( texcol.a*_MainColor.a - _Cutoff );
    54.            
    55.             SHADOW_CASTER_FRAGMENT(i)
    56.     }
    57.     ENDCG
    58.      
    59.             }
    60.            
    61.            
    62.                     Tags
    63.                     {
    64.     "Queue"="Geometry+500"
    65.     "IgnoreProjector"="False"
    66.     "RenderType"="Transparent"
    67.      
    68.                     }
    69.      
    70.                    
    71.     Cull Off
    72.     ZWrite On
    73.     ZTest LEqual
    74.     ColorMask RGBA
    75.     Alphatest Greater [_Cutoff]
    76.     Fog{
    77.     }
    78.      
    79.      
    80.                     CGPROGRAM
    81.     #pragma surface surf BlinnPhongEditor  vertex:vert
    82.     #pragma target 3.0
    83.      
    84.      
    85.     sampler2D _MainTex;
    86.      
    87.                             struct EditorSurfaceOutput {
    88.                                     half3 Albedo;
    89.                                     half3 Normal;
    90.                                     half3 Emission;
    91.                                     half3 Gloss;
    92.                                     half Specular;
    93.                                     half Alpha;
    94.                                     half4 Custom;
    95.                             };
    96.                            
    97.                             inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
    98.                             {
    99.     half3 spec = light.a * s.Gloss;
    100.     half4 c;
    101.     c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
    102.     c.a = s.Alpha;
    103.     return c;
    104.      
    105.                             }
    106.      
    107.                             inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
    108.                             {
    109.                                     half3 h = normalize (lightDir + viewDir);
    110.                                    
    111.                                     half diff = max (0, dot ( lightDir, s.Normal ));
    112.                                    
    113.                                     float nh = max (0, dot (s.Normal, h));
    114.                                     float spec = pow (nh, s.Specular*128.0);
    115.                                    
    116.                                     half4 res;
    117.                                     res.rgb = _LightColor0.rgb * diff;
    118.                                     res.w = spec * Luminance (_LightColor0.rgb);
    119.                                     res *= atten * 2.0;
    120.      
    121.                                     return LightingBlinnPhongEditor_PrePass( s, res );
    122.                             }
    123.                            
    124.                             struct Input {
    125.                                     float2 uv_MainTex;
    126.      
    127.                             };
    128.      
    129.                             void vert (inout appdata_full v, out Input o) {
    130.     float4 VertexOutputMaster0_0_NoInput = float4(0,0,0,0);
    131.     float4 VertexOutputMaster0_1_NoInput = float4(0,0,0,0);
    132.     float4 VertexOutputMaster0_2_NoInput = float4(0,0,0,0);
    133.     float4 VertexOutputMaster0_3_NoInput = float4(0,0,0,0);
    134.      
    135.      
    136.                             }
    137.                            
    138.      
    139.                             void surf (Input IN, inout EditorSurfaceOutput o) {
    140.                                     o.Normal = float3(0.0,0.0,1.0);
    141.                                     o.Alpha = 1.0;
    142.                                     o.Albedo = 0.0;
    143.                                     o.Emission = 0.0;
    144.                                     o.Gloss = 0.0;
    145.                                     o.Specular = 0.0;
    146.                                     o.Custom = 0.0;
    147.                                    
    148.     float4 Tex2D0=tex2D(_MainTex,(IN.uv_MainTex.xyxy).xy);
    149.     float4 Master0_1_NoInput = float4(0,0,1,1);
    150.     float4 Master0_2_NoInput = float4(0,0,0,0);
    151.     float4 Master0_3_NoInput = float4(0,0,0,0);
    152.     float4 Master0_4_NoInput = float4(0,0,0,0);
    153.     float4 Master0_7_NoInput = float4(0,0,0,0);
    154.     float4 Master0_6_NoInput = float4(1,1,1,1);
    155.     o.Albedo = Tex2D0;
    156.     o.Alpha = Tex2D0.aaaa;
    157.      
    158.                                     o.Normal = normalize(o.Normal);
    159.                             }
    160.                     ENDCG
    161.      
    162.             }
    163.            
    164.      
    165.             Fallback "Diffuse"
    166.     }
    167.  
    Btw, it works actually in case some other objects are background to the object by which material this shader is used.
     
    Last edited: Aug 28, 2013