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. Dismiss Notice

Cast shadow on transparent material?

Discussion in 'General Graphics' started by TOES2, May 28, 2015.

  1. TOES2

    TOES2

    Joined:
    May 20, 2013
    Posts:
    135
    I have a pre-rendered backdrop, and I want my game character to cast shadow on a horizontal plane, to make the illusion it is casting a shadow on the backdrop. But when I set the material on this plane to any transparent material, so that the pre-rendered backdrop can show through, shadows can no longer be received.

    Any ideas on how to make this work in Unity? The target platform is iPad.
     
  2. McDev02

    McDev02

    Joined:
    Nov 22, 2010
    Posts:
    661
  3. TOES2

    TOES2

    Joined:
    May 20, 2013
    Posts:
    135
    If you don't have an actual answer don't bother to reply with a link to google. Naturally I searched for it already, and all the shaders I found did not work with latest Unity / iPad.

    You should also learn how Internet works. By replying a question with a google search link, people who search for the same thing later will find your lame reply by googling it. Don't block threads of potential meaningful answers like this.
     
  4. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    In all fairness that vast majority of questions asked here can easily be answered with a quick Google search yet most ask before searching - so it becomes habit to assume that they haven't..

    Edit: It also seems that from looking at the results from that search that it is still not a trivial thing to do even in Unity 5 - so, I expect those links are going to be your best bet right now.
     
  5. Xspeed

    Xspeed

    Joined:
    Apr 16, 2015
    Posts:
    47
    It's Looks Like Your New To Unity Or Using Old Version. I Have Got Unity At 2015/04 v5. That's My First Use Of Unity. I Do Not Know About Other Versions. In Version 5

    Follow Steps Below : Based On Version 5.0.0
    1. Select The Material
    2. Change Shader To Standard
    3. Select Rendering Mode To Transparent
    4. Allow Shadow Casting On Lamps, Quality Settings
    Now You Can See A Shadow That Is Lighter Than Other Shadows. That Is Transparent Shadow

    ScreenShot :
    TransparentMaterialShadow.png
    Sample Mat : Transparent_MAT.unitypackage
     

    Attached Files:

  6. Xspeed

    Xspeed

    Joined:
    Apr 16, 2015
    Posts:
    47
    If You Talking About Shadows Over a Transparent Material.
    Me too Don't Have a Correct Answer. If I Found Anywhere,I'll
    Inform You. On This Forum
     
  7. TOES2

    TOES2

    Joined:
    May 20, 2013
    Posts:
    135
    I found a solution. The majority of the shaders claiming to fix this did not work, but the following one which I modified somewhat does the trick with Unity 5:

    Code (CSharp):
    1. Shader "custom/Transparent Shadow" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    6. }
    7.  
    8. SubShader {
    9.     Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    10.     LOD 200
    11.     ZWrite Off
    12.     Blend Zero SrcColor
    13.  
    14. CGPROGRAM
    15. #pragma surface surf ShadowOnly alphatest:_Cutoff
    16. fixed4 _Color;
    17.  
    18. struct Input {
    19.     float2 uv_MainTex;
    20. };
    21.  
    22. inline fixed4 LightingShadowOnly (SurfaceOutput s, fixed3 lightDir, fixed atten)
    23. {
    24.     fixed4 c;
    25.     c.rgb = s.Albedo*atten;
    26.     c.a = s.Alpha;
    27.     return c;
    28. }
    29.  
    30. void surf (Input IN, inout SurfaceOutput o) {
    31.     fixed4 c = _Color;
    32.     o.Albedo = c.rgb;
    33.     o.Alpha = 1;
    34. }
    35.  
    36. ENDCG
    37. }
    38.  
    39. Fallback "Transparent/Cutout/VertexLit"
    40. }
    Use with a opaque texture, and adjust the main colors RGBA until you get the desired effect.
     
    Last edited: May 30, 2015
  8. Xspeed

    Xspeed

    Joined:
    Apr 16, 2015
    Posts:
    47
    Tested, Not Working. i am now Making A Custom Shader. Code Coming Soon.
     
  9. echo4papa

    echo4papa

    Joined:
    Mar 26, 2015
    Posts:
    158
    I made one a while back. In Unity 5 the simplest way is to add an alpha blend but put it in the AlphaTest queue, and include a fallback for the shadow passes.
     
  10. TOES2

    TOES2

    Joined:
    May 20, 2013
    Posts:
    135
    This works for me, did you play around with the colors? Also, I used a white solid texture map.
     
  11. Xspeed

    Xspeed

    Joined:
    Apr 16, 2015
    Posts:
    47
    Can We Mix 2 shaders ?
    Diffuse And Transparent Shaders Mixed?
    Is It Possible?
     
  12. eliorubenmc

    eliorubenmc

    Joined:
    Sep 10, 2020
    Posts:
    2
    Solution:
    1. Set your MeshRenderer Component in Cast Shadows = Off
    2. Duplicate your game object and put as child, then, put a different material (opaque), and Set Cast Shadows = Shadows only. (Delete all components in the duplicated, keep only the Mesh Renderer Component)
    Done!

    I just did with my "Monster Marble game", with the marble and is working in URP.


    Soon I'll post another video in youtube about How I did that. this is my actual progress without shadows in the marble:
     
  13. sesco97

    sesco97

    Joined:
    Sep 26, 2022
    Posts:
    2
    Sorry to to be so late in this post, but I'm facing an analogous problem.
    I put a plane under my 3D model and apply to it a material with this shader attached. In scene view everything is good, but from camera preview I cannot see any shadow.

    Could anyone help me?
     
  14. drewjosh

    drewjosh

    Joined:
    Sep 24, 2019
    Posts:
    23
    This doesn't work for me with Unity 2021.3, I get following errors:

    Bildschirm­foto 2023-02-27 um 22.00.20.png