@margoengine/inspector 0.1.0 → 1.0.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 +201 -201
- package/NOTICE +4 -4
- package/README.md +623 -623
- package/dist/animation.js +4 -4
- package/dist/edit.js +4 -4
- package/dist/frame.js +4 -4
- package/dist/gizmo.js +4 -4
- package/dist/gl.js +4 -4
- package/dist/index.js +4 -4
- package/dist/mesh-debug.js +42 -42
- package/dist/packages.js +7 -7
- package/dist/remote.js +4 -4
- package/dist/shaders.js +4 -4
- package/dist/textures.js +2 -2
- package/dist/tools.js +4 -4
- package/package.json +149 -149
package/README.md
CHANGED
|
@@ -1,623 +1,623 @@
|
|
|
1
|
-
# @margoengine/inspector
|
|
2
|
-
|
|
3
|
-
Import `margoengine/inspector` from the `margoengine` package, or `@margoengine/inspector` from this package alone; the two are the same module. Neither is on npm yet: the [Install](../../README.md#install) section lists the two routes that work today (run in a clone, or install local tarballs).
|
|
4
|
-
|
|
5
|
-
Optional DOM inspector for core entities, including `stage.engine`. The hierarchy
|
|
6
|
-
updates as entities are added, renamed, reparented or disposed. Arrow keys select,
|
|
7
|
-
expand and collapse tree items. Selected entities expose visibility, local
|
|
8
|
-
transforms and their core PBR material's roughness and metallic values. Editing a
|
|
9
|
-
shared material changes every mesh using it.
|
|
10
|
-
|
|
11
|
-
```ts
|
|
12
|
-
import {Inspector} from 'margoengine/inspector';
|
|
13
|
-
import type {EngineKernel} from 'margoengine/core';
|
|
14
|
-
function attach(engine: EngineKernel, effects: {exposure: number; bloom: number}) {
|
|
15
|
-
return Inspector.attach(engine, {effects});
|
|
16
|
-
}
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
Pass the mutable options object of your attached post-processing effect to
|
|
20
|
-
`effects`; exposure/bloom controls bind directly to it. Unbound controls are disabled
|
|
21
|
-
and initially display zero. With no selected entity, roughness/metallic sliders
|
|
22
|
-
initially display the core material defaults, 0.5/0, while disabled.
|
|
23
|
-
|
|
24
|
-
## Place the panel in your page layout
|
|
25
|
-
|
|
26
|
-
By default the panel is a `position: fixed` element at the top right of the page with a high `z-index`. In an app with
|
|
27
|
-
its own buttons in that corner it sits on top of them and takes their clicks. Give it a container with `parent`, and
|
|
28
|
-
override its position with a style on `inspector.element` (a style set there wins over the panel's built-in one):
|
|
29
|
-
|
|
30
|
-
```ts
|
|
31
|
-
import {Inspector} from 'margoengine/inspector';
|
|
32
|
-
import type {EngineKernel} from 'margoengine/core';
|
|
33
|
-
function attachInSidebar(engine: EngineKernel, sidebar: HTMLElement) {
|
|
34
|
-
const inspector = Inspector.attach(engine, {parent: sidebar});
|
|
35
|
-
Object.assign(inspector.element.style, {
|
|
36
|
-
position: 'relative', top: 'auto', right: 'auto', width: '100%', maxHeight: '70vh', overflow: 'auto',
|
|
37
|
-
});
|
|
38
|
-
return inspector;
|
|
39
|
-
}
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
The panel then sits in the flow of `sidebar` with a bounded height and scrolls inside it. The
|
|
43
|
-
[debugging guide](../../docs/guides/debugging.md) runs this in a real browser and clicks the button next to the panel.
|
|
44
|
-
|
|
45
|
-
## Edit, undo, search, pick, save
|
|
46
|
-
|
|
47
|
-
Every change made in the panel (name, visibility, position, rotation, scale, colour, double sided, roughness, metallic,
|
|
48
|
-
bound effects) is recorded and can be undone with the Undo and Redo buttons, `Ctrl/Cmd+Z`, `Ctrl/Cmd+Shift+Z` or
|
|
49
|
-
`Ctrl/Cmd+Y` (inside a text field the browser keeps its own undo). A slider drag is one step. The Search box keeps the
|
|
50
|
-
entities whose name contains the text, and their parents. A click on the canvas selects the object under the pointer
|
|
51
|
-
(`pick: false` turns it off; a pointer that moves more than 4 px is a camera drag, not a click). Colour edits the core
|
|
52
|
-
material's base colour (sRGB in the panel, linear in the material).
|
|
53
|
-
|
|
54
|
-
```ts
|
|
55
|
-
import {Inspector} from 'margoengine/inspector';
|
|
56
|
-
import type {EngineKernel} from 'margoengine/core';
|
|
57
|
-
async function tour(engine: EngineKernel) {
|
|
58
|
-
const inspector = Inspector.attach(engine, {historyLimit: 500});
|
|
59
|
-
const saved = JSON.stringify(inspector.snapshot()); // names, visibility, transforms, material values
|
|
60
|
-
// ...edit in the panel or in code...
|
|
61
|
-
inspector.restore(JSON.parse(saved)); // one undoable step; checks the file first
|
|
62
|
-
inspector.undo();
|
|
63
|
-
inspector.redo();
|
|
64
|
-
console.log(inspector.resources()); // meshes, geometries, materials, triangles, plugins
|
|
65
|
-
console.log(inspector.shaderSource.length); // the scene WGSL of the latest WebGPU frame
|
|
66
|
-
inspector.history.onChange(() => console.log(inspector.history.nextUndo));
|
|
67
|
-
}
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
The panel's Save button downloads the snapshot as `scene.json` and Load restores one. `restore` matches meshes by id,
|
|
71
|
-
reports the ones that are gone (`missing`), changes nothing when the file is malformed or the engine rejects a value
|
|
72
|
-
(a colour out of range), and does not recreate or reparent meshes. The Resources section counts what the engine draws, and Scene
|
|
73
|
-
shader shows the WGSL of the latest WebGPU frame; both build their text only while open. Without the panel, import
|
|
74
|
-
`EditHistory` from `margoengine/inspector/history`, `snapshotMeshes`/`restoreSnapshot`/`parseSnapshot` from
|
|
75
|
-
`margoengine/inspector/snapshot` and `describeEngine` from `margoengine/inspector/resources`.
|
|
76
|
-
|
|
77
|
-
Creation, deletion and reparenting use the optional `/edit` entry; WGSL editing uses `/shaders`.
|
|
78
|
-
The root panel has no GPU memory view (WebGPU does not report resource sizes).
|
|
79
|
-
Other packages' lights, materials, shadows and effects are edited in the package panel below.
|
|
80
|
-
|
|
81
|
-
## Choose an optional editor
|
|
82
|
-
|
|
83
|
-
Import each feature from `@margoengine/inspector/<entry>` or its forwarding alias
|
|
84
|
-
`margoengine/inspector/<entry>`. Attaching the root Inspector does not attach these editors.
|
|
85
|
-
Use an existing inspector's `history` where an editor accepts one to share undo and redo.
|
|
86
|
-
Importing an entry does not install texture instrumentation, start a relay or create a panel.
|
|
87
|
-
|
|
88
|
-
| Entry | Public starting point | Purpose |
|
|
89
|
-
|---|---|---|
|
|
90
|
-
| `tweak` | `new TweakPanel(options)` | Bind application settings without an engine. |
|
|
91
|
-
| `gizmo` | `InspectorGizmo.attach(engine, inspector, options)` | Transform the inspector selection. |
|
|
92
|
-
| `edit` | `SceneEditor.attach(engine, inspector, options)` | Create and edit core entity hierarchies. |
|
|
93
|
-
| `textures` | `TextureCapture.start(options)`, `TextureViewer.attach(capture, options)` | Capture WebGPU textures before opening a viewer. |
|
|
94
|
-
| `animation` | `AnimationPanel.attach(engine, players, options)` | Inspect clip playback and skeletons. |
|
|
95
|
-
| `gl` | `GLInspector.attach(engine, options)`, `captureGLFrame(engine, options)` | Native WebGL2 preview overlays and frame capture. |
|
|
96
|
-
| `tools` | `CameraTools.attach(engine, options)` | Camera selection, canvas capture and glTF export. |
|
|
97
|
-
| `shaders` | `ShaderEditor.attach(engine, options)`, `recompileShader(mesh, wgsl, options)` | Edit a WgslMaterial with device diagnostics. |
|
|
98
|
-
| `remote` | `RemoteTarget.attach(engine, options)`, `new RemoteInspector(options)` | Inspect another running page. |
|
|
99
|
-
| `remote-relay` | `await createRemoteRelay(options)` | Node development relay for remote pages. |
|
|
100
|
-
|
|
101
|
-
## Application settings and gesture undo
|
|
102
|
-
|
|
103
|
-
`TweakPanel` needs a DOM but no engine. Its default parent is `document.body`, title is
|
|
104
|
-
`Parameters`, and history is a new `EditHistory`. `parent`, `title`, `history` and `signal`
|
|
105
|
-
override those choices. `add(object, key, options)` returns a binding for a number, string,
|
|
106
|
-
boolean, vector, enum or function button. `addColor` selects colour controls;
|
|
107
|
-
`addFolder(title)` creates a child panel sharing the history.
|
|
108
|
-
|
|
109
|
-
Numeric `min` and `max` default to unbounded, with no quantization until `step` is set.
|
|
110
|
-
`axes` overrides ranges per vector component. Colours accept `#rrggbb`, 24-bit integers,
|
|
111
|
-
or RGB(A) components; `colorScale` defaults to 1 and also accepts 255, while alpha stays
|
|
112
|
-
in 0..1. `options` supplies an enum array or a label-to-value map. `readonly` defaults
|
|
113
|
-
to false; `label` defaults to the property key. Function buttons are outside history
|
|
114
|
-
unless their binding has an `undo` function.
|
|
115
|
-
|
|
116
|
-
`binding.setValue(value)` validates and records one step. Input events in a gesture
|
|
117
|
-
coalesce until change or blur; Escape cancels the pending gesture. Vector and colour
|
|
118
|
-
component edits preserve the bound object's identity. `getValue`, `reset`, `onChange`
|
|
119
|
-
and `refresh` expose the binding without DOM events; `onChange` returns an unsubscribe
|
|
120
|
-
function. Call `panel.refresh()` after application writes: there is no polling timer.
|
|
121
|
-
`panel.undo()` and `panel.redo()` use its history. Dispose bindings or the whole panel
|
|
122
|
-
when finished; aborting the supplied signal disposes the panel and its folders.
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
```ts
|
|
126
|
-
import {TweakPanel} from '@margoengine/inspector/tweak';
|
|
127
|
-
const settings = {speed: 1, enabled: true, color: '#ff8800', position: [0, 1, 0], mode: 'solid', reset: () => console.log('reset')};
|
|
128
|
-
const panel = new TweakPanel({title: 'Preview'});
|
|
129
|
-
const folder = panel.addFolder('Appearance');
|
|
130
|
-
const speed = panel.add(settings, 'speed', {min: 0, max: 4, step: 0.1});
|
|
131
|
-
panel.add(settings, 'enabled');
|
|
132
|
-
folder.addColor(settings, 'color');
|
|
133
|
-
folder.add(settings, 'position', {kind: 'vector'});
|
|
134
|
-
folder.add(settings, 'mode', {options: ['solid', 'wire']});
|
|
135
|
-
panel.add(settings, 'reset');
|
|
136
|
-
speed.setValue(2);
|
|
137
|
-
panel.undo(); panel.redo();
|
|
138
|
-
console.log(settings.speed); // 2
|
|
139
|
-
panel.dispose(); // At application teardown; leave attached to edit interactively.
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
## Selection gizmos
|
|
143
|
-
|
|
144
|
-
Attach `InspectorGizmo` after its Inspector is attached to the same engine. The gizmo
|
|
145
|
-
follows `inspector.selected`; `sync()` updates it immediately after a programmatic
|
|
146
|
-
selection. `controls` exposes the underlying TransformControls. The toolbar defaults
|
|
147
|
-
to `inspector.root`; `parent: null` hides it. The default mode is translate, space is
|
|
148
|
-
world, size is 1, and translation/rotation/scale snapping is off. Other control options
|
|
149
|
-
and defaults are documented in [TransformControls](../controls/README.md).
|
|
150
|
-
|
|
151
|
-
Pass `cameraControls` to suspend those controls for a gizmo drag and restore their
|
|
152
|
-
previous enabled state afterward. A completed drag is one step in `inspector.history`.
|
|
153
|
-
Canvas keyboard shortcuts default to enabled: W/E/R choose move/rotate/scale, Q changes
|
|
154
|
-
space, +/- change size and Escape cancels a drag. `keyboard: false` leaves keyboard
|
|
155
|
-
handling to the application. Disposal removes listeners, toolbar and controls, restores
|
|
156
|
-
the canvas tabindex and camera control state, and detaches the owned plugin. An abort
|
|
157
|
-
signal, engine disposal or removal of a connected inspector also releases the gizmo.
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
```ts
|
|
161
|
-
import {Engine, box, PbrMaterial} from '@margoengine/core';
|
|
162
|
-
import {Inspector} from '@margoengine/inspector';
|
|
163
|
-
import {InspectorGizmo} from '@margoengine/inspector/gizmo';
|
|
164
|
-
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
165
|
-
canvas.width = 480; canvas.height = 320;
|
|
166
|
-
const engine = await Engine.create(canvas);
|
|
167
|
-
const mesh = engine.createMesh().setGeometry(box()).setMaterial(new PbrMaterial());
|
|
168
|
-
const inspector = Inspector.attach(engine);
|
|
169
|
-
const gizmo = InspectorGizmo.attach(engine, inspector);
|
|
170
|
-
inspector.select(mesh);
|
|
171
|
-
gizmo.sync();
|
|
172
|
-
engine.render();
|
|
173
|
-
// Keep these attached for interactive dragging, then release in this order.
|
|
174
|
-
gizmo.dispose(); inspector.dispose(); engine.dispose(); canvas.remove();
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
## Create, delete, duplicate and reparent
|
|
178
|
-
|
|
179
|
-
`SceneEditor` attaches to the inspector's scene, with one editor per scene. Its toolbar
|
|
180
|
-
defaults to `inspector.root` and may be hidden with `parent: null`. `createPrimitive`
|
|
181
|
-
defaults to a box at the root origin; supported kinds are box, sphere, plane, cylinder,
|
|
182
|
-
cone, capsule, torus, circle, ring and torusKnot, using geometry-package dimensions.
|
|
183
|
-
An optional parent makes the new primitive's origin parent-relative.
|
|
184
|
-
|
|
185
|
-
`createLight` supports point (default), spot, directional and ambient lights with white
|
|
186
|
-
colour, intensity 1, range 10, angle PI/4 and penumbra 0. The returned mesh is a
|
|
187
|
-
selectable transform anchor. `light(handle)` reads a copy of its settings and
|
|
188
|
-
`setLight(handle, values)` records a checked edit. Existing `lights` and `directional`
|
|
189
|
-
plugins must belong to this scene; otherwise the editor finds or attaches its lighting
|
|
190
|
-
plugins when needed. Clustered point/spot lighting requires WebGPU.
|
|
191
|
-
|
|
192
|
-
`delete()` and `duplicate()` default to the current selection and operate on its
|
|
193
|
-
subtree. Duplication shares immutable geometry and copies each distinct material once.
|
|
194
|
-
`reparent(handle, parent)` preserves the local transform, accepts null for the root,
|
|
195
|
-
and rejects cycles before editing. Tree dragging uses the same operation. Delete and
|
|
196
|
-
Ctrl/Cmd+D shortcuts in the tree default to enabled (`keyboard: false` disables them).
|
|
197
|
-
These operations use the inspector history; package-owned PBR subtrees must be edited
|
|
198
|
-
through their owning package rather than this core hierarchy editor.
|
|
199
|
-
|
|
200
|
-
Deletion retains original handles for undo, hides the subtree and removes its drawable
|
|
201
|
-
geometry. Deleted handles are finally disposed when their last retaining history entry
|
|
202
|
-
is discarded. Editor disposal removes its UI, input and owned lights while keeping
|
|
203
|
-
scene meshes and shared history usable. The supplied signal also disposes it.
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
```ts
|
|
207
|
-
import {Engine} from '@margoengine/core';
|
|
208
|
-
import {Inspector} from '@margoengine/inspector';
|
|
209
|
-
import {SceneEditor} from '@margoengine/inspector/edit';
|
|
210
|
-
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
211
|
-
canvas.width = 480; canvas.height = 320;
|
|
212
|
-
const engine = await Engine.create(canvas);
|
|
213
|
-
const inspector = Inspector.attach(engine);
|
|
214
|
-
const editor = SceneEditor.attach(engine, inspector);
|
|
215
|
-
const parent = editor.createPrimitive('box');
|
|
216
|
-
const child = editor.createPrimitive('sphere', parent);
|
|
217
|
-
const copy = editor.duplicate(child);
|
|
218
|
-
editor.reparent(copy, null);
|
|
219
|
-
editor.delete(copy);
|
|
220
|
-
inspector.undo(); // Restores the same retained handle.
|
|
221
|
-
const light = editor.createLight('ambient');
|
|
222
|
-
editor.setLight(light, {intensity: 0.5});
|
|
223
|
-
console.log(editor.light(light));
|
|
224
|
-
editor.dispose(); inspector.dispose(); engine.dispose(); canvas.remove();
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
## Texture capture and previews
|
|
228
|
-
|
|
229
|
-
Start `TextureCapture` before `Engine.create` and before loading textures. A viewer
|
|
230
|
-
opened later can then inspect earlier captured textures. The default GPU is
|
|
231
|
-
`navigator.gpu`; `gpu: null` watches only devices explicitly passed to `watchDevice`.
|
|
232
|
-
`watchGPU` follows future adapter/device requests, `watchAdapter` follows future
|
|
233
|
-
devices, and `watchDevice` captures future texture creation. Existing resources need
|
|
234
|
-
`register(device, texture)`, which does not change their usage. `watchCanvas(context)`
|
|
235
|
-
captures future swapchain textures and must run before the desired canvas frame.
|
|
236
|
-
|
|
237
|
-
`inspectable` defaults to true: captured creation descriptors gain TEXTURE_BINDING
|
|
238
|
-
without mutating application descriptors. Turning it off preserves usage flags, but
|
|
239
|
-
resources without TEXTURE_BINDING cannot be previewed. `list()` reports live metadata;
|
|
240
|
-
`onChange` returns an unsubscribe function. Destroyed or collected textures disappear.
|
|
241
|
-
|
|
242
|
-
`preview(id, options)` returns width, height and top-left RGBA display bytes, rather than
|
|
243
|
-
raw source-format data. Defaults are mip 0, layer 0, channel rgba, exposure 1 and range
|
|
244
|
-
[0,1]. Layers select array/cube faces or a 3D slice at the chosen mip. Channels are
|
|
245
|
-
rgba, rgb, r, g, b and a; isolated channels display as grayscale. Aspect defaults to
|
|
246
|
-
depth-only for depth formats, stencil-only for stencil8 and all otherwise. An explicit
|
|
247
|
-
aspect must exist in the source. Exposure multiplies values before range mapping and
|
|
248
|
-
clamping, so integer and HDR textures need an appropriate range. Invalid mip, layer,
|
|
249
|
-
aspect or mapping rejects; this is a display preview, not a lossless texture export.
|
|
250
|
-
|
|
251
|
-
`TextureViewer.attach` defaults to `document.body`; `select(id, options)` resolves after
|
|
252
|
-
the pixels are visible. A preview signal cancels its wait without cancelling unrelated
|
|
253
|
-
GPU work. Disposing the viewer cancels its preview but keeps the capture alive. Disposing
|
|
254
|
-
or aborting the capture removes its owned wrappers and metadata without destroying
|
|
255
|
-
application textures. Keep both handles and dispose each owner explicitly.
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
```ts
|
|
259
|
-
import {Engine} from '@margoengine/core';
|
|
260
|
-
import {TextureCapture, TextureViewer} from '@margoengine/inspector/textures';
|
|
261
|
-
const capture = TextureCapture.start(); // Before engine/device/texture creation.
|
|
262
|
-
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
263
|
-
canvas.width = 480; canvas.height = 320;
|
|
264
|
-
const engine = await Engine.create(canvas);
|
|
265
|
-
const device = engine.gpu!; // This example requires WebGPU.
|
|
266
|
-
const texture = device.createTexture({size: [1, 1], format: 'rgba8unorm', usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING});
|
|
267
|
-
device.queue.writeTexture({texture}, new Uint8Array([255, 128, 0, 255]), {bytesPerRow: 4}, [1, 1]);
|
|
268
|
-
const id = capture.register(device, texture);
|
|
269
|
-
const viewer = TextureViewer.attach(capture);
|
|
270
|
-
await viewer.select(id, {channel: 'rgba', mip: 0, layer: 0});
|
|
271
|
-
const preview = await capture.preview(id);
|
|
272
|
-
console.log(preview.width, preview.height);
|
|
273
|
-
viewer.dispose(); capture.dispose(); texture.destroy(); engine.dispose(); canvas.remove();
|
|
274
|
-
```
|
|
275
|
-
|
|
276
|
-
## Animation playback and skeletons
|
|
277
|
-
|
|
278
|
-
`AnimationPanel.attach(engine, players, options)` accepts live Animation players from
|
|
279
|
-
that engine. Omitting players discovers existing Animation plugins; `add(player)` adds
|
|
280
|
-
later players. `parent` defaults to `document.body`; null leaves the controls detached.
|
|
281
|
-
`state(player, clipIndex)` returns a copy of a clip's time, weight, playing, loop and
|
|
282
|
-
speed. Indices distinguish clips with duplicate names. `set(player, clipIndex, patch)`
|
|
283
|
-
updates these values and immediately poses scrubs and weight changes. Time, weight and
|
|
284
|
-
speed must be finite and nonnegative; time clamps to clip duration. UI scrubbing pauses
|
|
285
|
-
that clip. Positive clip weights normalize per model; all-zero weights show the pose
|
|
286
|
-
captured when the player was added.
|
|
287
|
-
|
|
288
|
-
The panel owns each added player's clock until `remove(player)` or disposal. Initial
|
|
289
|
-
time and weight select the player's current clip; other weights start at zero, while
|
|
290
|
-
playing, loop and speed follow its captured state. Skeleton checkboxes show an SVG
|
|
291
|
-
overlay on either backend when skins exist. Removal restores the saved pose and player
|
|
292
|
-
state for a live model, without disposing the player or model. Model disposal removes
|
|
293
|
-
its controls. Panel disposal, engine disposal or its abort signal releases all models,
|
|
294
|
-
helpers and DOM.
|
|
295
|
-
|
|
296
|
-
Serve the repository root so the included Fox GLB is available at the URL below.
|
|
297
|
-
|
|
298
|
-
```ts
|
|
299
|
-
import {Engine} from '@margoengine/core';
|
|
300
|
-
import {loadGLTF} from '@margoengine/gltf/pbr';
|
|
301
|
-
import {Animation} from '@margoengine/animation';
|
|
302
|
-
import {AnimationPanel} from '@margoengine/inspector/animation';
|
|
303
|
-
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
304
|
-
canvas.width = 480; canvas.height = 320;
|
|
305
|
-
const engine = await Engine.create(canvas);
|
|
306
|
-
const model = await loadGLTF(engine, '/examples/assets/khronos/Fox/Fox.glb');
|
|
307
|
-
const player = Animation.attach(model);
|
|
308
|
-
const panel = AnimationPanel.attach(engine, [player]);
|
|
309
|
-
panel.set(player, 0, {playing: false, time: 0.5, weight: 1});
|
|
310
|
-
console.log(panel.state(player, 0));
|
|
311
|
-
engine.render();
|
|
312
|
-
panel.remove(player); // Returns clock/pose ownership to the player.
|
|
313
|
-
panel.dispose(); player.dispose(); model.dispose(); engine.dispose(); canvas.remove();
|
|
314
|
-
```
|
|
315
|
-
|
|
316
|
-
## Native WebGL2 preview inspection
|
|
317
|
-
|
|
318
|
-
The root Inspector's debug overlays are WebGPU-only. For an engine using the WebGL2
|
|
319
|
-
preview backend, attach `GLInspector` and use `setDebug` with wireframe, normals, bounds
|
|
320
|
-
or lights. Its parent defaults to `document.body`; null keeps controls detached.
|
|
321
|
-
This entry operates on the engine's native WebGL2 context and owns its debug program
|
|
322
|
-
and overlays; it does not load a compatibility renderer or require a compatibility-package
|
|
323
|
-
peer dependency. Overlay drawing restores the GL state it changes.
|
|
324
|
-
|
|
325
|
-
`capture(options)` or standalone `captureGLFrame(engine, options)` reports native draw
|
|
326
|
-
calls, arguments, program/framebuffer/vertex-array ids, viewport, depth/blend/cull state,
|
|
327
|
-
shader sources and per-view passes with owners and CPU/GPU costs. `render` defaults to
|
|
328
|
-
true; false waits for the next application frame. `timeout` defaults to 5000 ms and
|
|
329
|
-
must be positive and finite. Only one capture per engine may be pending. `signal`
|
|
330
|
-
cancels capture; panel disposal cancels its own pending capture. Temporary capture
|
|
331
|
-
wrappers are removed on success or failure. GPU costs are null when timer queries are
|
|
332
|
-
unsupported or disjoint; a null value is not zero cost. Context loss disables overlays;
|
|
333
|
-
dispose and reattach after restoring the context.
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
```ts
|
|
337
|
-
import {Engine, box, PbrMaterial} from '@margoengine/core';
|
|
338
|
-
import {GLInspector} from '@margoengine/inspector/gl';
|
|
339
|
-
import {webglRenderer} from '@margoengine/webgl2-compat/preview';
|
|
340
|
-
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
341
|
-
const engine = await Engine.create(canvas, {backend: 'webgl2', fallback: webglRenderer});
|
|
342
|
-
engine.createMesh().setGeometry(box()).setMaterial(new PbrMaterial());
|
|
343
|
-
const panel = GLInspector.attach(engine);
|
|
344
|
-
panel.setDebug('bounds', true);
|
|
345
|
-
const report = await panel.capture({render: true, timeout: 5000});
|
|
346
|
-
console.log(report);
|
|
347
|
-
panel.dispose(); engine.dispose(); canvas.remove();
|
|
348
|
-
```
|
|
349
|
-
|
|
350
|
-
`GLInspector` is a reviewed preview route in `scripts/check-preview-contract.mjs` (2026-11-10, 3 `backend` tests): it exists for the small WebGL2 preview and draws its overlays with WebGL2 there. Pixel evidence: `test/gl-browser.mjs` (`npm run test:browser:gl`, and `test:browser:gl:compat`); unit evidence: `test/gl.test.js`. No behaviour changed; the row was missing, which kept `npm run verify` red.
|
|
351
|
-
|
|
352
|
-
## Cameras, screenshots, recordings and glTF
|
|
353
|
-
|
|
354
|
-
`CameraTools.attach` defaults to `document.body`; use `parent: inspector.root` to place
|
|
355
|
-
it beside the tree or null for detached controls. `cameras` supplies named application
|
|
356
|
-
cameras; `addCamera` adds or replaces one and `removeCamera` removes it. Id `default`
|
|
357
|
-
and ids starting with `model:` are reserved. `list()` includes application cameras and
|
|
358
|
-
live loaded-model cameras discovered from scene plugins or supplied in `models`.
|
|
359
|
-
Await `select(id)` to activate one. The selected camera's live pose/projection is copied
|
|
360
|
-
each update; selecting default restores the engine camera captured at attach time.
|
|
361
|
-
|
|
362
|
-
`screenshot(options)` renders and returns a Blob of canvas pixels, excluding DOM
|
|
363
|
-
overlays. Type defaults to image/png; JPEG/WebP requests may fall back to PNG in the
|
|
364
|
-
browser. Lossy `quality` defaults to the browser choice and accepts 0..1; `timeout`
|
|
365
|
-
defaults to 10000 ms. `startRecording(options)` lazily loads video/record and returns
|
|
366
|
-
its recording handle. Await `stopRecording()` for the final encoded Blob; the browser
|
|
367
|
-
must support canvas capture and MediaRecorder. Only one recording may start or run
|
|
368
|
-
at once. Recording options are those of [video/record](../video/README.md).
|
|
369
|
-
|
|
370
|
-
`exportGLTF(options)` lazily loads exporters. It defaults to binary GLB for the engine
|
|
371
|
-
scene; `binary: false` returns embedded-buffer glTF JSON. Passing a live `model` from
|
|
372
|
-
this engine selects model export, preserving its PBR data, skins and attached animations
|
|
373
|
-
through the model exporter. Screenshot, recording and export accept operation signals;
|
|
374
|
-
the panel's signal disposes the tools. Disposal restores the original camera, aborts
|
|
375
|
-
pending work, disposes recording, revokes owned download URLs and removes the panel.
|
|
376
|
-
Loaded-camera projection support is imported only when selecting a loaded camera.
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
```ts
|
|
380
|
-
import {Engine, box, PbrMaterial} from '@margoengine/core';
|
|
381
|
-
import {CameraTools} from '@margoengine/inspector/tools';
|
|
382
|
-
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
383
|
-
canvas.width = 480; canvas.height = 320;
|
|
384
|
-
const engine = await Engine.create(canvas);
|
|
385
|
-
engine.createMesh().setGeometry(box()).setMaterial(new PbrMaterial());
|
|
386
|
-
const tools = CameraTools.attach(engine);
|
|
387
|
-
await tools.select('default');
|
|
388
|
-
const png = await tools.screenshot({type: 'image/png'});
|
|
389
|
-
const glb = await tools.exportGLTF();
|
|
390
|
-
console.log(png.size, glb);
|
|
391
|
-
tools.dispose(); engine.dispose(); canvas.remove();
|
|
392
|
-
```
|
|
393
|
-
|
|
394
|
-
## Edit other packages' objects
|
|
395
|
-
|
|
396
|
-
`margoengine/inspector/packages` edits what other packages attach, found by shape without importing them: core's sun and
|
|
397
|
-
ambient, every light in a `clustered*` plugin's `list` (point, spot, directional, area), the glTF PBR materials of the
|
|
398
|
-
scene's gltf/pbr and FBX nodes (base colour, opacity, roughness, metallic, normal scale, occlusion, emissive, alpha
|
|
399
|
-
cutoff, double sided, wireframe), the `shadows` options (distance, cascade blend, light size, bias, normal bias, caster
|
|
400
|
-
margin) and every other plugin's `options` numbers and switches (Bloom's strength, threshold, exposure and radius, say).
|
|
401
|
-
An option the app never set shows its documented default; an undo back past the first edit unsets it again.
|
|
402
|
-
|
|
403
|
-
```ts
|
|
404
|
-
import {Inspector} from 'margoengine/inspector';
|
|
405
|
-
import {PackageEditor, findPackageObjects, editProperty} from 'margoengine/inspector/packages';
|
|
406
|
-
import type {EngineKernel} from 'margoengine/core';
|
|
407
|
-
function editPackages(engine: EngineKernel, settings: {speed: number}) {
|
|
408
|
-
const inspector = Inspector.attach(engine);
|
|
409
|
-
// One undo history for both panels; `objects` adds your own (a materials Material, app settings).
|
|
410
|
-
const editor = PackageEditor.attach(engine, {history: inspector.history, objects: {Settings: settings}});
|
|
411
|
-
const sun = findPackageObjects(engine).find((o) => o.id === 'light:sun')!;
|
|
412
|
-
editProperty(sun, 'intensity', 4, inspector.history); // checked: a negative intensity throws RangeError
|
|
413
|
-
editor.set('light:sun', 'color', [1, 0.9, 0.8]);
|
|
414
|
-
return editor;
|
|
415
|
-
}
|
|
416
|
-
```
|
|
417
|
-
|
|
418
|
-
Each object has an `id` (`light:clustered:0`, `material:0`, `shadows:shadows`, `effect:bloom`), a `group` and its
|
|
419
|
-
`properties` (`key`, `kind` number, boolean, color or vector, `min`, `max`, live `get` and `set`). Colours are linear
|
|
420
|
-
RGB (the panel's colour inputs are sRGB). The panel lists objects again every 500 ms when plugins, lights or meshes come
|
|
421
|
-
and go. A glTF material whose `@margoengine/materials` shading reports a version is shown read only (that shading
|
|
422
|
-
caches its values by version): pass its `Material` in `objects`, whose setters `describeObject` finds. A package reads
|
|
423
|
-
most of these values every frame; an option it reads only when it attaches keeps its old value until it is attached
|
|
424
|
-
again.
|
|
425
|
-
|
|
426
|
-
WebGPU wireframe edges, world-space vertex normals and AABB boxes use the existing
|
|
427
|
-
`@margoengine/lines` helpers. These overlays apply to core meshes and loaded glTF/FBX
|
|
428
|
-
PBR nodes, including every primitive on a node, and honor inherited visibility.
|
|
429
|
-
PBR wireframes, normals and bounds show the retained CPU bind pose with the current
|
|
430
|
-
world transform. They do not read animated vertex buffers back from the GPU.
|
|
431
|
-
The loader keeps ownership of its geometry, and no duplicate core draw is created.
|
|
432
|
-
Disposing a model removes its overlays. "Light helpers" draws the lights the engine has, found by shape without
|
|
433
|
-
importing their packages: core's sun, every `clustered*` plugin's `list` (point and spot, directional and area lights)
|
|
434
|
-
and an environment hemisphere or ambient light (lines' `LightsHelper`). Debug drawing is disabled on WebGL2. The hierarchy and property
|
|
435
|
-
editor still work there. Neither core nor the stage facade is modified.
|
|
436
|
-
|
|
437
|
-
With engine views, the debug helpers show in each view for the meshes that view draws (its culling and layers), the tree lists the meshes of the inspector's scene (`scene.use(inspector)` for another scene), and a frame capture lists every pass once per view with `view` (id, rectangle, scene); scenes created after a capture starts appear from the next capture. `select(mesh)` selects an entity. `setDebug('wireframe' | 'normals' | 'bounds' | 'lights', true)`
|
|
438
|
-
toggles a scene overlay. `dispose()` releases the DOM, timer, GPU resources and
|
|
439
|
-
registered passes; engine disposal also performs this cleanup.
|
|
440
|
-
|
|
441
|
-
## Mesh data debug view
|
|
442
|
-
|
|
443
|
-
`margoengine/inspector/mesh-debug` shows the data of the meshes on screen: triangle edges, vertex dots, a checker on
|
|
444
|
-
UV set 0 or 1, vertex colours, one colour per material, or plain camera-lit shading. It changes no material and no
|
|
445
|
-
geometry; `dispose()` gives the scene back its own picture. With no options it draws a wireframe over every mesh.
|
|
446
|
-
|
|
447
|
-
```ts
|
|
448
|
-
import {MeshDebugView} from 'margoengine/inspector/mesh-debug';
|
|
449
|
-
import {createMesh, Material} from 'margoengine/materials';
|
|
450
|
-
import {boxGeometry} from 'margoengine/geometry';
|
|
451
|
-
import {Engine} from 'margoengine/core';
|
|
452
|
-
const engine = await Engine.create(document.querySelector('canvas')!);
|
|
453
|
-
const crate = createMesh(engine, boxGeometry(1, 1, 1), new Material({color: '#8855cc'}));
|
|
454
|
-
const view = MeshDebugView.attach(engine); // edges over the material
|
|
455
|
-
view.set({mode: 'wireframe-vertices', wireWidth: 2, vertexRadius: 4});
|
|
456
|
-
view.set({mode: 'uv0', uvScale: 8}); // a lit checker in place of the material
|
|
457
|
-
view.set({mode: 'uv0', multiply: true, uvColors: ['#ffffff', '#808080']}); // the checker over it
|
|
458
|
-
view.set({mode: 'material-ids', multiply: null, materialColors: ['#e04040']});
|
|
459
|
-
view.set({mode: 'shaded', meshes: crate.ownedMeshes}); // one model only
|
|
460
|
-
engine.render();
|
|
461
|
-
```
|
|
462
|
-
|
|
463
|
-
| `mode` | Shows | `multiply` default |
|
|
464
|
-
| --- | --- | --- |
|
|
465
|
-
| `'wireframe'` (default) | every triangle edge in `wireColor` (`'#72e8cc'`), `wireWidth` 1 pixel (0 to 64) | true |
|
|
466
|
-
| `'vertices'` | a dot on every vertex in `vertexColor` (`'#ffb326'`), `vertexRadius` 3 pixels (0 to 64) | true |
|
|
467
|
-
| `'wireframe-vertices'` | both | true |
|
|
468
|
-
| `'uv0'`, `'uv1'` | a checker: `uvScale` 10 squares per UV unit (above 0, to 4096), `uvColors` `['#f0f0f0', '#3a6ea5']`, the first at UV (0, 0) | false |
|
|
469
|
-
| `'vertex-colors'` | the vertex colours (white where a mesh has none) | false |
|
|
470
|
-
| `'material-ids'` | one colour per material, given in draw order: `materialColors` (default `[]`) first, then generated hues; `resetMaterialIds()` starts again | false |
|
|
471
|
-
| `'shaded'` | camera-lit shading | false |
|
|
472
|
-
|
|
473
|
-
`multiply: true` keeps each mesh's own picture: lines and dots draw over it, and the checker, vertex colours, material
|
|
474
|
-
colours or shading multiply it. `false` replaces it: camera-lit shading (`diffuseColor` `'#a0a0a0'`, `specularColor`
|
|
475
|
-
`'#303030'`, `specularPower` 32, 1 to 10000; the light sits at the eye and both faces light alike) under the lines and
|
|
476
|
-
dots, a lit checker or material colour, and unlit vertex colours. `null` returns to the mode's default. `meshes`
|
|
477
|
-
(default `null`: every mesh of the view's scene) limits the view; `enabled: false` (default true) draws nothing and
|
|
478
|
-
frees the buffers. Colours are `'#rgb'`, `'#rrggbb'` (sRGB) or linear `[r, g, b]` in 0..1. `options` reads the settings
|
|
479
|
-
in effect. `set` checks everything before changing anything and throws `TypeError` or `RangeError` with a help link.
|
|
480
|
-
|
|
481
|
-
Core meshes and glTF, FBX and materials PBR nodes are drawn, with live skinned and morphed vertices
|
|
482
|
-
(`@margoengine/animation`) and instanced meshes. Core geometry has UV set 0 only when a package gave it one, and no
|
|
483
|
-
vertex colours; a mesh without a UV set shows the checker's first colour. Vertices moved in a material's own vertex
|
|
484
|
-
shader, alpha cut-outs and clipping planes are not applied (see [limits](../../docs/limits.md)). It runs on WebGPU and
|
|
485
|
-
the WebGL2 compatibility layer; native WebGL2 throws `MargoError` `'unsupported'`. The panel's wireframe and vertex
|
|
486
|
-
normal overlays (`setDebug`) stay as they are.
|
|
487
|
-
|
|
488
|
-
## Live shader editing
|
|
489
|
-
|
|
490
|
-
`margoengine/inspector/shaders` edits the WGSL of a mesh drawing a `WgslMaterial` from `margoengine/materials` and
|
|
491
|
-
recompiles it in place. `ShaderEditor.attach(engine, {meshes})` shows a panel with the mesh list and its WGSL; typing
|
|
492
|
-
applies after a short pause. Each edit is compiled on the device first, inside the module text the engine drew, so a
|
|
493
|
-
broken edit is never drawn: its errors are listed with their line and column, and the mesh keeps its last good shader.
|
|
494
|
-
A good edit swaps in a new material with the same uniforms, textures and other values, and undo puts the old one
|
|
495
|
-
back. `recompileShader(mesh, wgsl, {device: engine.gpu})` does the same without a panel and returns the compiler's
|
|
496
|
-
messages. Diagnostics need WebGPU; generated shaders of other materials stay read only in the Inspector.
|
|
497
|
-
|
|
498
|
-
Pass actual materials-package `createMesh` results drawing a `WgslMaterial` in `meshes`
|
|
499
|
-
(an array or a function called by `refresh`). `history` defaults to a new EditHistory;
|
|
500
|
-
pass the inspector history to share undo. `parent` defaults to `document.body`, `live`
|
|
501
|
-
defaults to true, `delayMs` defaults to 400 (0..10000), and `timeoutMs` defaults to 3000.
|
|
502
|
-
`apply()` resolves with the edit result; `result` holds the last displayed result.
|
|
503
|
-
|
|
504
|
-
Standalone `recompileShader` needs the drawing device for compiler checks. Without
|
|
505
|
-
`device`, it swaps unchecked. Its `settleMs` defaults to 50 and must be between zero and
|
|
506
|
-
`timeoutMs`; timeoutMs accepts 0..60000. Keep rendering while awaiting an edit: the
|
|
507
|
-
first checked edit may need a frame to learn the full generated module. Inspect `ok`,
|
|
508
|
-
`modules` and `diagnostics`, rather than assuming a resolved promise proves compilation.
|
|
509
|
-
Diagnostics use 1-based edited-text coordinates, with zero for engine-generated text.
|
|
510
|
-
An unchanged edit may return ok with zero modules. Zero observed modules do not prove
|
|
511
|
-
GPU compilation; keep the target visible and rendering for that evidence. Dispose the editor
|
|
512
|
-
to remove its timer, listeners and panel; meshes and their materials remain app-owned.
|
|
513
|
-
|
|
514
|
-
A standalone WebGPU edit with a visible mesh:
|
|
515
|
-
|
|
516
|
-
```ts
|
|
517
|
-
import {Engine} from '@margoengine/core';
|
|
518
|
-
import {boxGeometry} from '@margoengine/geometry';
|
|
519
|
-
import {WgslMaterial, createMesh} from '@margoengine/materials';
|
|
520
|
-
import {ShaderEditor, recompileShader} from '@margoengine/inspector/shaders';
|
|
521
|
-
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
522
|
-
const engine = await Engine.create(canvas, {backend: 'webgpu', camera: {position: [2, 2, 3], target: [0, 0, 0]}});
|
|
523
|
-
const wgsl = 'fn shade(i: Out, s: Surface) -> Surface { var o = s; o.base = vec4f(0.2, 0.6, 0.9, 1.0); return o; }';
|
|
524
|
-
const mesh = createMesh(engine, boxGeometry(1, 1, 1), new WgslMaterial({wgsl}));
|
|
525
|
-
const editor = ShaderEditor.attach(engine, {meshes: [mesh], live: false});
|
|
526
|
-
engine.start(); // The edit must render to compile its pipelines.
|
|
527
|
-
const result = await recompileShader(mesh, wgsl.replace('0.2, 0.6, 0.9', '0.9, 0.4, 0.2'), {device: engine.gpu, history: editor.history});
|
|
528
|
-
if (!result.ok || result.modules === 0) throw new Error(JSON.stringify(result));
|
|
529
|
-
console.log(result.diagnostics);
|
|
530
|
-
editor.history.undo();
|
|
531
|
-
editor.dispose(); engine.dispose(); canvas.remove();
|
|
532
|
-
```
|
|
533
|
-
|
|
534
|
-
## Remote inspection
|
|
535
|
-
|
|
536
|
-
`margoengine/inspector/remote` inspects a scene running on a phone or headset from a desktop browser. The page on the
|
|
537
|
-
device attaches a `RemoteTarget`, the desktop page creates a `RemoteInspector`, and the two talk over any string
|
|
538
|
-
channel: `channelTransport` wraps a MessagePort, BroadcastChannel or WebSocket the app already has, and
|
|
539
|
-
`relayTransport` goes through the development relay that `margoengine/inspector/remote-relay` starts in Node
|
|
540
|
-
(`createRemoteRelay({host: '0.0.0.0'})` returns a LAN url and a token). The desktop panel lists the device's lights,
|
|
541
|
-
materials, shadows, post effects and meshes with their values, edits them (each edit is checked on the device, with
|
|
542
|
-
undo), shows the frame rate and takes a screenshot of the device's canvas. The target runs only that fixed list of
|
|
543
|
-
operations and, when given a token, refuses requests without it. Development builds only.
|
|
544
|
-
|
|
545
|
-
Start the Node relay separately from the browser bundles. Its defaults are host
|
|
546
|
-
127.0.0.1, port 0 (a free port), a random 32-byte base64url token and a 16000000-byte
|
|
547
|
-
message limit. A supplied token needs at least 16 characters; maxBytes accepts
|
|
548
|
-
1..16000000. Use the returned `url` and `token` for each `relayTransport`, with role
|
|
549
|
-
target on the scene page and viewer on the desktop page. The token travels in the
|
|
550
|
-
Authorization header, never in the URL. Configure the same optional protocol token
|
|
551
|
-
on RemoteTarget and RemoteInspector as well. Anyone holding the relay token can
|
|
552
|
-
inspect the scene; keep this facility in development builds.
|
|
553
|
-
|
|
554
|
-
Attach the target before awaiting `viewer.connect()`. Connect resolves after hello
|
|
555
|
-
and scene delivery; `set`, `undo`, `redo` and `capture` return promises for target
|
|
556
|
-
operations. The viewer parent defaults to document.body, with null for no panel;
|
|
557
|
-
`timeoutMs` defaults to 10000 and accepts 100..120000. A screenshot returns base64 PNG
|
|
558
|
-
data, mimeType and dimensions, of canvas pixels only. `relayTransport`'s `onError` is called
|
|
559
|
-
once on a send/stream failure, after which that transport stops. Dispose the target
|
|
560
|
-
and viewer, close their transports, then await the relay's `close()` (or use async
|
|
561
|
-
disposal) when the session ends.
|
|
562
|
-
|
|
563
|
-
For browser checks, wait for relay state delivery and shader pipeline/submitted-work
|
|
564
|
-
readiness rather than sleeping for a guessed duration. The package's
|
|
565
|
-
`test:browser:remote` exercises two browser connections to a real local relay on one
|
|
566
|
-
machine; it does not prove phone/headset hardware behavior. `test:browser:shaders`
|
|
567
|
-
checks real compiler diagnostics, rollback and panel undo on WebGPU. Native preview
|
|
568
|
-
coverage is in `test:browser:gl` and `test:browser:gl:compat`.
|
|
569
|
-
|
|
570
|
-
This local browser example uses the same protocol without a server:
|
|
571
|
-
|
|
572
|
-
```ts
|
|
573
|
-
import {Engine} from '@margoengine/core';
|
|
574
|
-
import {RemoteTarget, RemoteInspector, channelTransport} from '@margoengine/inspector/remote';
|
|
575
|
-
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
576
|
-
canvas.width = 480; canvas.height = 320;
|
|
577
|
-
const engine = await Engine.create(canvas);
|
|
578
|
-
const channel = new MessageChannel();
|
|
579
|
-
const targetTransport = channelTransport(channel.port1);
|
|
580
|
-
const viewerTransport = channelTransport(channel.port2);
|
|
581
|
-
const target = RemoteTarget.attach(engine, {transport: targetTransport, token: 'local-development'});
|
|
582
|
-
const viewer = new RemoteInspector({transport: viewerTransport, token: 'local-development', parent: null});
|
|
583
|
-
await viewer.connect(); // Scene delivery, with no guessed sleep.
|
|
584
|
-
console.log(viewer.objects);
|
|
585
|
-
await viewer.set('light:sun', 'intensity', 2);
|
|
586
|
-
await viewer.undo();
|
|
587
|
-
viewer.dispose(); target.dispose();
|
|
588
|
-
targetTransport.close?.(); viewerTransport.close?.();
|
|
589
|
-
channel.port1.close(); channel.port2.close();
|
|
590
|
-
engine.dispose(); canvas.remove();
|
|
591
|
-
```
|
|
592
|
-
|
|
593
|
-
The relay runs separately in Node, never in a browser bundle:
|
|
594
|
-
|
|
595
|
-
```ts
|
|
596
|
-
import {createRemoteRelay} from '@margoengine/inspector/remote-relay';
|
|
597
|
-
const relay = await createRemoteRelay({host: '127.0.0.1'});
|
|
598
|
-
console.log(relay.url, relay.token); // Supply both privately to the two development pages.
|
|
599
|
-
await relay.close(); // On development server shutdown.
|
|
600
|
-
```
|
|
601
|
-
|
|
602
|
-
## Plugin and pass view
|
|
603
|
-
|
|
604
|
-
`margoengine/inspector/frame` records one frame. Its report lists every attached plugin in attach order with the
|
|
605
|
-
frame graph passes it registered and the time its `onPreUpdate` took in that frame, and every frame graph pass with
|
|
606
|
-
its owner, order and CPU and GPU cost. The panel shows both.
|
|
607
|
-
|
|
608
|
-
```ts
|
|
609
|
-
import {FrameDebugger} from 'margoengine/inspector/frame';
|
|
610
|
-
import type {EngineKernel} from 'margoengine/core';
|
|
611
|
-
async function pluginCosts(engine: EngineKernel) {
|
|
612
|
-
const debug = FrameDebugger.attach(engine, {parent: null}); // null: no panel
|
|
613
|
-
const report = await debug.capture({render: true});
|
|
614
|
-
for (const p of report.plugins) console.log(p.scene, p.order, p.name, p.passes, p.preUpdateMilliseconds);
|
|
615
|
-
for (const pass of report.graph) console.log(pass.owner, pass.order, pass.name, pass.cpuMilliseconds, pass.gpuMilliseconds);
|
|
616
|
-
debug.dispose();
|
|
617
|
-
return report.plugins;
|
|
618
|
-
}
|
|
619
|
-
```
|
|
620
|
-
|
|
621
|
-
`preUpdateMilliseconds` is null for a plugin with no `onPreUpdate` and for a frozen one (its hook cannot be wrapped);
|
|
622
|
-
a plugin of a scene the frame did not draw shows 0. The hooks are wrapped only for the captured frame. Plugins and
|
|
623
|
-
scenes added during the frame show from the next capture. Frame capture needs WebGPU.
|
|
1
|
+
# @margoengine/inspector
|
|
2
|
+
|
|
3
|
+
Import `margoengine/inspector` from the `margoengine` package, or `@margoengine/inspector` from this package alone; the two are the same module. Neither is on npm yet: the [Install](../../README.md#install) section lists the two routes that work today (run in a clone, or install local tarballs).
|
|
4
|
+
|
|
5
|
+
Optional DOM inspector for core entities, including `stage.engine`. The hierarchy
|
|
6
|
+
updates as entities are added, renamed, reparented or disposed. Arrow keys select,
|
|
7
|
+
expand and collapse tree items. Selected entities expose visibility, local
|
|
8
|
+
transforms and their core PBR material's roughness and metallic values. Editing a
|
|
9
|
+
shared material changes every mesh using it.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import {Inspector} from 'margoengine/inspector';
|
|
13
|
+
import type {EngineKernel} from 'margoengine/core';
|
|
14
|
+
function attach(engine: EngineKernel, effects: {exposure: number; bloom: number}) {
|
|
15
|
+
return Inspector.attach(engine, {effects});
|
|
16
|
+
}
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Pass the mutable options object of your attached post-processing effect to
|
|
20
|
+
`effects`; exposure/bloom controls bind directly to it. Unbound controls are disabled
|
|
21
|
+
and initially display zero. With no selected entity, roughness/metallic sliders
|
|
22
|
+
initially display the core material defaults, 0.5/0, while disabled.
|
|
23
|
+
|
|
24
|
+
## Place the panel in your page layout
|
|
25
|
+
|
|
26
|
+
By default the panel is a `position: fixed` element at the top right of the page with a high `z-index`. In an app with
|
|
27
|
+
its own buttons in that corner it sits on top of them and takes their clicks. Give it a container with `parent`, and
|
|
28
|
+
override its position with a style on `inspector.element` (a style set there wins over the panel's built-in one):
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import {Inspector} from 'margoengine/inspector';
|
|
32
|
+
import type {EngineKernel} from 'margoengine/core';
|
|
33
|
+
function attachInSidebar(engine: EngineKernel, sidebar: HTMLElement) {
|
|
34
|
+
const inspector = Inspector.attach(engine, {parent: sidebar});
|
|
35
|
+
Object.assign(inspector.element.style, {
|
|
36
|
+
position: 'relative', top: 'auto', right: 'auto', width: '100%', maxHeight: '70vh', overflow: 'auto',
|
|
37
|
+
});
|
|
38
|
+
return inspector;
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The panel then sits in the flow of `sidebar` with a bounded height and scrolls inside it. The
|
|
43
|
+
[debugging guide](../../docs/guides/debugging.md) runs this in a real browser and clicks the button next to the panel.
|
|
44
|
+
|
|
45
|
+
## Edit, undo, search, pick, save
|
|
46
|
+
|
|
47
|
+
Every change made in the panel (name, visibility, position, rotation, scale, colour, double sided, roughness, metallic,
|
|
48
|
+
bound effects) is recorded and can be undone with the Undo and Redo buttons, `Ctrl/Cmd+Z`, `Ctrl/Cmd+Shift+Z` or
|
|
49
|
+
`Ctrl/Cmd+Y` (inside a text field the browser keeps its own undo). A slider drag is one step. The Search box keeps the
|
|
50
|
+
entities whose name contains the text, and their parents. A click on the canvas selects the object under the pointer
|
|
51
|
+
(`pick: false` turns it off; a pointer that moves more than 4 px is a camera drag, not a click). Colour edits the core
|
|
52
|
+
material's base colour (sRGB in the panel, linear in the material).
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
import {Inspector} from 'margoengine/inspector';
|
|
56
|
+
import type {EngineKernel} from 'margoengine/core';
|
|
57
|
+
async function tour(engine: EngineKernel) {
|
|
58
|
+
const inspector = Inspector.attach(engine, {historyLimit: 500});
|
|
59
|
+
const saved = JSON.stringify(inspector.snapshot()); // names, visibility, transforms, material values
|
|
60
|
+
// ...edit in the panel or in code...
|
|
61
|
+
inspector.restore(JSON.parse(saved)); // one undoable step; checks the file first
|
|
62
|
+
inspector.undo();
|
|
63
|
+
inspector.redo();
|
|
64
|
+
console.log(inspector.resources()); // meshes, geometries, materials, triangles, plugins
|
|
65
|
+
console.log(inspector.shaderSource.length); // the scene WGSL of the latest WebGPU frame
|
|
66
|
+
inspector.history.onChange(() => console.log(inspector.history.nextUndo));
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The panel's Save button downloads the snapshot as `scene.json` and Load restores one. `restore` matches meshes by id,
|
|
71
|
+
reports the ones that are gone (`missing`), changes nothing when the file is malformed or the engine rejects a value
|
|
72
|
+
(a colour out of range), and does not recreate or reparent meshes. The Resources section counts what the engine draws, and Scene
|
|
73
|
+
shader shows the WGSL of the latest WebGPU frame; both build their text only while open. Without the panel, import
|
|
74
|
+
`EditHistory` from `margoengine/inspector/history`, `snapshotMeshes`/`restoreSnapshot`/`parseSnapshot` from
|
|
75
|
+
`margoengine/inspector/snapshot` and `describeEngine` from `margoengine/inspector/resources`.
|
|
76
|
+
|
|
77
|
+
Creation, deletion and reparenting use the optional `/edit` entry; WGSL editing uses `/shaders`.
|
|
78
|
+
The root panel has no GPU memory view (WebGPU does not report resource sizes).
|
|
79
|
+
Other packages' lights, materials, shadows and effects are edited in the package panel below.
|
|
80
|
+
|
|
81
|
+
## Choose an optional editor
|
|
82
|
+
|
|
83
|
+
Import each feature from `@margoengine/inspector/<entry>` or its forwarding alias
|
|
84
|
+
`margoengine/inspector/<entry>`. Attaching the root Inspector does not attach these editors.
|
|
85
|
+
Use an existing inspector's `history` where an editor accepts one to share undo and redo.
|
|
86
|
+
Importing an entry does not install texture instrumentation, start a relay or create a panel.
|
|
87
|
+
|
|
88
|
+
| Entry | Public starting point | Purpose |
|
|
89
|
+
|---|---|---|
|
|
90
|
+
| `tweak` | `new TweakPanel(options)` | Bind application settings without an engine. |
|
|
91
|
+
| `gizmo` | `InspectorGizmo.attach(engine, inspector, options)` | Transform the inspector selection. |
|
|
92
|
+
| `edit` | `SceneEditor.attach(engine, inspector, options)` | Create and edit core entity hierarchies. |
|
|
93
|
+
| `textures` | `TextureCapture.start(options)`, `TextureViewer.attach(capture, options)` | Capture WebGPU textures before opening a viewer. |
|
|
94
|
+
| `animation` | `AnimationPanel.attach(engine, players, options)` | Inspect clip playback and skeletons. |
|
|
95
|
+
| `gl` | `GLInspector.attach(engine, options)`, `captureGLFrame(engine, options)` | Native WebGL2 preview overlays and frame capture. |
|
|
96
|
+
| `tools` | `CameraTools.attach(engine, options)` | Camera selection, canvas capture and glTF export. |
|
|
97
|
+
| `shaders` | `ShaderEditor.attach(engine, options)`, `recompileShader(mesh, wgsl, options)` | Edit a WgslMaterial with device diagnostics. |
|
|
98
|
+
| `remote` | `RemoteTarget.attach(engine, options)`, `new RemoteInspector(options)` | Inspect another running page. |
|
|
99
|
+
| `remote-relay` | `await createRemoteRelay(options)` | Node development relay for remote pages. |
|
|
100
|
+
|
|
101
|
+
## Application settings and gesture undo
|
|
102
|
+
|
|
103
|
+
`TweakPanel` needs a DOM but no engine. Its default parent is `document.body`, title is
|
|
104
|
+
`Parameters`, and history is a new `EditHistory`. `parent`, `title`, `history` and `signal`
|
|
105
|
+
override those choices. `add(object, key, options)` returns a binding for a number, string,
|
|
106
|
+
boolean, vector, enum or function button. `addColor` selects colour controls;
|
|
107
|
+
`addFolder(title)` creates a child panel sharing the history.
|
|
108
|
+
|
|
109
|
+
Numeric `min` and `max` default to unbounded, with no quantization until `step` is set.
|
|
110
|
+
`axes` overrides ranges per vector component. Colours accept `#rrggbb`, 24-bit integers,
|
|
111
|
+
or RGB(A) components; `colorScale` defaults to 1 and also accepts 255, while alpha stays
|
|
112
|
+
in 0..1. `options` supplies an enum array or a label-to-value map. `readonly` defaults
|
|
113
|
+
to false; `label` defaults to the property key. Function buttons are outside history
|
|
114
|
+
unless their binding has an `undo` function.
|
|
115
|
+
|
|
116
|
+
`binding.setValue(value)` validates and records one step. Input events in a gesture
|
|
117
|
+
coalesce until change or blur; Escape cancels the pending gesture. Vector and colour
|
|
118
|
+
component edits preserve the bound object's identity. `getValue`, `reset`, `onChange`
|
|
119
|
+
and `refresh` expose the binding without DOM events; `onChange` returns an unsubscribe
|
|
120
|
+
function. Call `panel.refresh()` after application writes: there is no polling timer.
|
|
121
|
+
`panel.undo()` and `panel.redo()` use its history. Dispose bindings or the whole panel
|
|
122
|
+
when finished; aborting the supplied signal disposes the panel and its folders.
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import {TweakPanel} from '@margoengine/inspector/tweak';
|
|
127
|
+
const settings = {speed: 1, enabled: true, color: '#ff8800', position: [0, 1, 0], mode: 'solid', reset: () => console.log('reset')};
|
|
128
|
+
const panel = new TweakPanel({title: 'Preview'});
|
|
129
|
+
const folder = panel.addFolder('Appearance');
|
|
130
|
+
const speed = panel.add(settings, 'speed', {min: 0, max: 4, step: 0.1});
|
|
131
|
+
panel.add(settings, 'enabled');
|
|
132
|
+
folder.addColor(settings, 'color');
|
|
133
|
+
folder.add(settings, 'position', {kind: 'vector'});
|
|
134
|
+
folder.add(settings, 'mode', {options: ['solid', 'wire']});
|
|
135
|
+
panel.add(settings, 'reset');
|
|
136
|
+
speed.setValue(2);
|
|
137
|
+
panel.undo(); panel.redo();
|
|
138
|
+
console.log(settings.speed); // 2
|
|
139
|
+
panel.dispose(); // At application teardown; leave attached to edit interactively.
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Selection gizmos
|
|
143
|
+
|
|
144
|
+
Attach `InspectorGizmo` after its Inspector is attached to the same engine. The gizmo
|
|
145
|
+
follows `inspector.selected`; `sync()` updates it immediately after a programmatic
|
|
146
|
+
selection. `controls` exposes the underlying TransformControls. The toolbar defaults
|
|
147
|
+
to `inspector.root`; `parent: null` hides it. The default mode is translate, space is
|
|
148
|
+
world, size is 1, and translation/rotation/scale snapping is off. Other control options
|
|
149
|
+
and defaults are documented in [TransformControls](../controls/README.md).
|
|
150
|
+
|
|
151
|
+
Pass `cameraControls` to suspend those controls for a gizmo drag and restore their
|
|
152
|
+
previous enabled state afterward. A completed drag is one step in `inspector.history`.
|
|
153
|
+
Canvas keyboard shortcuts default to enabled: W/E/R choose move/rotate/scale, Q changes
|
|
154
|
+
space, +/- change size and Escape cancels a drag. `keyboard: false` leaves keyboard
|
|
155
|
+
handling to the application. Disposal removes listeners, toolbar and controls, restores
|
|
156
|
+
the canvas tabindex and camera control state, and detaches the owned plugin. An abort
|
|
157
|
+
signal, engine disposal or removal of a connected inspector also releases the gizmo.
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
import {Engine, box, PbrMaterial} from '@margoengine/core';
|
|
162
|
+
import {Inspector} from '@margoengine/inspector';
|
|
163
|
+
import {InspectorGizmo} from '@margoengine/inspector/gizmo';
|
|
164
|
+
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
165
|
+
canvas.width = 480; canvas.height = 320;
|
|
166
|
+
const engine = await Engine.create(canvas);
|
|
167
|
+
const mesh = engine.createMesh().setGeometry(box()).setMaterial(new PbrMaterial());
|
|
168
|
+
const inspector = Inspector.attach(engine);
|
|
169
|
+
const gizmo = InspectorGizmo.attach(engine, inspector);
|
|
170
|
+
inspector.select(mesh);
|
|
171
|
+
gizmo.sync();
|
|
172
|
+
engine.render();
|
|
173
|
+
// Keep these attached for interactive dragging, then release in this order.
|
|
174
|
+
gizmo.dispose(); inspector.dispose(); engine.dispose(); canvas.remove();
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Create, delete, duplicate and reparent
|
|
178
|
+
|
|
179
|
+
`SceneEditor` attaches to the inspector's scene, with one editor per scene. Its toolbar
|
|
180
|
+
defaults to `inspector.root` and may be hidden with `parent: null`. `createPrimitive`
|
|
181
|
+
defaults to a box at the root origin; supported kinds are box, sphere, plane, cylinder,
|
|
182
|
+
cone, capsule, torus, circle, ring and torusKnot, using geometry-package dimensions.
|
|
183
|
+
An optional parent makes the new primitive's origin parent-relative.
|
|
184
|
+
|
|
185
|
+
`createLight` supports point (default), spot, directional and ambient lights with white
|
|
186
|
+
colour, intensity 1, range 10, angle PI/4 and penumbra 0. The returned mesh is a
|
|
187
|
+
selectable transform anchor. `light(handle)` reads a copy of its settings and
|
|
188
|
+
`setLight(handle, values)` records a checked edit. Existing `lights` and `directional`
|
|
189
|
+
plugins must belong to this scene; otherwise the editor finds or attaches its lighting
|
|
190
|
+
plugins when needed. Clustered point/spot lighting requires WebGPU.
|
|
191
|
+
|
|
192
|
+
`delete()` and `duplicate()` default to the current selection and operate on its
|
|
193
|
+
subtree. Duplication shares immutable geometry and copies each distinct material once.
|
|
194
|
+
`reparent(handle, parent)` preserves the local transform, accepts null for the root,
|
|
195
|
+
and rejects cycles before editing. Tree dragging uses the same operation. Delete and
|
|
196
|
+
Ctrl/Cmd+D shortcuts in the tree default to enabled (`keyboard: false` disables them).
|
|
197
|
+
These operations use the inspector history; package-owned PBR subtrees must be edited
|
|
198
|
+
through their owning package rather than this core hierarchy editor.
|
|
199
|
+
|
|
200
|
+
Deletion retains original handles for undo, hides the subtree and removes its drawable
|
|
201
|
+
geometry. Deleted handles are finally disposed when their last retaining history entry
|
|
202
|
+
is discarded. Editor disposal removes its UI, input and owned lights while keeping
|
|
203
|
+
scene meshes and shared history usable. The supplied signal also disposes it.
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
```ts
|
|
207
|
+
import {Engine} from '@margoengine/core';
|
|
208
|
+
import {Inspector} from '@margoengine/inspector';
|
|
209
|
+
import {SceneEditor} from '@margoengine/inspector/edit';
|
|
210
|
+
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
211
|
+
canvas.width = 480; canvas.height = 320;
|
|
212
|
+
const engine = await Engine.create(canvas);
|
|
213
|
+
const inspector = Inspector.attach(engine);
|
|
214
|
+
const editor = SceneEditor.attach(engine, inspector);
|
|
215
|
+
const parent = editor.createPrimitive('box');
|
|
216
|
+
const child = editor.createPrimitive('sphere', parent);
|
|
217
|
+
const copy = editor.duplicate(child);
|
|
218
|
+
editor.reparent(copy, null);
|
|
219
|
+
editor.delete(copy);
|
|
220
|
+
inspector.undo(); // Restores the same retained handle.
|
|
221
|
+
const light = editor.createLight('ambient');
|
|
222
|
+
editor.setLight(light, {intensity: 0.5});
|
|
223
|
+
console.log(editor.light(light));
|
|
224
|
+
editor.dispose(); inspector.dispose(); engine.dispose(); canvas.remove();
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Texture capture and previews
|
|
228
|
+
|
|
229
|
+
Start `TextureCapture` before `Engine.create` and before loading textures. A viewer
|
|
230
|
+
opened later can then inspect earlier captured textures. The default GPU is
|
|
231
|
+
`navigator.gpu`; `gpu: null` watches only devices explicitly passed to `watchDevice`.
|
|
232
|
+
`watchGPU` follows future adapter/device requests, `watchAdapter` follows future
|
|
233
|
+
devices, and `watchDevice` captures future texture creation. Existing resources need
|
|
234
|
+
`register(device, texture)`, which does not change their usage. `watchCanvas(context)`
|
|
235
|
+
captures future swapchain textures and must run before the desired canvas frame.
|
|
236
|
+
|
|
237
|
+
`inspectable` defaults to true: captured creation descriptors gain TEXTURE_BINDING
|
|
238
|
+
without mutating application descriptors. Turning it off preserves usage flags, but
|
|
239
|
+
resources without TEXTURE_BINDING cannot be previewed. `list()` reports live metadata;
|
|
240
|
+
`onChange` returns an unsubscribe function. Destroyed or collected textures disappear.
|
|
241
|
+
|
|
242
|
+
`preview(id, options)` returns width, height and top-left RGBA display bytes, rather than
|
|
243
|
+
raw source-format data. Defaults are mip 0, layer 0, channel rgba, exposure 1 and range
|
|
244
|
+
[0,1]. Layers select array/cube faces or a 3D slice at the chosen mip. Channels are
|
|
245
|
+
rgba, rgb, r, g, b and a; isolated channels display as grayscale. Aspect defaults to
|
|
246
|
+
depth-only for depth formats, stencil-only for stencil8 and all otherwise. An explicit
|
|
247
|
+
aspect must exist in the source. Exposure multiplies values before range mapping and
|
|
248
|
+
clamping, so integer and HDR textures need an appropriate range. Invalid mip, layer,
|
|
249
|
+
aspect or mapping rejects; this is a display preview, not a lossless texture export.
|
|
250
|
+
|
|
251
|
+
`TextureViewer.attach` defaults to `document.body`; `select(id, options)` resolves after
|
|
252
|
+
the pixels are visible. A preview signal cancels its wait without cancelling unrelated
|
|
253
|
+
GPU work. Disposing the viewer cancels its preview but keeps the capture alive. Disposing
|
|
254
|
+
or aborting the capture removes its owned wrappers and metadata without destroying
|
|
255
|
+
application textures. Keep both handles and dispose each owner explicitly.
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
```ts
|
|
259
|
+
import {Engine} from '@margoengine/core';
|
|
260
|
+
import {TextureCapture, TextureViewer} from '@margoengine/inspector/textures';
|
|
261
|
+
const capture = TextureCapture.start(); // Before engine/device/texture creation.
|
|
262
|
+
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
263
|
+
canvas.width = 480; canvas.height = 320;
|
|
264
|
+
const engine = await Engine.create(canvas);
|
|
265
|
+
const device = engine.gpu!; // This example requires WebGPU.
|
|
266
|
+
const texture = device.createTexture({size: [1, 1], format: 'rgba8unorm', usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING});
|
|
267
|
+
device.queue.writeTexture({texture}, new Uint8Array([255, 128, 0, 255]), {bytesPerRow: 4}, [1, 1]);
|
|
268
|
+
const id = capture.register(device, texture);
|
|
269
|
+
const viewer = TextureViewer.attach(capture);
|
|
270
|
+
await viewer.select(id, {channel: 'rgba', mip: 0, layer: 0});
|
|
271
|
+
const preview = await capture.preview(id);
|
|
272
|
+
console.log(preview.width, preview.height);
|
|
273
|
+
viewer.dispose(); capture.dispose(); texture.destroy(); engine.dispose(); canvas.remove();
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## Animation playback and skeletons
|
|
277
|
+
|
|
278
|
+
`AnimationPanel.attach(engine, players, options)` accepts live Animation players from
|
|
279
|
+
that engine. Omitting players discovers existing Animation plugins; `add(player)` adds
|
|
280
|
+
later players. `parent` defaults to `document.body`; null leaves the controls detached.
|
|
281
|
+
`state(player, clipIndex)` returns a copy of a clip's time, weight, playing, loop and
|
|
282
|
+
speed. Indices distinguish clips with duplicate names. `set(player, clipIndex, patch)`
|
|
283
|
+
updates these values and immediately poses scrubs and weight changes. Time, weight and
|
|
284
|
+
speed must be finite and nonnegative; time clamps to clip duration. UI scrubbing pauses
|
|
285
|
+
that clip. Positive clip weights normalize per model; all-zero weights show the pose
|
|
286
|
+
captured when the player was added.
|
|
287
|
+
|
|
288
|
+
The panel owns each added player's clock until `remove(player)` or disposal. Initial
|
|
289
|
+
time and weight select the player's current clip; other weights start at zero, while
|
|
290
|
+
playing, loop and speed follow its captured state. Skeleton checkboxes show an SVG
|
|
291
|
+
overlay on either backend when skins exist. Removal restores the saved pose and player
|
|
292
|
+
state for a live model, without disposing the player or model. Model disposal removes
|
|
293
|
+
its controls. Panel disposal, engine disposal or its abort signal releases all models,
|
|
294
|
+
helpers and DOM.
|
|
295
|
+
|
|
296
|
+
Serve the repository root so the included Fox GLB is available at the URL below.
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
import {Engine} from '@margoengine/core';
|
|
300
|
+
import {loadGLTF} from '@margoengine/gltf/pbr';
|
|
301
|
+
import {Animation} from '@margoengine/animation';
|
|
302
|
+
import {AnimationPanel} from '@margoengine/inspector/animation';
|
|
303
|
+
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
304
|
+
canvas.width = 480; canvas.height = 320;
|
|
305
|
+
const engine = await Engine.create(canvas);
|
|
306
|
+
const model = await loadGLTF(engine, '/examples/assets/khronos/Fox/Fox.glb');
|
|
307
|
+
const player = Animation.attach(model);
|
|
308
|
+
const panel = AnimationPanel.attach(engine, [player]);
|
|
309
|
+
panel.set(player, 0, {playing: false, time: 0.5, weight: 1});
|
|
310
|
+
console.log(panel.state(player, 0));
|
|
311
|
+
engine.render();
|
|
312
|
+
panel.remove(player); // Returns clock/pose ownership to the player.
|
|
313
|
+
panel.dispose(); player.dispose(); model.dispose(); engine.dispose(); canvas.remove();
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
## Native WebGL2 preview inspection
|
|
317
|
+
|
|
318
|
+
The root Inspector's debug overlays are WebGPU-only. For an engine using the WebGL2
|
|
319
|
+
preview backend, attach `GLInspector` and use `setDebug` with wireframe, normals, bounds
|
|
320
|
+
or lights. Its parent defaults to `document.body`; null keeps controls detached.
|
|
321
|
+
This entry operates on the engine's native WebGL2 context and owns its debug program
|
|
322
|
+
and overlays; it does not load a compatibility renderer or require a compatibility-package
|
|
323
|
+
peer dependency. Overlay drawing restores the GL state it changes.
|
|
324
|
+
|
|
325
|
+
`capture(options)` or standalone `captureGLFrame(engine, options)` reports native draw
|
|
326
|
+
calls, arguments, program/framebuffer/vertex-array ids, viewport, depth/blend/cull state,
|
|
327
|
+
shader sources and per-view passes with owners and CPU/GPU costs. `render` defaults to
|
|
328
|
+
true; false waits for the next application frame. `timeout` defaults to 5000 ms and
|
|
329
|
+
must be positive and finite. Only one capture per engine may be pending. `signal`
|
|
330
|
+
cancels capture; panel disposal cancels its own pending capture. Temporary capture
|
|
331
|
+
wrappers are removed on success or failure. GPU costs are null when timer queries are
|
|
332
|
+
unsupported or disjoint; a null value is not zero cost. Context loss disables overlays;
|
|
333
|
+
dispose and reattach after restoring the context.
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
```ts
|
|
337
|
+
import {Engine, box, PbrMaterial} from '@margoengine/core';
|
|
338
|
+
import {GLInspector} from '@margoengine/inspector/gl';
|
|
339
|
+
import {webglRenderer} from '@margoengine/webgl2-compat/preview';
|
|
340
|
+
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
341
|
+
const engine = await Engine.create(canvas, {backend: 'webgl2', fallback: webglRenderer});
|
|
342
|
+
engine.createMesh().setGeometry(box()).setMaterial(new PbrMaterial());
|
|
343
|
+
const panel = GLInspector.attach(engine);
|
|
344
|
+
panel.setDebug('bounds', true);
|
|
345
|
+
const report = await panel.capture({render: true, timeout: 5000});
|
|
346
|
+
console.log(report);
|
|
347
|
+
panel.dispose(); engine.dispose(); canvas.remove();
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
`GLInspector` is a reviewed preview route in `scripts/check-preview-contract.mjs` (2026-11-10, 3 `backend` tests): it exists for the small WebGL2 preview and draws its overlays with WebGL2 there. Pixel evidence: `test/gl-browser.mjs` (`npm run test:browser:gl`, and `test:browser:gl:compat`); unit evidence: `test/gl.test.js`. No behaviour changed; the row was missing, which kept `npm run verify` red.
|
|
351
|
+
|
|
352
|
+
## Cameras, screenshots, recordings and glTF
|
|
353
|
+
|
|
354
|
+
`CameraTools.attach` defaults to `document.body`; use `parent: inspector.root` to place
|
|
355
|
+
it beside the tree or null for detached controls. `cameras` supplies named application
|
|
356
|
+
cameras; `addCamera` adds or replaces one and `removeCamera` removes it. Id `default`
|
|
357
|
+
and ids starting with `model:` are reserved. `list()` includes application cameras and
|
|
358
|
+
live loaded-model cameras discovered from scene plugins or supplied in `models`.
|
|
359
|
+
Await `select(id)` to activate one. The selected camera's live pose/projection is copied
|
|
360
|
+
each update; selecting default restores the engine camera captured at attach time.
|
|
361
|
+
|
|
362
|
+
`screenshot(options)` renders and returns a Blob of canvas pixels, excluding DOM
|
|
363
|
+
overlays. Type defaults to image/png; JPEG/WebP requests may fall back to PNG in the
|
|
364
|
+
browser. Lossy `quality` defaults to the browser choice and accepts 0..1; `timeout`
|
|
365
|
+
defaults to 10000 ms. `startRecording(options)` lazily loads video/record and returns
|
|
366
|
+
its recording handle. Await `stopRecording()` for the final encoded Blob; the browser
|
|
367
|
+
must support canvas capture and MediaRecorder. Only one recording may start or run
|
|
368
|
+
at once. Recording options are those of [video/record](../video/README.md).
|
|
369
|
+
|
|
370
|
+
`exportGLTF(options)` lazily loads exporters. It defaults to binary GLB for the engine
|
|
371
|
+
scene; `binary: false` returns embedded-buffer glTF JSON. Passing a live `model` from
|
|
372
|
+
this engine selects model export, preserving its PBR data, skins and attached animations
|
|
373
|
+
through the model exporter. Screenshot, recording and export accept operation signals;
|
|
374
|
+
the panel's signal disposes the tools. Disposal restores the original camera, aborts
|
|
375
|
+
pending work, disposes recording, revokes owned download URLs and removes the panel.
|
|
376
|
+
Loaded-camera projection support is imported only when selecting a loaded camera.
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
```ts
|
|
380
|
+
import {Engine, box, PbrMaterial} from '@margoengine/core';
|
|
381
|
+
import {CameraTools} from '@margoengine/inspector/tools';
|
|
382
|
+
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
383
|
+
canvas.width = 480; canvas.height = 320;
|
|
384
|
+
const engine = await Engine.create(canvas);
|
|
385
|
+
engine.createMesh().setGeometry(box()).setMaterial(new PbrMaterial());
|
|
386
|
+
const tools = CameraTools.attach(engine);
|
|
387
|
+
await tools.select('default');
|
|
388
|
+
const png = await tools.screenshot({type: 'image/png'});
|
|
389
|
+
const glb = await tools.exportGLTF();
|
|
390
|
+
console.log(png.size, glb);
|
|
391
|
+
tools.dispose(); engine.dispose(); canvas.remove();
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
## Edit other packages' objects
|
|
395
|
+
|
|
396
|
+
`margoengine/inspector/packages` edits what other packages attach, found by shape without importing them: core's sun and
|
|
397
|
+
ambient, every light in a `clustered*` plugin's `list` (point, spot, directional, area), the glTF PBR materials of the
|
|
398
|
+
scene's gltf/pbr and FBX nodes (base colour, opacity, roughness, metallic, normal scale, occlusion, emissive, alpha
|
|
399
|
+
cutoff, double sided, wireframe), the `shadows` options (distance, cascade blend, light size, bias, normal bias, caster
|
|
400
|
+
margin) and every other plugin's `options` numbers and switches (Bloom's strength, threshold, exposure and radius, say).
|
|
401
|
+
An option the app never set shows its documented default; an undo back past the first edit unsets it again.
|
|
402
|
+
|
|
403
|
+
```ts
|
|
404
|
+
import {Inspector} from 'margoengine/inspector';
|
|
405
|
+
import {PackageEditor, findPackageObjects, editProperty} from 'margoengine/inspector/packages';
|
|
406
|
+
import type {EngineKernel} from 'margoengine/core';
|
|
407
|
+
function editPackages(engine: EngineKernel, settings: {speed: number}) {
|
|
408
|
+
const inspector = Inspector.attach(engine);
|
|
409
|
+
// One undo history for both panels; `objects` adds your own (a materials Material, app settings).
|
|
410
|
+
const editor = PackageEditor.attach(engine, {history: inspector.history, objects: {Settings: settings}});
|
|
411
|
+
const sun = findPackageObjects(engine).find((o) => o.id === 'light:sun')!;
|
|
412
|
+
editProperty(sun, 'intensity', 4, inspector.history); // checked: a negative intensity throws RangeError
|
|
413
|
+
editor.set('light:sun', 'color', [1, 0.9, 0.8]);
|
|
414
|
+
return editor;
|
|
415
|
+
}
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
Each object has an `id` (`light:clustered:0`, `material:0`, `shadows:shadows`, `effect:bloom`), a `group` and its
|
|
419
|
+
`properties` (`key`, `kind` number, boolean, color or vector, `min`, `max`, live `get` and `set`). Colours are linear
|
|
420
|
+
RGB (the panel's colour inputs are sRGB). The panel lists objects again every 500 ms when plugins, lights or meshes come
|
|
421
|
+
and go. A glTF material whose `@margoengine/materials` shading reports a version is shown read only (that shading
|
|
422
|
+
caches its values by version): pass its `Material` in `objects`, whose setters `describeObject` finds. A package reads
|
|
423
|
+
most of these values every frame; an option it reads only when it attaches keeps its old value until it is attached
|
|
424
|
+
again.
|
|
425
|
+
|
|
426
|
+
WebGPU wireframe edges, world-space vertex normals and AABB boxes use the existing
|
|
427
|
+
`@margoengine/lines` helpers. These overlays apply to core meshes and loaded glTF/FBX
|
|
428
|
+
PBR nodes, including every primitive on a node, and honor inherited visibility.
|
|
429
|
+
PBR wireframes, normals and bounds show the retained CPU bind pose with the current
|
|
430
|
+
world transform. They do not read animated vertex buffers back from the GPU.
|
|
431
|
+
The loader keeps ownership of its geometry, and no duplicate core draw is created.
|
|
432
|
+
Disposing a model removes its overlays. "Light helpers" draws the lights the engine has, found by shape without
|
|
433
|
+
importing their packages: core's sun, every `clustered*` plugin's `list` (point and spot, directional and area lights)
|
|
434
|
+
and an environment hemisphere or ambient light (lines' `LightsHelper`). Debug drawing is disabled on WebGL2. The hierarchy and property
|
|
435
|
+
editor still work there. Neither core nor the stage facade is modified.
|
|
436
|
+
|
|
437
|
+
With engine views, the debug helpers show in each view for the meshes that view draws (its culling and layers), the tree lists the meshes of the inspector's scene (`scene.use(inspector)` for another scene), and a frame capture lists every pass once per view with `view` (id, rectangle, scene); scenes created after a capture starts appear from the next capture. `select(mesh)` selects an entity. `setDebug('wireframe' | 'normals' | 'bounds' | 'lights', true)`
|
|
438
|
+
toggles a scene overlay. `dispose()` releases the DOM, timer, GPU resources and
|
|
439
|
+
registered passes; engine disposal also performs this cleanup.
|
|
440
|
+
|
|
441
|
+
## Mesh data debug view
|
|
442
|
+
|
|
443
|
+
`margoengine/inspector/mesh-debug` shows the data of the meshes on screen: triangle edges, vertex dots, a checker on
|
|
444
|
+
UV set 0 or 1, vertex colours, one colour per material, or plain camera-lit shading. It changes no material and no
|
|
445
|
+
geometry; `dispose()` gives the scene back its own picture. With no options it draws a wireframe over every mesh.
|
|
446
|
+
|
|
447
|
+
```ts
|
|
448
|
+
import {MeshDebugView} from 'margoengine/inspector/mesh-debug';
|
|
449
|
+
import {createMesh, Material} from 'margoengine/materials';
|
|
450
|
+
import {boxGeometry} from 'margoengine/geometry';
|
|
451
|
+
import {Engine} from 'margoengine/core';
|
|
452
|
+
const engine = await Engine.create(document.querySelector('canvas')!);
|
|
453
|
+
const crate = createMesh(engine, boxGeometry(1, 1, 1), new Material({color: '#8855cc'}));
|
|
454
|
+
const view = MeshDebugView.attach(engine); // edges over the material
|
|
455
|
+
view.set({mode: 'wireframe-vertices', wireWidth: 2, vertexRadius: 4});
|
|
456
|
+
view.set({mode: 'uv0', uvScale: 8}); // a lit checker in place of the material
|
|
457
|
+
view.set({mode: 'uv0', multiply: true, uvColors: ['#ffffff', '#808080']}); // the checker over it
|
|
458
|
+
view.set({mode: 'material-ids', multiply: null, materialColors: ['#e04040']});
|
|
459
|
+
view.set({mode: 'shaded', meshes: crate.ownedMeshes}); // one model only
|
|
460
|
+
engine.render();
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
| `mode` | Shows | `multiply` default |
|
|
464
|
+
| --- | --- | --- |
|
|
465
|
+
| `'wireframe'` (default) | every triangle edge in `wireColor` (`'#72e8cc'`), `wireWidth` 1 pixel (0 to 64) | true |
|
|
466
|
+
| `'vertices'` | a dot on every vertex in `vertexColor` (`'#ffb326'`), `vertexRadius` 3 pixels (0 to 64) | true |
|
|
467
|
+
| `'wireframe-vertices'` | both | true |
|
|
468
|
+
| `'uv0'`, `'uv1'` | a checker: `uvScale` 10 squares per UV unit (above 0, to 4096), `uvColors` `['#f0f0f0', '#3a6ea5']`, the first at UV (0, 0) | false |
|
|
469
|
+
| `'vertex-colors'` | the vertex colours (white where a mesh has none) | false |
|
|
470
|
+
| `'material-ids'` | one colour per material, given in draw order: `materialColors` (default `[]`) first, then generated hues; `resetMaterialIds()` starts again | false |
|
|
471
|
+
| `'shaded'` | camera-lit shading | false |
|
|
472
|
+
|
|
473
|
+
`multiply: true` keeps each mesh's own picture: lines and dots draw over it, and the checker, vertex colours, material
|
|
474
|
+
colours or shading multiply it. `false` replaces it: camera-lit shading (`diffuseColor` `'#a0a0a0'`, `specularColor`
|
|
475
|
+
`'#303030'`, `specularPower` 32, 1 to 10000; the light sits at the eye and both faces light alike) under the lines and
|
|
476
|
+
dots, a lit checker or material colour, and unlit vertex colours. `null` returns to the mode's default. `meshes`
|
|
477
|
+
(default `null`: every mesh of the view's scene) limits the view; `enabled: false` (default true) draws nothing and
|
|
478
|
+
frees the buffers. Colours are `'#rgb'`, `'#rrggbb'` (sRGB) or linear `[r, g, b]` in 0..1. `options` reads the settings
|
|
479
|
+
in effect. `set` checks everything before changing anything and throws `TypeError` or `RangeError` with a help link.
|
|
480
|
+
|
|
481
|
+
Core meshes and glTF, FBX and materials PBR nodes are drawn, with live skinned and morphed vertices
|
|
482
|
+
(`@margoengine/animation`) and instanced meshes. Core geometry has UV set 0 only when a package gave it one, and no
|
|
483
|
+
vertex colours; a mesh without a UV set shows the checker's first colour. Vertices moved in a material's own vertex
|
|
484
|
+
shader, alpha cut-outs and clipping planes are not applied (see [limits](../../docs/limits.md)). It runs on WebGPU and
|
|
485
|
+
the WebGL2 compatibility layer; native WebGL2 throws `MargoError` `'unsupported'`. The panel's wireframe and vertex
|
|
486
|
+
normal overlays (`setDebug`) stay as they are.
|
|
487
|
+
|
|
488
|
+
## Live shader editing
|
|
489
|
+
|
|
490
|
+
`margoengine/inspector/shaders` edits the WGSL of a mesh drawing a `WgslMaterial` from `margoengine/materials` and
|
|
491
|
+
recompiles it in place. `ShaderEditor.attach(engine, {meshes})` shows a panel with the mesh list and its WGSL; typing
|
|
492
|
+
applies after a short pause. Each edit is compiled on the device first, inside the module text the engine drew, so a
|
|
493
|
+
broken edit is never drawn: its errors are listed with their line and column, and the mesh keeps its last good shader.
|
|
494
|
+
A good edit swaps in a new material with the same uniforms, textures and other values, and undo puts the old one
|
|
495
|
+
back. `recompileShader(mesh, wgsl, {device: engine.gpu})` does the same without a panel and returns the compiler's
|
|
496
|
+
messages. Diagnostics need WebGPU; generated shaders of other materials stay read only in the Inspector.
|
|
497
|
+
|
|
498
|
+
Pass actual materials-package `createMesh` results drawing a `WgslMaterial` in `meshes`
|
|
499
|
+
(an array or a function called by `refresh`). `history` defaults to a new EditHistory;
|
|
500
|
+
pass the inspector history to share undo. `parent` defaults to `document.body`, `live`
|
|
501
|
+
defaults to true, `delayMs` defaults to 400 (0..10000), and `timeoutMs` defaults to 3000.
|
|
502
|
+
`apply()` resolves with the edit result; `result` holds the last displayed result.
|
|
503
|
+
|
|
504
|
+
Standalone `recompileShader` needs the drawing device for compiler checks. Without
|
|
505
|
+
`device`, it swaps unchecked. Its `settleMs` defaults to 50 and must be between zero and
|
|
506
|
+
`timeoutMs`; timeoutMs accepts 0..60000. Keep rendering while awaiting an edit: the
|
|
507
|
+
first checked edit may need a frame to learn the full generated module. Inspect `ok`,
|
|
508
|
+
`modules` and `diagnostics`, rather than assuming a resolved promise proves compilation.
|
|
509
|
+
Diagnostics use 1-based edited-text coordinates, with zero for engine-generated text.
|
|
510
|
+
An unchanged edit may return ok with zero modules. Zero observed modules do not prove
|
|
511
|
+
GPU compilation; keep the target visible and rendering for that evidence. Dispose the editor
|
|
512
|
+
to remove its timer, listeners and panel; meshes and their materials remain app-owned.
|
|
513
|
+
|
|
514
|
+
A standalone WebGPU edit with a visible mesh:
|
|
515
|
+
|
|
516
|
+
```ts
|
|
517
|
+
import {Engine} from '@margoengine/core';
|
|
518
|
+
import {boxGeometry} from '@margoengine/geometry';
|
|
519
|
+
import {WgslMaterial, createMesh} from '@margoengine/materials';
|
|
520
|
+
import {ShaderEditor, recompileShader} from '@margoengine/inspector/shaders';
|
|
521
|
+
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
522
|
+
const engine = await Engine.create(canvas, {backend: 'webgpu', camera: {position: [2, 2, 3], target: [0, 0, 0]}});
|
|
523
|
+
const wgsl = 'fn shade(i: Out, s: Surface) -> Surface { var o = s; o.base = vec4f(0.2, 0.6, 0.9, 1.0); return o; }';
|
|
524
|
+
const mesh = createMesh(engine, boxGeometry(1, 1, 1), new WgslMaterial({wgsl}));
|
|
525
|
+
const editor = ShaderEditor.attach(engine, {meshes: [mesh], live: false});
|
|
526
|
+
engine.start(); // The edit must render to compile its pipelines.
|
|
527
|
+
const result = await recompileShader(mesh, wgsl.replace('0.2, 0.6, 0.9', '0.9, 0.4, 0.2'), {device: engine.gpu, history: editor.history});
|
|
528
|
+
if (!result.ok || result.modules === 0) throw new Error(JSON.stringify(result));
|
|
529
|
+
console.log(result.diagnostics);
|
|
530
|
+
editor.history.undo();
|
|
531
|
+
editor.dispose(); engine.dispose(); canvas.remove();
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
## Remote inspection
|
|
535
|
+
|
|
536
|
+
`margoengine/inspector/remote` inspects a scene running on a phone or headset from a desktop browser. The page on the
|
|
537
|
+
device attaches a `RemoteTarget`, the desktop page creates a `RemoteInspector`, and the two talk over any string
|
|
538
|
+
channel: `channelTransport` wraps a MessagePort, BroadcastChannel or WebSocket the app already has, and
|
|
539
|
+
`relayTransport` goes through the development relay that `margoengine/inspector/remote-relay` starts in Node
|
|
540
|
+
(`createRemoteRelay({host: '0.0.0.0'})` returns a LAN url and a token). The desktop panel lists the device's lights,
|
|
541
|
+
materials, shadows, post effects and meshes with their values, edits them (each edit is checked on the device, with
|
|
542
|
+
undo), shows the frame rate and takes a screenshot of the device's canvas. The target runs only that fixed list of
|
|
543
|
+
operations and, when given a token, refuses requests without it. Development builds only.
|
|
544
|
+
|
|
545
|
+
Start the Node relay separately from the browser bundles. Its defaults are host
|
|
546
|
+
127.0.0.1, port 0 (a free port), a random 32-byte base64url token and a 16000000-byte
|
|
547
|
+
message limit. A supplied token needs at least 16 characters; maxBytes accepts
|
|
548
|
+
1..16000000. Use the returned `url` and `token` for each `relayTransport`, with role
|
|
549
|
+
target on the scene page and viewer on the desktop page. The token travels in the
|
|
550
|
+
Authorization header, never in the URL. Configure the same optional protocol token
|
|
551
|
+
on RemoteTarget and RemoteInspector as well. Anyone holding the relay token can
|
|
552
|
+
inspect the scene; keep this facility in development builds.
|
|
553
|
+
|
|
554
|
+
Attach the target before awaiting `viewer.connect()`. Connect resolves after hello
|
|
555
|
+
and scene delivery; `set`, `undo`, `redo` and `capture` return promises for target
|
|
556
|
+
operations. The viewer parent defaults to document.body, with null for no panel;
|
|
557
|
+
`timeoutMs` defaults to 10000 and accepts 100..120000. A screenshot returns base64 PNG
|
|
558
|
+
data, mimeType and dimensions, of canvas pixels only. `relayTransport`'s `onError` is called
|
|
559
|
+
once on a send/stream failure, after which that transport stops. Dispose the target
|
|
560
|
+
and viewer, close their transports, then await the relay's `close()` (or use async
|
|
561
|
+
disposal) when the session ends.
|
|
562
|
+
|
|
563
|
+
For browser checks, wait for relay state delivery and shader pipeline/submitted-work
|
|
564
|
+
readiness rather than sleeping for a guessed duration. The package's
|
|
565
|
+
`test:browser:remote` exercises two browser connections to a real local relay on one
|
|
566
|
+
machine; it does not prove phone/headset hardware behavior. `test:browser:shaders`
|
|
567
|
+
checks real compiler diagnostics, rollback and panel undo on WebGPU. Native preview
|
|
568
|
+
coverage is in `test:browser:gl` and `test:browser:gl:compat`.
|
|
569
|
+
|
|
570
|
+
This local browser example uses the same protocol without a server:
|
|
571
|
+
|
|
572
|
+
```ts
|
|
573
|
+
import {Engine} from '@margoengine/core';
|
|
574
|
+
import {RemoteTarget, RemoteInspector, channelTransport} from '@margoengine/inspector/remote';
|
|
575
|
+
const canvas = document.body.appendChild(document.createElement('canvas'));
|
|
576
|
+
canvas.width = 480; canvas.height = 320;
|
|
577
|
+
const engine = await Engine.create(canvas);
|
|
578
|
+
const channel = new MessageChannel();
|
|
579
|
+
const targetTransport = channelTransport(channel.port1);
|
|
580
|
+
const viewerTransport = channelTransport(channel.port2);
|
|
581
|
+
const target = RemoteTarget.attach(engine, {transport: targetTransport, token: 'local-development'});
|
|
582
|
+
const viewer = new RemoteInspector({transport: viewerTransport, token: 'local-development', parent: null});
|
|
583
|
+
await viewer.connect(); // Scene delivery, with no guessed sleep.
|
|
584
|
+
console.log(viewer.objects);
|
|
585
|
+
await viewer.set('light:sun', 'intensity', 2);
|
|
586
|
+
await viewer.undo();
|
|
587
|
+
viewer.dispose(); target.dispose();
|
|
588
|
+
targetTransport.close?.(); viewerTransport.close?.();
|
|
589
|
+
channel.port1.close(); channel.port2.close();
|
|
590
|
+
engine.dispose(); canvas.remove();
|
|
591
|
+
```
|
|
592
|
+
|
|
593
|
+
The relay runs separately in Node, never in a browser bundle:
|
|
594
|
+
|
|
595
|
+
```ts
|
|
596
|
+
import {createRemoteRelay} from '@margoengine/inspector/remote-relay';
|
|
597
|
+
const relay = await createRemoteRelay({host: '127.0.0.1'});
|
|
598
|
+
console.log(relay.url, relay.token); // Supply both privately to the two development pages.
|
|
599
|
+
await relay.close(); // On development server shutdown.
|
|
600
|
+
```
|
|
601
|
+
|
|
602
|
+
## Plugin and pass view
|
|
603
|
+
|
|
604
|
+
`margoengine/inspector/frame` records one frame. Its report lists every attached plugin in attach order with the
|
|
605
|
+
frame graph passes it registered and the time its `onPreUpdate` took in that frame, and every frame graph pass with
|
|
606
|
+
its owner, order and CPU and GPU cost. The panel shows both.
|
|
607
|
+
|
|
608
|
+
```ts
|
|
609
|
+
import {FrameDebugger} from 'margoengine/inspector/frame';
|
|
610
|
+
import type {EngineKernel} from 'margoengine/core';
|
|
611
|
+
async function pluginCosts(engine: EngineKernel) {
|
|
612
|
+
const debug = FrameDebugger.attach(engine, {parent: null}); // null: no panel
|
|
613
|
+
const report = await debug.capture({render: true});
|
|
614
|
+
for (const p of report.plugins) console.log(p.scene, p.order, p.name, p.passes, p.preUpdateMilliseconds);
|
|
615
|
+
for (const pass of report.graph) console.log(pass.owner, pass.order, pass.name, pass.cpuMilliseconds, pass.gpuMilliseconds);
|
|
616
|
+
debug.dispose();
|
|
617
|
+
return report.plugins;
|
|
618
|
+
}
|
|
619
|
+
```
|
|
620
|
+
|
|
621
|
+
`preUpdateMilliseconds` is null for a plugin with no `onPreUpdate` and for a frozen one (its hook cannot be wrapped);
|
|
622
|
+
a plugin of a scene the frame did not draw shows 0. The hooks are wrapped only for the captured frame. Plugins and
|
|
623
|
+
scenes added during the frame show from the next capture. Frame capture needs WebGPU.
|