Skip to content

Commit

Permalink
refactor: add namespace for TerrainOcclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
FlayaN committed Aug 18, 2024
1 parent d119bde commit 67e2bf3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,60 @@ Texture2D<float> TexTerraOcc : register(t40);
Texture2D<float> TexNormalisedHeight : register(t41);
Texture2D<float2> TexShadowHeight : register(t42);

float2 GetTerrainOcclusionUV(float2 xy)
namespace TerrainOcclusion
{
return xy * terraOccSettings.Scale.xy + terraOccSettings.Offset.xy;
}

float2 GetTerrainOcclusionXY(float2 uv)
{
return (uv - terraOccSettings.Offset.xy) * terraOccSettings.InvScale.xy;
}

float GetTerrainZ(float norm_z)
{
return lerp(terraOccSettings.ZRange.x, terraOccSettings.ZRange.y, norm_z) + terraOccSettings.HeightBias;
}

float2 GetTerrainZ(float2 norm_z)
{
return float2(GetTerrainZ(norm_z.x), GetTerrainZ(norm_z.y));
}

void GetTerrainOcclusion(
const float3 worldPos, const float viewDistance, SamplerState samp,
out float terrainShadow, out float terrainAo)
{
terrainShadow = 1;
terrainAo = 1;

float2 terraOccUV = GetTerrainOcclusionUV(worldPos.xy);

if (any(terraOccUV < 0) && any(terraOccUV > 1))
return;

[flatten] if (terraOccSettings.EnableTerrainShadow && (viewDistance > terraOccSettings.ShadowFadeDistance.x))
float2 GetTerrainOcclusionUV(float2 xy)
{
float fadeFactor = saturate((viewDistance - terraOccSettings.ShadowFadeDistance.x) / (terraOccSettings.ShadowFadeDistance.y - terraOccSettings.ShadowFadeDistance.x));
float2 shadowHeight = GetTerrainZ(TexShadowHeight.SampleLevel(samp, terraOccUV, 0));
float shadowFraction = saturate((worldPos.z - shadowHeight.y) / (shadowHeight.x - shadowHeight.y));
terrainShadow = lerp(1, shadowFraction, fadeFactor);
return xy * terraOccSettings.Scale.xy + terraOccSettings.Offset.xy;
}
[flatten] if (terraOccSettings.EnableTerrainAO)

float2 GetTerrainOcclusionXY(float2 uv)
{
float terrainHeight = GetTerrainZ(TexNormalisedHeight.SampleLevel(samp, terraOccUV, 0).x);
terrainAo = TexTerraOcc.SampleLevel(samp, terraOccUV, 0).x;
return (uv - terraOccSettings.Offset.xy) * terraOccSettings.InvScale.xy;
}

// power
terrainAo = pow(abs(terrainAo), terraOccSettings.AOPower);
float GetTerrainZ(float norm_z)
{
return lerp(terraOccSettings.ZRange.x, terraOccSettings.ZRange.y, norm_z) + terraOccSettings.HeightBias;
}

// height fadeout
float fadeOut = saturate((worldPos.z - terrainHeight) * terraOccSettings.AOFadeOutHeightRcp);
terrainAo = lerp(terrainAo, 1, fadeOut);
float2 GetTerrainZ(float2 norm_z)
{
return float2(GetTerrainZ(norm_z.x), GetTerrainZ(norm_z.y));
}

terrainAo = lerp(1, terrainAo, terraOccSettings.AOMix);
void GetTerrainOcclusion(
const float3 worldPos, const float viewDistance, SamplerState samp,
out float terrainShadow, out float terrainAo)
{
terrainShadow = 1;
terrainAo = 1;

float2 terraOccUV = GetTerrainOcclusionUV(worldPos.xy);

if (any(terraOccUV < 0) && any(terraOccUV > 1))
return;

[flatten] if (terraOccSettings.EnableTerrainShadow && (viewDistance > terraOccSettings.ShadowFadeDistance.x))
{
float fadeFactor = saturate((viewDistance - terraOccSettings.ShadowFadeDistance.x) / (terraOccSettings.ShadowFadeDistance.y - terraOccSettings.ShadowFadeDistance.x));
float2 shadowHeight = GetTerrainZ(TexShadowHeight.SampleLevel(samp, terraOccUV, 0));
float shadowFraction = saturate((worldPos.z - shadowHeight.y) / (shadowHeight.x - shadowHeight.y));
terrainShadow = lerp(1, shadowFraction, fadeFactor);
}
[flatten] if (terraOccSettings.EnableTerrainAO)
{
float terrainHeight = GetTerrainZ(TexNormalisedHeight.SampleLevel(samp, terraOccUV, 0).x);
terrainAo = TexTerraOcc.SampleLevel(samp, terraOccUV, 0).x;

// power
terrainAo = pow(abs(terrainAo), terraOccSettings.AOPower);

// height fadeout
float fadeOut = saturate((worldPos.z - terrainHeight) * terraOccSettings.AOFadeOutHeightRcp);
terrainAo = lerp(terrainAo, 1, fadeOut);

terrainAo = lerp(1, terrainAo, terraOccSettings.AOMix);
}
}
}
}
2 changes: 1 addition & 1 deletion package/Shaders/Common/ShadowSampling.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ float3 GetWorldShadow(float3 positionWS, float depth, float3 offset, uint eyeInd
#if defined(TERRA_OCC)
float terrainShadow = 1.0;
float terrainAo = 1.0;
GetTerrainOcclusion(positionWS + offset + CameraPosAdjust[eyeIndex], depth, LinearSampler, terrainShadow, terrainAo);
TerrainOcclusion::GetTerrainOcclusion(positionWS + offset + CameraPosAdjust[eyeIndex], depth, LinearSampler, terrainShadow, terrainAo);
worldShadow = terrainShadow;
if (worldShadow == 0.0)
return 0.0;
Expand Down
2 changes: 1 addition & 1 deletion package/Shaders/DistantTree.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ PS_OUTPUT main(PS_INPUT input)
if (dirShadow > 0.0) {
float terrainShadow = 1;
float terrainAo = 1;
GetTerrainOcclusion(input.WorldPosition.xyz + CameraPosAdjust[eyeIndex], length(input.WorldPosition.xyz), SampDiffuse, terrainShadow, terrainAo);
TerrainOcclusion::GetTerrainOcclusion(input.WorldPosition.xyz + CameraPosAdjust[eyeIndex], length(input.WorldPosition.xyz), SampDiffuse, terrainShadow, terrainAo);
dirShadow = min(dirShadow, terrainShadow);
}
# endif
Expand Down
2 changes: 1 addition & 1 deletion package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -1948,7 +1948,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# if defined(TERRA_OCC)
float terrainShadow = 1;
float terrainAo = 1;
GetTerrainOcclusion(input.WorldPosition.xyz + CameraPosAdjust[eyeIndex].xyz, length(input.WorldPosition.xyz), SampColorSampler, terrainShadow, terrainAo);
TerrainOcclusion::GetTerrainOcclusion(input.WorldPosition.xyz + CameraPosAdjust[eyeIndex].xyz, length(input.WorldPosition.xyz), SampColorSampler, terrainShadow, terrainAo);
dirShadow *= terrainShadow;
inDirShadow = inDirShadow || dirShadow == 0.0;
# endif
Expand Down
2 changes: 1 addition & 1 deletion package/Shaders/RunGrass.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
if (dirShadow > 0.0) {
float terrainShadow = 1;
float terrainAo = 1;
GetTerrainOcclusion(input.WorldPosition.xyz + CameraPosAdjust[eyeIndex].xyz, length(input.WorldPosition.xyz), SampBaseSampler, terrainShadow, terrainAo);
TerrainOcclusion::GetTerrainOcclusion(input.WorldPosition.xyz + CameraPosAdjust[eyeIndex].xyz, length(input.WorldPosition.xyz), SampBaseSampler, terrainShadow, terrainAo);
dirShadow *= terrainShadow;
}
# endif // TERRA_OCC
Expand Down

0 comments on commit 67e2bf3

Please sign in to comment.