Skip to content

[Preview] Triangles and debug pages#13

Open
apresmoi wants to merge 5 commits intoLayoutitStudio:mainfrom
apresmoi:triangles-and-debug-pages
Open

[Preview] Triangles and debug pages#13
apresmoi wants to merge 5 commits intoLayoutitStudio:mainfrom
apresmoi:triangles-and-debug-pages

Conversation

@apresmoi
Copy link
Copy Markdown
Contributor

@apresmoi apresmoi commented May 3, 2026

Hey! This PR adds triangle / polygon shapes to voxcss, plus the loaders and tooling needed to actually use them.

What's new in voxcss core

  • shape: "triangle" and shape: "polygon" — both render through the same SVG-on-matrix3d renderer (triangle is the strict 3-vertex case, polygon accepts any N ≥ 3 coplanar vertices). Backface culling is automatic via backface-visibility: hidden on each face's outward normal — no per-frame JS.
  • Bbox auto-derivation. Voxel stays strict internally, but the public input type InputVoxel lets you write { shape: "triangle", vertices: [...], color } without passing x/y/z/x2/y2/z2. A new normalizeVoxels step at scene-context ingress fills them in.
  • mergeVoxels: "poly" — new merge mode that walks the triangle adjacency graph and consolidates coplanar same-color triangles into N-vertex polygons (sphere / dodecahedron meshes shrink ~50% in DOM count without losing geometry).
  • parseObj, parseMtl, parseGltf — runtime loaders for .obj (with companion .mtl for proper Kd colors) and .glb (full glTF 2.0 with node TRS traversal, baseColorFactor materials, configurable up-axis). Output is InputVoxel[] you feed straight into <VoxScene>.
  • DirectionalLight — opt-in directionalLight prop on <VoxScene> that drives per-triangle Lambert shading. Channel-wise, so a warm light on a blue surface gives proper tinted highlights, not just brightness scaling. Configurable direction + ambient strength + light/ambient colors.

Internal /debug pages (not committed for end-users)

Built a small debug shell under /debug/* with a portal-based layout (DebugLayout + DebugSection + DebugScene), shared control primitives (Pills, Slider, Select, Checkbox), and live FPS / DOM / camera-rotation stats. Pages:

  • /debug/sphere — voxelize a sphere as cubes (with merge modes) or geodesic triangles (with subdivision + poly-merge), side-by-side voxcss + canvas reference.
  • /debug/platonic — all 5 platonic solids + cuboctahedron, render as triangles or native polygons.
  • /debug/triangle-editor — interactive vertex editor for arbitrary triangles, with copy-as-JSON.
  • /debug/spans — a hand-crafted scene of multi-cell x2/y2/z2 voxels (cubes, slabs, columns, ramps, wedges, spikes, 4-spike pyramid) for testing merge-mode behavior on shape voxels.
  • /debug/meshes — gallery-style picker for the OBJ/GLB samples in public/gallery/. Hooked into a unified useObjModel that handles both formats + optional .mtl. Includes mesh decimation (vertex clustering, shortest-edge collapse, QEM) and the lighting controls.

Other things that landed along the way

  • Wedge polygons in the canvas reference (voxelToPolygons for wedge was missing — was rendering as cube).
  • Multi-cell shape merge fixmergeVoxels was z-slicing every multi-cell-tall voxel, which broke ramps/wedges/spikes/triangles. Now only cubes get sliced.
  • Triangle z-stacking — triangles opt out of isCovered because the bbox-based check over-occludes sparse surface meshes.

The bulk of the diff is the bundled public/gallery/obj/*.obj + gallery/glb/tree.glb test assets — the actual core/react/html source changes are small and mostly additive.

apresmoi added 5 commits May 3, 2026 20:52
Adds a new "triangle" shape that renders an arbitrary planar polygon
(N >= 3 vertices) as a single matrix3d-transformed SVG. Includes a
mergePolygons pass that consolidates coplanar same-color adjacent
triangles into N-vertex polygons, plus a parseObj loader for
.obj imports. Bundles the existing OBJ samples under
public/gallery/obj/ and an internal /debug/* set of pages
(sphere, platonic, triangle-editor, spans, obj) for testing.
- Add `polygon` shape (alongside `triangle`) — same renderer, accepts N >= 3 vertices.
- Drop bbox requirement for triangle/polygon: introduce `InputVoxel` (loose,
  bbox optional) and `normalizeVoxels` that derives x/y/z/x2/y2/z2 from
  vertices at scene-context ingress. Strict `Voxel` stays the internal type.
- Add `parseGltf` (.glb) loader to core — reads positions/indices/material
  baseColorFactor, applies node TRS transforms, supports `upAxis: "y" | "z"`.
- Add `parseMtl` companion loader for OBJ material files; `useObjModel`
  fetches and merges colors when `mtlUrl` is provided.
- Add `DirectionalLight` to GridContext + `directionalLight` prop on VoxScene.
  Triangle/polygon renderer applies channel-wise Lambert shading with
  configurable direction, ambient strength, light/ambient colors.
- Rename debug page Obj → Meshes, route to /debug/meshes; add Light controls
  (azimuth/elevation/ambient/colors with enable toggles).
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