• 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
2
Question by finnusmagic · Oct 09, 2019 at 01:04 PM · shader programming

Make objects transparent without affecting the shadow?

Hey,

I'm currently working on a 3D game and just implemented a logic that makes any object transparent that is between the player and the main camera. Everything works fine, but when an object gets "faded out" or "faded in", the shadow changes as well. So I was wondering if there's an option to keep the shadow strength as it is while fading between the default and the transparent material. For the transparent material I am using a transparent shader with z-buffer that I found online. Unfortunately I have not enough experience in shader programming to solve that problem myself. I would appreciate any help!

Here's the code for the transparency shader I'm using right now:

 Shader "Transparent/VertexLit with Z" 
 {
 Properties 
 {
     _Color ("Main Color", Color) = (1,1,1,1)
     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
 }
  
 SubShader 
 {
     Tags 
     {
     "RenderType"="Transparent" 
     "Queue"="Transparent"
     }
 
     Pass 
     {
         ColorMask 0
     }
 
     Pass 
     {
         ZWrite Off
         Blend SrcAlpha OneMinusSrcAlpha
         ColorMask RGB
         Material 
         {
             Diffuse [_Color]
             Ambient [_Color]
         }
         Lighting On
         SetTexture [_MainTex] 
         {
             Combine texture * primary DOUBLE, texture * primary
         } 
     }
 }
 }
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
2
Best Answer

Answer by Glurth · Oct 09, 2019 at 04:08 PM

Edit: Below is shader to do what you want.. but I don't think you actually need it!!! In the MeshRenderer component, under lighting, there is a setting under "cast shadow" called "Shadows only", which does exactly what you're looking for.. and I think it will work with any material. The shader version does have one advantage though: rather than being just on or off, you can fade out the material's color's alpha slowly, without affecting the shadow.


Shader:

Here is a simple vertex shader that does that. Note that the second pass, is where the shadow is drawn.

  • The material/shader has a parameter called hide. Set this value to Zero to display the object & show. Set it to not-zero to hide the object but not the shadow: (this flag simply replaces the _Color alpha with 0)

  • Or, you can simply adjust the material's color's alpha manually.


(I probably have more #includes and #pragama than necessary, but didn't want to experiment to see which ones could be removed.)

 Shader "Unlit/InvisWithShadow"
 {
     Properties
     {
         _Color("Main Color", Color) = (1,1,1,1)
         _MainTex("Texture", 2D) = "white" {}
         _Hide("Hide", Float) = 0.1
     }
 
     SubShader
     {
         Tags
         {
             "RenderType" = "Transparent"
             "Queue" = "Transparent"
         }
         Blend SrcAlpha OneMinusSrcAlpha
         Pass
         {
             Name "MAPHEIGHT"
             Tags{ "LightMode" = "ForwardBase" }
             ZWrite On
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile_fog    
             #include "UnityCG.cginc"
             #include "Lighting.cginc"
             #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
             #include "AutoLight.cginc"            
 
             float4 _Color;
             float _Hide;
             //sampler2D _MainTex;
 
             struct v2f {
                 half2 uv : TEXCOORD3;
                 float4 pos : SV_POSITION;
             };
 
 
             v2f vert(float4 vertex : POSITION, float3 normal : NORMAL,float2 uv : TEXCOORD0)
             {
                 v2f o;
                 o.pos = UnityObjectToClipPos(vertex);
                 o.uv = uv;
                 TRANSFER_VERTEX_TO_FRAGMENT(o);
                 TRANSFER_SHADOW(o);
                 return o;
             }
 
             sampler2D _MainTex;
             fixed4 frag(v2f i) : SV_Target
             {
                 fixed4 c = tex2D(_MainTex, i.uv)* _Color;
                 if(_Hide)
                     c.a = 0;
                 return c;
             }
 
             ENDCG
         }
             // shadow casting support
             // shadow caster rendering pass, implemented manually
         Pass
         {
             Name "SHADOWCASTER"
             Tags{ "LightMode" = "ShadowCaster" }
 
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #pragma multi_compile_shadowcaster
             #include "UnityCG.cginc"
 
             struct v2f {
                 V2F_SHADOW_CASTER;
             };
 
             v2f vert(appdata_base v)
             {
                 v2f o;
                 TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
                     return o;
             }
 
             float4 frag(v2f i) : SV_Target
             {
                 SHADOW_CASTER_FRAGMENT(i)
             }
             ENDCG
         }
     }//end subshader
 }//end shader




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 finnusmagic · Oct 10, 2019 at 08:45 AM 0
Share

Wow! That's one helpful answer right there. Thank you so much! Especially for giving me some insights on what options I have. I went for the one with the shader so I can, like you already mentioned, work with the fade.

Thanks and have a nice day!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

116 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

Related Questions

Grid Projector only for up-facing normals 1 Answer

Silhouette overlay shader 0 Answers

Electric Wireframe Line Shader 1 Answer

Matte Shadow Shader in Unity 5 1 Answer

Issue with scaling shader and non-zero local position 0 Answers


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