Skip to content

Fix incorrect clipping polygon region selection in vertex shaders#13298

Open
alarkbentley wants to merge 5 commits intoCesiumGS:mainfrom
alarkbentley:alark/clipping_polygon_shader_bug
Open

Fix incorrect clipping polygon region selection in vertex shaders#13298
alarkbentley wants to merge 5 commits intoCesiumGS:mainfrom
alarkbentley:alark/clipping_polygon_shader_bug

Conversation

@alarkbentley
Copy link
Copy Markdown
Contributor

@alarkbentley alarkbentley commented Mar 13, 2026

Description

Fixes a bug where the clipping polygon vertex shaders (GlobeVS.glsl and ModelClippingPolygonsStageVS.glsl) could select the wrong clipping region for a vertex, leading to incorrect polygon clipping results.

Demo

Sandcastle

1. Open Sandcastle Link

It should correctly render Clipping Polygon A and Clipping Polygon B

image

2. Change "Spacing = 5", "Scale = 2" and click "Update ClippingPolygons"

Clipping Polygon A fails because the large distance forces the two polygons to reside in different regions in the texture atlas and Polygon A incorrectly uses the region data from Polygon B where the scale differs.

image

Problem

The previous implementation used a minimum-distance heuristic to choose which clipping region a vertex belongs to. It tracked the closest region boundary (minDistance) and always updated v_clippingPosition whenever a closer boundary was found — even if the vertex was outside that region. The v_regionIndex was only set when the vertex was actually inside the region, but v_clippingPosition could be overwritten by an incorrect, closer-but-wrong region.

This had two issues:

  1. v_clippingPosition could refer to the wrong region. The distance comparison minDistance.x > distance.x || minDistance.y > distance.y would overwrite v_clippingPosition with UV coordinates from a region the vertex doesn't belong to, as long as that region's boundary was closer on either axis.

  2. Unnecessary computation. Clamping, distance calculations, and comparisons were performed for every region even when a simple containment check suffices.

Solution

Replace the minimum-distance heuristic with a direct containment test. For each region, compute the UV coordinates and check whether the vertex falls within the region bounds (with a small threshold). On the first match, set both v_clippingPosition and v_regionIndex and move on — since clipping regions should never overlap, the first match is always the correct one.

Changes

  • packages/engine/Source/Shaders/GlobeVS.glsl — Replaced distance-based region selection with containment test in the ENABLE_CLIPPING_POLYGONS block.
  • packages/engine/Source/Shaders/Model/ModelClippingPolygonsStageVS.glsl — Same fix applied to the model clipping polygons stage.

Testing

Verified visually that clipping polygons render correctly and clip to the intended regions. The fix is a shader-only change with no API surface modifications.

npm test -- --include Scene/GlobeSurfaceTileProvider
npm test -- --include Scene/Model/Model3DTileContent

Author checklist

  • I have submitted a Contributor License Agreement
  • I have added my name to CONTRIBUTORS.md
  • I have updated CHANGES.md with a short summary of my change
  • I have added or updated unit tests to ensure consistent code coverage
  • I have updated the inline documentation, and included code examples where relevant
  • I have performed a self-review of my code

@github-actions
Copy link
Copy Markdown
Contributor

Thank you for the pull request, @alarkbentley!

✅ We can confirm we have a CLA on file for you.

@alarkbentley alarkbentley marked this pull request as ready for review March 16, 2026 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant