UnityGame/Library/PackageCache/com.unity.render-pipelines.universal/ShaderLibrary/AmbientOcclusion.hlsl

69 lines
2.3 KiB
HLSL
Raw Normal View History

2024-10-27 10:53:47 +03:00
#ifndef AMBIENT_OCCLUSION_INCLUDED
#define AMBIENT_OCCLUSION_INCLUDED
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceData.hlsl"
// Ambient occlusion
TEXTURE2D_X(_ScreenSpaceOcclusionTexture);
// 2023.3 Deprecated. This is for backwards compatibility. Remove in the future.
#define sampler_ScreenSpaceOcclusionTexture sampler_LinearClamp
struct AmbientOcclusionFactor
{
half indirectAmbientOcclusion;
half directAmbientOcclusion;
};
half SampleAmbientOcclusion(float2 normalizedScreenSpaceUV)
{
float2 uv = UnityStereoTransformScreenSpaceTex(normalizedScreenSpaceUV);
return half(SAMPLE_TEXTURE2D_X(_ScreenSpaceOcclusionTexture, sampler_LinearClamp, uv).x);
}
AmbientOcclusionFactor GetScreenSpaceAmbientOcclusion(float2 normalizedScreenSpaceUV)
{
AmbientOcclusionFactor aoFactor;
#if defined(_SCREEN_SPACE_OCCLUSION) && !defined(_SURFACE_TYPE_TRANSPARENT)
float ssao = saturate(SampleAmbientOcclusion(normalizedScreenSpaceUV) + (1.0 - _AmbientOcclusionParam.x));
aoFactor.indirectAmbientOcclusion = ssao;
aoFactor.directAmbientOcclusion = lerp(half(1.0), ssao, _AmbientOcclusionParam.w);
#else
aoFactor.directAmbientOcclusion = half(1.0);
aoFactor.indirectAmbientOcclusion = half(1.0);
#endif
#if defined(DEBUG_DISPLAY)
switch(_DebugLightingMode)
{
case DEBUGLIGHTINGMODE_LIGHTING_WITHOUT_NORMAL_MAPS:
aoFactor.directAmbientOcclusion = 0.5;
aoFactor.indirectAmbientOcclusion = 0.5;
break;
case DEBUGLIGHTINGMODE_LIGHTING_WITH_NORMAL_MAPS:
aoFactor.directAmbientOcclusion *= 0.5;
aoFactor.indirectAmbientOcclusion *= 0.5;
break;
}
#endif
return aoFactor;
}
AmbientOcclusionFactor CreateAmbientOcclusionFactor(float2 normalizedScreenSpaceUV, half occlusion)
{
AmbientOcclusionFactor aoFactor = GetScreenSpaceAmbientOcclusion(normalizedScreenSpaceUV);
aoFactor.indirectAmbientOcclusion = min(aoFactor.indirectAmbientOcclusion, occlusion);
return aoFactor;
}
AmbientOcclusionFactor CreateAmbientOcclusionFactor(InputData inputData, SurfaceData surfaceData)
{
return CreateAmbientOcclusionFactor(inputData.normalizedScreenSpaceUV, surfaceData.occlusion);
}
#endif