Skip to content

feat: Add triangular patches to @typegpu/geometry - #3014

Open
reczkok wants to merge 14 commits into
feat/geometry-meshesfrom
feat/geometry-patches
Open

reczkok wants to merge 14 commits into
feat/geometry-meshesfrom
feat/geometry-patches

Conversation

@reczkok

@reczkok reczkok commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Adds triangular patch surfaces to meshes, with subdivision separate from surface sampling

  • icosphere, capsule and rounded box primitives
  • tessellate into indexed geometry or sample patches directly in vertex shaders
  • shared index buffer across subdivision levels, so detail can change without rebuilding buffers
  • spherical interpolation helpers + an interactive patch tessellation example

Tessellation and spherical interpolation ideas yoinked from @deluksic’s de/geometry-spheres branch, shared in the original PR comment

Stacks on #2997

@github-actions

github-actions Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

pkg.pr.new

packages
Ready to be installed by your favorite package manager ⬇️

https://pkg.pr.new/software-mansion/TypeGPU/eslint-plugin-typegpu@cc314f0b6258a1a62003450303cfb8cc0e324fc7
https://pkg.pr.new/software-mansion/TypeGPU/tinyest@cc314f0b6258a1a62003450303cfb8cc0e324fc7
https://pkg.pr.new/software-mansion/TypeGPU/tinyest-for-wgsl@cc314f0b6258a1a62003450303cfb8cc0e324fc7
https://pkg.pr.new/software-mansion/TypeGPU/typegpu@cc314f0b6258a1a62003450303cfb8cc0e324fc7
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/gl@cc314f0b6258a1a62003450303cfb8cc0e324fc7
https://pkg.pr.new/software-mansion/TypeGPU/unplugin-typegpu@cc314f0b6258a1a62003450303cfb8cc0e324fc7

benchmark
view benchmark

commit
view commit

@github-actions

github-actions Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

Bundle size comparison (import * as ... in PR vs import * as ... in target):

🟢 Decreased ➖ Unchanged 🔴 Increased ❔ Unknown
0 360 0 0

No notable changes.

If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu.

@github-actions

github-actions Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

Resolution Time Benchmark

---
config:
  themeVariables:
    xyChart:
      plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
  title "Random Branching (🔴 PR | 🔵 main | 🟢 release)"
  x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
  y-axis "time (ms)"
  line [0.70, 1.47, 3.46, 5.01, 6.27, 10.31, 21.13, 25.52]
  line [0.70, 1.52, 3.20, 5.43, 6.57, 11.41, 22.52, 23.21]
  line [0.78, 1.63, 3.36, 6.10, 6.73, 10.39, 21.18, 24.19]
Loading
---
config:
  themeVariables:
    xyChart:
      plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
  title "Linear Recursion (🔴 PR | 🔵 main | 🟢 release)"
  x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
  y-axis "time (ms)"
  line [0.23, 0.37, 0.51, 0.61, 0.84, 0.89, 1.08, 1.16]
  line [0.21, 0.45, 0.55, 0.61, 0.84, 0.86, 1.15, 1.14]
  line [0.23, 0.42, 0.49, 0.61, 0.87, 0.89, 1.06, 1.17]
Loading
---
config:
  themeVariables:
    xyChart:
      plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
  title "Full Tree (🔴 PR | 🔵 main | 🟢 release)"
  x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
  y-axis "time (ms)"
  line [0.87, 2.02, 3.59, 6.03, 12.95, 25.12, 51.34, 105.83]
  line [0.82, 2.19, 3.62, 5.87, 11.68, 26.08, 51.23, 107.84]
  line [0.94, 1.66, 3.02, 5.63, 10.89, 22.32, 46.48, 99.75]
Loading

