Skip to content

Commit

Permalink
feat: effect shadows improvements (#743)
Browse files Browse the repository at this point in the history
* feat: effect shadows improvements

* style: 🎨 apply clang-format changes

* fix: shadow sampling

* fix: ambient color wrong on some weathers

---------

Co-authored-by: doodlum <doodlum@users.noreply.github.com>
  • Loading branch information
doodlum and doodlum authored Nov 13, 2024
1 parent ce54085 commit 0de3ecf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 23 deletions.
6 changes: 3 additions & 3 deletions package/Shaders/Common/ShadowSampling.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ float Get3DFilteredShadow(float3 positionWS, float3 viewDirection, float2 screen
PerGeometry sD = SharedPerShadow[0];

float fadeFactor = 1.0 - pow(saturate(dot(positionWS, positionWS) / sD.ShadowLightParam.z), 8);
uint sampleCount = ceil(16.0 * (1.0 - saturate(length(positionWS) / sqrt(sD.ShadowLightParam.z))));
uint sampleCount = ceil(8.0 * (1.0 - saturate(length(positionWS) / sqrt(sD.ShadowLightParam.z))));

if (sampleCount == 0)
return 1.0;
Expand All @@ -57,8 +57,8 @@ float Get3DFilteredShadow(float3 positionWS, float3 viewDirection, float2 screen
float r = rnd.z;
float4 sincos_phi;
sincos(phi, sincos_phi.y, sincos_phi.x);
float3 sampleOffset = viewDirection * i * 64 * rcpSampleCount;
sampleOffset += (float3(r * sin_theta * sincos_phi.x, r * sin_theta * sincos_phi.y, r * cos_theta) * 0.5 + 0.5) * 64;
float3 sampleOffset = viewDirection * (float(i) - float(sampleCount) * 0.5) * 64 * rcpSampleCount;
sampleOffset += float3(r * sin_theta * sincos_phi.x, r * sin_theta * sincos_phi.y, r * cos_theta) * 64;

uint cascadeIndex = sD.EndSplitDistances.x < GetShadowDepth(positionWS.xyz + viewDirection * (sampleOffset.x + sampleOffset.y), eyeIndex); // Stochastic cascade sampling

Expand Down
28 changes: 8 additions & 20 deletions package/Shaders/Effect.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -522,27 +522,15 @@ float3 GetLightingColor(float3 msPosition, float3 worldPosition, float4 screenPo
float4 lightDistanceSquared = (PLightPositionX[eyeIndex] - msPosition.xxxx) * (PLightPositionX[eyeIndex] - msPosition.xxxx) + (PLightPositionY[eyeIndex] - msPosition.yyyy) * (PLightPositionY[eyeIndex] - msPosition.yyyy) + (PLightPositionZ[eyeIndex] - msPosition.zzzz) * (PLightPositionZ[eyeIndex] - msPosition.zzzz);
float4 lightFadeMul = 1.0.xxxx - saturate(PLightingRadiusInverseSquared * lightDistanceSquared);

float3 color = 0.0;
float angleShadow = saturate(DirLightDirectionShared.z) * saturate(DirLightDirectionShared.z);
float dirLightBacklighting = 1.0 + saturate(dot(normalize(worldPosition), -DirLightDirectionShared.xyz));

if (ExtraShaderDescriptor & ExtraFlags::EffectShadows) {
if (InMapMenu)
color = dirLightBacklighting * DirLightColorShared * angleShadow;
else if (!InInterior && (ExtraShaderDescriptor & ExtraFlags::InWorld))
color = dirLightBacklighting * DirLightColorShared * GetEffectShadow(worldPosition, normalize(worldPosition), screenPosition, eyeIndex);
else
color = dirLightBacklighting * DirLightColorShared * 0.5;
} else {
if (InMapMenu)
color = dirLightBacklighting * DirLightColorShared * angleShadow;
else if (!InInterior && (ExtraShaderDescriptor & ExtraFlags::InWorld))
color = dirLightBacklighting * DirLightColorShared * GetWorldShadow(worldPosition, length(worldPosition), 0.0, eyeIndex) * angleShadow;
else
color = dirLightBacklighting * DirLightColorShared * 0.5;
}
float3 color = DLightColor.xyz;

color += mul(DirectionalAmbientShared, float4(0, 0, 1, 1));
if ((ExtraShaderDescriptor & ExtraFlags::EffectShadows) && !InMapMenu && !InInterior) {
float3 dirLightColor = DirLightColorShared * 0.5;
float3 ambientColor = mul(DirectionalAmbientShared, float4(0, 0, 1, 1));

color = ambientColor;
color += dirLightColor * GetEffectShadow(worldPosition, normalize(worldPosition), screenPosition, eyeIndex);
}

color.x += dot(PLightColorR * lightFadeMul, 1.0.xxxx);
color.y += dot(PLightColorG * lightFadeMul, 1.0.xxxx);
Expand Down

0 comments on commit 0de3ecf

Please sign in to comment.