Skip to content

Commit

Permalink
fix: caustics and shadow fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
doodlum committed Nov 29, 2024
1 parent 5ea04f6 commit 49ded11
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 43 deletions.
49 changes: 12 additions & 37 deletions features/Water Lighting/Shaders/WaterLighting/WaterCaustics.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,46 @@ Texture2D<float4> WaterCaustics : register(t70);

namespace WaterLighting
{
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Caustics
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

float2 PanCausticsUV(float2 uv, float speed, float tiling)
{
return (float2(1, 0) * Timer * speed) + (uv * tiling);
return frac((float2(1, 0) * Timer * speed) + (uv * tiling));
}

float3 SampleCaustics(float2 uv)
{
return WaterCaustics.Sample(SampColorSampler, uv).r;
}

float3 ComputeCaustics(float2 uv)
{
float2 causticsUV = uv * 10.0;

float2 causticsUV1 = PanCausticsUV(causticsUV, 0.5 * 0.2, 1.0);
float2 causticsUV2 = PanCausticsUV(causticsUV, 1.0 * 0.2, -0.5);

float3 caustics1 = SampleCaustics(causticsUV1);
float3 caustics2 = SampleCaustics(causticsUV2);

float3 caustics = min(caustics1, caustics2) * 5.0;

return caustics;
}

float3 ComputeCaustics(float4 waterData, float3 worldPosition, float3 worldSpaceNormal)
float3 ComputeCaustics(float4 waterData, float3 worldPosition, uint eyeIndex)
{
float causticsDistToWater = waterData.w - worldPosition.z;
float shoreFactorCaustics = saturate(causticsDistToWater / 64.0);
float upAmount = worldSpaceNormal.z * 0.5 + 0.5;

float causticsAmount = shoreFactorCaustics * upAmount;

if (causticsAmount > 0.0) {
if (shoreFactorCaustics > 0.0) {
float causticsFade = 1.0 - saturate(causticsDistToWater / 1024.0);
causticsFade *= causticsFade;

float2 causticsUV = (worldPosition.xy + CameraPosAdjust[0].xy) * 0.005;
float2 causticsUV = (worldPosition.xy + CameraPosAdjust[eyeIndex].xy) * 0.005;

float2 causticsUV1 = PanCausticsUV(causticsUV, 0.5 * 0.2, 1.0);
float2 causticsUV2 = PanCausticsUV(causticsUV, 1.0 * 0.2, -0.5);

float3 caustics1 = SampleCaustics(causticsUV1);
float3 caustics2 = SampleCaustics(causticsUV2);

float3 causticsHigh = min(caustics1, caustics2) * 5.0;
float3 causticsHigh = 1.0;

if (causticsFade > 0.0)
causticsHigh = min(SampleCaustics(causticsUV1), SampleCaustics(causticsUV2));

causticsUV *= 0.5;

causticsUV1 = PanCausticsUV(causticsUV, 0.5 * 0.1, 1.0);
causticsUV2 = PanCausticsUV(causticsUV, 1.0 * 0.1, -0.5);

caustics1 = SampleCaustics(causticsUV1);
caustics2 = SampleCaustics(causticsUV2);

float3 causticsLow = sqrt(min(caustics1, caustics2));
float3 causticsLow = 1.0;

float3 caustics = lerp(causticsLow, causticsHigh, causticsFade);
caustics = lerp(causticsLow, caustics, upAmount);
if (causticsFade < 1.0)
causticsLow = min(SampleCaustics(causticsUV1), SampleCaustics(causticsUV2));

return lerp(1.0, caustics, shoreFactorCaustics * upAmount);
return lerp(causticsLow * 4.0, causticsHigh * 4.0, causticsFade);
}

return 1.0;
Expand Down
2 changes: 1 addition & 1 deletion package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -1855,7 +1855,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
float3 dirLightColorMultiplier = 1;

# if defined(WATER_LIGHTING)
dirLightColorMultiplier *= WaterLighting::ComputeCaustics(waterData, input.WorldPosition.xyz, worldSpaceNormal);
dirLightColorMultiplier *= WaterLighting::ComputeCaustics(waterData, input.WorldPosition.xyz, eyeIndex);
# endif

float selfShadowFactor = 1.0f;
Expand Down
27 changes: 22 additions & 5 deletions package/Shaders/RunGrass.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,10 @@ cbuffer AlphaTestRefCB : register(b11)
# include "Skylighting/Skylighting.hlsli"
# endif

# if defined(WATER_LIGHTING)
# include "WaterLighting/WaterCaustics.hlsli"
# endif

# ifdef GRASS_LIGHTING
# if defined(TRUE_PBR)

Expand Down Expand Up @@ -540,7 +544,7 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
float dirShadow = !InInterior ? shadowColor.x : 1.0;
float dirDetailShadow = 1.0;

if (dirShadow > 0.0) {
if (dirShadow > 0.0 && !InInterior) {
if (dirLightAngle > 0.0) {
# if defined(SCREEN_SPACE_SHADOWS)
dirDetailShadow = ScreenSpaceShadows::GetScreenSpaceShadow(input.HPosition.xyz, screenUV, screenNoise, eyeIndex);
Expand All @@ -555,10 +559,17 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# endif // TERRAIN_SHADOWS

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

# if defined(WATER_LIGHTING)
if (dirShadow > 0.0) {
float4 waterData = SharedData::GetWaterData(input.WorldPosition.xyz);
dirShadow *= WaterLighting::ComputeCaustics(waterData, input.WorldPosition.xyz, eyeIndex);
}
# endif
}

float3 diffuseColor = 0;
Expand Down Expand Up @@ -764,10 +775,9 @@ PS_OUTPUT main(PS_INPUT input)
float4 shadowColor = TexShadowMaskSampler.Load(int3(input.HPosition.xy, 0));

float dirShadow = !InInterior ? shadowColor.x : 1.0;

float dirDetailShadow = 1.0;

if (dirShadow > 0.0) {
if (dirShadow > 0.0 && !InInterior) {
# if defined(SCREEN_SPACE_SHADOWS)
dirDetailShadow = ScreenSpaceShadows::GetScreenSpaceShadow(input.HPosition.xyz, screenUV, screenNoise, eyeIndex);
# endif // SCREEN_SPACE_SHADOWS
Expand All @@ -780,10 +790,17 @@ PS_OUTPUT main(PS_INPUT input)
# endif // TERRAIN_SHADOWS

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

# if defined(WATER_LIGHTING)
if (dirShadow > 0.0) {
float4 waterData = SharedData::GetWaterData(input.WorldPosition.xyz);
dirShadow *= WaterLighting::ComputeCaustics(waterData, input.WorldPosition.xyz, eyeIndex);
}
# endif
}

float3 diffuseColor = DirLightColorShared.xyz * dirShadow * lerp(dirDetailShadow, 1.0, 0.5) * 0.5;
Expand Down

0 comments on commit 49ded11

Please sign in to comment.