-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Model polygon clipping shader impl #13647
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mzschwartz5
wants to merge
7
commits into
model-clipping-foundation
Choose a base branch
from
model-clipping-pipeline-wiring
base: model-clipping-foundation
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d0af07b
Wires up vector-based model polygon clipping
mzschwartz5 df11427
Implements special high-precision model clipping shader
mzschwartz5 b331d01
Extract cartographic delta glsl function into reusable common func
mzschwartz5 f84155d
Update specs for uniforms and model clipping pipeline stage
mzschwartz5 1214bcb
Refactors clipping pipeline stage to be more robust to model readiness
mzschwartz5 cd21225
Disable model.show in inverse mode when no clipping overlap
mzschwartz5 05a2b04
Base clippingPolygonsState field on collection length
mzschwartz5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/engine/Source/Shaders/Builtin/Functions/eyeToCartographicDelta.glsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * Computes the geodetic offset (delta longitude, latitude, and height) from a reference cartographic position | ||
| * to a point given in eye coordinates. | ||
| * | ||
| * This is designed to preserve precision. Rather than converting the point's absolute world position to | ||
| * cartographic -- which is too large to process precisely at 32 bits -- it works entirely with the small | ||
| * delta between the point and the camera. By projecting the eye-space offset onto the ellipsoid's equatorial | ||
| * and meridional planes, it derives the change in (longitude, latitude, height) as small, precisely-representable | ||
| * quantities. The delta gets smaller and more precise as one zooms in. | ||
| * <br /><br /> | ||
| * This assumes an ellipsoid of revolution (equatorial radii equal, as with WGS84), so that longitude is exact. | ||
| * The latitude calculation is only first-order accurate, since the meridian is an ellipse rather than a circle. | ||
| * | ||
| * @name czm_eyeToCartographicDelta | ||
| * @glslFunction | ||
| * | ||
| * @param {vec3} positionEC The position, in eye coordinates, to measure to. | ||
| * | ||
| * @returns {vec3} The geodetic offset from the camera to <code>positionEC</code>, as (delta longitude, delta latitude in radians, delta height in meters). | ||
| */ | ||
| vec3 czm_eyeToCartographicDelta(vec3 positionEC) | ||
| { | ||
| // A vector representing the camera-to-vertex offset, in an ENU oriented reference frame (centered at the camera) | ||
| vec3 cameraToVertex = czm_eyeToEnu * positionEC; | ||
|
|
||
| float cosLatitude = cos(czm_eyeCartographic.y); | ||
| float sinLatitude = sin(czm_eyeCartographic.y); | ||
|
|
||
| // To derive longitude, project the camera and vertex onto the equatorial plane, in a frame such that the camera lies along the +x axis. In this frame, | ||
| // the vertex's (delta) longitude is simply the atan of its x and y components. | ||
| float primeVerticalRadius = 1.0 / czm_eyeEllipsoidCurvature.x; | ||
| vec2 cameraEquatorialPos = vec2((primeVerticalRadius + czm_eyeCartographic.z) * cosLatitude, 0.0); | ||
| vec2 vertexEquatorialPos = cameraEquatorialPos + vec2(-cameraToVertex.y * sinLatitude + cameraToVertex.z * cosLatitude, cameraToVertex.x); | ||
| float deltaLongitude = atan(vertexEquatorialPos.y, vertexEquatorialPos.x); | ||
|
|
||
| // Deriving latitude is a bit harder: we can't directly project the vertex onto the camera's meridian — the latitude projection is dependent on the longitude. | ||
| // Instead we can rotate the vertex (by -deltaLongitude) onto the camera's meridional plane. (Note: (unlike the exact longitude case) this is only first-order accurate because the meridian is an ellipse rather than a circle) | ||
| // Using a 2D rotation formula introduces precision issues (subtraction of large-magnitude quantities), so instead we can calculate the vector difference | ||
| // between the vertex and its rotated version, and apply that offset to the cameraToVertex vector. Then, the cameraToVertex vector accurately | ||
| // reflects the difference between the camera and the _rotated_ vertex, so we can then project the camera onto the meridional plane and apply this offset - just as we did for deltaLongitude, above. | ||
| // Best of all, we can do this all with small delta quantities which preserve precision. | ||
| // | ||
| // (I suggest drawing this out -- with the vertex and camera vectors projected onto the equatorial plane, with the camera on the +x axis) | ||
| // Mathematically: if you compare (subtract) vertexEquatorialPos and the same vector rotated onto the camera's meridional plane, you get | ||
| // |dx| = |vertexEquatorialPos| - vertexEquatorialPos.x = (r - x) = r * (1 - cos(deltaLongitude)) | ||
| // |dy| = cameraToVertex.x (the east component) | ||
| // (To avoid precision issues, we'll use the identity (1 - cos(x) = 2 * sin^2(x/2))) | ||
| // | ||
| // Since these offsets were produced in the equatorial plane, and cameraToVertex is in the camera's ENU frame, we need to deconstruct along the camera's north and up axes. And we only care about | ||
| // dx, since dy is in the camera's east direction, and that component gets zeroed out when projecting onto the camera's meridional plane. | ||
| float sinHalfLongitude = sin(deltaLongitude * 0.5); | ||
| float dx = length(vertexEquatorialPos) * 2.0 * sinHalfLongitude * sinHalfLongitude; | ||
| vec3 meridionalOffset = vec3( | ||
| 0.0, // east | ||
| cameraToVertex.y - dx * sinLatitude, // north | ||
| cameraToVertex.z + dx * cosLatitude // up | ||
| ); | ||
|
|
||
| // Reframe the camera in a meridional plane, where it lies along the +z axis, and apply the meridionalOffset to get the vertex's position in that plane. | ||
| // Then, deltaLatitude is simply the atan of its x and y components. | ||
| float meridionalRadius = 1.0 / czm_eyeEllipsoidCurvature.y; | ||
| vec2 cameraMeridionalPos = vec2(meridionalRadius + czm_eyeCartographic.z, 0.0); | ||
| vec2 vertMeridionalPos = cameraMeridionalPos + vec2(meridionalOffset.z, meridionalOffset.y); | ||
| float deltaLatitude = atan(vertMeridionalPos.y, vertMeridionalPos.x); | ||
|
|
||
| // Finally, derive the change in height above the ellipsoid. This is the meridional-plane analogue of the dx step above: | ||
| // there we rotated the vertex (in the equatorial plane) to the camera's longitude; here we rotate it (in the meridional plane, by -deltaLatitude) | ||
| // to the camera's latitude, aligning it with the camera's radial (up) direction. | ||
| float sinHalfLatitude = sin(deltaLatitude * 0.5); | ||
| float dz = length(vertMeridionalPos) * 2.0 * sinHalfLatitude * sinHalfLatitude; | ||
| float deltaHeight = meridionalOffset.z + dz; | ||
|
|
||
| return vec3(deltaLongitude, deltaLatitude, deltaHeight); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that this PR removes
CLIPPING_POLYGON_REGIONS_LENGTH, the compiled shader only depends on the enabled/inverse booleans, but the polygon count is still baked into this state. That means every add/remove triggersresetDrawCommandson every model sharing the collection (for example, all tile contents in a tileset) and rebuilds identical shaders.Collapsing this to just the sign isn't safe on its own though, since #13641 also uses this state to gate releasing
_clippingPolygonData. I think the two invalidations want to be decoupled: shader rebuild keyed on the booleans only, and texture-data release driven separately (either thepolygonAdded/polygonRemovedevents, or a version counter on the collection, which would also close the same-vertex-count-swap gap from my comment on #13641).