@zkmake/three-meter 0.7.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,29 +1,38 @@
1
1
  # @zkmake/three-meter
2
2
 
3
- Frame metrics for a three.js renderer: FPS, CPU ms, GPU ms, draw calls, render passes, triangles,
4
- geometries, textures and programs, with a small dockable HUD. Zero dependencies. Works with
5
- `WebGLRenderer` and `WebGPURenderer`, with or without React.
3
+ Frame metrics for a three.js renderer, with a small dockable HUD: FPS, CPU and GPU time, stutter
4
+ (1% low, p99 frame time, hitches), draw calls, render passes, triangles, and the geometries,
5
+ textures and shaders in GPU memory. Zero dependencies. Works with `WebGLRenderer` and
6
+ `WebGPURenderer`, with or without React.
6
7
 
7
8
  ```sh
8
9
  bun add -d @zkmake/three-meter # or npm i -D / pnpm add -D
9
10
  ```
10
11
 
11
12
  Live demo: [three-meter.pages.dev](https://three-meter.pages.dev/), with a vanilla three and a
12
- React Three Fiber take on the same scene. Add `?webgpu` for the WebGPU renderer and `?count=5000` to
13
- load the scene up.
13
+ React Three Fiber take on the same scene. Add `?webgpu` for the WebGPU renderer and `?count=30000` to
14
+ load the scene up past the demo's triangle budget.
14
15
 
15
16
  <p align="center">
16
17
  <img
17
18
  src="https://raw.githubusercontent.com/zkmake/three-meter/main/docs/hud-full.png"
18
19
  width="320"
19
- alt="The HUD in full mode, dark theme. Option rows for theme (light, system, dark), dim on leave, explain metrics and a copy-report button; FPS, CPU and GPU sparklines with live values; then one row per metric, each with an icon, its value and a checkbox choosing whether the compact card shows it (FPS, calls, CPU, GPU, 1% low, frame p99, hitches, triangles, lines, points, render passes, geometries, textures, shaders); and a footer with the three-meter version, the three revision, a WebGL2 backend badge and the GPU name. Two discs beside it: a drag grip and the compact/full toggle."
20
+ alt="The HUD in full mode, dark theme. Option rows for theme (light, system, dark), explain metrics and a copy-report button; FPS, CPU and GPU sparklines with live values, the FPS graph showing a dashed budget line; then one row per metric, each with an icon, its value and a checkbox choosing whether the compact card shows it (FPS, calls, CPU, GPU, 1% low, frame p99, hitches, triangles, lines, points, render passes, geometries, textures, shaders), with the triangle count in amber for being over its budget; a top costs toggle; and a footer with the three-meter version, the three revision, a WebGL2 backend badge and the GPU name. Three discs beside it: a drag grip, the compact/full toggle and dim on leave."
20
21
  />
21
22
  </p>
22
23
 
23
- The sampler and the card are separate components, so toggling the HUD never remounts your canvas.
24
- GPU time is measured on WebGPU as well as WebGL. The counters are draw calls, render passes and
25
- resource counts, which is what you watch when a scene is instanced. The card docks to a screen edge,
26
- remembers where you put it, and hides its controls until the pointer comes near.
24
+ - **Spot problems at a glance.** Values past their budget turn amber, and every metric explains
25
+ itself on hover, so you don't need to know what a p99 is to see that one is bad.
26
+ - **See stutter, not just averages.** 1% low, p99 frame time and hitch counts catch the frame that
27
+ drops every few seconds while the FPS average still reads 60.
28
+ - **Real GPU time** from timer queries on WebGPU, and on WebGL in Chrome and Edge, not an estimate
29
+ from frame intervals.
30
+ - **Find what's expensive.** A breakdown of draw calls and triangles by mesh and material shows
31
+ which objects to instance or merge.
32
+ - **One-click bug reports.** Copy versions, backend, GPU and the current numbers as Markdown.
33
+ - **Stays out of the way.** The sampler and the card are separate components, so toggling the HUD
34
+ never remounts your canvas. The card docks to a screen edge, remembers where you put it, and hides
35
+ its controls until the pointer comes near; a third disc there dims it when the pointer leaves.
27
36
 
28
37
  ## Entry points
29
38
 
@@ -90,66 +99,34 @@ Canvas is its own React root. To run two canvases on one page, pass the same `st
90
99
  | `injectStyles` | `true` | Append the stylesheet once per document. |
91
100
  | `refreshHz` | `10` | Repaint rate. |
92
101
  | `budgets` | timing defaults | Limits past which a value turns amber. See [Budgets](#budgets). |
102
+ | `label` | `"Performance"` | Accessible name of the panel. |
93
103
 
94
104
  `PerformanceMonitor` and `PerfSampler` take `trackGPU` (default true), `gpuQueryPoolSize` (default 5),
95
105
  `historySize` (default 120) and `frameStatsSize` (default 1000).
96
106
 
97
- ## Theme
98
-
99
- The HUD ships a dark and a light palette. Two layers decide which shows:
100
-
101
- 1. **The panel's own pick.** The full view has a light / system / dark row. A pick there is kept
102
- with the other settings under `storageKey` and wins while set.
103
- 2. **Your `theme` option.** `dark`, `light`, or `system` (the default), which follows the OS and
104
- switches when it does. Applies whenever the person using the HUD hasn't picked anything.
105
-
106
- Change your layer later from the handle, or from the `PerfHud` prop in React, which applies without
107
- remounting:
108
-
109
- ```ts
110
- const hud = mountPerfHud(monitor, { theme: "system" });
111
- hud.setTheme("light"); // your layer: "dark" | "light" | "system"
112
- hud.getTheme(); // your layer, as asked for
113
- hud.settings.theme; // the panel's pick, or null
114
- hud.settings.setTheme(null); // clear the pick so your layer applies again
115
- hud.theme.effective; // whichever layer is in force
116
- hud.theme.resolved; // "dark" | "light", what is on screen right now
117
- hud.theme.subscribe(() => syncMyPageWith(hud.theme.resolved));
118
- ```
119
-
120
- The resolved theme lands on `data-theme` of `.perf-hud` and `.perf-monitor`. To restyle either
121
- palette, override the `--perf-*` custom properties (`bg`, `fg`, `fg-dim`, `muted`, `row`,
122
- `border`, `accent`, `warn`, `shadow`) on those selectors. `warn` is the over-budget amber.
107
+ ## Reading the panel
123
108
 
124
- ## GPU timing
109
+ Every row has an icon, its value, and a checkbox on the right that puts it in the compact HUD. Hover
110
+ a label for a one-line explanation of the metric and what a bad reading means, or tick **explain
111
+ metrics** to show them all under the rows (also works on touch).
125
112
 
126
- On WebGL2 the monitor uses `EXT_disjoint_timer_query_webgl2`, which Chrome and Edge expose. Safari
127
- and Firefox don't, so the GPU row shows `—` and `sample.gpu.available` is `false`.
113
+ ## Stutter
128
114
 
129
- On WebGPU, construct the renderer with `trackTimestamp: true`. Timings resolve a frame or two late.
115
+ Average FPS hides a hitch every few seconds. Three rows in the full view catch it, and any of them
116
+ can be ticked into the compact HUD:
130
117
 
131
- ## Environment
118
+ - **1% low**: mean FPS across the slowest 1% of frames.
119
+ - **Frame p99**: 99% of frames finish within this many ms.
120
+ - **Hitches**: frames over twice the median frame time.
132
121
 
133
- The full view's footer has two rows: the three-meter version (linked to its release notes) and the
134
- three revision, then the backend and the GPU name. An amber `WebGL2 fallback` badge means
135
- `WebGPURenderer` couldn't get WebGPU. Its checkbox (off by default) shows the footer in the compact
136
- HUD too. The same data is on the monitor:
122
+ They cover the last `frameStatsSize` frames (1000 by default, about 16 s at 60 Hz). Gaps over a
123
+ second, like a hidden tab, are left out. Read them with `monitor.getFrameStats()`:
137
124
 
138
125
  ```ts
139
- monitor.getEnvironment();
140
- // { three: "186", backend: "webgpu", fallback: false, gpu: "Apple metal-3" }
126
+ monitor.getFrameStats();
127
+ // { frames: 1000, lowFps: 97.6, p99Ms: 10.2, hitches: 3 }
141
128
  ```
142
129
 
143
- `backend` is `null` until `WebGPURenderer.init()` settles. `gpu` comes from WebGPU's adapter info or
144
- WebGL's unmasked renderer string, and is `null` where the browser hides it. Chrome's WebGPU gives
145
- vendor and architecture, not the model.
146
-
147
- ## Reading the panel
148
-
149
- Every row has an icon, its value, and a checkbox on the right that puts it in the compact HUD. Hover
150
- a label for a one-line explanation of the metric and what a bad reading means, or tick **explain
151
- metrics** to show them all under the rows (also works on touch).
152
-
153
130
  ## Budgets
154
131
 
155
132
  A value past its budget turns amber in the full view and the compact HUD, and its tooltip names the
@@ -167,47 +144,108 @@ hud.setBudgets({ gpu: null }); // null drops a default; false drops them all
167
144
 
168
145
  `PerfHud` takes the same `budgets` prop and applies changes live.
169
146
 
170
- ## Stutter
147
+ ## Top costs
171
148
 
172
- Average FPS hides a hitch every few seconds. Three rows in the full view catch it, and any of them
173
- can be ticked into the compact HUD:
149
+ The HUD tells you there are 400 draw calls; **top costs** tells you which objects they come from.
150
+ Tick it in the full view for the five biggest costs in the main render pass, grouped by **meshes** or
151
+ **materials**, sorted by draw calls or triangles. Click a row to log its three objects to the
152
+ console.
174
153
 
175
- - **1% low**: mean FPS across the slowest 1% of frames.
176
- - **Frame p99**: 99% of frames finish within this many ms.
177
- - **Hitches**: frames over twice the median frame time.
154
+ ```text
155
+ calls tris
156
+ tree ×300 300 9,600 ← 300 separate meshes: instance or merge them
157
+ house 6 12 ← one draw per material in the array
158
+ dome 2 7,936 ← transparent and double-sided draws twice
159
+ grass ×20,000 1 40,000 ← already instanced
160
+ ```
178
161
 
179
- They cover the last `frameStatsSize` frames (1000 by default, about 16 s at 60 Hz). Gaps over a
180
- second, like a hidden tab, are left out. Read them with `monitor.getFrameStats()`:
162
+ Meshes with the same name and geometry share a row, so unmerged copies stand out. The numbers are
163
+ estimated from the scene graph the way three walks it: hidden objects, other layers and anything
164
+ outside the camera's frustum are left out. Against three's own `renderer.info` on a test scene they
165
+ matched exactly. Shadow maps and post-processing passes aren't included; the main pass is the render
166
+ call that drew the most, so a full-screen quad rendered last doesn't stand in for the scene. It
167
+ walks the scene only while open, twice a second.
181
168
 
182
169
  ```ts
183
- monitor.getFrameStats();
184
- // { frames: 1000, lowFps: 97.6, p99Ms: 10.2, hitches: 3 }
170
+ monitor.getSceneCost();
171
+ // { calls: 309, triangles: 57548, meshes: [{ label: "tree", calls: 300, … }, …], materials: […] }
185
172
  ```
186
173
 
187
174
  ## Bug reports
188
175
 
189
176
  The **report** row's copy button puts a Markdown snapshot on the clipboard: versions, backend, GPU,
190
- the current metrics, the stutter stats, the viewport and the user agent. Paste it into an issue.
177
+ the current metrics, the stutter stats, the top three meshes, the viewport and the user agent. Paste it into an issue.
191
178
  `formatReport(monitor)` from `@zkmake/three-meter/ui` returns the same text.
192
179
 
193
180
  ```text
194
- three-meter v0.5.0 · three r186 · WebGL2 · Apple M4 Max
181
+ three-meter v0.7.0 · three r186 · WebGL2 · Apple M4 Max
195
182
  FPS 120 · 1% low 98 · p99 10.2 ms · hitches 3 / 1,000 frames
196
183
  CPU 0.2 ms · GPU 0.7 ms
197
184
  Calls 1 · passes 1 · triangles 24,000 · lines 0 · points 0
198
185
  Geometries 1 · textures 1 · shaders 1
186
+ Top: tree ×300 (300 calls, 9,600 tris) · house (6 calls, 12 tris) · dome (2 calls, 7,936 tris)
199
187
  Viewport 1280×720 @2x
200
188
  Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) …
