• 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
Question by t3pes · Oct 04, 2016 at 09:03 PM · shaderterrainshadowstree

Tree Shadows Disappearing

So I needed to have transparent terrain trees (for when the camera moves through them) and still have cutout shadows generated correctly. Unfortunatly I didn't get it to work using build-in shaders and pragmas since the build-in shaders would always run the shadow calculation after everything else I'm interested in. As a work around I took a look at the fallbacks defined in the standard shaders and copied the last pass defined in "Legacy Shaders/Transparent/Cutout/VertexLit" which actually calculates the shadows in the build-in shaders, and run it as my first. After that I run a simplified version of the surface function defined in "Nature/Tree Creator Leaves" in which I apply an additional alpha modification based on the z coord of the pixel. So far the shader works good however if a tree is close outside and behind the camera frustrum it won't cast a shadow. I quick search yielded some simular question but no awnser.

Is there some magic in regards to how terrain handles shadows? I'm kind of in the dark here, since this is exactly what should be happening using the build-in shader since the shadow function is the same and the shaders just pass it down to "Legacy Shaders/Transparent/Cutout/VertexLit".

*I'm well aware the shader as is not completely compatible with the Level of Detail system. But I set the values to max so this shader will be the only one used. I tried to stay as close as possible to the build in shaders. The only thing I did is inverting the passes.

Did I miss something?

Is there an easier way to do that?

 Shader "Custom/CGDepthLeaves" {
         Properties {
             _Color ("Main Color", Color) = (1,1,1,1)
             _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
             _BumpMap ("Normalmap", 2D) = "bump" {}
             _CutOff ("Cutoff value", Range(0,1)) = 0.3
             _NearDistance ("Near plane value", Range(1, 50))  = 4
         }
         SubShader {
             Tags { "Queue"="Transparent"}
             LOD 200
     
             //////////////////////////
             //Shadowcaster...       //
             //////////////////////////
     
             Pass
             {
                 Name "Caster"
                 Tags {"LightMode" = "ShadowCaster" }
     
                 CGPROGRAM
                 #pragma vertex vert
                 #pragma fragment frag
                 #pragma multi_compile_shadowcaster
                 #include "UnityCG.cginc"
     
                 struct v2f
                 {
                     V2F_SHADOW_CASTER;
                     float2 uv: TEEXCOORD1;
                 };
     
                 uniform float4 _MainTex_ST;
     
                 v2f vert(appdata_base v)
                 {
                     v2f o;
                     TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
                     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                     return o;
                 }
     
                 uniform sampler2D     _MainTex;
                 uniform fixed         _CutOff;
                 uniform fixed4         _Color;
     
                 float4 frag( v2f i ) : SV_Target
                 {
                     fixed4 texcol = tex2D(_MainTex, i.uv);
                     clip(texcol.a- _CutOff );
     
     
     
                     SHADOW_CASTER_FRAGMENT(i)
                 }
                 ENDCG
             }
             ////////////////////////////
             //Surface Lighting/Trans  //
             ////////////////////////////
     
             CGPROGRAM
             #pragma surface surf Lambert nolightmap noforwardadd alpha:fade
     
     
             fixed4 LightingUnlit(SurfaceOutput s, fixed3 lightDir, fixed atten)
             {
                 fixed4 c;
                 c.rgb = s.Albedo;
                 c.a = s.Alpha;
                 return c;
             }
     
             sampler2D _MainTex;
             sampler2D _BumpMap;
             fixed4 _Color;
             float  _NearDistance;
     
             struct Input 
             {
                 float2 uv_MainTex;
                 float4 screenPos;
             };
     
             void surf (Input IN, inout SurfaceOutput o) 
             {
                 fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
                 o.Albedo =  c.rgb;
                 o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
                 clip(c.a - 0.3);
                 o.Alpha = c.a*max(0, min(1, IN.screenPos.z - _NearDistance));
             }
             ENDCG
     
     
         }
     
     Fallback "Diffuse"
     }

Comment

People who like this

0 Show 0
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

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Altering the Terrain Tree Billboard Shader 1 Answer

Black Trees 2 Answers

Tree billboard with tree creator shaders 0 Answers

Trees - Lightning bug 1 Answer

Billboard trees glow white in dark scene. 2 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