Hello,
I’ve been following this example from the Unity documentation on custom shaders:
And I can’t seem to get shadow casting to work for URP. I’ve applied the following shader to both the plane and the cube shown in the photo. It seems receiving shadows works, there is just a problem with casting shadows.
Any ideas on what could be missing here?
Thank you!
Shader "Custom/Radial_Bend"
{
Properties
{
_MainTex("Sprite Texture", 2D) = "white" {}
}
SubShader
{
Pass
{
Tags
{
"LightMode" = "UniversalForward"
"RenderType" = "Opaque"
"RenderPipeline" = "UniversalPipeline"
"UniversalMaterialType" = "Lit"
"IgnoreProjector" = "True"
}
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile __ BEND_ON
#pragma multi_compile __ BEND_OFF
#include "UnityCG.cginc"
#include "Lighting.cginc"
#pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
#include "AutoLight.cginc"
float4 _MainTex_ST;
sampler2D _MainTex;
struct v2f
{
float2 uv : TEXCOORD0;
SHADOW_COORDS(1)
fixed3 diff : COLOR0;
fixed3 ambient : COLOR1;
float4 pos : SV_POSITION;
};
uniform float _HORIZON = 0.0f;
uniform float _ATTENUATE = 0.0f;
uniform float _SPREAD = 0.0f;
float4 BendEffect(float4 v)
{
// Put the given vertex in world space
float4 t = mul(unity_ObjectToWorld, v);
// Calculate z-depth
float dist = max(0, abs(_HORIZON - t.z) - _SPREAD);
// Apply parabola formula (-ve to bend down)
t.y -= dist * dist * _ATTENUATE;
// Put vertex back in object space
t.xyz = mul(unity_WorldToObject, t).xyz * 1.0;
return t;
}
v2f vert(appdata_base v)
{
v2f o;
o.pos = UnityObjectToClipPos(BendEffect(v.vertex));
o.uv = v.texcoord;
// For lighting
half3 worldNormal = UnityObjectToWorldNormal(v.normal);
half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
o.diff = nl * _LightColor0.rgb;
o.ambient = ShadeSH9(half4(worldNormal, 1));
// compute shadows data
TRANSFER_SHADOW(o)
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
// compute shadow attenuation (1.0 = fully lit, 0.0 = fully shadowed)
fixed shadow = SHADOW_ATTENUATION(i);
// darken light's illumination with shadow, keep ambient intact
fixed3 lighting = i.diff * shadow + i.ambient;
col.rgb *= lighting;
return col;
}
ENDHLSL
}
// shadow casting support
// UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
UsePass "Universal Render Pipeline/Lit/ShadowCaster"
}
}