201
189
  ```
202
190
 
191
+ ## Environment
192
+
193
+ The full view's footer has two rows: the three-meter version (linked to its release notes) and the
194
+ three revision, then the backend and the GPU name. An amber `WebGL2 fallback` badge means
195
+ `WebGPURenderer` couldn't get WebGPU. Its checkbox (off by default) shows the footer in the compact
196
+ HUD too. The same data is on the monitor:
197
+
198
+ ```ts
199
+ monitor.getEnvironment();
200
+ // { three: "186", backend: "webgpu", fallback: false, gpu: "Apple metal-3" }
201
+ ```
202
+
203
+ `backend` is `null` until `WebGPURenderer.init()` settles. `gpu` comes from WebGPU's adapter info or
204
+ WebGL's unmasked renderer string, and is `null` where the browser hides it. Chrome's WebGPU gives
205
+ vendor and architecture, not the model.
206
+
207
+ ## GPU timing
208
+
209
+ On WebGL2 the monitor uses `EXT_disjoint_timer_query_webgl2`, which Chrome and Edge expose. Safari
210
+ and Firefox don't, so the GPU row shows `—` and `sample.gpu.available` is `false`.
211
+
212
+ On WebGPU, construct the renderer with `trackTimestamp: true`. Timings resolve a frame or two late.
213
+
214
+ ## Theme
215
+
216
+ The HUD ships a dark and a light palette. Two layers decide which shows:
217
+
218
+ 1. **The panel's own pick.** The full view has a light / system / dark row. A pick there is kept
219
+ with the other settings under `storageKey` and wins while set.
220
+ 2. **Your `theme` option.** `dark`, `light`, or `system` (the default), which follows the OS and
221
+ switches when it does. Applies whenever the person using the HUD hasn't picked anything.
222
+
223
+ Change your layer later from the handle, or from the `PerfHud` prop in React, which applies without
224
+ remounting:
225
+
226
+ ```ts
227
+ const hud = mountPerfHud(monitor, { theme: "system" });
228
+ hud.setTheme("light"); // your layer: "dark" | "light" | "system"
229
+ hud.getTheme(); // your layer, as asked for
230
+ hud.settings.theme; // the panel's pick, or null
231
+ hud.settings.setTheme(null); // clear the pick so your layer applies again
232
+ hud.theme.effective; // whichever layer is in force
233
+ hud.theme.resolved; // "dark" | "light", what is on screen right now
234
+ hud.theme.subscribe(() => syncMyPageWith(hud.theme.resolved));
235
+ ```
236
+
237
+ The resolved theme lands on `data-theme` of `.perf-hud` and `.perf-monitor`. To restyle either
238
+ palette, override the `--perf-*` custom properties (`bg`, `fg`, `fg-dim`, `muted`, `row`,
239
+ `border`, `accent`, `warn`, `shadow`) on those selectors. `warn` is the over-budget amber.
240
+
203
241
  ## Examples
204
242
 
205
243
  [`examples/site`](examples/site) is what runs at [three-meter.pages.dev](https://three-meter.pages.dev/):
206
244
  one Vite app with both integrations of the same scene, swapped from the header.
207
245
  [`src/demos/vanilla.ts`](examples/site/src/demos/vanilla.ts) is plain three with `mountPerfHud`;
208
246
  [`src/demos/r3f.tsx`](examples/site/src/demos/r3f.tsx) is React Three Fiber with `PerfSampler` and
209
- `PerfHud`. `?r3f` opens on Fiber, `?webgpu` uses `WebGPURenderer` in either, `?count=` scales the
210
- scene. The header's theme toggle sets the page theme and the HUD's `theme` layer together; the row
247
+ `PerfHud`. Both set a 250K triangle budget on top of the timing defaults. `?r3f` opens on Fiber,
248
+ `?webgpu` uses `WebGPURenderer` in either, `?count=` scales the scene. The header's theme toggle sets the page theme and the HUD's `theme` layer together; the row
211
249
  inside the panel overrides the HUD alone. Run `bun run dev` inside it.
212
250
 
213
251
  ## Shipping it
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { a as PerfRenderer, c as RenderCounts, d as TimingMetric, i as GpuTiming, l as ResourceCounts, n as Environment, o as PerformanceMonitorOptions, r as FrameStats, s as RenderBackend, t as PerformanceMonitor, u as Sample } from "./performance-monitor-CBN05_BC.js";
1
+ import { a as GpuTiming, c as RenderBackend, d as Sample, f as SceneCost, i as FrameStats, l as RenderCounts, n as CostEntry, o as PerfRenderer, p as TimingMetric, r as Environment, s as PerformanceMonitorOptions, t as PerformanceMonitor, u as ResourceCounts } from "./performance-monitor-DgHxeQ2X.js";
2
2
  //#region src/core/ring-buffer.d.ts
3
3
  /**
4
4
  * Fixed-capacity numeric ring buffer. Backs the per-frame timing history the
@@ -32,5 +32,5 @@ type AnimationLoop<Args extends unknown[]> = (...args: Args) => void;
32
32
  */
33
33
  declare function wrapAnimationLoop<Args extends unknown[]>(monitor: PerformanceMonitor, loop: AnimationLoop<Args>): AnimationLoop<Args>;
34
34
  //#endregion
35
- export { type Environment, type FrameStats, type GpuTiming, type PerfRenderer, PerformanceMonitor, type PerformanceMonitorOptions, type RenderBackend, type RenderCounts, type ResourceCounts, RingBuffer, type Sample, type TimingMetric, wrapAnimationLoop };
35
+ export { type CostEntry, type Environment, type FrameStats, type GpuTiming, type PerfRenderer, PerformanceMonitor, type PerformanceMonitorOptions, type RenderBackend, type RenderCounts, type ResourceCounts, RingBuffer, type Sample, type SceneCost, type TimingMetric, wrapAnimationLoop };
36
36
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { n as RingBuffer, t as PerformanceMonitor } from "./performance-monitor-yxsxs57v.js";
1
+ import { n as RingBuffer, t as PerformanceMonitor } from "./performance-monitor-DsBp13ts.js";
2
2
  //#region src/core/wrap-animation-loop.ts
3
3
  /**
4
4
  * Wrap a `renderer.setAnimationLoop` callback so each tick is one frame:
@@ -1,4 +1,4 @@
1
- import { d as TimingMetric, t as PerformanceMonitor } from "./performance-monitor-CBN05_BC.js";
1
+ import { p as TimingMetric, t as PerformanceMonitor } from "./performance-monitor-DgHxeQ2X.js";
2
2
  //#region src/ui/budgets.d.ts
3
3
  /**
4
4
  * Budgets: the value each metric should stay within. A reading past its
@@ -188,7 +188,6 @@ declare class PerformanceView {
188
188
  private readonly graphCheckboxes;
189
189
  private readonly statValueEls;
190
190
  private readonly statCheckboxes;
191
- private dimCheckbox;
192
191
  private infoCheckbox;
193
192
  private readonly themeRadios;
194
193
  private footerThreeEl;
@@ -205,6 +204,14 @@ declare class PerformanceView {
205
204
  private lastPaintAt;
206
205
  private readonly resizeObserver;
207
206
  private readonly unsubscribe;
207
+ private costsOpen;
208
+ private costGroup;
209
+ private costSort;
210
+ private lastCostsAt;
211
+ private costsBodyEl;
212
+ private costsListEl;
213
+ private readonly costGroupRadios;
214
+ private readonly costSortButtons;
208
215
  constructor(options: PerformanceViewOptions);
209
216
  start(): void;
210
217
  stop(): void;
@@ -215,6 +222,8 @@ declare class PerformanceView {
215
222
  dispose(): void;
216
223
  private frame;
217
224
  private render;
225
+ private renderCosts;
226
+ private costsNote;
218
227
  /** The monitor caches the environment once its backend settles; until then this re-reads. */
219
228
  private renderFooter;
220
229
  private renderHud;
@@ -229,6 +238,8 @@ declare class PerformanceView {
229
238
  private onSelectionChanged;
230
239
  /** Repaint the palette and mark the radio matching what applies (pick, else consumer mode). */
231
240
  private onThemeChanged;
241
+ /** "top costs": a toggle row, then meshes / materials and a sortable top five. */
242
+ private buildCosts;
232
243
  private buildReportRow;
233
244
  private buildCheckbox;
234
245
  private applyInfoClass;
@@ -290,4 +301,4 @@ type PerfHudHandle = {
290
301
  declare const mountPerfHud: (monitor: PerformanceMonitor, options?: MountPerfHudOptions) => PerfHudHandle;
291
302
  //#endregion
292
303
  export { dockPanel as C, resolveBudgets as D, ResolvedBudgets as E, ScreenEdge as S, Budgets as T, DefaultPlacement as _, PerformanceViewMode as a, DockPanelOptions as b, HudDefaults as c, HudSettingsOptions as d, HudTheme as f, isThemeMode as g, ThemeMode as h, PerformanceView as i, HudSelection as l, THEME_MODES as m, PerfHudHandle as n, PerformanceViewOptions as o, ResolvedTheme as p, mountPerfHud as r, DEFAULT_STORAGE_KEY as s, MountPerfHudOptions as t, HudSettings as u, DockAlign as v, BudgetKey as w, Placement as x, DockHandle as y };
293
- //# sourceMappingURL=mount-perf-hud-6LeJ93Jg.d.ts.map
304
+ //# sourceMappingURL=mount-perf-hud-BKpk62Pp.d.ts.map