diff --git a/crest/Assets/Crest/Crest/Shaders/OceanEmission.hlsl b/crest/Assets/Crest/Crest/Shaders/OceanEmission.hlsl index e323c84f1..4e1b3b245 100644 --- a/crest/Assets/Crest/Crest/Shaders/OceanEmission.hlsl +++ b/crest/Assets/Crest/Crest/Shaders/OceanEmission.hlsl @@ -195,6 +195,7 @@ half3 OceanEmission const half2 refractOffset = _RefractionStrength * i_n_pixel.xz * min(1.0, 0.5*(i_sceneZ - i_pixelZ)) / i_sceneZ; const float rawDepth = CREST_SAMPLE_SCENE_DEPTH_X(i_uvDepth + refractOffset); half2 uvBackgroundRefract; + float sceneZ = i_sceneZ; // Compute depth fog alpha based on refracted position if it landed on an underwater surface, or on unrefracted depth otherwise #if UNITY_REVERSED_Z @@ -204,12 +205,14 @@ half3 OceanEmission #endif { uvBackgroundRefract = uvBackground + refractOffset; - depthFogDistance = CrestLinearEyeDepth(CREST_MULTISAMPLE_SCENE_DEPTH(uvBackgroundRefract, rawDepth)) - i_pixelZ; + sceneZ = CrestLinearEyeDepth(CREST_MULTISAMPLE_SCENE_DEPTH(uvBackgroundRefract, rawDepth)); + depthFogDistance = sceneZ - i_pixelZ; } else { + sceneZ = CrestLinearEyeDepth(CREST_MULTISAMPLE_SCENE_DEPTH(uvBackground, i_rawDepth)); // It seems that when MSAA is enabled this can sometimes be negative - depthFogDistance = max(CrestLinearEyeDepth(CREST_MULTISAMPLE_SCENE_DEPTH(uvBackground, i_rawDepth)) - i_pixelZ, 0.0); + depthFogDistance = max(sceneZ - i_pixelZ, 0.0); // We have refracted onto a surface in front of the water. Cancel the refraction offset. uvBackgroundRefract = uvBackground; @@ -217,8 +220,8 @@ half3 OceanEmission sceneColour = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_BackgroundTexture, uvBackgroundRefract).rgb; #if _CAUSTICS_ON - float3 scenePos = _WorldSpaceCameraPos - i_view * i_sceneZ / dot(unity_CameraToWorld._m02_m12_m22, -i_view); - ApplyCaustics(_CausticsTiledTexture, _CausticsDistortionTiledTexture, i_positionSS, scenePos, i_lightDir, i_sceneZ, i_underwater, sceneColour, _LD_SliceIndex + 1, cascadeData1); + float3 scenePos = _WorldSpaceCameraPos - i_view * sceneZ / dot(unity_CameraToWorld._m02_m12_m22, -i_view); + ApplyCaustics(_CausticsTiledTexture, _CausticsDistortionTiledTexture, uvBackgroundRefract * _ScreenParams.xy, scenePos, i_lightDir, sceneZ, i_underwater, sceneColour, _LD_SliceIndex + 1, cascadeData1); #endif alpha = 1.0 - exp(-_DepthFogDensity.xyz * depthFogDistance); }