@reczkok
reczkok force-pushed the feat/geometry-patches branch from 4513b17 to 177de99 Compare October 6, 2026 15:11
@reczkok
reczkok force-pushed the feat/geometry-patches branch from 177de99 to 73218eb Compare October 16, 2026 18:56
@reczkok
reczkok force-pushed the feat/geometry-patches branch 3 times, most recently from 4e3132a to 36cdb51 Compare October 22, 2026 12:44
@reczkok
reczkok marked this pull request as ready for review October 22, 2026 13:00
Copilot AI balanced review requested due to automatic review settings October 22, 2026 13:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Rounded-box radius handling can invert faces for thin dimensions, while fully rounded boxes retain unnecessary collapsed geometry.

2 open issues
What changed in this PR

Adds triangular patch surfaces to the meshes API, separating surface sampling from subdivision. This supports both indexed geometry and direct vertex-shader evaluation.

Changes:

  • Adds icosphere, capsule, and rounded-box patches with tessellated convenience constructors.
  • Introduces triangle subdivision utilities, reusable index prefixes, and spherical interpolation helpers.
  • Adds an interactive example and tests for topology, surface sampling, and shader resolution.
File Description
packages/​typegpu-geometry/​tests/​patches.test.ts Tests subdivision, watertightness, and interpolation specialization.
packages/​typegpu-geometry/​tests/​mesh.test.ts Extends primitive and degenerate-triangle checks.
packages/​typegpu-geometry/​src/​mesh/​triangles.ts Defines patch surfaces and subdivision utilities.
packages/​typegpu-geometry/​src/​mesh/​patchPrimitives.ts Adds tessellated primitive constructors.
packages/​typegpu-geometry/​src/​mesh/​patches/​spherical.ts Implements spherical interpolation helpers.
packages/​typegpu-geometry/​src/​mesh/​patches/​rounded.ts Implements capsule and rounded-box patches.
packages/​typegpu-geometry/​src/​mesh/​patches/​index.ts Exports patch constructors and interpolation helpers.
packages/​typegpu-geometry/​src/​mesh/​patches/​icosphere.ts Implements icosphere patches.
packages/​typegpu-geometry/​src/​mesh/​index.ts Exposes the new mesh APIs.
apps/​typegpu-docs/​tests/​individual-example-tests/​mesh-patches.test.ts Checks example pipeline shader resolution.
apps/​typegpu-docs/​src/​examples/​rendering/​mesh-patches/​meta.json Registers the example metadata.
apps/​typegpu-docs/​src/​examples/​rendering/​mesh-patches/​index.ts Demonstrates adjustable subdivision with shared indices.
apps/​typegpu-docs/​src/​examples/​rendering/​mesh-patches/​index.html Provides the example canvas.

Review effort: Balanced

@pullfrog pullfrog Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

roundedBox silently produces inside-out geometry when radius exceeds half the smallest box dimension, and a majority-degenerate, non-manifold mesh at exactly half. inset = halfSize - radius is never clamped or validated.

( ◦°^°◦) 🏴‍☠️

Ahoy! I plundered the diff o' PR #3014, ran the geometry tests, typecheck and linters, and keelhauled one leaky roundedBox radius that flips the hull inside-out when it be past half the smallest beam. One warnin' be posted below, savvy?

Reviewed changes

  • Patch surfaces — adds icosphere, capsule and roundedBox as shader-evaluated PatchSurface objects under src/mesh/patches/, with spherical interpolation helpers (linear, uniformArea, arc, warp).
  • Triangle tessellation — triangles.ts adds the CPU helpers triangleVertexCount/triangleIndexCount/triangleIndices and the GPU helpers triangleBarycentrics/triangleIndexAt, plus tessellate mapping a linear vertex index onto a triangular grid.
  • Shared index buffer — triangleIndexAt is segment-independent, so one max-segment index buffer works as a prefix at every lower subdivision; the new docs example drives detail from a single buffer while the vertex shader recomputes barycentrics.
  • Patch primitives + example — patchPrimitives.ts tessellates the three shapes with a segments option, plus a new mesh-patches docs example and jsdom example test.

