comfy-ts 2.21.0 → 2.23.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/dist/{ServeApp-ByMobrWW.js → ServeApp-DolRCMHj.js} +183 -14
- package/dist/ServeApp-DolRCMHj.js.map +1 -0
- package/dist/cli.js +5 -5
- package/dist/{exampleAssets-B5sMqNia.js → exampleAssets-Duneoq6r.js} +331 -42
- package/dist/exampleAssets-Duneoq6r.js.map +1 -0
- package/dist/{findDefinedWorkflow-BNZgYIww.js → findDefinedWorkflow-DjoJA8y7.js} +2 -2
- package/dist/{findDefinedWorkflow-BNZgYIww.js.map → findDefinedWorkflow-DjoJA8y7.js.map} +1 -1
- package/dist/index.cjs +381 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +201 -24
- package/dist/index.d.ts +201 -24
- package/dist/index.js +380 -35
- package/dist/index.js.map +1 -1
- package/dist/{run-run-CuBSygku.js → run-run-Cr0L9lCK.js} +5 -5
- package/dist/{run-run-CuBSygku.js.map → run-run-Cr0L9lCK.js.map} +1 -1
- package/dist/{run-serve-COk_G0Qz.js → run-serve---qA7o_m.js} +4 -4
- package/dist/{run-serve-COk_G0Qz.js.map → run-serve---qA7o_m.js.map} +1 -1
- package/dist/{run-tui-CB6i_x2y.js → run-tui-DxXiWo5J.js} +4 -4
- package/dist/{run-tui-CB6i_x2y.js.map → run-tui-DxXiWo5J.js.map} +1 -1
- package/dist/{schemaDrift-CHDVYlaj.js → schemaDrift-BTxdABHi.js} +36 -2
- package/dist/schemaDrift-BTxdABHi.js.map +1 -0
- package/dist/serve-web.js +207 -47
- package/dist/web.d.ts +201 -24
- package/dist/web.js +380 -35
- package/dist/web.js.map +1 -1
- package/examples/README.md +2 -0
- package/examples/comfy-cloud/sdk.d.ts +102 -1
- package/examples/depth-explorer/models.ts +722 -0
- package/examples/depth-explorer/serve.ts +140 -0
- package/examples/depth-explorer/thumbs/bae.webp +0 -0
- package/examples/depth-explorer/thumbs/da3-mono-large.webp +0 -0
- package/examples/depth-explorer/thumbs/dav1-vitl.webp +0 -0
- package/examples/depth-explorer/thumbs/dav2-vitl.webp +0 -0
- package/examples/depth-explorer/thumbs/dsine.webp +0 -0
- package/examples/depth-explorer/thumbs/iid-albedo.webp +0 -0
- package/examples/depth-explorer/thumbs/iid-metalness.webp +0 -0
- package/examples/depth-explorer/thumbs/iid-roughness.webp +0 -0
- package/examples/depth-explorer/thumbs/iid-shading.webp +0 -0
- package/examples/depth-explorer/thumbs/iid-specular.webp +0 -0
- package/examples/depth-explorer/thumbs/leres.webp +0 -0
- package/examples/depth-explorer/thumbs/lotus-depth.webp +0 -0
- package/examples/depth-explorer/thumbs/marigold2-albedo.webp +0 -0
- package/examples/depth-explorer/thumbs/marigold2-depth.webp +0 -0
- package/examples/depth-explorer/thumbs/marigold2-normal.webp +0 -0
- package/examples/depth-explorer/thumbs/metric3d-depth.webp +0 -0
- package/examples/depth-explorer/thumbs/metric3d-normal.webp +0 -0
- package/examples/depth-explorer/thumbs/midas-normal.webp +0 -0
- package/examples/depth-explorer/thumbs/midas.webp +0 -0
- package/examples/depth-explorer/thumbs/moge2-depth.webp +0 -0
- package/examples/depth-explorer/thumbs/moge2-normal.webp +0 -0
- package/examples/depth-explorer/thumbs/zoe-da.webp +0 -0
- package/examples/depth-explorer/thumbs/zoe.webp +0 -0
- package/examples/depth-explorer/web/App.tsx +457 -0
- package/examples/depth-explorer/web/gl.ts +202 -0
- package/examples/depth-explorer/web/index.html +14 -0
- package/examples/depth-explorer/web/main.tsx +19 -0
- package/examples/depth-explorer/web/pixels.ts +86 -0
- package/examples/depth-explorer/web/state.ts +329 -0
- package/examples/depth-explorer/web/styles.css +366 -0
- package/examples/depth-explorer/web/tsconfig.json +16 -0
- package/examples/rvion/17-relight.cflow.ts +110 -0
- package/examples/rvion/18-world.cflow.ts +101 -0
- package/examples/world-explorer/serve.ts +171 -0
- package/examples/world-explorer/web/index.html +14 -0
- package/examples/world-explorer/web/main.tsx +200 -0
- package/examples/world-explorer/web/state.ts +212 -0
- package/examples/world-explorer/web/styles.css +131 -0
- package/examples/world-explorer/web/thumb.ts +21 -0
- package/examples/world-explorer/web/tsconfig.json +11 -0
- package/examples/world-explorer/world-state.ts +40 -0
- package/package.json +6 -7
- package/src/cli/serve/ServeApp.ts +106 -6
- package/src/cli/serve/describeVar.ts +12 -1
- package/src/cli/serve/resultHistory.ts +28 -5
- package/src/cli/serve/thumbs.ts +77 -0
- package/src/cli/serve/web/api.ts +9 -1
- package/src/cli/serve/web/components/Gallery.tsx +26 -1
- package/src/cli/serve/web/components/controls/BasicControls.tsx +14 -17
- package/src/cli/serve/web/components/controls/MediaControl.tsx +62 -9
- package/src/cli/serve/web/components/controls/RichSelect.tsx +262 -0
- package/src/cli/serve/web/state/RunSt.poll.test.ts +4 -1
- package/src/cli/serve/web/state/choiceButtons.ts +26 -2
- package/src/cli/serve/web/styles.ts +42 -0
- package/src/cli/serve/web/widgets/OutputWidgets.tsx +19 -0
- package/src/cli/serve/web/widgets/RelightSt.ts +113 -0
- package/src/cli/serve/web/widgets/RelightWidget.tsx +305 -0
- package/src/cli/serve/web/widgets/relightGl.ts +305 -0
- package/src/cli/serve/web/widgets/widgetStyles.ts +19 -0
- package/src/coreExports.ts +10 -0
- package/src/graph/ComfyNode.ts +12 -2
- package/src/host/ComfyHost.ts +153 -2
- package/src/host/ResilientWebsocket.ts +10 -0
- package/src/manager/script-update-manager.bun.ts +0 -7
- package/src/runner/ComfyExecution.ts +165 -20
- package/src/runner/ComfyWsApi.ts +2 -0
- package/src/runner/historyReplay.ts +53 -0
- package/src/runner/outputs3d.ts +18 -0
- package/src/sdk-generator/comfyui-sdk-codegen.ts +7 -2
- package/src/vars/ComfyVars.ts +32 -2
- package/src/vars/DefinedWorkflow.ts +4 -0
- package/src/vars/outputWidgets.ts +47 -0
- package/dist/ServeApp-ByMobrWW.js.map +0 -1
- package/dist/exampleAssets-B5sMqNia.js.map +0 -1
- package/dist/schemaDrift-CHDVYlaj.js.map +0 -1
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// world explorer: one world of .comfy-ts/worlds/<name>/ drawn in the browser (a panorama to look around, a mesh to
|
|
2
|
+
// walk, a splat to orbit, the source picture), the view page of the `world` thread kind (world-kind.shipkit.ts).
|
|
3
|
+
// bun examples/world-explorer/serve.ts → http://127.0.0.1:8293/?key=<world>
|
|
4
|
+
// the hub serves this page again through its proxy: every path the page asks is RELATIVE, so the bundle is built with
|
|
5
|
+
// publicPath './' and the world files answer under `worlds/<name>/<file>`
|
|
6
|
+
import { closeSync, existsSync, openSync, readdirSync, readSync, renameSync, statSync } from 'node:fs'
|
|
7
|
+
import { join, resolve } from 'pathe'
|
|
8
|
+
|
|
9
|
+
const PORT = Number(process.env.PORT ?? 8293)
|
|
10
|
+
const ROOT = resolve(import.meta.dir, '../..')
|
|
11
|
+
const WORLDS = join(ROOT, '.comfy-ts/worlds')
|
|
12
|
+
const WEB = join(import.meta.dir, 'web')
|
|
13
|
+
|
|
14
|
+
/** the files a world may hold, in the order the page shows their tabs */
|
|
15
|
+
const FILES = ['panorama.png', 'world.glb', 'splat.ply', 'source.png', 'thumb.webp'] as const
|
|
16
|
+
/** a thumbnail is a few tens of KB: anything far past it is not one */
|
|
17
|
+
const THUMB_MAX = 2_000_000
|
|
18
|
+
|
|
19
|
+
type WorldFile = { name: string; v: string }
|
|
20
|
+
type World = { name: string; files: WorldFile[]; splatFov: number | null }
|
|
21
|
+
|
|
22
|
+
/** the vertical fov of the camera a SHARP ply was made for, from its `intrinsic` (fy at index 4) and `image_size`
|
|
23
|
+
* elements after the vertices; null when the header names none of them */
|
|
24
|
+
function splatFov(path: string): number | null {
|
|
25
|
+
const fd = openSync(path, 'r')
|
|
26
|
+
try {
|
|
27
|
+
const head = Buffer.alloc(4096)
|
|
28
|
+
readSync(fd, head, 0, head.length, 0)
|
|
29
|
+
const end = head.indexOf('end_header\n')
|
|
30
|
+
if (end < 0) return null
|
|
31
|
+
const lines = head.subarray(0, end).toString('latin1').split('\n')
|
|
32
|
+
const sizes: Record<string, number> = { float: 4, uint: 4, int: 4, uchar: 1, double: 8 }
|
|
33
|
+
let offset = end + 'end_header\n'.length
|
|
34
|
+
let el: { name: string; count: number; stride: number } | null = null
|
|
35
|
+
const els: { name: string; count: number; stride: number; at: number }[] = []
|
|
36
|
+
for (const l of lines) {
|
|
37
|
+
const w = l.trim().split(/\s+/)
|
|
38
|
+
if (w[0] === 'element') {
|
|
39
|
+
el = { name: String(w[1]), count: Number(w[2]), stride: 0 }
|
|
40
|
+
els.push({ ...el, at: 0 })
|
|
41
|
+
} else if (w[0] === 'property' && el != null) {
|
|
42
|
+
const last = els[els.length - 1]
|
|
43
|
+
if (last != null) last.stride += sizes[String(w[1])] ?? 4
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
for (const e of els) {
|
|
47
|
+
e.at = offset
|
|
48
|
+
offset += e.count * e.stride
|
|
49
|
+
}
|
|
50
|
+
const intr = els.find((e) => e.name === 'intrinsic')
|
|
51
|
+
const size = els.find((e) => e.name === 'image_size')
|
|
52
|
+
if (intr == null || size == null) return null
|
|
53
|
+
const buf = Buffer.alloc(8)
|
|
54
|
+
readSync(fd, buf, 0, 4, intr.at + 4 * 4)
|
|
55
|
+
const fy = buf.readFloatLE(0)
|
|
56
|
+
readSync(fd, buf, 0, 8, size.at)
|
|
57
|
+
const h = buf.readUInt32LE(4)
|
|
58
|
+
if (!(fy > 0 && h > 0)) return null
|
|
59
|
+
return (2 * Math.atan(h / 2 / fy) * 180) / Math.PI
|
|
60
|
+
} finally {
|
|
61
|
+
closeSync(fd)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const fovCache = new Map<string, number | null>()
|
|
66
|
+
|
|
67
|
+
function worldOf(name: string): World | null {
|
|
68
|
+
if (!/^[a-z0-9][a-z0-9-]*$/.test(name)) return null
|
|
69
|
+
const dir = join(WORLDS, name)
|
|
70
|
+
if (!existsSync(dir)) return null
|
|
71
|
+
const files: WorldFile[] = []
|
|
72
|
+
for (const f of FILES) {
|
|
73
|
+
const p = join(dir, f)
|
|
74
|
+
if (!existsSync(p)) continue
|
|
75
|
+
const s = statSync(p)
|
|
76
|
+
files.push({ name: f, v: `${s.size.toString(36)}-${Math.round(s.mtimeMs).toString(36)}` })
|
|
77
|
+
}
|
|
78
|
+
const ply = files.find((f) => f.name === 'splat.ply')
|
|
79
|
+
let fov: number | null = null
|
|
80
|
+
if (ply != null) {
|
|
81
|
+
const k = `${name}/${ply.v}`
|
|
82
|
+
if (!fovCache.has(k)) fovCache.set(k, splatFov(join(dir, ply.name)))
|
|
83
|
+
fov = fovCache.get(k) ?? null
|
|
84
|
+
}
|
|
85
|
+
return { name, files, splatFov: fov }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function worlds(): World[] {
|
|
89
|
+
if (!existsSync(WORLDS)) return []
|
|
90
|
+
return readdirSync(WORLDS)
|
|
91
|
+
.sort()
|
|
92
|
+
.map(worldOf)
|
|
93
|
+
.filter((w): w is World => w != null && w.files.length > 0)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** the page's bundle, in memory, built again when a file of web/ moved since */
|
|
97
|
+
let bundle: { stamp: string; files: Map<string, Blob> } | null = null
|
|
98
|
+
|
|
99
|
+
function webStamp(): string {
|
|
100
|
+
return readdirSync(WEB)
|
|
101
|
+
.map((f) => `${f}:${statSync(join(WEB, f)).mtimeMs}`)
|
|
102
|
+
.join('|')
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function built(): Promise<Map<string, Blob>> {
|
|
106
|
+
const stamp = webStamp()
|
|
107
|
+
if (bundle?.stamp === stamp) return bundle.files
|
|
108
|
+
const started = performance.now()
|
|
109
|
+
const r = await Bun.build({
|
|
110
|
+
entrypoints: [join(WEB, 'index.html')],
|
|
111
|
+
publicPath: './',
|
|
112
|
+
target: 'browser',
|
|
113
|
+
minify: true,
|
|
114
|
+
throw: false,
|
|
115
|
+
})
|
|
116
|
+
if (!r.success) throw new Error(`the page does not build:\n${r.logs.map(String).join('\n')}`)
|
|
117
|
+
const files = new Map<string, Blob>()
|
|
118
|
+
for (const o of r.outputs) files.set(o.path.replace(/^\.\//, ''), o)
|
|
119
|
+
bundle = { stamp, files }
|
|
120
|
+
console.log(`🟢 page built in ${Math.round(performance.now() - started)} ms (${[...files.keys()].join(', ')})`)
|
|
121
|
+
return files
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const json = (data: unknown, status = 200): Response =>
|
|
125
|
+
Response.json(data, { status, headers: { 'cache-control': 'no-store' } })
|
|
126
|
+
|
|
127
|
+
const server = Bun.serve({
|
|
128
|
+
port: PORT,
|
|
129
|
+
hostname: '127.0.0.1',
|
|
130
|
+
idleTimeout: 120,
|
|
131
|
+
async fetch(req) {
|
|
132
|
+
const url = new URL(req.url)
|
|
133
|
+
const path = decodeURIComponent(url.pathname).replace(/^\/+/, '')
|
|
134
|
+
if (path === 'api/worlds') return json(worlds())
|
|
135
|
+
// the page renders a world's thumbnail from its first view at the default camera and sends it here
|
|
136
|
+
const t = /^api\/thumb\/([^/]+)$/.exec(path)
|
|
137
|
+
if (t != null && req.method === 'POST') {
|
|
138
|
+
const w = worldOf(String(t[1]))
|
|
139
|
+
if (w == null) return json({ error: `no world ${t[1]}` }, 404)
|
|
140
|
+
const bytes = new Uint8Array(await req.arrayBuffer())
|
|
141
|
+
const webp = bytes.length > 12 && new TextDecoder().decode(bytes.subarray(8, 12)) === 'WEBP'
|
|
142
|
+
if (!webp || bytes.length > THUMB_MAX) return json({ error: 'a thumbnail is a webp under 2 MB' }, 400)
|
|
143
|
+
const dest = join(WORLDS, w.name, 'thumb.webp')
|
|
144
|
+
await Bun.write(`${dest}.tmp`, bytes)
|
|
145
|
+
renameSync(`${dest}.tmp`, dest)
|
|
146
|
+
console.log(`🟢 thumbnail of ${w.name}: ${bytes.length} bytes`)
|
|
147
|
+
return json({ ok: true, bytes: bytes.length })
|
|
148
|
+
}
|
|
149
|
+
const m = /^worlds\/([^/]+)\/([^/]+)$/.exec(path)
|
|
150
|
+
if (m != null) {
|
|
151
|
+
const w = worldOf(String(m[1]))
|
|
152
|
+
const f = w?.files.find((x) => x.name === m[2])
|
|
153
|
+
if (w == null || f == null) return json({ error: `no ${m[2]} in world ${m[1]}` }, 404)
|
|
154
|
+
// a world file weighs 60 to 80 MB: under its stamp it never changes, so the browser keeps it
|
|
155
|
+
const cache = url.searchParams.get('v') === f.v ? 'public, max-age=31536000, immutable' : 'no-cache'
|
|
156
|
+
return new Response(Bun.file(join(WORLDS, w.name, f.name)), { headers: { 'cache-control': cache } })
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
const files = await built()
|
|
160
|
+
const file = files.get(path === '' ? 'index.html' : path)
|
|
161
|
+
if (file == null) return json({ error: `no ${path}` }, 404)
|
|
162
|
+
return new Response(file, { headers: { 'cache-control': 'no-cache' } })
|
|
163
|
+
} catch (e) {
|
|
164
|
+
const message = e instanceof Error ? e.message : String(e)
|
|
165
|
+
console.error(`🔴 ${message}`)
|
|
166
|
+
return new Response(message, { status: 500, headers: { 'content-type': 'text/plain; charset=utf-8' } })
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
console.log(`world explorer → http://127.0.0.1:${server.port}/ (${worlds().length} worlds in ${WORLDS})`)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
6
|
+
<title>world explorer</title>
|
|
7
|
+
<meta name="shot-ready" content="wait" />
|
|
8
|
+
<link rel="stylesheet" href="./styles.css" />
|
|
9
|
+
</head>
|
|
10
|
+
<body>
|
|
11
|
+
<div id="root"></div>
|
|
12
|
+
<script type="module" src="./main.tsx"></script>
|
|
13
|
+
</body>
|
|
14
|
+
</html>
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { compareStructural, reaction } from 'mobx'
|
|
2
|
+
import { observer } from 'mobx-react-lite'
|
|
3
|
+
import { useEffect, useRef } from 'react'
|
|
4
|
+
import { createRoot } from 'react-dom/client'
|
|
5
|
+
import { frameClient } from 'rvlib-shipkit/src/pages/frame-client.ts'
|
|
6
|
+
import { camText, View3d, type Space } from 'rvlib-shipkit/src/view3d/viewer.ts'
|
|
7
|
+
import { WorldSt, type View } from './state.ts'
|
|
8
|
+
import { postThumb, sendThumb, THUMB_W } from './thumb.ts'
|
|
9
|
+
|
|
10
|
+
// one world of .comfy-ts/worlds, the view page of the `world` thread kind (agent/examples.md, world-explorer). Framed by
|
|
11
|
+
// the hub or alone at its url: the frame client hands it its keys either way
|
|
12
|
+
|
|
13
|
+
/** the house viewer's space for each 3D view; `source` is a flat picture, no viewer */
|
|
14
|
+
const SPACE: Record<Exclude<View, 'source'>, Space> = { pano: 'pano', world: 'mesh', splat: 'splat' }
|
|
15
|
+
const LABEL: Record<View, string> = { pano: '360°', world: 'walk', splat: 'splat', source: 'picture' }
|
|
16
|
+
const HINT: Record<View, string> = {
|
|
17
|
+
pano: 'drag to look around · wheel or pinch to zoom',
|
|
18
|
+
world: 'drag to look · right drag or shift to pan · WASD to walk, Q E down and up · wheel to go forward',
|
|
19
|
+
splat: 'drag to orbit · right drag or shift to pan · WASD to fly, Q E down and up · wheel to go forward',
|
|
20
|
+
source: '',
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** the page's own full screen, for the page opened alone: in the hub's lightbox (`mode=lightbox`) the hub fills the
|
|
24
|
+
* window and closes it. Esc leaves it (the browser's own key); F toggles it */
|
|
25
|
+
async function toggleFull(): Promise<void> {
|
|
26
|
+
if (st.mode === 'lightbox') return
|
|
27
|
+
if (document.fullscreenElement != null) return void (await document.exitFullscreen())
|
|
28
|
+
try {
|
|
29
|
+
await document.documentElement.requestFullscreen()
|
|
30
|
+
} catch (e) {
|
|
31
|
+
st.setNotice(`full screen refused here: ${(e as Error).message}`)
|
|
32
|
+
setTimeout(() => st.setNotice(null), 4000)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
document.addEventListener('fullscreenchange', () => st.setFull(document.fullscreenElement != null))
|
|
36
|
+
window.addEventListener('keydown', (e) => {
|
|
37
|
+
if (e.key === 'Escape' && document.fullscreenElement != null) void document.exitFullscreen()
|
|
38
|
+
if (e.code === 'KeyF' && !e.altKey && !e.metaKey && !e.ctrlKey) void toggleFull()
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
let viewer: View3d | null = null
|
|
42
|
+
const st = new WorldSt((k) => frame.setKeys(k))
|
|
43
|
+
// alone at its url, the client hands the address's keys at once: the state exists first. A camera from the keys
|
|
44
|
+
// (the other side of a change) is set on the viewer; one equal to its own is left alone, so a glide goes on
|
|
45
|
+
const frame = frameClient({
|
|
46
|
+
onKeys: (k) => {
|
|
47
|
+
st.fromKeys(k)
|
|
48
|
+
if (viewer != null && camText(viewer.cam) !== camText(st.cam)) viewer.setCam(st.cam)
|
|
49
|
+
},
|
|
50
|
+
// a frame the hub does not show draws nothing
|
|
51
|
+
onFocus: (on) => viewer?.pause(!on),
|
|
52
|
+
})
|
|
53
|
+
// console debugging path (app-state-tree: expose the root)
|
|
54
|
+
Object.assign(window, { world: st })
|
|
55
|
+
|
|
56
|
+
const shotReady = (on: boolean): void =>
|
|
57
|
+
document.querySelector('meta[name="shot-ready"]')?.setAttribute('content', on ? 'ready' : 'wait')
|
|
58
|
+
|
|
59
|
+
/** a world's view drawn once: the shot's ready mark, and its thumbnail when it has none and he has not moved yet */
|
|
60
|
+
function drawn(v: View3d): void {
|
|
61
|
+
shotReady(true)
|
|
62
|
+
if (!st.wantsThumb || st.shown !== st.thumbView || st.touched) return
|
|
63
|
+
st.thumbSent()
|
|
64
|
+
v.snapshot({ type: 'image/webp', quality: 0.85, width: THUMB_W })
|
|
65
|
+
.then((blob) => postThumb(st.name, blob))
|
|
66
|
+
.catch((e: unknown) => st.setNotice((e as Error).message))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** the 3D views through the house viewer: one viewer while a 3D tab shows, each tab's file loaded into it */
|
|
70
|
+
const Scene = observer(function Scene() {
|
|
71
|
+
const host = useRef<HTMLDivElement>(null)
|
|
72
|
+
// in the hub's lightbox the wheel is the view's at once; inline or alone it scrolls until he clicks in
|
|
73
|
+
const mode = st.mode === 'lightbox' ? 'lightbox' : 'thumb'
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
const el = host.current
|
|
76
|
+
if (el == null) return
|
|
77
|
+
const v: View3d = new View3d(el, {
|
|
78
|
+
keys: 'window',
|
|
79
|
+
mode,
|
|
80
|
+
onCam: (c) => st.setCam(c),
|
|
81
|
+
onLoading: (what) => st.setLoading(what),
|
|
82
|
+
onError: (why) => st.setError(why),
|
|
83
|
+
onDrawn: () => drawn(v),
|
|
84
|
+
})
|
|
85
|
+
viewer = v
|
|
86
|
+
const stop = reaction(
|
|
87
|
+
() => {
|
|
88
|
+
const view = st.shown
|
|
89
|
+
if (view == null || view === 'source') return null
|
|
90
|
+
return { space: SPACE[view], url: st.fileUrl(view), fov: st.info?.splatFov ?? undefined }
|
|
91
|
+
},
|
|
92
|
+
(src) => {
|
|
93
|
+
if (src?.url == null) return
|
|
94
|
+
void v.load({ space: src.space, url: src.url, fov: src.fov }).then(() => v.setCam(st.cam))
|
|
95
|
+
},
|
|
96
|
+
{ fireImmediately: true, equals: compareStructural },
|
|
97
|
+
)
|
|
98
|
+
return () => {
|
|
99
|
+
stop()
|
|
100
|
+
v.destroy()
|
|
101
|
+
if (viewer === v) viewer = null
|
|
102
|
+
}
|
|
103
|
+
}, [mode])
|
|
104
|
+
return <div ref={host} className="view" />
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
const Stage = observer(function Stage() {
|
|
108
|
+
const shown = st.shown
|
|
109
|
+
const src = shown === 'source' ? st.fileUrl('source') : null
|
|
110
|
+
useEffect(() => shotReady(shown === 'source'), [shown])
|
|
111
|
+
return (
|
|
112
|
+
<div className="stage">
|
|
113
|
+
{shown != null && shown !== 'source' ? <Scene /> : null}
|
|
114
|
+
{src != null ? (
|
|
115
|
+
<img
|
|
116
|
+
src={src}
|
|
117
|
+
alt={st.name}
|
|
118
|
+
draggable={false}
|
|
119
|
+
onLoad={(e) => {
|
|
120
|
+
const img = e.currentTarget
|
|
121
|
+
if (!st.wantsThumb || st.thumbView !== 'source') return
|
|
122
|
+
st.thumbSent()
|
|
123
|
+
sendThumb(st.name, img, img.naturalWidth, img.naturalHeight).catch((x: unknown) =>
|
|
124
|
+
st.setNotice((x as Error).message),
|
|
125
|
+
)
|
|
126
|
+
}}
|
|
127
|
+
/>
|
|
128
|
+
) : null}
|
|
129
|
+
{st.error != null ? (
|
|
130
|
+
<div className="note">{st.error}</div>
|
|
131
|
+
) : st.loading != null ? (
|
|
132
|
+
<div className="note">loading {st.loading}…</div>
|
|
133
|
+
) : null}
|
|
134
|
+
{st.name === '' && st.standalone ? <Worlds /> : null}
|
|
135
|
+
{shown != null && HINT[shown] && !st.touched && st.loading == null && st.mode !== 'thumb' ? (
|
|
136
|
+
<div className="hint">{HINT[shown]}</div>
|
|
137
|
+
) : null}
|
|
138
|
+
{st.notice != null && st.mode !== 'thumb' ? <div className="hint notice">{st.notice}</div> : null}
|
|
139
|
+
</div>
|
|
140
|
+
)
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
const Worlds = observer(function Worlds() {
|
|
144
|
+
return (
|
|
145
|
+
<div className="note worlds">
|
|
146
|
+
<div>
|
|
147
|
+
{st.all.map((w) => (
|
|
148
|
+
<a key={w.name} href={`?key=${w.name}`}>
|
|
149
|
+
{w.name}
|
|
150
|
+
</a>
|
|
151
|
+
))}
|
|
152
|
+
</div>
|
|
153
|
+
</div>
|
|
154
|
+
)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
const App = observer(function App() {
|
|
158
|
+
const shown = st.shown
|
|
159
|
+
// the hub's still: the scene alone, no bar, no hint
|
|
160
|
+
if (st.mode === 'thumb')
|
|
161
|
+
return (
|
|
162
|
+
<div className="app thumb">
|
|
163
|
+
<Stage />
|
|
164
|
+
</div>
|
|
165
|
+
)
|
|
166
|
+
return (
|
|
167
|
+
<div className="app">
|
|
168
|
+
<div className="bar">
|
|
169
|
+
{/* tabs only when the page is opened alone: in a thread the hub switches views, one still per view */}
|
|
170
|
+
{(st.standalone ? st.views : []).map((v) => (
|
|
171
|
+
<button key={v} className={`tab${v === shown ? ' on' : ''}`} onClick={() => st.setView(v)}>
|
|
172
|
+
{LABEL[v]}
|
|
173
|
+
</button>
|
|
174
|
+
))}
|
|
175
|
+
<span className="line">
|
|
176
|
+
<b>{st.name || 'worlds'}</b>
|
|
177
|
+
{st.side ? ` · ${st.side}` : ''}
|
|
178
|
+
</span>
|
|
179
|
+
<span className="tools">
|
|
180
|
+
{shown != null && shown !== 'source' ? (
|
|
181
|
+
<button className="tab" title="R, or a double click" onClick={() => viewer?.reset()}>
|
|
182
|
+
reset view
|
|
183
|
+
</button>
|
|
184
|
+
) : null}
|
|
185
|
+
{st.mode === 'lightbox' ? null : (
|
|
186
|
+
<button className="tab" title={st.full ? 'Esc' : 'F'} onClick={() => void toggleFull()}>
|
|
187
|
+
{st.full ? '✕ close' : '⤢ full screen'}
|
|
188
|
+
</button>
|
|
189
|
+
)}
|
|
190
|
+
</span>
|
|
191
|
+
</div>
|
|
192
|
+
<Stage />
|
|
193
|
+
</div>
|
|
194
|
+
)
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
void st.boot()
|
|
198
|
+
const rootEl = document.getElementById('root')
|
|
199
|
+
if (rootEl == null) throw new Error('#root missing from index.html')
|
|
200
|
+
createRoot(rootEl).render(<App />)
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { makeAutoObservable, runInAction } from 'mobx'
|
|
2
|
+
import { CAM0, camOf, camText, type Cam } from 'rvlib-shipkit/src/view3d/cam.ts'
|
|
3
|
+
|
|
4
|
+
// the page's state: which world, which of its files, the tab, the camera. Its keys (agent/examples.md, world-explorer):
|
|
5
|
+
// `key` the world, `view` the tab, `cam` the camera; framed by the hub, `state` is the url of a version's side and
|
|
6
|
+
// `view` + `cam` are what a before and an after share, so both sides switch tab and turn together
|
|
7
|
+
|
|
8
|
+
export const VIEWS = ['pano', 'world', 'splat', 'source'] as const
|
|
9
|
+
export type View = (typeof VIEWS)[number]
|
|
10
|
+
|
|
11
|
+
/** the file each view draws */
|
|
12
|
+
export const FILE_OF: Record<View, string> = {
|
|
13
|
+
pano: 'panorama.png',
|
|
14
|
+
world: 'world.glb',
|
|
15
|
+
splat: 'splat.ply',
|
|
16
|
+
source: 'source.png',
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type WorldFile = { name: string; v: string }
|
|
20
|
+
export type WorldInfo = { name: string; files: WorldFile[]; splatFov: number | null }
|
|
21
|
+
|
|
22
|
+
/** the keys posted at most this often while he moves: the other side follows, the hub is not flooded */
|
|
23
|
+
const POST_MS = 40
|
|
24
|
+
|
|
25
|
+
export class WorldSt {
|
|
26
|
+
name = ''
|
|
27
|
+
side = ''
|
|
28
|
+
/** how the hub frames the page: `thumb` for the still it shoots (no ui), `lightbox` (the hub closes it), else inline or alone */
|
|
29
|
+
mode: '' | 'thumb' | 'lightbox' = ''
|
|
30
|
+
/** the files the shown version names: a side's state, else every file the world has on disk */
|
|
31
|
+
wanted: string[] = []
|
|
32
|
+
all: WorldInfo[] = []
|
|
33
|
+
view: View | null = null
|
|
34
|
+
cam: Cam = { ...CAM0 }
|
|
35
|
+
error: string | null = null
|
|
36
|
+
/** what loads now and how far, for the one line */
|
|
37
|
+
loading: string | null = null
|
|
38
|
+
private stateUrl = ''
|
|
39
|
+
|
|
40
|
+
constructor(private post: (keys: string) => void) {
|
|
41
|
+
makeAutoObservable<WorldSt, 'post' | 'postTimer' | 'postedAt'>(this, {
|
|
42
|
+
post: false,
|
|
43
|
+
postTimer: false,
|
|
44
|
+
postedAt: false,
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get info(): WorldInfo | null {
|
|
49
|
+
return this.all.find((w) => w.name === this.name) ?? null
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get views(): View[] {
|
|
53
|
+
const info = this.info
|
|
54
|
+
if (info == null) return []
|
|
55
|
+
return VIEWS.filter((v) => this.wanted.includes(FILE_OF[v]) && info.files.some((f) => f.name === FILE_OF[v]))
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** the tab drawn: the one asked when the world has it, else the first */
|
|
59
|
+
get shown(): View | null {
|
|
60
|
+
const vs = this.views
|
|
61
|
+
return this.view != null && vs.includes(this.view) ? this.view : (vs[0] ?? null)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** the view a thumbnail shows: the world's first, at the default camera */
|
|
65
|
+
get thumbView(): View | null {
|
|
66
|
+
return this.views[0] ?? null
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** the page makes the thumbnail when the world has none, or when its keys say `thumb=again` */
|
|
70
|
+
thumbAgain = false
|
|
71
|
+
|
|
72
|
+
get wantsThumb(): boolean {
|
|
73
|
+
const info = this.info
|
|
74
|
+
return info != null && (this.thumbAgain || !info.files.some((f) => f.name === 'thumb.webp'))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** the thumbnail sent: the world lists it now, so no other frame sends one */
|
|
78
|
+
thumbSent(): void {
|
|
79
|
+
this.thumbAgain = false
|
|
80
|
+
const info = this.info
|
|
81
|
+
if (info != null && !info.files.some((f) => f.name === 'thumb.webp'))
|
|
82
|
+
info.files.push({ name: 'thumb.webp', v: 'new' })
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** a world file by its relative path, under its stamp so the browser keeps it */
|
|
86
|
+
fileUrl(view: View): string | null {
|
|
87
|
+
const f = this.info?.files.find((x) => x.name === FILE_OF[view])
|
|
88
|
+
return f == null ? null : `worlds/${this.name}/${f.name}?v=${f.v}`
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
get keys(): string {
|
|
92
|
+
const q = new URLSearchParams()
|
|
93
|
+
if (this.name !== '') q.set('key', this.name)
|
|
94
|
+
if (this.view != null) q.set('view', this.view)
|
|
95
|
+
const cam = camText(this.cam)
|
|
96
|
+
if (cam !== '') q.set('cam', cam)
|
|
97
|
+
return q.toString().replaceAll('%2C', ',')
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** the keys from the hub or the address: they never post back, only a move of his does */
|
|
101
|
+
fromKeys(keys: string): void {
|
|
102
|
+
const q = new URLSearchParams(keys)
|
|
103
|
+
this.side = q.get('side') ?? ''
|
|
104
|
+
const mode = q.get('mode')
|
|
105
|
+
this.mode = mode === 'thumb' || mode === 'lightbox' ? mode : ''
|
|
106
|
+
const view = q.get('view')
|
|
107
|
+
this.view = VIEWS.find((v) => v === view) ?? null
|
|
108
|
+
this.cam = camOf(q.get('cam'))
|
|
109
|
+
this.thumbAgain = q.get('thumb') === 'again'
|
|
110
|
+
const state = q.get('state') ?? ''
|
|
111
|
+
if (state !== '' && state !== this.stateUrl) void this.loadState(state)
|
|
112
|
+
else if (state === '') {
|
|
113
|
+
this.name = q.get('key') ?? ''
|
|
114
|
+
this.wanted = Object.values(FILE_OF)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async boot(): Promise<void> {
|
|
119
|
+
try {
|
|
120
|
+
const r = await fetch('api/worlds')
|
|
121
|
+
if (!r.ok) throw new Error(`api/worlds: ${r.status}`)
|
|
122
|
+
const all = (await r.json()) as WorldInfo[]
|
|
123
|
+
runInAction(() => (this.all = all))
|
|
124
|
+
} catch (e) {
|
|
125
|
+
runInAction(() => (this.error = (e as Error).message))
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
private async loadState(url: string): Promise<void> {
|
|
130
|
+
this.stateUrl = url
|
|
131
|
+
try {
|
|
132
|
+
const r = await fetch(url)
|
|
133
|
+
if (!r.ok) throw new Error(`the version's state: ${r.status} ${await r.text()}`)
|
|
134
|
+
const s = (await r.json()) as { json?: { name?: unknown; files?: unknown } }
|
|
135
|
+
const name = s.json?.name
|
|
136
|
+
const files = s.json?.files
|
|
137
|
+
if (typeof name !== 'string') throw new Error('the version names no world')
|
|
138
|
+
runInAction(() => {
|
|
139
|
+
this.name = name
|
|
140
|
+
this.wanted = Array.isArray(files)
|
|
141
|
+
? files.filter((f): f is string => typeof f === 'string')
|
|
142
|
+
: Object.values(FILE_OF)
|
|
143
|
+
this.error = null
|
|
144
|
+
})
|
|
145
|
+
} catch (e) {
|
|
146
|
+
runInAction(() => (this.error = (e as Error).message))
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** standalone: every file of the world on disk */
|
|
151
|
+
get standalone(): boolean {
|
|
152
|
+
return this.stateUrl === ''
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
setView(v: View): void {
|
|
156
|
+
if (v === this.shown) return
|
|
157
|
+
this.view = v
|
|
158
|
+
this.cam = { ...CAM0 }
|
|
159
|
+
this.moved()
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** his move in the viewer: the eye as it is now, posted with the other keys */
|
|
163
|
+
setCam(c: Cam): void {
|
|
164
|
+
this.cam = c
|
|
165
|
+
this.moved()
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private postTimer: ReturnType<typeof setTimeout> | null = null
|
|
169
|
+
private postedAt = 0
|
|
170
|
+
|
|
171
|
+
/** his move: the keys go out now, or once the last post is POST_MS old */
|
|
172
|
+
private moved(): void {
|
|
173
|
+
const wait = this.postedAt + POST_MS - performance.now()
|
|
174
|
+
if (wait <= 0) {
|
|
175
|
+
this.postedAt = performance.now()
|
|
176
|
+
this.post(this.keys)
|
|
177
|
+
return
|
|
178
|
+
}
|
|
179
|
+
if (this.postTimer != null) return
|
|
180
|
+
this.postTimer = setTimeout(() => {
|
|
181
|
+
this.postTimer = null
|
|
182
|
+
this.postedAt = performance.now()
|
|
183
|
+
this.post(this.keys)
|
|
184
|
+
}, wait)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** the page fills the screen (the browser's fullscreen on its root): the lightbox */
|
|
188
|
+
full = false
|
|
189
|
+
/** a line that says what did not happen (a fullscreen the frame refused), gone after a few seconds */
|
|
190
|
+
notice: string | null = null
|
|
191
|
+
|
|
192
|
+
setFull(on: boolean): void {
|
|
193
|
+
this.full = on
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
setNotice(n: string | null): void {
|
|
197
|
+
this.notice = n
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** his first move hides the hint */
|
|
201
|
+
get touched(): boolean {
|
|
202
|
+
return camText(this.cam) !== ''
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
setLoading(what: string | null): void {
|
|
206
|
+
this.loading = what
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
setError(e: string | null): void {
|
|
210
|
+
this.error = e
|
|
211
|
+
}
|
|
212
|
+
}
|