Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Breinhardte · Sep 12, 2021 at 11:29 AM · shaderlightingshadowstransparencyshader programming

Receiving Shadows On a Tranparent Shader

In Unity 2020.1.3f1

I'm currently working on a small shader which takes the alpha component of a mesh's vertex colours and uses it for transparency, the result is a fade out along the edge of the shape:

alt text

Fairly simple stuff, and works generally okay. The only noticable bug is that it doesn't receive shadows.


Through a bit of reading, I know that it's possible to receive shadows on transparent shaders such as these and have seen working visual examples, but there's a lot of dubious information on the exact reason and resolution to this problem.

The shader is as follows:

 Shader "Custom/StandardVertexAlpha"
 {
     Properties
     {
         _Color("Color", Color) = (1,1,1,1)
         _MainTex("Texture", 2D) = "white" {}
         _Emission("Emission", Color) = (0,0,0,0)
     }
         SubShader
     {
         Tags { "RenderType" = "Transparent"  "Queue" = "AlphaTest"}
 
         Cull Off
         Lighting Off
 
         CGPROGRAM
         #pragma surface surf Lambert alpha:blend
 
         struct Input
         {
             float2 uv_MainTex;
             half4 color : COLOR;
         };
 
         sampler2D _MainTex;
         fixed4 _Color;
         fixed4 _Emission;
 
         void surf(Input IN, inout SurfaceOutput o)
         {
             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
             o.Albedo = c.rgb * IN.color.rgb;
             o.Alpha = IN.color.a;
             o.Emission = _Emission;
         }
 
         float ShadowAttenuation(int index, float3 worldPos) 
         {
             return 1.0;
         }
 
         float CascadedShadowAttenuation(float3 worldPos) 
         {
             return 1.0;
         }
 
         ENDCG
     }
         Fallback "Diffuse"
 }

The two methods "ShadowAttenuation" and "CascadedShadowAttenuation" are taken from an old Unity tutorial, but don't seem to function anymore.

abtpxlg.jpeg (89.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Namey5 · Sep 13, 2021 at 10:38 AM

First, 2 reasons those functions don't work:

  1. Like any method in a script, a function needs to be called in order to do anything - in this case, neither function is called so of course they aren't going to do anything.

  2. Both of those shadowing functions just return '1' no matter what, so even if they were called they wouldn't do anything. My guess is that they were copied from an incomplete tutorial.


That aside, in general there is nothing stopping transparent objects from receiving shadows - the reason they don't in Unity is down to implementation. Unity renders directional light shadows into a screenspace texture via the depth buffer before rendering takes place - this is then composited back onto the scene when needed. Transparent objects don't write to depth, and as such any projected shadows would show those of the objects behind rather than any cast on the transparent surface itself.

However, in your case the transparent surface in question lies directly on the surface of an opaque object - meaning we can abuse this and just pretend that the path is opaque and let it draw shadows.

  Shader "Custom/StandardVertexAlpha"
  {
      Properties
      {
          _Color("Color", Color) = (1,1,1,1)
          _MainTex("Texture", 2D) = "white" {}
          _Emission("Emission", Color) = (0,0,0,0)
      }

      SubShader
      {
          // Use a high render queue to make sure we draw after everything (however anything in the transparent queue won't get shadow info, so 2500 should work fine)
          Tags { "RenderType" = "Transparent"  "Queue" = "AlphaTest+50"}
  
          Cull Off
  
          CGPROGRAM
          // Use the decal blend function to keep the shadow rendering code - any normal blend will strip shadows
          #pragma surface surf Standard fullforwardshadows decal:blend
  
          struct Input
          {
              float2 uv_MainTex;
              half4 color : COLOR;
          };
  
          sampler2D _MainTex;
          fixed4 _Color;
          fixed4 _Emission;
  
          void surf (Input IN, inout SurfaceOutput o)
          {
              fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
              o.Albedo = c.rgb * IN.color.rgb;
              o.Alpha = IN.color.a;
              o.Emission = _Emission;
          }
          ENDCG
      }
      Fallback "Diffuse"
  }

This will have issues when the object lies in front of other transparent objects and the skybox, so make sure the geometry is tight to the terrain. It also probably won't work in Deferred rendering.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Breinhardte · Sep 13, 2021 at 11:45 AM 0
Share

Fantastic answer, thanks most kindly for the explanation and example shader setup!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

220 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to modify shadows with an image effect 0 Answers

HDRP double sided light problem 0 Answers

How can I add volume/depth to shadows? 0 Answers

Any way to get rid of squares (artifacts) in a semi transparent shadow of a fade shader? 0 Answers

How to add shadows to surface shader? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges