addShadow

This commit is contained in:
Mrhongxing committed 2026-09-16 13:32:04 +08:00
1 parent c2a9014239
commit b9129b0d47
8 files changed
+332 -19

No files matched your search

+59 -1
View File
@@ -156,6 +156,64 @@ Shader "BSDF/URP"
}
ENDHLSL
}
Pass{
Name "ShadowCaster"
Tags {
"LightMode" = "ShadowCaster"//阴影投射
}
ZWrite On
ZTest LEqual
ColorMask 0
Cull Off
HLSLPROGRAM //着色器
#pragma multi_compile instancing
#pragma multi_compile DOTS INSTANCING ON
#pragma multi_compile CASTING PUNCTUAL LIGHT SHADOW
#pragma vertex ShadowVS
#pragma fragment ShadowFS
float3 _LightDirection;
float3 _LightPosition;
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
};
//将阴影的世界空间顶点位置转换为适合阴影投射的裁剪空间位置
float4 GetShadowPositionHClip(Attributes input){
float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
float3 normalWS = TransformObjectToWorldDir(input.normalOS);
#if _CASTING_PUNCTUAL_LIGHT_SHADOW
float3 lightDirectionWS = normalize(_LightPosition - positionWS);
#else
float3 lightDirectionWS = _LightDirection;
#endif
float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, lightDirectionWS));
#if UNITY_REVERSED_Z
positionCS.z = min(positionCS.z, UNITY_NEAR_CLIP_VALUE);
#else
positionCS.z = max(positionCS.z, UNITY_NEAR_CLIP_VALUE);
#endif
return positionCS;
}
Varyings ShadowVS(Attributes input){
Varyings output;
output.positionCS = GetShadowPositionHClip(input);
return output;
}
half4 ShadowFS(Varyings input):SV_TARGET{
return 0;
}
ENDHLSL
}
}
}