- Home /
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"
}
Your answer
Follow this Question
Related Questions
Billboard trees glow white in dark scene. 2 Answers
Trees - Lightning bug 1 Answer
Does the built-in Terrain Tree Billboard cast shadows? 0 Answers
Rim Lighting on Terrain? 0 Answers
How do I make the default grass billboard shader opaque? (URP) 0 Answers