wgpu: area measurement tool (A hotkey, BFS coplanar patch + cyan highlight)

Ports Bonsai's AreaMeasurement onto WgpuViewportWindow as a new
WgpuAreaMeasurement class. Each LMB pick resolves to (instance,
triangle), BFS-expands the coplanar patch (dot(normal, seed_normal)
> 0.9999, ~0.81° tolerance), and toggles it in/out of the running set.
Alt+LMB skips BFS for single-triangle accumulate. Connected-components
sweep over the selected set produces one "X.XXXX m²" label per patch
at its area-weighted centroid in world space; HUD shows the running
total + triangle count.

Dependencies layered in:

- WgpuOverlayRenderer.setHighlightTriangles / encodeHighlightTriangles:
  translucent world-space triangle list (cyan @ 0.45 alpha), depth-
  tested but depth-write off so the corner gizmo + labels still sit
  on top.
- WgpuViewportWindow.pickMeshLocalAt: reuses pickSurfaceAt for the
  world hit, then inverts the instance's composed transform to express
  it in mesh-local space — what the BFS needs. Uses the live map key
  (`mid`) rather than InstanceCpu.model_id, which is whatever the GL
  streamer wrote at sidecar-write time and goes stale across sessions.
- WgpuViewportWindow.readbackMeshTriangles: CPU mesh shadow lookup.
  The shadow itself is populated during the same dequant pass that
  computes mesh-local volume — applyCachedModel for full loads and
  applyStreamedChunk for streaming, so the BFS has data the moment
  the user can pick it.

WgpuModelGpuData gains a MeshTriangles vector indexed by mesh_id;
doubles per-vertex CPU memory (12 B/vert) but skips wgpu mapAsync
plumbing for now. Bounds-check at pick time gracefully no-ops when a
stale sidecar field is out of range.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Dion Moult
2026-05-31 22:12:26 +10:00
parent 0774398d4e
commit 8761f9ac46
7 changed files with 977 additions and 39 deletions
+28
View File
@@ -97,6 +97,18 @@ public:
const WgpuOverlayFrame& f,
const std::vector<WgpuSectionPlane>& planes);
// Replace the highlight-triangle list. `world_xyz` is 3 floats per
// vertex, 3 vertices per triangle, in world space (post-composed-
// transform). Empty disables the overlay. Color is RGBA in [0, 1] —
// alpha < 1 gives the translucent patch shading the Area tool uses.
// Drawn inside the main MSAA pass so depth-test hides patches
// behind closer geometry; depth-write stays off so later overlays
// can still draw over the highlight.
void setHighlightTriangles(const std::vector<float>& world_xyz,
float r, float g, float b, float a);
void encodeHighlightTriangles(WGPURenderPassEncoder pass,
const WgpuOverlayFrame& f);
// One stylistic group of world-space line segments. Mirrors GL
// OverlayRenderer::LineGroup so callers can target either backend
// with one struct.
@@ -193,6 +205,7 @@ private:
bool buildMarquee();
bool buildOverlayLines();
bool buildOverlayPoints();
bool buildHighlightTriangles();
bool buildLabels();
// Rasterise a single string at `font_pt` with dark-grey padded
@@ -290,6 +303,21 @@ private:
WGPUBindGroup overlay_point_bind_group_ = nullptr;
uint32_t overlay_point_vertex_count_ = 0;
// ---- Highlight triangles (translucent world-space triangle list) ----
// One pipeline + one uniform buffer (view_proj + RGBA tint). Vertex
// buffer grows on demand to fit the current set; empty set ⇒ encode
// is a no-op.
WGPUShaderModule highlight_shader_module_ = nullptr;
WGPUBindGroupLayout highlight_bgl_ = nullptr;
WGPUPipelineLayout highlight_pipeline_layout_ = nullptr;
WGPURenderPipeline highlight_pipeline_ = nullptr;
WGPUBuffer highlight_vertex_buffer_ = nullptr;
uint64_t highlight_vertex_capacity_ = 0;
WGPUBuffer highlight_uniform_buffer_ = nullptr;
WGPUBindGroup highlight_bind_group_ = nullptr;
uint32_t highlight_vertex_count_ = 0;
float highlight_color_[4] = {0, 0, 0, 0};
// ---- Labels + HUD text (textured quads, cached by content) ----
// One QPainter-rasterised QImage per unique text string, uploaded as
// an RGBA8 texture and re-used across frames. Per-frame work is