您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
101 行
3.6 KiB
101 行
3.6 KiB
Shader "Hidden/Universal Render Pipeline/ScreenSpaceShadows"
|
|
{
|
|
SubShader
|
|
{
|
|
Tags{ "RenderPipeline" = "UniversalPipeline" "IgnoreProjector" = "True"}
|
|
|
|
HLSLINCLUDE
|
|
|
|
// Note: Screenspace shadow resolve is only performed when shadow cascades are enabled
|
|
// Shadow cascades require cascade index and shadowCoord to be computed on pixel.
|
|
#define _MAIN_LIGHT_SHADOWS_CASCADE
|
|
|
|
#pragma prefer_hlslcc gles
|
|
#pragma exclude_renderers d3d11_9x
|
|
//Keep compiler quiet about Shadows.hlsl.
|
|
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/ImageBasedLighting.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
|
|
|
|
#if defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_STEREO_MULTIVIEW_ENABLED)
|
|
TEXTURE2D_ARRAY_FLOAT(_CameraDepthTexture);
|
|
#else
|
|
TEXTURE2D_FLOAT(_CameraDepthTexture);
|
|
#endif
|
|
SAMPLER(sampler_CameraDepthTexture);
|
|
|
|
struct Attributes
|
|
{
|
|
float4 positionOS : POSITION;
|
|
float2 texcoord : TEXCOORD0;
|
|
UNITY_VERTEX_INPUT_INSTANCE_ID
|
|
};
|
|
|
|
struct Varyings
|
|
{
|
|
half4 positionCS : SV_POSITION;
|
|
half4 uv : TEXCOORD0;
|
|
UNITY_VERTEX_OUTPUT_STEREO
|
|
};
|
|
|
|
Varyings Vertex(Attributes input)
|
|
{
|
|
Varyings output;
|
|
UNITY_SETUP_INSTANCE_ID(input);
|
|
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
|
|
|
|
output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
|
|
|
|
float4 projPos = output.positionCS * 0.5;
|
|
projPos.xy = projPos.xy + projPos.w;
|
|
|
|
output.uv.xy = UnityStereoTransformScreenSpaceTex(input.texcoord);
|
|
output.uv.zw = projPos.xy;
|
|
|
|
return output;
|
|
}
|
|
|
|
half4 Fragment(Varyings input) : SV_Target
|
|
{
|
|
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
|
|
|
|
float deviceDepth = SAMPLE_TEXTURE2D_X(_CameraDepthTexture, sampler_CameraDepthTexture, input.uv.xy).r;
|
|
|
|
#if UNITY_REVERSED_Z
|
|
deviceDepth = 1 - deviceDepth;
|
|
#endif
|
|
deviceDepth = 2 * deviceDepth - 1; //NOTE: Currently must massage depth before computing CS position.
|
|
|
|
float3 vpos = ComputeViewSpacePosition(input.uv.zw, deviceDepth, unity_CameraInvProjection);
|
|
float3 wpos = mul(unity_CameraToWorld, float4(vpos, 1)).xyz;
|
|
|
|
//Fetch shadow coordinates for cascade.
|
|
float4 coords = TransformWorldToShadowCoord(wpos);
|
|
|
|
// Screenspace shadowmap is only used for directional lights which use orthogonal projection.
|
|
ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
|
|
half4 shadowParams = GetMainLightShadowParams();
|
|
return SampleShadowmap(TEXTURE2D_ARGS(_MainLightShadowmapTexture, sampler_MainLightShadowmapTexture), coords, shadowSamplingData, shadowParams, false);
|
|
}
|
|
|
|
ENDHLSL
|
|
|
|
Pass
|
|
{
|
|
Name "ScreenSpaceShadows"
|
|
ZTest Always
|
|
ZWrite Off
|
|
Cull Off
|
|
|
|
HLSLPROGRAM
|
|
#pragma multi_compile _MAIN_LIGHT_SHADOWS
|
|
#pragma multi_compile _ _SHADOWS_SOFT
|
|
|
|
#pragma vertex Vertex
|
|
#pragma fragment Fragment
|
|
ENDHLSL
|
|
}
|
|
}
|
|
}
|