@zkmake/three-meter 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Zubin Khavarian
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # @zkmake/three-meter
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.
6
+
7
+ ```sh
8
+ bun add -d @zkmake/three-meter # or npm i -D / pnpm add -D
9
+ ```
10
+
11
+ The sampler and the card are separate components, so toggling the HUD never remounts your canvas.
12
+ GPU time is measured on WebGPU as well as WebGL. The counters are draw calls, render passes and
13
+ resource counts, which is what you watch when a scene is instanced. The card docks to a screen edge,
14
+ remembers where you put it, and hides its controls until the pointer comes near.
15
+
16
+ ## Entry points
17
+
18
+ | Import | What it is |
19
+ | --------------------------- | ---------------------------------------------------------------------------- |
20
+ | `@zkmake/three-meter` | `PerformanceMonitor`. `begin()` and `end()` bracket a frame. No DOM. |
21
+ | `@zkmake/three-meter/ui` | `mountPerfHud(monitor, options)`, the dockable card. Vanilla DOM. |
22
+ | `@zkmake/three-meter/react` | `PerfSampler` inside `<Canvas>`, `PerfHud` outside it. Peers: react and r3f. |
23
+
24
+ ## Vanilla three
25
+
26
+ ```ts
27
+ import { PerformanceMonitor, wrapAnimationLoop } from "@zkmake/three-meter";
28
+ import { mountPerfHud } from "@zkmake/three-meter/ui";
29
+
30
+ const monitor = new PerformanceMonitor({ renderer });
31
+ const hud = mountPerfHud(monitor); // appends to document.body, docks left-centre
32
+
33
+ renderer.setAnimationLoop(
34
+ wrapAnimationLoop(monitor, () => {
35
+ update();
36
+ renderer.render(scene, camera);
37
+ }),
38
+ );
39
+
40
+ // later
41
+ hud.dispose();
42
+ monitor.dispose();
43
+ ```
44
+
45
+ If you run your own loop, call `monitor.begin()` before the frame's work and `monitor.end()` after
46
+ the render.
47
+
48
+ ## React Three Fiber
49
+
50
+ ```tsx
51
+ import { Canvas } from "@react-three/fiber";
52
+ import { PerfHud, PerfSampler } from "@zkmake/three-meter/react";
53
+
54
+ <>
55
+ <Canvas>
56
+ <PerfSampler />
57
+ {/* scene */}
58
+ </Canvas>
59
+ <PerfHud />
60
+ </>;
61
+ ```
62
+
63
+ `PerfHud` must sit outside `<Canvas>`, because Fiber treats HTML under it as three objects. The two
64
+ components find each other through a small shared store. A React context can't do this, since the
65
+ Canvas is its own React root. To run two canvases on one page, pass the same `store` prop to both.
66
+
67
+ ## Options
68
+
69
+ `mountPerfHud` and `PerfHud` take:
70
+
71
+ | Option | Default | Meaning |
72
+ | ------------------ | ------------------ | -------------------------------------------------------------------------------------------- |
73
+ | `mode` | `"compact"` | `compact` is the card. `full` is the checkbox list that configures it. |
74
+ | `defaultPlacement` | `{ edge: "left" }` | First-visit dock, as `edge` plus `align` of `start`, `center` or `end`. A drag overrides it. |
75
+ | `storageKey` | `three-meter` | localStorage key for the selection and the dock. `null` disables persistence. |
76
+ | `parent` | `document.body` | Where the host element is appended. |
77
+ | `injectStyles` | `true` | Append the stylesheet once per document. |
78
+ | `refreshHz` | `10` | Repaint rate. |
79
+
80
+ `PerformanceMonitor` and `PerfSampler` take `trackGPU` (default true), `gpuQueryPoolSize` (default 5)
81
+ and `historySize` (default 120).
82
+
83
+ ## GPU timing
84
+
85
+ On WebGL2 the monitor uses `EXT_disjoint_timer_query_webgl2`, which Chrome and Edge expose. Safari
86
+ and Firefox don't, so the GPU row shows `—` and `sample.gpu.available` is `false`.
87
+
88
+ On WebGPU, construct the renderer with `trackTimestamp: true`. Timings resolve a frame or two late.
89
+
90
+ ## Examples
91
+
92
+ [`examples/vanilla`](examples/vanilla) is plain three. Add `?webgpu` to use `WebGPURenderer` and
93
+ `?count=` to scale the scene. [`examples/r3f`](examples/r3f) is the React Three Fiber version. Each
94
+ is a Vite app. Run `bun run dev` inside it.
95
+
96
+ ## Shipping it
97
+
98
+ Styles inject at runtime by default. To link them instead, pass `injectStyles: false` and import
99
+ `@zkmake/three-meter/styles.css`.
100
+
101
+ The package never reads `NODE_ENV` or `import.meta.env`. Whether the HUD exists in production is
102
+ your call. Gate the import in the app with a `?debug=` query, a build flag or a dynamic `import()`.
103
+
104
+ ## Contract
105
+
106
+ While a monitor is attached, `renderer.info.autoReset` is off and `begin()` resets `info`. One
107
+ monitor per renderer. A second throws. `dispose()` restores everything. Theme with the `--perf-*`
108
+ custom properties on `.perf-monitor`.
109
+
110
+ ## Contributing
111
+
112
+ See [CONTRIBUTING.md](CONTRIBUTING.md). Changes ship with a changeset.
113
+
114
+ ## License
115
+
116
+ MIT
@@ -0,0 +1,34 @@
1
+ import { a as RenderCounts, c as TimingMetric, i as PerformanceMonitorOptions, n as GpuTiming, o as ResourceCounts, r as PerfRenderer, s as Sample, t as PerformanceMonitor } from "./performance-monitor-WGLWbxOZ.js";
2
+ //#region src/core/ring-buffer.d.ts
3
+ /**
4
+ * Fixed-capacity numeric ring buffer. Backs the per-frame timing history the
5
+ * view reads to draw sparklines. Push is O(1); `toArray()` returns values in
6
+ * chronological (oldest → newest) order.
7
+ */
8
+ declare class RingBuffer {
9
+ private readonly data;
10
+ private readonly capacity;
11
+ private head;
12
+ private count;
13
+ constructor(capacity: number);
14
+ push(value: number): void;
15
+ /** Values oldest → newest. Length grows up to capacity as samples accumulate. */
16
+ toArray(): number[];
17
+ clear(): void;
18
+ }
19
+ //#endregion
20
+ //#region src/core/wrap-animation-loop.d.ts
21
+ type AnimationLoop<Args extends unknown[]> = (...args: Args) => void;
22
+ /**
23
+ * Wrap a `renderer.setAnimationLoop` callback so each tick closes the previous
24
+ * frame and opens the next, the same bracketing the R3F sampler does. CPU time
25
+ * therefore spans the whole tick (your update code plus the render dispatch).
26
+ *
27
+ * ```ts
28
+ * renderer.setAnimationLoop(wrapAnimationLoop(monitor, (time) => { … renderer.render(scene, camera); }));
29
+ * ```
30
+ */
31
+ declare function wrapAnimationLoop<Args extends unknown[]>(monitor: PerformanceMonitor, loop: AnimationLoop<Args>): AnimationLoop<Args>;
32
+ //#endregion
33
+ export { type GpuTiming, type PerfRenderer, PerformanceMonitor, type PerformanceMonitorOptions, type RenderCounts, type ResourceCounts, RingBuffer, type Sample, type TimingMetric, wrapAnimationLoop };
34
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,24 @@
1
+ import { n as RingBuffer, t as PerformanceMonitor } from "./performance-monitor-qswCj9Nl.js";
2
+ //#region src/core/wrap-animation-loop.ts
3
+ /**
4
+ * Wrap a `renderer.setAnimationLoop` callback so each tick closes the previous
5
+ * frame and opens the next, the same bracketing the R3F sampler does. CPU time
6
+ * therefore spans the whole tick (your update code plus the render dispatch).
7
+ *
8
+ * ```ts
9
+ * renderer.setAnimationLoop(wrapAnimationLoop(monitor, (time) => { … renderer.render(scene, camera); }));
10
+ * ```
11
+ */
12
+ function wrapAnimationLoop(monitor, loop) {
13
+ let started = false;
14
+ return (...args) => {
15
+ if (started) monitor.end();
16
+ started = true;
17
+ monitor.begin();
18
+ loop(...args);
19
+ };
20
+ }
21
+ //#endregion
22
+ export { PerformanceMonitor, RingBuffer, wrapAnimationLoop };
23
+
24
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/core/wrap-animation-loop.ts"],"sourcesContent":["import type { PerformanceMonitor } from \"./performance-monitor.ts\";\n\ntype AnimationLoop<Args extends unknown[]> = (...args: Args) => void;\n\n/**\n * Wrap a `renderer.setAnimationLoop` callback so each tick closes the previous\n * frame and opens the next, the same bracketing the R3F sampler does. CPU time\n * therefore spans the whole tick (your update code plus the render dispatch).\n *\n * ```ts\n * renderer.setAnimationLoop(wrapAnimationLoop(monitor, (time) => { … renderer.render(scene, camera); }));\n * ```\n */\nfunction wrapAnimationLoop<Args extends unknown[]>(\n monitor: PerformanceMonitor,\n loop: AnimationLoop<Args>,\n): AnimationLoop<Args> {\n let started = false;\n\n return (...args) => {\n if (started) {\n monitor.end();\n }\n\n started = true;\n monitor.begin();\n loop(...args);\n };\n}\n\nexport { wrapAnimationLoop };\n"],"mappings":";;;;;;;;;;;AAaA,SAAS,kBACP,SACA,MACqB;CACrB,IAAI,UAAU;CAEd,QAAQ,GAAG,SAAS;EAClB,IAAI,SACF,QAAQ,IAAI;EAGd,UAAU;EACV,QAAQ,MAAM;EACd,KAAK,GAAG,IAAI;CACd;AACF"}