I verified the geometry tests (44 passing), typecheck, lint and formatting locally, and independently checked that the index-prefix property holds for every segment value in [1..32] and that capsule/icosphere stay watertight and correctly wound across the probed parameter ranges. The only issue found is the unclamped radius below.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

( ◦°^°◦) 🏴‍☠️

Ahoy! I boarded the new commit on PR #3014 and found the leak I'd flagged last voyage already plugged — the roundedBox radius be clamped to half the smallest beam, so no more inside-out hulls. Batten down the tests and let her sail.

Reviewed changes

  • Clamped the rounded-box radius — roundedBox now resolves radius as Math.min(maxRadius, halfSize.x, halfSize.y, halfSize.z) (patches/rounded.ts:92) and documents it as "Clamped to half of the smallest dimension". This removes the negative inset that previously mirrored face/edge patches and wound ~55% of triangles inside-out once radius exceeded half the smallest dimension.
  • Added a thin-box winding case — mesh.test.ts gains ['thin rounded box', meshes.roundedBox({ height: 0.05 })], exercising the newly clamped path where inset reaches zero on the smallest axis.

I re-ran the geometry suite (45 passing) and package typecheck, and independently probed five configurations (radius well above min/2, exactly min/2, thin boxes, and a normal case): none produce inside-out triangles anymore. The residual trade-off — collapsing to exactly min/2 leaves zero-area face/edge patches on the smallest axis (e.g. radius: 0.5 on a unit cube yields 576/704 degenerate triangles) — is wasted geometry rather than a rendering defect, and matches the clamp semantics the author chose. Prior review's inline thread is resolved on GitHub.

Pullfrog  | Fix it ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@reczkok
reczkok force-pushed the feat/geometry-patches branch from b83da47 to 7c7b644 Compare October 27, 2026 17:08
@reczkok
reczkok force-pushed the feat/geometry-patches branch 2 times, most recently from 13fb69d to e9de4bc Compare November 1, 2026 14:47
@reczkok
reczkok force-pushed the feat/geometry-patches branch 2 times, most recently from 104e9bf to 3f883fc Compare November 4, 2026 09:21
@reczkok
reczkok force-pushed the feat/geometry-patches branch from 3f883fc to 7a2cd21 Compare November 5, 2026 13:31
@reczkok
reczkok force-pushed the feat/geometry-patches branch from 7a2cd21 to a457593 Compare November 14, 2026 00:25

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

( ◦°^°◦) 🏴‍☠️

Ahoy! I re-scanned the fresh commits on PR #3014, saw the example hand its resizin' duties to the shared auto-resizer, and found nary a leak to swab — she sails clean.

Reviewed changes

  • Ported the docs example to common.attachAutoResizer — mesh-patches now creates the shared auto-resizer with an onResize callback that rebuilds the depth texture and requests a frame, and onCleanup detaches it, replacing the hand-rolled ResizeObserver.
  • Switched the canvas to utility-class sizing — index.html now uses class="w-full h-full" instead of the unused data-fit-to-container attribute, matching the other rendering examples.

I re-ran the geometry suite (5 files / 50 tests) and the mesh-patches example test (1 passed) at a457593a; both pass. The only authored delta since the prior review is these two example files — the rest of the range-diff is base-branch rebase noise.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@reczkok
reczkok force-pushed the feat/geometry-patches branch from a457593 to f14bae5 Compare November 18, 2026 11:54

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

( ◦°^°◦) 🏴‍☠️

Ahoy! I checked the fresh head o' PR #3014 and found it a bare rebase — nary a line o' the PR's own cargo shifted, so I let her sail on with a clean bill.

Reviewed changes

  • Rebased onto an updated feat/geometry-meshes base — no PR-owned file changed between the prior reviewed head a457593a and f14bae57; git diff-tree -r over all 13 PR paths is empty, and every difference between the two heads belongs to the base branch. The previously reviewed roundedBox radius clamp and the attachAutoResizer example port are unchanged.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@reczkok
