102 lines
3.0 KiB
HLSL
102 lines
3.0 KiB
HLSL
#pragma once
|
|
|
|
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
|
|
|
|
#include "Includes/Common/Input.hlsl"
|
|
|
|
// Cutout and transparent objects need an Alpha value; enable this define to get Alpha value
|
|
#if defined(_ALPHATEST_ON) || defined(_SURFACE_TYPE_TRANSPARENT)
|
|
#define ALPHA 1
|
|
#endif
|
|
|
|
// Whether to use dither mask to sparsify transparent areas of ShadowMap; when generating screen space ShadowMap, invert to calculate transparent results
|
|
// UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS is determined by TierSettings.semitransparentShadows
|
|
#if defined(_SURFACE_TYPE_TRANSPARENT) && defined(UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS)
|
|
#define USE_DITHER_MASK 1
|
|
#endif
|
|
|
|
sampler3D _DitherMaskLOD;
|
|
full3 _LightDirection;
|
|
|
|
struct Attributes
|
|
{
|
|
full4 positionOS : POSITION;
|
|
half3 normalOS : NORMAL;
|
|
half4 tangentOS : TANGENT;
|
|
full2 uv : TEXCOORD0;
|
|
UNITY_VERTEX_INPUT_INSTANCE_ID
|
|
};
|
|
|
|
struct Varyings
|
|
{
|
|
full4 positionCS : SV_POSITION;
|
|
full2 uv : TEXCOORD0; // Transparent shadows need to sample main texture data, so UV is required
|
|
};
|
|
|
|
full4 GetShadowPositionHClip(Attributes input)
|
|
{
|
|
full3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
|
|
full3 normalWS = TransformObjectToWorldNormal(input.normalOS);
|
|
|
|
full4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
|
|
|
|
#if UNITY_REVERSED_Z
|
|
{
|
|
positionCS.z = min(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
|
|
}
|
|
#else
|
|
{
|
|
positionCS.z = max(positionCS.z, positionCS.w * UNITY_NEAR_CLIP_VALUE);
|
|
}
|
|
#endif
|
|
|
|
return positionCS;
|
|
}
|
|
|
|
void VertexShadowCaster(Attributes input, out Varyings output)
|
|
{
|
|
UNITY_SETUP_INSTANCE_ID(input);
|
|
output.positionCS = GetShadowPositionHClip(input);
|
|
output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
|
|
}
|
|
|
|
half4 FragmentShadowCaster(Varyings input) : SV_Target
|
|
{
|
|
ApplyLODCrossFade(input.positionCS.xy);
|
|
|
|
#if defined(ALPHA)
|
|
{
|
|
half alpha = tex2D(_BaseMap, input.uv).a * _BaseColor.a;
|
|
|
|
// Cutout is simple; discard pixels outside threshold
|
|
#if defined(_ALPHATEST_ON)
|
|
clip (alpha - _Cutoff);
|
|
#endif
|
|
|
|
// Fade or Transparent
|
|
#if defined(_SURFACE_TYPE_TRANSPARENT)
|
|
{
|
|
#if defined(USE_DITHER_MASK)
|
|
{
|
|
// Apply dither sparsification
|
|
// Use dither mask for alpha blended shadows, based on pixel position xy and alpha level.
|
|
// Our dither texture is 4x4x16.
|
|
half3 vpos = input.positionCS.xyz / input.positionCS.w;
|
|
half alphaRef = tex3D(_DitherMaskLOD, half3(vpos.xy * 0.25, alpha * 0.9375)).a;
|
|
clip(alphaRef - 0.01);
|
|
}
|
|
#else
|
|
{
|
|
// Without dithering, it's similar to Cutout
|
|
clip(alpha - _Cutoff);
|
|
}
|
|
#endif
|
|
}
|
|
#endif
|
|
}
|
|
#endif
|
|
|
|
return 0;
|
|
}
|