@margoengine/inspector 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +623 -3
- package/dist/animation.d.ts +53 -0
- package/dist/animation.js +234 -0
- package/dist/color-D6qetPCJ.js +10 -0
- package/dist/color.d.ts +4 -0
- package/dist/debug-geometry-D61TEkP9.js +61 -0
- package/dist/debug-geometry.d.ts +15 -0
- package/dist/edit-state-Dm3G17js.js +4 -0
- package/dist/edit-state.d.ts +3 -0
- package/dist/edit.d.ts +105 -0
- package/dist/edit.js +379 -0
- package/dist/errors-D8rc8kzg.js +7 -0
- package/dist/frame-panel.d.ts +16 -0
- package/dist/frame.d.ts +264 -0
- package/dist/frame.js +881 -0
- package/dist/gizmo.d.ts +49 -0
- package/dist/gizmo.js +131 -0
- package/dist/gl.d.ts +89 -0
- package/dist/gl.js +284 -0
- package/dist/history.d.ts +65 -0
- package/dist/history.js +66 -0
- package/dist/index.d.ts +122 -0
- package/dist/index.js +490 -0
- package/dist/mesh-debug.d.ts +115 -0
- package/dist/mesh-debug.js +501 -0
- package/dist/mesh-source-BSQA_1GQ.js +31 -0
- package/dist/packages.d.ts +98 -0
- package/dist/packages.js +337 -0
- package/dist/remote-relay.d.ts +25 -0
- package/dist/remote-relay.js +64 -0
- package/dist/remote.d.ts +175 -0
- package/dist/remote.js +349 -0
- package/dist/resources.d.ts +30 -0
- package/dist/resources.js +42 -0
- package/dist/shaders.d.ts +122 -0
- package/dist/shaders.js +285 -0
- package/dist/snapshot.d.ts +47 -0
- package/dist/snapshot.js +90 -0
- package/dist/textures.d.ts +127 -0
- package/dist/textures.js +380 -0
- package/dist/tools.d.ts +101 -0
- package/dist/tools.js +220 -0
- package/dist/tweak.d.ts +86 -0
- package/dist/tweak.js +291 -0
- package/dist/view-rect-CTC0iGjf.js +23 -0
- package/dist/visibility-7_j5J9Nv.js +7 -0
- package/package.json +149 -6
package/dist/remote.d.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import { type Engine, type EnginePlugin } from '@margoengine/core/extension';
|
|
4
|
+
import { EditHistory } from './history.js';
|
|
5
|
+
import { type PackageObject, type PackageObjectsOptions, type PropertyKind, type PropertyValue } from './packages.js';
|
|
6
|
+
/** A two-way string channel between target and viewer. */
|
|
7
|
+
export interface RemoteTransport {
|
|
8
|
+
send(data: string): void;
|
|
9
|
+
/** Calls `handler` with each message received; returns a function that stops it. */
|
|
10
|
+
listen(handler: (data: string) => void): () => void;
|
|
11
|
+
/** Ends the channel (optional). */
|
|
12
|
+
close?(): void;
|
|
13
|
+
}
|
|
14
|
+
/** Operations a viewer may ask of a target. */
|
|
15
|
+
export type RemoteOperation = 'hello' | 'scene' | 'set' | 'undo' | 'redo' | 'capture' | 'stats';
|
|
16
|
+
/** One editable value as sent over the wire. */
|
|
17
|
+
export interface RemoteProperty {
|
|
18
|
+
key: string;
|
|
19
|
+
kind: PropertyKind;
|
|
20
|
+
min: number | null;
|
|
21
|
+
max: number | null;
|
|
22
|
+
value: PropertyValue;
|
|
23
|
+
}
|
|
24
|
+
/** One object of the remote scene as sent over the wire. */
|
|
25
|
+
export interface RemoteObject {
|
|
26
|
+
id: string;
|
|
27
|
+
label: string;
|
|
28
|
+
group: PackageObject['group'];
|
|
29
|
+
readOnly: string | null;
|
|
30
|
+
properties: RemoteProperty[];
|
|
31
|
+
}
|
|
32
|
+
/** What the target says about itself. */
|
|
33
|
+
export interface RemoteHello {
|
|
34
|
+
backend: string;
|
|
35
|
+
width: number;
|
|
36
|
+
height: number;
|
|
37
|
+
userAgent: string;
|
|
38
|
+
objects: number;
|
|
39
|
+
}
|
|
40
|
+
/** The target's frame rate and frame time, averaged over its last frames. */
|
|
41
|
+
export interface RemoteStats {
|
|
42
|
+
fps: number;
|
|
43
|
+
frameMilliseconds: number;
|
|
44
|
+
frames: number;
|
|
45
|
+
meshes: number;
|
|
46
|
+
}
|
|
47
|
+
/** A PNG of the target's canvas. */
|
|
48
|
+
export interface RemoteCapture {
|
|
49
|
+
data: string;
|
|
50
|
+
mimeType: 'image/png';
|
|
51
|
+
width: number;
|
|
52
|
+
height: number;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* A transport over a channel with `postMessage` (MessagePort, BroadcastChannel, Worker) or `send` (WebSocket,
|
|
56
|
+
* RTCDataChannel) and `message` events carrying strings.
|
|
57
|
+
*/
|
|
58
|
+
export declare function channelTransport(channel: {
|
|
59
|
+
postMessage?(data: string): void;
|
|
60
|
+
send?(data: string): void;
|
|
61
|
+
addEventListener(type: 'message', listener: (event: {
|
|
62
|
+
data: unknown;
|
|
63
|
+
}) => void): void;
|
|
64
|
+
removeEventListener(type: 'message', listener: (event: {
|
|
65
|
+
data: unknown;
|
|
66
|
+
}) => void): void;
|
|
67
|
+
start?(): void;
|
|
68
|
+
close?(): void;
|
|
69
|
+
}): RemoteTransport;
|
|
70
|
+
/** Options for `relayTransport`. */
|
|
71
|
+
export interface RelayTransportOptions {
|
|
72
|
+
/** The relay's URL, as `createRemoteRelay` reports it (`http://192.168.1.20:8787`). */
|
|
73
|
+
url: string;
|
|
74
|
+
/** The relay's token. Sent in a header, never in the URL. */
|
|
75
|
+
token: string;
|
|
76
|
+
/** Which side this page is: the scene (`target`) or the desktop panel (`viewer`). */
|
|
77
|
+
role: 'target' | 'viewer';
|
|
78
|
+
/** Called once when the stream or a send fails; the transport then stops. */
|
|
79
|
+
onError?(error: Error): void;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A transport through `@margoengine/inspector/remote-relay`: each message is POSTed, and the other side's messages
|
|
83
|
+
* arrive on one streamed response, one per line. Works in any browser with streamed fetch responses.
|
|
84
|
+
*/
|
|
85
|
+
export declare function relayTransport(options: RelayTransportOptions): RemoteTransport;
|
|
86
|
+
/** `RemoteTarget` options. */
|
|
87
|
+
export interface RemoteTargetOptions extends PackageObjectsOptions {
|
|
88
|
+
transport: RemoteTransport;
|
|
89
|
+
/** When set, every request must carry it (`RemoteInspector`'s `token`). */
|
|
90
|
+
token?: string;
|
|
91
|
+
/** Milliseconds between the stats the target sends unasked, 100 to 60,000, or 0 for none. Default 1,000. */
|
|
92
|
+
statsIntervalMs?: number;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The scene side: an engine plugin that answers a `RemoteInspector` over `transport`. Attach it in development
|
|
96
|
+
* builds of the page running on the phone or headset: `RemoteTarget.attach(engine, { transport, token })`.
|
|
97
|
+
*/
|
|
98
|
+
export declare class RemoteTarget implements EnginePlugin, Disposable {
|
|
99
|
+
private readonly options;
|
|
100
|
+
readonly name = "inspector:remote";
|
|
101
|
+
readonly history: EditHistory;
|
|
102
|
+
private engine?;
|
|
103
|
+
private stop?;
|
|
104
|
+
private timer?;
|
|
105
|
+
private readonly times;
|
|
106
|
+
private frames;
|
|
107
|
+
private disposed;
|
|
108
|
+
static attach(engine: EngineKernel, options: RemoteTargetOptions): RemoteTarget;
|
|
109
|
+
constructor(options: RemoteTargetOptions);
|
|
110
|
+
setup(engine: Engine): void;
|
|
111
|
+
onPreUpdate(delta: number): void;
|
|
112
|
+
/** The frame rate and frame time over the last 60 frames. */
|
|
113
|
+
stats(): RemoteStats;
|
|
114
|
+
/** Runs one operation as a viewer's request would (no token check): for tests and local use. */
|
|
115
|
+
handle(op: RemoteOperation, args?: Record<string, unknown>): Promise<unknown>;
|
|
116
|
+
private receive;
|
|
117
|
+
private post;
|
|
118
|
+
dispose(): void;
|
|
119
|
+
[Symbol.dispose](): void;
|
|
120
|
+
}
|
|
121
|
+
/** `RemoteInspector` options. */
|
|
122
|
+
export interface RemoteInspectorOptions {
|
|
123
|
+
transport: RemoteTransport;
|
|
124
|
+
/** The target's token, when it has one. */
|
|
125
|
+
token?: string;
|
|
126
|
+
/** Element for the panel; null for no panel. Default `document.body`. */
|
|
127
|
+
parent?: HTMLElement | null;
|
|
128
|
+
/** Most milliseconds to wait for a reply, 100 to 120,000. Default 10,000. */
|
|
129
|
+
timeoutMs?: number;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The desktop side: asks a `RemoteTarget` for its scene, edits it, takes screenshots and shows its frame rate, in a
|
|
133
|
+
* panel (`parent: null` for none). `await viewer.connect()` says hello and lists the scene.
|
|
134
|
+
*/
|
|
135
|
+
export declare class RemoteInspector implements Disposable {
|
|
136
|
+
private readonly options;
|
|
137
|
+
readonly element?: HTMLElement;
|
|
138
|
+
/** The objects of the last scene list. */
|
|
139
|
+
objects: RemoteObject[];
|
|
140
|
+
/** The target's latest stats (sent every second by default). */
|
|
141
|
+
stats?: RemoteStats;
|
|
142
|
+
/** The target's hello, once connected. */
|
|
143
|
+
target?: RemoteHello;
|
|
144
|
+
private next;
|
|
145
|
+
/** Tells this viewer's replies from another viewer's on the same relay. */
|
|
146
|
+
private readonly viewer;
|
|
147
|
+
private readonly waiting;
|
|
148
|
+
private readonly stop;
|
|
149
|
+
private readonly timeout;
|
|
150
|
+
private readonly body?;
|
|
151
|
+
private readonly status?;
|
|
152
|
+
private readonly image?;
|
|
153
|
+
private disposed;
|
|
154
|
+
constructor(options: RemoteInspectorOptions);
|
|
155
|
+
/** Sends one operation and resolves with the target's result; rejects with its error or on timeout. */
|
|
156
|
+
request<T = unknown>(op: RemoteOperation, args?: Record<string, unknown>): Promise<T>;
|
|
157
|
+
/** Says hello and lists the scene. */
|
|
158
|
+
connect(): Promise<RemoteHello>;
|
|
159
|
+
/** Lists the target's objects again and rebuilds the panel. */
|
|
160
|
+
scene(): Promise<RemoteObject[]>;
|
|
161
|
+
/** Edits one value on the target (checked there) and returns the object as it is now. */
|
|
162
|
+
set(id: string, key: string, value: PropertyValue): Promise<RemoteObject>;
|
|
163
|
+
/** Undoes the target's last remote edit; false when there was none. */
|
|
164
|
+
undo(): Promise<boolean>;
|
|
165
|
+
redo(): Promise<boolean>;
|
|
166
|
+
/** A PNG of the target's canvas, shown in the panel. */
|
|
167
|
+
capture(): Promise<RemoteCapture>;
|
|
168
|
+
private receive;
|
|
169
|
+
private line;
|
|
170
|
+
private show;
|
|
171
|
+
private build;
|
|
172
|
+
private inputs;
|
|
173
|
+
dispose(): void;
|
|
174
|
+
[Symbol.dispose](): void;
|
|
175
|
+
}
|
package/dist/remote.js
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
const s=(engine,version="0.1.0",source=import.meta.url)=>__margoRuntime(engine,version,source);
|
|
4
|
+
import { n as e } from "./errors-D8rc8kzg.js";
|
|
5
|
+
import { EditHistory as t } from "./history.js";
|
|
6
|
+
import { n, t as r } from "./color-D6qetPCJ.js";
|
|
7
|
+
import { describeObject as i, editProperty as a, findPackageObjects as o } from "./packages.js";
|
|
8
|
+
import { engineRuntime as __margoRuntime } from "@margoengine/core/extension";
|
|
9
|
+
//#region src/remote.ts
|
|
10
|
+
var c = "margo-remote", l = 1, u = 16e6;
|
|
11
|
+
function d(e) {
|
|
12
|
+
if (!(typeof e != "string" || e.length > u || !e.includes(c))) try {
|
|
13
|
+
let t = JSON.parse(e);
|
|
14
|
+
return t && typeof t == "object" && t[c] === l ? t : void 0;
|
|
15
|
+
} catch {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function f(e) {
|
|
20
|
+
let t = e?.send ?? e?.postMessage;
|
|
21
|
+
if (typeof t != "function" || typeof e.addEventListener != "function") throw TypeError("channelTransport needs a MessagePort, BroadcastChannel or WebSocket; pass one");
|
|
22
|
+
return {
|
|
23
|
+
send: (n) => t.call(e, n),
|
|
24
|
+
listen(t) {
|
|
25
|
+
let n = (e) => typeof e.data == "string" && t(e.data);
|
|
26
|
+
return e.addEventListener("message", n), e.start?.(), () => e.removeEventListener("message", n);
|
|
27
|
+
},
|
|
28
|
+
close: () => e.close?.()
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function p(t) {
|
|
32
|
+
let { url: n, token: r, role: i, onError: a } = t ?? {};
|
|
33
|
+
if (typeof n != "string" || !/^https?:\/\//.test(n)) throw TypeError("relayTransport needs the relay url (http or https); pass what createRemoteRelay reports");
|
|
34
|
+
if (typeof r != "string" || !r) throw TypeError("relayTransport needs the relay token; pass it");
|
|
35
|
+
if (i !== "target" && i !== "viewer") throw TypeError("relayTransport role must be 'target' or 'viewer'");
|
|
36
|
+
let o = n.replace(/\/+$/, ""), s = {
|
|
37
|
+
Authorization: `Bearer ${r}`,
|
|
38
|
+
"X-Margo-Role": i
|
|
39
|
+
}, c = new AbortController(), l = !1, u = Promise.resolve(), d = (e) => {
|
|
40
|
+
l || c.signal.aborted || (l = !0, c.abort(), a?.(e instanceof Error ? e : Error(String(e))));
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
send(t) {
|
|
44
|
+
u = u.then(async () => {
|
|
45
|
+
if (!(l || c.signal.aborted)) try {
|
|
46
|
+
let n = await fetch(`${o}/send`, {
|
|
47
|
+
method: "POST",
|
|
48
|
+
headers: {
|
|
49
|
+
...s,
|
|
50
|
+
"Content-Type": "application/json"
|
|
51
|
+
},
|
|
52
|
+
body: t,
|
|
53
|
+
signal: c.signal
|
|
54
|
+
});
|
|
55
|
+
if (!n.ok) throw e("failed", `Relay refused a message (${n.status}); check the url and token`);
|
|
56
|
+
} catch (e) {
|
|
57
|
+
d(e);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
listen(t) {
|
|
62
|
+
let n = new AbortController(), r = AbortSignal.any([n.signal, c.signal]);
|
|
63
|
+
return (async () => {
|
|
64
|
+
try {
|
|
65
|
+
let n = await fetch(`${o}/events`, {
|
|
66
|
+
headers: s,
|
|
67
|
+
signal: r
|
|
68
|
+
});
|
|
69
|
+
if (!n.ok || !n.body) throw e("failed", `Relay refused the stream (${n.status}); check the url and token`);
|
|
70
|
+
let i = n.body.pipeThrough(new TextDecoderStream()).getReader(), a = "";
|
|
71
|
+
for (;;) {
|
|
72
|
+
let { value: e, done: n } = await i.read();
|
|
73
|
+
if (n) break;
|
|
74
|
+
let r = (a + e).split("\n");
|
|
75
|
+
a = r.pop();
|
|
76
|
+
for (let e of r) e && t(e);
|
|
77
|
+
}
|
|
78
|
+
if (!r.aborted) throw e("failed", "Relay closed the stream; restart the relay and reconnect");
|
|
79
|
+
} catch (e) {
|
|
80
|
+
r.aborted || d(e);
|
|
81
|
+
}
|
|
82
|
+
})(), () => n.abort();
|
|
83
|
+
},
|
|
84
|
+
close: () => c.abort()
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function m(e, t) {
|
|
88
|
+
let n = o(e, t);
|
|
89
|
+
for (let t of e.scene.meshes.values()) n.push({
|
|
90
|
+
id: `mesh:${t.id}`,
|
|
91
|
+
group: "object",
|
|
92
|
+
label: t.name || `Mesh ${t.id}`,
|
|
93
|
+
target: t,
|
|
94
|
+
properties: i(t)
|
|
95
|
+
});
|
|
96
|
+
return n;
|
|
97
|
+
}
|
|
98
|
+
var h = (e) => ({
|
|
99
|
+
id: e.id,
|
|
100
|
+
label: e.label,
|
|
101
|
+
group: e.group,
|
|
102
|
+
readOnly: e.readOnly ?? null,
|
|
103
|
+
properties: e.properties.map((e) => {
|
|
104
|
+
let t = e.get();
|
|
105
|
+
return {
|
|
106
|
+
key: e.key,
|
|
107
|
+
kind: e.kind,
|
|
108
|
+
min: Number.isFinite(e.min) ? e.min : null,
|
|
109
|
+
max: Number.isFinite(e.max) ? e.max : null,
|
|
110
|
+
value: typeof t == "object" ? Array.from(t) : t
|
|
111
|
+
};
|
|
112
|
+
})
|
|
113
|
+
}), g = class n {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
114
|
+
static attach(e, t) {__margoAssertCore(e,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
115
|
+
let r = new n(t);
|
|
116
|
+
return s(e).use(r), r;
|
|
117
|
+
}
|
|
118
|
+
constructor(e) {
|
|
119
|
+
if (this.options = e, this.name = "inspector:remote", this.history = new t(), this.times = [], this.frames = 0, this.disposed = !1, !e?.transport || typeof e.transport.send != "function") throw TypeError("RemoteTarget needs a transport; pass channelTransport(...) or relayTransport(...)");
|
|
120
|
+
let n = e.statsIntervalMs ?? 1e3;
|
|
121
|
+
if (!Number.isFinite(n) || n !== 0 && (n < 100 || n > 6e4)) throw RangeError("RemoteTarget statsIntervalMs must be 0 or 100 to 60000; pass milliseconds in range");
|
|
122
|
+
if (e.token !== void 0 && (typeof e.token != "string" || e.token.length < 8)) throw TypeError("RemoteTarget token must be a string of 8 or more characters; pass a longer secret");
|
|
123
|
+
}
|
|
124
|
+
setup(t) {
|
|
125
|
+
if (this.disposed || this.engine) throw e("attached", "RemoteTarget is disposed or already attached; create a new RemoteTarget");
|
|
126
|
+
this.engine = t, this.stop = this.options.transport.listen((e) => void this.receive(e));
|
|
127
|
+
let n = this.options.statsIntervalMs ?? 1e3;
|
|
128
|
+
n && (this.timer = setInterval(() => this.post({
|
|
129
|
+
[c]: l,
|
|
130
|
+
event: "stats",
|
|
131
|
+
data: this.stats()
|
|
132
|
+
}), n));
|
|
133
|
+
}
|
|
134
|
+
onPreUpdate(e) {
|
|
135
|
+
this.frames++, e > 0 && (this.times.push(e), this.times.length > 60 && this.times.shift());
|
|
136
|
+
}
|
|
137
|
+
stats() {
|
|
138
|
+
let e = this.times.length ? this.times.reduce((e, t) => e + t, 0) / this.times.length : 0;
|
|
139
|
+
return {
|
|
140
|
+
fps: e ? 1 / e : 0,
|
|
141
|
+
frameMilliseconds: e * 1e3,
|
|
142
|
+
frames: this.frames,
|
|
143
|
+
meshes: this.engine?.scene.meshes.size ?? 0
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
async handle(t, n = {}) {
|
|
147
|
+
let r = this.engine;
|
|
148
|
+
if (!r || this.disposed) throw e("state", "RemoteTarget is not attached; attach it to an engine first");
|
|
149
|
+
if (t === "stats") return this.stats();
|
|
150
|
+
if (t === "undo" || t === "redo") return this.history[t]();
|
|
151
|
+
if (t === "capture") return _(r);
|
|
152
|
+
let i = m(r, this.options);
|
|
153
|
+
if (t === "hello") {
|
|
154
|
+
let e = r.canvas;
|
|
155
|
+
return {
|
|
156
|
+
backend: r.backend,
|
|
157
|
+
width: e.width ?? 0,
|
|
158
|
+
height: e.height ?? 0,
|
|
159
|
+
userAgent: typeof navigator > "u" ? "" : navigator.userAgent,
|
|
160
|
+
objects: i.length
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
if (t === "scene") return i.map(h);
|
|
164
|
+
if (t === "set") {
|
|
165
|
+
let e = i.find((e) => e.id === n.id);
|
|
166
|
+
if (!e) throw TypeError(`No object ${String(n.id)}; pick an id from the scene list`);
|
|
167
|
+
if (typeof n.key != "string") throw TypeError("set needs a property key; pass one from the scene list");
|
|
168
|
+
return a(e, n.key, n.value, this.history), h(e);
|
|
169
|
+
}
|
|
170
|
+
throw TypeError(`Unknown remote operation ${String(t)}; use hello, scene, set, undo, redo, capture or stats`);
|
|
171
|
+
}
|
|
172
|
+
async receive(t) {
|
|
173
|
+
let n = d(t);
|
|
174
|
+
if (!n || typeof n.op != "string" || typeof n.id != "number") return;
|
|
175
|
+
let r;
|
|
176
|
+
try {
|
|
177
|
+
if (this.options.token !== void 0 && n.token !== this.options.token) throw e("state", "Wrong or missing token; pass the token the target page was given");
|
|
178
|
+
r = {
|
|
179
|
+
[c]: l,
|
|
180
|
+
id: n.id,
|
|
181
|
+
ok: !0,
|
|
182
|
+
result: await this.handle(n.op, n.args ?? {})
|
|
183
|
+
};
|
|
184
|
+
} catch (e) {
|
|
185
|
+
r = {
|
|
186
|
+
[c]: l,
|
|
187
|
+
id: n.id,
|
|
188
|
+
ok: !1,
|
|
189
|
+
error: e?.message ?? String(e)
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
typeof n.viewer == "string" && (r.viewer = n.viewer), this.post(r);
|
|
193
|
+
}
|
|
194
|
+
post(e) {
|
|
195
|
+
this.disposed || this.options.transport.send(JSON.stringify(e));
|
|
196
|
+
}
|
|
197
|
+
dispose() {
|
|
198
|
+
if (this.disposed) return;
|
|
199
|
+
this.disposed = !0, clearInterval(this.timer), this.stop?.();
|
|
200
|
+
let e = this.engine;
|
|
201
|
+
this.engine = void 0, e?.sceneOf(this)?.removePlugin(this.name);
|
|
202
|
+
}
|
|
203
|
+
[Symbol.dispose]() {
|
|
204
|
+
this.dispose();
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
async function _(t) {
|
|
208
|
+
t.render();
|
|
209
|
+
let n = t.canvas;
|
|
210
|
+
if (typeof n.toBlob != "function") throw e("unsupported", "Capture needs an HTML canvas; capture on a page, not a worker canvas");
|
|
211
|
+
let r = await new Promise((e) => n.toBlob(e, "image/png"));
|
|
212
|
+
if (!r) throw e("failed", "Canvas encoding failed; use only origin-clean textures");
|
|
213
|
+
let i = new Uint8Array(await r.arrayBuffer());
|
|
214
|
+
if (i.length > 12e6) throw RangeError("Canvas PNG exceeds 12 MB; lower the canvas resolution");
|
|
215
|
+
let a = "";
|
|
216
|
+
for (let e = 0; e < i.length; e += 8192) a += String.fromCharCode(...i.subarray(e, e + 8192));
|
|
217
|
+
return {
|
|
218
|
+
data: btoa(a),
|
|
219
|
+
mimeType: "image/png",
|
|
220
|
+
width: n.width,
|
|
221
|
+
height: n.height
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
var v = "\n:host{position:fixed;left:12px;top:12px;width:340px;max-width:calc(100vw - 24px);max-height:calc(100dvh - 24px);overflow:auto;z-index:10001;display:block;color:#202226;background:#fafbfc;border:1px solid #b9bec7;border-radius:4px;font:12px/1.4 system-ui;letter-spacing:0}\n*{box-sizing:border-box}h2{font-size:14px;margin:0;padding:10px 12px;border-bottom:1px solid #d5d9df}.row{display:flex;gap:8px;align-items:center;padding:6px 12px}.status{color:#5d636b}.status[data-state=error]{color:#b42318}\ndetails{border-bottom:1px solid #d5d9df;padding:6px 12px}summary{cursor:pointer;font-weight:600}label{display:flex;align-items:center;justify-content:space-between;gap:8px;min-height:28px}input{font:inherit;min-width:0}input[type=number]{width:72px}.axes{display:flex;gap:4px}.axes input{width:56px}\ninput[type=color]{width:48px;height:24px;padding:0}input:disabled{opacity:.5}img{display:block;max-width:calc(100% - 24px);margin:6px 12px}button{font:inherit}\n", y = class {
|
|
225
|
+
constructor(e) {
|
|
226
|
+
if (this.options = e, this.objects = [], this.next = 1, this.viewer = Math.random().toString(36).slice(2) + Date.now().toString(36), this.waiting = /* @__PURE__ */ new Map(), this.disposed = !1, !e?.transport || typeof e.transport.listen != "function") throw TypeError("RemoteInspector needs a transport; pass channelTransport(...) or relayTransport(...)");
|
|
227
|
+
if (this.timeout = e.timeoutMs ?? 1e4, !Number.isFinite(this.timeout) || this.timeout < 100 || this.timeout > 12e4) throw RangeError("RemoteInspector timeoutMs must be 100 to 120000; pass milliseconds in range");
|
|
228
|
+
this.stop = e.transport.listen((e) => this.receive(e));
|
|
229
|
+
let t = e.parent === void 0 ? document.body : e.parent;
|
|
230
|
+
if (!t) return;
|
|
231
|
+
let n = t.ownerDocument;
|
|
232
|
+
this.element = n.createElement("div"), this.element.dataset.margoRemoteInspector = "";
|
|
233
|
+
let r = this.element.attachShadow({ mode: "open" }), i = n.createElement("style"), a = n.createElement("h2"), o = n.createElement("div"), s = n.createElement("button"), c = n.createElement("button");
|
|
234
|
+
i.textContent = v, a.textContent = "Remote scene", o.className = "row", s.textContent = "Refresh", c.textContent = "Screenshot", this.status = n.createElement("span"), this.status.className = "status", this.status.setAttribute("role", "status"), this.status.textContent = "Not connected", this.image = n.createElement("img"), this.image.alt = "Remote canvas", this.image.hidden = !0, this.body = n.createElement("div"), o.append(s, c, this.status), r.append(i, a, o, this.image, this.body), t.append(this.element), s.addEventListener("click", () => void this.scene().catch((e) => this.show(e))), c.addEventListener("click", () => void this.capture().catch((e) => this.show(e)));
|
|
235
|
+
}
|
|
236
|
+
request(t, n) {
|
|
237
|
+
if (this.disposed) return Promise.reject(e("disposed", "RemoteInspector is disposed; create a new one"));
|
|
238
|
+
let r = this.next++;
|
|
239
|
+
return new Promise((i, a) => {
|
|
240
|
+
let o = setTimeout(() => {
|
|
241
|
+
this.waiting.delete(r), a(e("timeout", `The target did not answer ${t} in ${this.timeout} ms; check that its page is open and attached`));
|
|
242
|
+
}, this.timeout);
|
|
243
|
+
this.waiting.set(r, {
|
|
244
|
+
resolve: i,
|
|
245
|
+
reject: a,
|
|
246
|
+
timer: o
|
|
247
|
+
}), this.options.transport.send(JSON.stringify({
|
|
248
|
+
[c]: l,
|
|
249
|
+
id: r,
|
|
250
|
+
op: t,
|
|
251
|
+
args: n,
|
|
252
|
+
token: this.options.token,
|
|
253
|
+
viewer: this.viewer
|
|
254
|
+
}));
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
async connect() {
|
|
258
|
+
return this.target = await this.request("hello"), await this.scene(), this.target;
|
|
259
|
+
}
|
|
260
|
+
async scene() {
|
|
261
|
+
return this.objects = await this.request("scene"), this.build(), this.objects;
|
|
262
|
+
}
|
|
263
|
+
async set(e, t, n) {
|
|
264
|
+
let r = await this.request("set", {
|
|
265
|
+
id: e,
|
|
266
|
+
key: t,
|
|
267
|
+
value: n
|
|
268
|
+
}), i = this.objects.findIndex((t) => t.id === e);
|
|
269
|
+
return i >= 0 && (this.objects[i] = r), r;
|
|
270
|
+
}
|
|
271
|
+
undo() {
|
|
272
|
+
return this.request("undo");
|
|
273
|
+
}
|
|
274
|
+
redo() {
|
|
275
|
+
return this.request("redo");
|
|
276
|
+
}
|
|
277
|
+
async capture() {
|
|
278
|
+
let e = await this.request("capture");
|
|
279
|
+
return this.image && (this.image.src = `data:${e.mimeType};base64,${e.data}`, this.image.hidden = !1), e;
|
|
280
|
+
}
|
|
281
|
+
receive(e) {
|
|
282
|
+
let t = d(e);
|
|
283
|
+
if (!t) return;
|
|
284
|
+
if (t.event === "stats") {
|
|
285
|
+
this.stats = t.data, this.line();
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
if (t.viewer !== void 0 && t.viewer !== this.viewer) return;
|
|
289
|
+
let n = typeof t.id == "number" ? this.waiting.get(t.id) : void 0;
|
|
290
|
+
n && t.op === void 0 && (this.waiting.delete(t.id), clearTimeout(n.timer), t.ok ? n.resolve(t.result) : n.reject(Error(t.error ?? "The target refused the request; read the target page console")));
|
|
291
|
+
}
|
|
292
|
+
line() {
|
|
293
|
+
if (!this.status) return;
|
|
294
|
+
this.status.dataset.state = "ok";
|
|
295
|
+
let e = this.target, t = this.stats;
|
|
296
|
+
this.status.textContent = [e ? `${e.backend} ${e.width}x${e.height}` : "", t ? `${t.fps.toFixed(0)} fps` : ""].filter(Boolean).join(", ");
|
|
297
|
+
}
|
|
298
|
+
show(e) {
|
|
299
|
+
this.status && (this.status.dataset.state = "error", this.status.textContent = e.message);
|
|
300
|
+
}
|
|
301
|
+
build() {
|
|
302
|
+
if (!this.body) return;
|
|
303
|
+
this.line();
|
|
304
|
+
let e = this.body.ownerDocument;
|
|
305
|
+
this.body.replaceChildren(...this.objects.map((t) => {
|
|
306
|
+
let n = e.createElement("details"), r = e.createElement("summary");
|
|
307
|
+
r.textContent = `${t.label} (${t.group})`, n.append(r);
|
|
308
|
+
for (let r of t.properties) {
|
|
309
|
+
let i = e.createElement("label"), a = e.createElement("span"), o = this.inputs(e, t, r);
|
|
310
|
+
a.textContent = r.key, i.append(a, ...o), n.append(i);
|
|
311
|
+
}
|
|
312
|
+
return n;
|
|
313
|
+
}));
|
|
314
|
+
}
|
|
315
|
+
inputs(e, t, i) {
|
|
316
|
+
let a = (e) => void this.set(t.id, i.key, e).catch((e) => this.show(e)), o = (n, r) => {
|
|
317
|
+
let a = e.createElement("input");
|
|
318
|
+
return a.type = n, a.value = r, a.disabled = !!t.readOnly, a.setAttribute("aria-label", `${t.label} ${i.key}`), i.min !== null && (a.min = String(i.min)), i.max !== null && (a.max = String(i.max)), n === "number" && (a.step = "any"), a;
|
|
319
|
+
};
|
|
320
|
+
if (i.kind === "boolean") {
|
|
321
|
+
let e = o("checkbox", "");
|
|
322
|
+
return e.checked = i.value === !0, e.addEventListener("change", () => a(e.checked)), [e];
|
|
323
|
+
}
|
|
324
|
+
if (i.kind === "number") {
|
|
325
|
+
let e = o("number", String(i.value));
|
|
326
|
+
return e.addEventListener("change", () => a(Number(e.value))), [e];
|
|
327
|
+
}
|
|
328
|
+
if (i.kind === "color") {
|
|
329
|
+
let e = o("color", r(i.value));
|
|
330
|
+
return e.addEventListener("input", () => a(n(e.value))), [e];
|
|
331
|
+
}
|
|
332
|
+
let s = e.createElement("span"), c = i.value.map((e) => o("number", String(e)));
|
|
333
|
+
s.className = "axes", s.append(...c);
|
|
334
|
+
for (let e of c) e.addEventListener("change", () => a(c.map((e) => Number(e.value))));
|
|
335
|
+
return [s];
|
|
336
|
+
}
|
|
337
|
+
dispose() {
|
|
338
|
+
if (!this.disposed) {
|
|
339
|
+
this.disposed = !0, this.stop();
|
|
340
|
+
for (let [, t] of this.waiting) clearTimeout(t.timer), t.reject(e("disposed", "RemoteInspector was disposed before the target answered"));
|
|
341
|
+
this.waiting.clear(), this.element?.remove();
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
[Symbol.dispose]() {
|
|
345
|
+
this.dispose();
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
//#endregion
|
|
349
|
+
export { y as RemoteInspector, g as RemoteTarget, f as channelTransport, p as relayTransport };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Engine, Mesh } from '@margoengine/core/extension';
|
|
2
|
+
/** What the engine holds, counted from its meshes: the numbers an inspector shows in its Resources section. */
|
|
3
|
+
export interface EngineResources {
|
|
4
|
+
/** `'webgpu'` or `'webgl2'`. */
|
|
5
|
+
backend: string;
|
|
6
|
+
/** Meshes in the scene. */
|
|
7
|
+
meshes: number;
|
|
8
|
+
/** Meshes currently visible. */
|
|
9
|
+
visibleMeshes: number;
|
|
10
|
+
/** Distinct geometry or model sources: meshes sharing one count once. */
|
|
11
|
+
geometries: number;
|
|
12
|
+
/** Distinct materials. */
|
|
13
|
+
materials: number;
|
|
14
|
+
/** Vertices of the distinct geometries. */
|
|
15
|
+
vertices: number;
|
|
16
|
+
/** Triangles of the visible meshes: each mesh counts its geometry, so instances count each time. */
|
|
17
|
+
triangles: number;
|
|
18
|
+
/** Names of the attached plugins (post effects, sky, controls...), in attach order. */
|
|
19
|
+
plugins: string[];
|
|
20
|
+
/** Drawing-buffer size in pixels. */
|
|
21
|
+
canvas: {
|
|
22
|
+
width: number;
|
|
23
|
+
height: number;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Counts what `engine` draws from `meshes` (default: every mesh of the engine's default scene).
|
|
28
|
+
* @throws TypeError when `engine` is not an engine.
|
|
29
|
+
*/
|
|
30
|
+
export declare function describeEngine(engine: Engine, meshes?: Iterable<Mesh>): EngineResources;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { r as e } from "./mesh-source-BSQA_1GQ.js";
|
|
2
|
+
//#region src/resources.ts
|
|
3
|
+
function t(t, n) {
|
|
4
|
+
if (!t || typeof t != "object" || !t.meshes || !t.scene) throw TypeError("Not an engine; pass the Engine from Engine.create or the inspector engine");
|
|
5
|
+
let r = n ?? t.scene.meshes.values(), i = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Set(), o = 0, s = 0, c = 0, l = 0;
|
|
6
|
+
for (let t of r) {
|
|
7
|
+
o++, t.material && a.add(t.material);
|
|
8
|
+
let n = e(t);
|
|
9
|
+
if (!n) continue;
|
|
10
|
+
let r = n.kind === "core" ? [{
|
|
11
|
+
vertices: n.geometry.vertices.length / 6,
|
|
12
|
+
indices: n.geometry.indices.length
|
|
13
|
+
}] : n.source.primitives.map((e) => ({
|
|
14
|
+
vertices: e.vertices.length / 20,
|
|
15
|
+
indices: e.indices.length
|
|
16
|
+
})), u = n.kind === "core" ? n.geometry : n.source;
|
|
17
|
+
if (!i.has(u)) {
|
|
18
|
+
i.add(u);
|
|
19
|
+
for (let e of r) c += e.vertices;
|
|
20
|
+
}
|
|
21
|
+
if (t.visible) {
|
|
22
|
+
s++;
|
|
23
|
+
for (let e of r) l += e.indices / 3;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
backend: t.backend,
|
|
28
|
+
meshes: o,
|
|
29
|
+
visibleMeshes: s,
|
|
30
|
+
geometries: i.size,
|
|
31
|
+
materials: a.size,
|
|
32
|
+
vertices: c,
|
|
33
|
+
triangles: l,
|
|
34
|
+
plugins: [...t.plugins.keys()],
|
|
35
|
+
canvas: {
|
|
36
|
+
width: t.canvas.width,
|
|
37
|
+
height: t.canvas.height
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
export { t as describeEngine };
|