reczkok force-pushed the feat/geometry-patches branch from f14bae5 to 061fffe Compare November 18, 2026 13:27

@aleksanderkatan aleksanderkatan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks clean!

I didn't understand everyting (like warp function) but I trust

const start = std.intdiv(row * (row + 1), 2);
const corner = index % 3;
if (col <= row) {
if (corner === 0) return start + col;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no block after if

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

Suggested change
if (corner === 0) return start + col;
if (corner === 0) {
return start + col;
}

);

const signatures = shaderCodes.match(/^fn .*(?= \{$)/gm);
expect([...new Set(signatures)].join('\n')).toMatchInlineSnapshot(`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice for human readability!
Could you add a full snapshot in as well? I think of them as a low maintenance canary for wgsl generation changes

@reczkok
reczkok force-pushed the feat/geometry-patches branch from 061fffe to 75e94c7 Compare November 30, 2026 12:24
@iwoplaza

iwoplaza commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Could you add a thumbnail for the Mesh Patches example?

const start = std.intdiv(row * (row + 1), 2);
const corner = index % 3;
if (col <= row) {
if (corner === 0) return start + col;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed

Suggested change
if (corner === 0) return start + col;
if (corner === 0) {
return start + col;
}

Comment on lines +38 to +39
if (row * row > triangle) row -= 1;
else if ((row + 1) * (row + 1) <= triangle) row += 1;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (row * row > triangle) row -= 1;
else if ((row + 1) * (row + 1) <= triangle) row += 1;
if (row * row > triangle) {
row -= 1;
} else if ((row + 1) * (row + 1) <= triangle) {
row += 1;
}

Comment on lines +48 to +50
if (corner === 0) return vertex;
if (corner === 1) return vertex + row + 2;
return vertex + 1;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this reads very nice, maybe let's keep it without the brackets

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

braced it to match the if above and the rest of the file

Comment on lines +24 to +25
if (std.intdiv(row * (row + 1), 2) > vertex) row -= 1;
else if (std.intdiv((row + 1) * (row + 2), 2) <= vertex) row += 1;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (std.intdiv(row * (row + 1), 2) > vertex) row -= 1;
else if (std.intdiv((row + 1) * (row + 2), 2) <= vertex) row += 1;
if (std.intdiv(row * (row + 1), 2) > vertex) {
row -= 1;
} else if (std.intdiv((row + 1) * (row + 2), 2) <= vertex) {
row += 1;
}

Comment on lines +9 to +10
export * from './patchPrimitives.ts';
export * from './triangles.ts';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like in the PR under this one, I'd prefer if we listed what gets exported here. Doing a * export from index.ts files is fine imo, as it is below.

Comment on lines +33 to +34
radius?: number;
interpolate?: (a: d.v3f, b: d.v3f, c: d.v3f, weights: d.v3f) => d.v3f;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
radius?: number;
interpolate?: (a: d.v3f, b: d.v3f, c: d.v3f, weights: d.v3f) => d.v3f;
radius?: number | undefined;
interpolate?: ((a: d.v3f, b: d.v3f, c: d.v3f, weights: d.v3f) => d.v3f) | undefined;

Comment on lines +1 to +2
export * from './icosphere.ts';
export * from './rounded.ts';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would appreciate listing out the exports here 🫡

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these functions deserve a JS doc describing what their purpose and/or different names, right now they're available as meshes.patches.linear and meshes.patches.uniformArea.

Maybe meshes.patches.linearWeightDistribution and meshes.patches.uniformAreaWeightDistribution?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heavy Java vibes

@reczkok
reczkok force-pushed the feat/geometry-patches branch from 75e94c7 to cc314f0 Compare December 4, 2026 14:46

This branch has not been deployed

No deployments
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.

4 participants