Skip to content

Commit

Permalink
chore: effect shader improvements (#725)
Browse files Browse the repository at this point in the history
* chore: effect shader improvements

* style: 🎨 apply clang-format changes

* fix: remove testing code

---------

Co-authored-by: doodlum <doodlum@users.noreply.github.com>
  • Loading branch information
doodlum and doodlum authored Nov 9, 2024
1 parent 5dcd915 commit f4f1383
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
9 changes: 5 additions & 4 deletions package/Shaders/Common/ShadowSampling.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ float Get3DFilteredShadow(float3 positionWS, float3 viewDirection, float2 screen
uint3 seed = Random::pcg3d(uint3(screenPosition.xy, screenPosition.x * Math::PI));

float2 compareValue;
compareValue.x = mul(transpose(sD.ShadowMapProj[eyeIndex][0]), float4(positionWS, 1)).z;
compareValue.y = mul(transpose(sD.ShadowMapProj[eyeIndex][1]), float4(positionWS, 1)).z;
compareValue.x = mul(transpose(sD.ShadowMapProj[eyeIndex][0]), float4(positionWS, 1)).z - 0.01;
compareValue.y = mul(transpose(sD.ShadowMapProj[eyeIndex][1]), float4(positionWS, 1)).z - 0.01;

float shadow = 0.0;
if (sD.EndSplitDistances.z >= GetShadowDepth(positionWS, eyeIndex)) {
Expand All @@ -57,10 +57,11 @@ 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 * 256 * rcpSampleCount;
sampleOffset += float3(r * sin_theta * sincos_phi.x, r * sin_theta * sincos_phi.y, r * cos_theta) * 16;
float3 sampleOffset = viewDirection * i * 128 * rcpSampleCount;
sampleOffset += (float3(r * sin_theta * sincos_phi.x, r * sin_theta * sincos_phi.y, r * cos_theta) * 0.5 + 0.5) * 128;

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

float3 positionLS = mul(transpose(sD.ShadowMapProj[eyeIndex][cascadeIndex]), float4(positionWS + sampleOffset, 1));

float4 depths = SharedTexShadowMapSampler.GatherRed(LinearSampler, float3(saturate(positionLS.xy), cascadeIndex), 0);
Expand Down
17 changes: 9 additions & 8 deletions package/Shaders/Effect.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -528,21 +528,22 @@ float3 GetLightingColor(float3 msPosition, float3 worldPosition, float4 screenPo

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

if ((ExtendedFlags & Effect_Shadows) != 0) {
if (ExtendedFlags & Effect_Shadows) {
if (InMapMenu)
color = DirLightColorShared * angleShadow;
color = dirLightBacklighting * DirLightColorShared * angleShadow;
else if (!InInterior && (ExtraShaderDescriptor & ExtraFlags::InWorld))
color = DirLightColorShared * GetEffectShadow(worldPosition, normalize(worldPosition), screenPosition, eyeIndex);
color = dirLightBacklighting * DirLightColorShared * GetEffectShadow(worldPosition, normalize(worldPosition), screenPosition, eyeIndex);
else
color = DirLightColorShared * 0.5;
color = dirLightBacklighting * DirLightColorShared * 0.5;
} else {
if (InMapMenu)
color = DirLightColorShared * angleShadow;
color = dirLightBacklighting * DirLightColorShared * angleShadow;
else if (!InInterior && (ExtraShaderDescriptor & ExtraFlags::InWorld))
color = DirLightColorShared * GetWorldShadow(worldPosition, length(worldPosition), 0.0, eyeIndex) * angleShadow * 0.5;
color = dirLightBacklighting * DirLightColorShared * GetWorldShadow(worldPosition, length(worldPosition), 0.0, eyeIndex) * angleShadow * 0.5;
else
color = DirLightColorShared * 0.5;
color = dirLightBacklighting * DirLightColorShared * 0.5;
}

color += DirectionalAmbientShared._14_24_34;
Expand Down Expand Up @@ -792,7 +793,7 @@ PS_OUTPUT main(PS_INPUT input)
psout.ScreenSpaceNormals.xy = screenSpaceNormal.xy + 0.5.xx;
psout.ScreenSpaceNormals.zw = 0.0.xx;
# else
psout.Normal.xyz = float3(1, 0, 0);
psout.Normal.xyz = float3(!(ExtendedFlags & Effect_Shadows), 0, 0);
psout.Normal.w = finalColor.w;

psout.Color2 = finalColor;
Expand Down

0 comments on commit f4f1383

Please sign in to comment.