109 lines
4.1 KiB
Plaintext
Raw Normal View History

Shader "Unlit/SoftShadowMask"
{
SubShader
{
Pass
{
ZTest Always
ZWrite Off
Cull Off
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma enable_d3d11_debug_symbols
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
#pragma multi_compile _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE
#pragma multi_compile_fragment _ _Use16Samples
struct Varyings
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
Varyings vert(uint vertexID: SV_VertexID)
{
Varyings o;
o.vertex = GetFullScreenTriangleVertexPosition(vertexID);
o.uv = GetFullScreenTriangleTexCoord(vertexID);
return o;
}
#ifdef _Use16Samples
float4 _Offset16[8];
#else
float4 _Offset5[2];
#endif
half GetDepth(float2 uv)
{
#if UNITY_REVERSED_Z
float deviceDepth = SAMPLE_TEXTURE2D_X(_CameraDepthTexture, sampler_PointClamp, uv).r;
#else
float deviceDepth = SAMPLE_TEXTURE2D_X(_CameraDepthTexture, sampler_PointClamp, uv).r;
deviceDepth = deviceDepth * 2.0 - 1.0;
#endif
return deviceDepth;
}
half frag(Varyings input) : SV_Target
{
float2 uv = input.uv;
//Fetch shadow coordinates for cascade.
float3 wpos = ComputeWorldSpacePosition(uv, GetDepth(uv), unity_MatrixInvVP);
float4 coords = TransformWorldToShadowCoord(wpos);
if(BEYOND_SHADOW_FAR(coords))
{
return 1;
}
half shadow = SAMPLE_TEXTURE2D_SHADOW(_MainLightShadowmapTexture, sampler_LinearClampCompare,
coords.xyz);
#ifdef _Use16Samples
for (int i = 0; i < 8; ++i)
{
uv = input.uv+ _Offset16[i].xy;
wpos = ComputeWorldSpacePosition(uv, GetDepth(uv), unity_MatrixInvVP);
coords = TransformWorldToShadowCoord(wpos);
shadow += BEYOND_SHADOW_FAR(coords) ? 1 : SAMPLE_TEXTURE2D_SHADOW(_MainLightShadowmapTexture, sampler_LinearClampCompare,coords.xyz);
uv = input.uv+ _Offset16[i].zw;
wpos = ComputeWorldSpacePosition(uv, GetDepth(uv), unity_MatrixInvVP);
coords = TransformWorldToShadowCoord(wpos);
shadow += BEYOND_SHADOW_FAR(coords) ? 1 : SAMPLE_TEXTURE2D_SHADOW(_MainLightShadowmapTexture, sampler_LinearClampCompare, coords.xyz );
}
#else
for (int i = 0; i < 2; ++i)
{
uv = input.uv + _Offset5[i].xy;
wpos = ComputeWorldSpacePosition(uv, GetDepth(uv), unity_MatrixInvVP);
coords = TransformWorldToShadowCoord(wpos);
shadow += BEYOND_SHADOW_FAR(coords) ? 1 : SAMPLE_TEXTURE2D_SHADOW(_MainLightShadowmapTexture, sampler_LinearClampCompare,
coords.xyz);
uv = input.uv + _Offset5[i].zw;
wpos = ComputeWorldSpacePosition(uv, GetDepth(uv), unity_MatrixInvVP);
coords = TransformWorldToShadowCoord(wpos);
shadow += BEYOND_SHADOW_FAR(coords) ? 1 : SAMPLE_TEXTURE2D_SHADOW(_MainLightShadowmapTexture, sampler_LinearClampCompare,
coords.xyz);
}
shadow *= 0.2f;
#endif
return shadow;
}
ENDHLSL
}
}
}