Skip to content

Commit 40b2bd7

Browse files
authored
Merge pull request wave-harmonic#914 from wave-harmonic/fix/clip-incorrect-slices
Fix clip surface using incorrect slices
2 parents 51236c5 + 98d179a commit 40b2bd7

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

crest/Assets/Crest/Crest/Shaders/Ocean.shader

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,36 @@ Shader "Crest/Ocean"
438438
// We need this when sampling a screenspace texture.
439439
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
440440

441+
#if _CLIPSURFACE_ON
442+
{
443+
// Clip surface
444+
half clipValue = 0.0;
445+
446+
uint slice0; uint slice1; float alpha;
447+
PosToSliceIndices(input.worldPos.xz, 0.0, _CrestCascadeData[0]._scale, slice0, slice1, alpha);
448+
449+
const CascadeParams cascadeData0 = _CrestCascadeData[slice0];
450+
const CascadeParams cascadeData1 = _CrestCascadeData[slice1];
451+
const float weight0 = (1.0 - alpha) * cascadeData0._weight;
452+
const float weight1 = (1.0 - weight0) * cascadeData1._weight;
453+
454+
if (weight0 > 0.001)
455+
{
456+
const float3 uv = WorldToUV(input.worldPos.xz, cascadeData0, slice0);
457+
SampleClip(_LD_TexArray_ClipSurface, uv, weight0, clipValue);
458+
}
459+
if (weight1 > 0.001)
460+
{
461+
const float3 uv = WorldToUV(input.worldPos.xz, cascadeData1, slice1);
462+
SampleClip(_LD_TexArray_ClipSurface, uv, weight1, clipValue);
463+
}
464+
465+
clipValue = lerp(_CrestClipByDefault, clipValue, weight0 + weight1);
466+
// Add 0.5 bias to tighten and smooth clipped edges.
467+
clip(-clipValue + 0.5);
468+
}
469+
#endif // _CLIPSURFACE_ON
470+
441471
const CascadeParams cascadeData0 = _CrestCascadeData[_LD_SliceIndex];
442472
const CascadeParams cascadeData1 = _CrestCascadeData[_LD_SliceIndex + 1];
443473
const PerCascadeInstanceData instanceData = _CrestPerCascadeInstanceData[_LD_SliceIndex];
@@ -453,24 +483,6 @@ Shader "Crest/Ocean"
453483
const float wt_smallerLod = (1.0 - lodAlpha) * cascadeData0._weight;
454484
const float wt_biggerLod = (1.0 - wt_smallerLod) * cascadeData1._weight;
455485

456-
#if _CLIPSURFACE_ON
457-
// Clip surface
458-
half clipVal = 0.0;
459-
if (wt_smallerLod > 0.001)
460-
{
461-
const float3 uv_slice_smallerLod = WorldToUV(input.worldPos.xz, cascadeData0, _LD_SliceIndex);
462-
SampleClip(_LD_TexArray_ClipSurface, uv_slice_smallerLod, wt_smallerLod, clipVal);
463-
}
464-
if (wt_biggerLod > 0.001)
465-
{
466-
const float3 uv_slice_biggerLod = WorldToUV(input.worldPos.xz, cascadeData1, _LD_SliceIndex + 1);
467-
SampleClip(_LD_TexArray_ClipSurface, uv_slice_biggerLod, wt_biggerLod, clipVal);
468-
}
469-
clipVal = lerp(_CrestClipByDefault, clipVal, wt_smallerLod + wt_biggerLod);
470-
// Add 0.5 bias for LOD blending and texel resolution correction. This will help to tighten and smooth clipped edges
471-
clip(-clipVal + 0.5);
472-
#endif
473-
474486
#if _CLIPUNDERTERRAIN_ON
475487
clip(input.lodAlpha_worldXZUndisplaced_oceanDepth.w + 2.0);
476488
#endif

docs/about/history.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Fixed
2828
- Fix incorrect baked depth cache data that were baked since `Crest` 4.14.
2929
- Fix XR `SPI` underwater rendering for Unity 2021.2 standalone.
3030
- Fix *Underwater Renderer* not rendering on *Intel iGPUs*.
31+
- Fix clip surface inputs losing accuracy with large waves.
3132

3233
.. only:: hdrp
3334

0 commit comments

Comments
 (0)