Skip to content

Commit 3aa8b22

Browse files
committed
🐛 Fix refraction and caustics mismatch
1 parent 5f7afd0 commit 3aa8b22

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

crest/Assets/Crest/Crest/Shaders/OceanEmission.hlsl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ half3 OceanEmission
193193
if (!i_underwater)
194194
{
195195
const half2 refractOffset = _RefractionStrength * i_n_pixel.xz * min(1.0, 0.5*(i_sceneZ - i_pixelZ)) / i_sceneZ;
196-
const float rawDepth = CREST_SAMPLE_SCENE_DEPTH_X(i_uvDepth + refractOffset);
196+
float rawDepth = CREST_SAMPLE_SCENE_DEPTH_X(i_uvDepth + refractOffset);
197197
half2 uvBackgroundRefract;
198+
float sceneZ = i_sceneZ;
198199

199200
// Compute depth fog alpha based on refracted position if it landed on an underwater surface, or on unrefracted depth otherwise
200201
#if UNITY_REVERSED_Z
@@ -204,21 +205,24 @@ half3 OceanEmission
204205
#endif
205206
{
206207
uvBackgroundRefract = uvBackground + refractOffset;
207-
depthFogDistance = CrestLinearEyeDepth(CREST_MULTISAMPLE_SCENE_DEPTH(uvBackgroundRefract, rawDepth)) - i_pixelZ;
208+
rawDepth = CREST_MULTISAMPLE_SCENE_DEPTH(uvBackgroundRefract, rawDepth);
209+
sceneZ = CrestLinearEyeDepth(rawDepth);
210+
depthFogDistance = sceneZ - i_pixelZ;
208211
}
209212
else
210213
{
211-
// It seems that when MSAA is enabled this can sometimes be negative
212-
depthFogDistance = max(CrestLinearEyeDepth(CREST_MULTISAMPLE_SCENE_DEPTH(uvBackground, i_rawDepth)) - i_pixelZ, 0.0);
213-
214214
// We have refracted onto a surface in front of the water. Cancel the refraction offset.
215215
uvBackgroundRefract = uvBackground;
216+
rawDepth = CREST_MULTISAMPLE_SCENE_DEPTH(uvBackground, i_rawDepth);
217+
sceneZ = CrestLinearEyeDepth(rawDepth);
218+
// It seems that when MSAA is enabled this can sometimes be negative
219+
depthFogDistance = max(sceneZ - i_pixelZ, 0.0);
216220
}
217221

218222
sceneColour = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_BackgroundTexture, uvBackgroundRefract).rgb;
219223
#if _CAUSTICS_ON
220-
float3 scenePos = _WorldSpaceCameraPos - i_view * i_sceneZ / dot(unity_CameraToWorld._m02_m12_m22, -i_view);
221-
ApplyCaustics(_CausticsTiledTexture, _CausticsDistortionTiledTexture, i_positionSS, scenePos, i_lightDir, i_sceneZ, i_underwater, sceneColour, _LD_SliceIndex + 1, cascadeData1);
224+
float3 scenePos = _WorldSpaceCameraPos - i_view * sceneZ / dot(unity_CameraToWorld._m02_m12_m22, -i_view);
225+
ApplyCaustics(_CausticsTiledTexture, _CausticsDistortionTiledTexture, uvBackgroundRefract * _ScreenParams.xy, scenePos, i_lightDir, sceneZ, i_underwater, sceneColour, _LD_SliceIndex + 1, cascadeData1);
222226
#endif
223227
alpha = 1.0 - exp(-_DepthFogDensity.xyz * depthFogDistance);
224228
}

0 commit comments

Comments
 (0)