Skip to content

Commit

Permalink
refactor: add namespace for CloudShadows
Browse files Browse the repository at this point in the history
  • Loading branch information
FlayaN committed Aug 18, 2024
1 parent 421c74b commit b2db46f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
31 changes: 17 additions & 14 deletions features/Cloud Shadows/Shaders/CloudShadows/CloudShadows.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ TextureCube<float4> cloudShadowsTexture : register(t27);
#define PlanetRadius (6371e3f / 1.428e-2)
#define RcpHPlusR (1.0 / (CloudHeight + PlanetRadius))

float3 GetCloudShadowSampleDir(float3 rel_pos, float3 eye_to_sun)
namespace CloudShadows
{
float r = PlanetRadius;
float3 p = (rel_pos + float3(0, 0, r)) * RcpHPlusR;
float dotprod = dot(p, eye_to_sun);
float lengthsqr = dot(p, p);
float t = -dotprod + sqrt(dotprod * dotprod - dot(p, p) + 1);
float3 v = (p + eye_to_sun * t) * (r + CloudHeight) - float3(0, 0, r);
return v;
}
float3 GetCloudShadowSampleDir(float3 rel_pos, float3 eye_to_sun)
{
float r = PlanetRadius;
float3 p = (rel_pos + float3(0, 0, r)) * RcpHPlusR;
float dotprod = dot(p, eye_to_sun);
float lengthsqr = dot(p, p);
float t = -dotprod + sqrt(dotprod * dotprod - dot(p, p) + 1);
float3 v = (p + eye_to_sun * t) * (r + CloudHeight) - float3(0, 0, r);
return v;
}

float3 GetCloudShadowMult(float3 worldPosition, SamplerState textureSampler)
{
float3 cloudSampleDir = GetCloudShadowSampleDir(worldPosition, DirLightDirectionShared.xyz).xyz;
float cloudCubeSample = cloudShadowsTexture.SampleLevel(textureSampler, cloudSampleDir, 0).w;
return 1.0 - saturate(cloudCubeSample);
float3 GetCloudShadowMult(float3 worldPosition, SamplerState textureSampler)
{
float3 cloudSampleDir = GetCloudShadowSampleDir(worldPosition, DirLightDirectionShared.xyz).xyz;
float cloudCubeSample = cloudShadowsTexture.SampleLevel(textureSampler, cloudSampleDir, 0).w;
return 1.0 - saturate(cloudCubeSample);
}
}
2 changes: 1 addition & 1 deletion package/Shaders/Common/ShadowSampling.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ float3 GetWorldShadow(float3 positionWS, float depth, float3 offset, uint eyeInd
#endif

#if defined(CLOUD_SHADOWS)
worldShadow *= GetCloudShadowMult(positionWS + offset, LinearSampler);
worldShadow *= CloudShadows::GetCloudShadowMult(positionWS + offset, LinearSampler);
if (worldShadow == 0.0)
return 0.0;
#endif
Expand Down
2 changes: 1 addition & 1 deletion package/Shaders/DistantTree.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ PS_OUTPUT main(PS_INPUT input)

# if defined(CLOUD_SHADOWS)
if (dirShadow > 0.0) {
dirShadow *= GetCloudShadowMult(input.WorldPosition, SampDiffuse);
dirShadow *= CloudShadows::GetCloudShadowMult(input.WorldPosition, SampDiffuse);
}
# 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 @@ -1956,7 +1956,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

# if defined(CLOUD_SHADOWS)
if (!inDirShadow) {
dirShadow *= GetCloudShadowMult(input.WorldPosition.xyz, SampColorSampler);
dirShadow *= CloudShadows::GetCloudShadowMult(input.WorldPosition.xyz, SampColorSampler);
}
# 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 @@ -528,7 +528,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

# if defined(CLOUD_SHADOWS)
if (dirShadow != 0.0) {
dirShadow *= GetCloudShadowMult(input.WorldPosition.xyz, SampBaseSampler);
dirShadow *= CloudShadows::GetCloudShadowMult(input.WorldPosition.xyz, SampBaseSampler);
}
# endif // CLOUD_SHADOWS
}
Expand Down

0 comments on commit b2db46f

Please sign in to comment.