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

shadow on the plane, everything else to be transparent? how to?

Discussion in 'Scripting' started by pretender, Dec 31, 2010.

  1. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    is it possible to have a plane that can receive shadow, but everything else to be transparent
    so only thing that would be visible is the shadow?

    thanks!
     
  2. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    any ideas?
     
  3. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    are you saying the plane is invisible? like glass?
     
  4. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    yes the plane should be transparent, just shadow to be visible (texture with alpha)
     
  5. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    so are you having problems with the the transparent plane, or are you having problems with the shadow?
     
  6. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    i know how to apply texture to the plane, and i know about how to make texture with alpha channel so i know how to fake the shadow.
    what i was trying to figure out is how to get the real shadow from the light, and i know how to do that, the shadow shows on the plane, but
    what i do not know is how to get only shadow on the plane, everything else to be transparent. only the portion of the plane that has shadow to show...

    so i was thinking to have a camera that will render only shadow and then apply that texture to the plane...but how to do it?
    am i thinking in the right direction?
     
  7. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    any ideas?
     
  8. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    probably use layers, set waht you want to show on one layer, and tell the camera not to see any of the other layers
     
  9. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,007
    I wrote a basic shader to accomplish this recently. Its not fully tested for all applications, pretty sure it wont work with Unity 3 deferred rendering, might not work with Unity 3 at all, essentially it works for my purposes in 2.6.

    Copy and paste this code into a new text file and name it Transparent_ShadowSupport.shader then apply the shader to a material and apply the material to your plane.

    Only 'shadow' cast from a directional light will be rendered on the plane, anywhere that is not shadowed will be transparent showing through anything behind it.

    Oh and obviously the plane should be set to receive but not cast shadows.

    Code (csharp):
    1. // Transparent_ShadowSupport.shader  
    2. // Author: Noisecrime
    3. // Date:   14.10.10
    4. // Version 0.3
    5. // Based on the Normal-Diffuse.shader
    6.  
    7.  
    8. // INFO:
    9. // Special FX shader that will only render a shadow onto the polygons, outside of the shadow area will be completely transparent.
    10. // Shadow can be given a specific colour and alpha partly controls intensity, though mainly still controlled via light>shadow property.
    11. // This is useful if you want to have a shadow on a ground plane, but not display the actual plane.
    12. // Currently only works for Directional Lights.
    13.  
    14.  
    15. // USAGE:
    16. // Just apply the shader to a material.
    17.  
    18. // NOTES:
    19. // Not tested with fog and may require tweaking to work with specific projects.
    20.  
    21.  
    22. Shader "ncp/FX/Transparent_ShadowSupport" {
    23. Properties
    24. {
    25.     _Color ("Shadow Color", Color) = (1,1,1,1)
    26. }
    27.  
    28. Category {
    29.     //Tags { "RenderType"="Opaque" }
    30.     //  Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    31.     Tags { "RenderType"="Transparent" }
    32.     LOD 200
    33.     Zwrite Off
    34.     Blend  SrcAlpha OneMinusSrcAlpha
    35.     Fog { Color [_AddFog] }  // or  Fog { Color (0,0,0,0) }
    36.     Lighting Off
    37.    
    38.     // ------------------------------------------------------------------
    39.     // ARB fragment program
    40.    
    41.     SubShader
    42.     {
    43.         // Pixel lights
    44.         Pass
    45.         {
    46.             Name "PPL"
    47.             Tags { "LightMode" = "Pixel" }
    48.                 CGPROGRAM
    49.                 #pragma vertex vert
    50.                 #pragma fragment frag
    51.                 #pragma multi_compile_builtin
    52.                 #pragma fragmentoption ARB_fog_exp2
    53.                 #pragma fragmentoption ARB_precision_hint_fastest
    54.                 #include "UnityCG.cginc"
    55.                 #include "AutoLight.cginc"
    56.  
    57.                 struct v2f {
    58.                     V2F_POS_FOG;
    59.                     LIGHTING_COORDS
    60.                         };
    61.  
    62.                 uniform float4 _Color;
    63.  
    64.                 v2f vert (appdata_base v)
    65.                 {
    66.                     v2f o;
    67.                     PositionFog( v.vertex, o.pos, o.fog );
    68.                     TRANSFER_VERTEX_TO_FRAGMENT(o);
    69.                                            
    70.                     return o;
    71.                 }
    72.  
    73.  
    74.                 float4 frag (v2f i) : COLOR
    75.                 {
    76.                     half4 texcol = _Color*(1.0f-SHADOW_ATTENUATION(i));
    77.                     return texcol;  
    78.                 }
    79.                 ENDCG
    80.  
    81.         }
    82.     }
    83. }
    84.  
    85. Fallback "VertexLit", 2
    86.  
    87. }
    88.  
     
    Last edited: Jan 6, 2011
  10. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    works great! that is what i was searching for! i am using 2.6
     
  11. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,007
    Glad to hear it works for you.
     
  12. Kaya

    Kaya

    Joined:
    Dec 21, 2010
    Posts:
    50
    Unfortunatelly it doesn't work in Unity 3.1 pro.
    I hope you also develop a Unity 3 compatipble version.

    -Kaya
     
  13. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,007
    Yeah I developed this before I installed Unity 3, just after getting to grips with Unity 2.6 shaders. Haven't had time to dig into the new shader stuff yet, though the basic concept should still work.
     
  14. mrjovis

    mrjovis

    Joined:
    Jan 12, 2011
    Posts:
    1
    This is compatible with Unity 3.1, but only supports Forward Rendering.

    Code (csharp):
    1.  
    2. Shader "TransparentShadowSupport" {
    3.  
    4. Properties
    5. {
    6.     _ShadowColor ("Shadow Color", Color) = (0,0,0,1)
    7. }
    8.  
    9. Category {
    10.  
    11.     Blend  SrcAlpha OneMinusSrcAlpha
    12.    
    13.     Lighting Off
    14.     Zwrite Off
    15.     LOD 200
    16.    
    17.    
    18.     SubShader
    19.     {
    20.         Tags { "RenderType"="Transparent" }
    21.        
    22.         CGPROGRAM
    23.         #pragma surface surf Custom
    24.  
    25.         struct Input {
    26.             float2 pos : POSITION;
    27.         };
    28.  
    29.         uniform float4 _ShadowColor;
    30.                
    31.         void surf(Input IN, inout SurfaceOutput o)
    32.         {
    33.             //Pass through shadow colour to lighting model
    34.             o.Albedo = _ShadowColor.rgb;
    35.             o.Alpha  = _ShadowColor.a;
    36.         }
    37.                
    38.         half4 LightingCustom(SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
    39.         {
    40.             half4 c;
    41.  
    42.             //Inverse illumination - atten accounts for shadowing
    43.             c.rgb = s.Albedo.rgb * 1.0f-atten;
    44.             c.a   = s.Alpha * 1.0f-atten;
    45.                        
    46.             return c;
    47.         }
    48.         ENDCG
    49.     }
    50. }
    51.  
    52. Fallback "VertexLit", 2
    53.  
    54. }
    55.  
     
    Last edited: Jan 12, 2011
  15. Linkitch

    Linkitch

    Joined:
    Jan 6, 2011
    Posts:
    11
    The above shader doesn't seem to work in Unity 3.5.
    It appears to work in the editor, but when I switch to game view, it doesn't display the shadow.
     
  16. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,519
    Is your game view using forward rendering?
     
  17. Linkitch

    Linkitch

    Joined:
    Jan 6, 2011
    Posts:
    11
    Yes, changed it in the player settings, and even tried setting the camera directly to forward rendering, same result.

    I'm going to try installing 3.4 to see if that helps.
     
  18. ElmarKleijn

    ElmarKleijn

    Joined:
    May 11, 2009
    Posts:
    87
  19. koek

    koek

    Joined:
    May 6, 2012
    Posts:
    4
    Thats exactly what I am looking for. I need a 100% transparent plane. Only the shadows of the objects that are placed on it shall be visible.
    I just tried to use klakos shader, but I just receive a grey plane. I dont know how to turn it transparent.

    Would you mind posting the shader you used?
    That would be awesome!
     
  20. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,508
    I keep seeing this same site posted everywhere. Let me warn everyone who thinks this is the solution for shadows cast onto transparent objects -- it does not work the way it appears. When using a plane with this shader on it, it appears to work as long as there's another solid plane directly behind the transparent plane. The shadow is really being cast on the background plane and the shadows are showing THROUGH the transparent object. So as long as your transparent object is essentially a decal plane above another solid plane, it may work. But try the case below with a convex object:

    See how you can see the shadows THROUGH the transparent plane? You're actually seeing the shadows on the white background plane. No shadows are drawn on the contours of the mound of sand. You can even see the shadows that fall on the object sticking out of the mound through the sand.


    Another angle shows there's nothing being cast on the mound itself. You're only seeing every shadow in the scene through it.
     
  21. Kaboom

    Kaboom

    Joined:
    Mar 16, 2014
    Posts:
    32
    I came across this thread while searching for a solution to a similar problem: a shadow-receiving plane I could put over a 2D background. I had one but needed support for coloured lights. Looks like I stumbled onto a solution which I've posted here: http://answers.unity3d.com/answers/854639/view.html
     
  22. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,508
    I'm getting an access denied error on your link.
     
  23. Kaboom

    Kaboom

    Joined:
    Mar 16, 2014
    Posts:
    32
    Oh, it said "pending moderation" (or something similar) a while ago, but not anymore. I'll just copy/paste it here.

    The original shader posted in the question was:

    Code (CSharp):
    1. Shader "Custom/Matte Shadow" {
    2.      
    3.      Properties {
    4.          _Color ("Main Color", Color) = (1,1,1,1)
    5.          _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    6.          _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    7.      }
    8.      
    9.      SubShader {
    10.          Tags {"Queue"="AlphaTest" "RenderType"="TransparentCutout"}
    11.          LOD 200
    12.          Blend Zero SrcColor
    13.          Offset 0, -1
    14.      
    15.      CGPROGRAM
    16.      
    17.      #pragma surface surf ShadowOnly alphatest:_Cutoff fullforwardshadows
    18.          
    19.      fixed4 _Color;
    20.      
    21.      struct Input {
    22.          float2 uv_MainTex;
    23.      };
    24.      
    25.      inline fixed4 LightingShadowOnly (SurfaceOutput s, fixed3 lightDir, fixed atten) {
    26.          fixed4 c;
    27.        
    28.          c.rgb = s.Albedo*atten;
    29.          c.a = s.Alpha;
    30.          return c;
    31.      }
    32.      
    33.      void surf (Input IN, inout SurfaceOutput o) {
    34.          fixed4 c = _Color;
    35.          o.Albedo = c.rgb;
    36.          o.Alpha = 1.0f;
    37.      }
    38.      ENDCG
    39.      }
    40.      Fallback "Transparent/Cutout/VertexLit"
    41. }
    I've changed line 34 to

    Code (CSharp):
    1. fixed4 c = _LightColor0 + _LightColor0 * _Color;
    At first I just tried adding the two colors (_LightColor0 + _Color) but then the lights are too bright. Multiplying them makes everything darker. This, to me, looks exactly right.

    If someone can chip in as to why/how this works, I'd be interested!
     
    macros2 and BlueOnE like this.
  24. BlueOnE

    BlueOnE

    Joined:
    Jan 24, 2015
    Posts:
    16
    Sorry, didn't mean to give you false hope for answers to your question, but wanted to let you know you pasting the code here saved my life.
    I've been searching for hours for a (working) shader that does exactly this, so thanks.:)
     
  25. Kaboom

    Kaboom

    Joined:
    Mar 16, 2014
    Posts:
    32
    Ha, well glad to hear it helped you out :)
     
  26. Evgeny1001

    Evgeny1001

    Joined:
    Feb 15, 2015
    Posts:
    3
    Big thnx!! :) Everything works well!

    Small remark - set "Alpha cutoff" to 0 in shader's properties in Editor.
     
  27. Kaboom

    Kaboom

    Joined:
    Mar 16, 2014
    Posts:
    32
    > set "Alpha cutoff" to 0 in shader's properties in Editor.

    Oh, what does that do/change?
     
  28. Evgeny1001

    Evgeny1001

    Joined:
    Feb 15, 2015
    Posts:
    3
    Without it shadows won't be
     
  29. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    Yes, this works BUT only when the Camera is set to depth. Which is a huge problem if you need it for your base camera. It is a problem because your Game can't be played in multiple resolutions because the Depth layer won't scale with it but stays in the same place.
     
  30. Purpleshine84

    Purpleshine84

    Joined:
    Apr 8, 2013
    Posts:
    194
    I hope there is a shader that would only cast shadows and never see the plane, but maybe this is not even possible.