comfy-ts 2.20.2 → 2.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -1
- package/dist/{ServeApp-B469W26E.js → ServeApp-DuxYdaZZ.js} +142 -7
- package/dist/ServeApp-DuxYdaZZ.js.map +1 -0
- package/dist/cli.js +5 -5
- package/dist/{exampleAssets-yBLaKXLq.js → exampleAssets-1NHndnNB.js} +59 -17
- package/dist/exampleAssets-1NHndnNB.js.map +1 -0
- package/dist/{findDefinedWorkflow-B90-xFxX.js → findDefinedWorkflow-CRsBTTJK.js} +2 -2
- package/dist/{findDefinedWorkflow-B90-xFxX.js.map → findDefinedWorkflow-CRsBTTJK.js.map} +1 -1
- package/dist/index.cjs +118 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +90 -9
- package/dist/index.d.ts +90 -9
- package/dist/index.js +117 -20
- package/dist/index.js.map +1 -1
- package/dist/{run-run-C-dDD9Dc.js → run-run-Y5ZI48vV.js} +5 -5
- package/dist/{run-run-C-dDD9Dc.js.map → run-run-Y5ZI48vV.js.map} +1 -1
- package/dist/{run-serve-CsEYPGm9.js → run-serve-CICU_wTL.js} +4 -4
- package/dist/{run-serve-CsEYPGm9.js.map → run-serve-CICU_wTL.js.map} +1 -1
- package/dist/{run-tui-bsmja4hi.js → run-tui-CViJXsHL.js} +4 -4
- package/dist/{run-tui-bsmja4hi.js.map → run-tui-CViJXsHL.js.map} +1 -1
- package/dist/{schemaDrift-DaWsE-mt.js → schemaDrift-CzjaIwZX.js} +53 -20
- package/dist/schemaDrift-CzjaIwZX.js.map +1 -0
- package/dist/serve-web.js +204 -47
- package/dist/web.d.ts +90 -9
- package/dist/web.js +117 -20
- package/dist/web.js.map +1 -1
- package/examples/README.md +2 -0
- 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/package.json +3 -2
- package/src/cli/serve/ServeApp.ts +79 -4
- package/src/cli/serve/describeVar.ts +12 -1
- package/src/cli/serve/resultHistory.ts +6 -1
- package/src/cli/serve/thumbs.ts +66 -0
- package/src/cli/serve/web/api.ts +5 -1
- package/src/cli/serve/web/components/Gallery.tsx +2 -0
- 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 +39 -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 +9 -0
- package/src/host/ComfyHost.ts +21 -0
- package/src/host/leastBusy.ts +37 -0
- package/src/state.ts +6 -0
- package/src/vars/ComfyVars.ts +32 -2
- package/src/vars/DefinedWorkflow.ts +42 -21
- package/src/vars/outputWidgets.ts +47 -0
- package/dist/ServeApp-B469W26E.js.map +0 -1
- package/dist/exampleAssets-yBLaKXLq.js.map +0 -1
- package/dist/schemaDrift-DaWsE-mt.js.map +0 -1
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { WIDGET_STYLES } from 'src/cli/serve/web/widgets/widgetStyles.ts'
|
|
2
|
+
|
|
1
3
|
// the ONE stylesheet, injected by main.tsx — no css framework on purpose
|
|
2
4
|
// (agent/coding.md: prefer obvious code over a new dependency)
|
|
3
5
|
export const STYLES = /* css */ `
|
|
@@ -537,6 +539,42 @@ input[type='range'].setting-range:disabled { opacity: 0.3; }
|
|
|
537
539
|
.media-drop { border: 1px dashed var(--border); border-radius: 6px; padding: 6px; }
|
|
538
540
|
.media-drop.over { border-color: var(--accent); background: var(--accent-dim); }
|
|
539
541
|
.media-drop .error { color: var(--red); font-size: 12px; margin-top: 4px; white-space: pre-wrap; }
|
|
542
|
+
/* a media var's one-click presets (v.image(…, { presets })) */
|
|
543
|
+
.media-presets { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
|
|
544
|
+
.media-preset { display: flex; flex-direction: column; align-items: stretch; gap: 3px; width: 92px; padding: 3px; font-size: 11px; color: var(--dim); background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; cursor: pointer; }
|
|
545
|
+
.media-preset img, .media-preset-none { width: 100%; height: 62px; object-fit: cover; border-radius: 4px; background: #0b0c10; display: block; }
|
|
546
|
+
.media-preset span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: left; }
|
|
547
|
+
.media-preset:hover:not(:disabled) { border-color: var(--accent); color: var(--text); }
|
|
548
|
+
.media-preset.sel { border-color: var(--sel-border); background: var(--sel-bg); color: var(--sel-text); }
|
|
549
|
+
.media-preset:disabled { opacity: 0.35; cursor: not-allowed; }
|
|
550
|
+
/* the rich choice dropdown (components/controls/RichSelect.tsx) */
|
|
551
|
+
.rs-trigger { display: flex; align-items: center; gap: 8px; width: 100%; max-width: 620px; padding: 4px 8px; font: inherit; color: var(--text); text-align: left; background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; cursor: pointer; }
|
|
552
|
+
.rs-trigger:hover, .rs-trigger.open { border-color: var(--accent); }
|
|
553
|
+
.rs-trigger-text { display: flex; flex-direction: column; min-width: 0; flex: 1; }
|
|
554
|
+
.rs-trigger .rs-badges { flex-shrink: 0; }
|
|
555
|
+
.rs-chevron { color: var(--dim); }
|
|
556
|
+
.rs-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
557
|
+
.rs-value { color: var(--dim); font-size: 11px; font-family: ui-monospace, monospace; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
558
|
+
.rs-thumb { width: 30px; height: 30px; flex-shrink: 0; object-fit: cover; border-radius: 4px; background: #0b0c10; display: inline-flex; align-items: center; justify-content: center; }
|
|
559
|
+
.rs-thumb.big { width: 56px; height: 56px; }
|
|
560
|
+
.rs-pop { z-index: 60; display: flex; flex-direction: column; max-width: min(560px, calc(100vw - 16px)); background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45); overflow: hidden; }
|
|
561
|
+
.rs-filter { margin: 8px; padding: 5px 8px; font: inherit; color: var(--text); background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; }
|
|
562
|
+
.rs-list { overflow-y: auto; outline: none; padding: 4px; }
|
|
563
|
+
.rs-row { display: flex; align-items: flex-start; gap: 10px; padding: 6px 8px; border-radius: 6px; cursor: pointer; }
|
|
564
|
+
.rs-row.active { background: var(--panel-2); }
|
|
565
|
+
.rs-row.sel { box-shadow: inset 2px 0 0 var(--accent); }
|
|
566
|
+
.rs-row-text { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1; }
|
|
567
|
+
.rs-row-head { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
|
|
568
|
+
.rs-desc { color: var(--dim); font-size: 12px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
|
|
569
|
+
.rs-badges { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; }
|
|
570
|
+
.rs-badge { font-size: 11px; color: var(--dim); background: var(--panel-2); border: 1px solid var(--hairline); border-radius: 4px; padding: 0 5px; white-space: nowrap; }
|
|
571
|
+
.rs-quality { display: inline-flex; gap: 2px; margin-left: 2px; }
|
|
572
|
+
.rs-quality i { width: 6px; height: 10px; border-radius: 2px; background: var(--border); }
|
|
573
|
+
.rs-quality i.on { background: var(--green); }
|
|
574
|
+
.rs-check { color: var(--accent); font-weight: 700; }
|
|
575
|
+
.rs-none .rs-thumb { color: var(--dim); font-size: 22px; border: 1px dashed var(--border); }
|
|
576
|
+
.rs-empty { color: var(--dim); padding: 10px; font-size: 12px; }
|
|
577
|
+
.rs-foot { color: var(--dim); font-size: 11px; padding: 5px 10px; border-top: 1px solid var(--hairline); }
|
|
540
578
|
.media-preview-audio audio { width: min(100%, 360px); }
|
|
541
579
|
.media-preview-video video { max-width: 320px; max-height: 220px; border-radius: 6px; border: 1px solid var(--border); background: #000; }
|
|
542
580
|
.run-card.running { border-color: var(--accent-dim); }
|
|
@@ -1233,4 +1271,5 @@ button .icon + * { margin-left: 4px; }
|
|
|
1233
1271
|
.enh-grid { grid-template-columns: minmax(0, 1fr); }
|
|
1234
1272
|
.enh-grid > label { text-align: left; }
|
|
1235
1273
|
}
|
|
1274
|
+
${WIDGET_STYLES}
|
|
1236
1275
|
`
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// the output widget REGISTRY: a widget kind a module names → the component that draws it over a
|
|
2
|
+
// run's tagged images. Built into the panel bundle; a module ships no browser code.
|
|
3
|
+
import type { GeneratedImage } from 'src/cli/serve/web/api.ts'
|
|
4
|
+
import { RelightWidgetView } from 'src/cli/serve/web/widgets/RelightWidget.tsx'
|
|
5
|
+
import type { OutputWidget } from 'src/vars/outputWidgets.ts'
|
|
6
|
+
|
|
7
|
+
export function OutputWidgets(p: { widgets: OutputWidget[] | undefined; images: GeneratedImage[] }) {
|
|
8
|
+
if (p.widgets == null || p.widgets.length === 0) return null
|
|
9
|
+
return (
|
|
10
|
+
<div className="output-widgets">
|
|
11
|
+
{p.widgets.map((w, ix) => {
|
|
12
|
+
switch (w.kind) {
|
|
13
|
+
case 'relight':
|
|
14
|
+
return <RelightWidgetView key={ix} widget={w} images={p.images} />
|
|
15
|
+
}
|
|
16
|
+
})}
|
|
17
|
+
</div>
|
|
18
|
+
)
|
|
19
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// the relight widget's state: the light, the motes, the view. One instance per widget on screen
|
|
2
|
+
// (component tier: created by the view, disposed with it). Time drives the orbit and the motes, so
|
|
3
|
+
// `at(t)` draws any instant: the video capture steps it frame by frame.
|
|
4
|
+
import { makeAutoObservable } from 'mobx'
|
|
5
|
+
import { MAX_MOTES, type RelightParams, type RelightView } from 'src/cli/serve/web/widgets/relightGl.ts'
|
|
6
|
+
|
|
7
|
+
/** a deterministic 0..1 per (mote, salt): the motes keep their paths across frames */
|
|
8
|
+
function hash(i: number, salt: number): number {
|
|
9
|
+
const x = Math.sin(i * 127.1 + salt * 311.7) * 43758.5453
|
|
10
|
+
return x - Math.floor(x)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function hexToRgb(hex: string): [number, number, number] {
|
|
14
|
+
const n = Number.parseInt(hex.replace('#', ''), 16)
|
|
15
|
+
if (Number.isNaN(n)) return [1, 1, 1]
|
|
16
|
+
return [((n >> 16) & 255) / 255, ((n >> 8) & 255) / 255, (n & 255) / 255]
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class RelightSt {
|
|
20
|
+
lightX = 0.3
|
|
21
|
+
lightY = 0.3
|
|
22
|
+
/** above the surface, in image heights */
|
|
23
|
+
lightZ = 0.35
|
|
24
|
+
color = '#ffd9a8'
|
|
25
|
+
intensity = 1.7
|
|
26
|
+
falloff = 0.45
|
|
27
|
+
ambient = 0.4
|
|
28
|
+
depthScale = 0.35
|
|
29
|
+
exposure = 1.0
|
|
30
|
+
shadows = true
|
|
31
|
+
orbit = false
|
|
32
|
+
particles = false
|
|
33
|
+
moteCount = 10
|
|
34
|
+
showLights = true
|
|
35
|
+
view: RelightView = 'lit'
|
|
36
|
+
/** seconds, advanced by the animation loop or set by `at(t)` */
|
|
37
|
+
time = 0
|
|
38
|
+
/** every map of the run is on the GPU: nothing is drawn before */
|
|
39
|
+
ready = false
|
|
40
|
+
|
|
41
|
+
constructor() {
|
|
42
|
+
makeAutoObservable(this)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
get animating(): boolean {
|
|
46
|
+
return this.orbit || this.particles
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** the light where the orbit puts it at `time`, else where the pointer left it */
|
|
50
|
+
get light(): [number, number, number] {
|
|
51
|
+
if (!this.orbit) return [this.lightX, this.lightY, this.lightZ]
|
|
52
|
+
const a = this.time * 0.9
|
|
53
|
+
return [0.5 + Math.cos(a) * 0.34, 0.45 + Math.sin(a) * 0.28, this.lightZ]
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
get motes(): [number, number, number][] {
|
|
57
|
+
if (!this.particles) return []
|
|
58
|
+
const out: [number, number, number][] = []
|
|
59
|
+
const n = Math.min(MAX_MOTES, this.moteCount)
|
|
60
|
+
for (let i = 0; i < n; i++) {
|
|
61
|
+
const speed = 0.15 + hash(i, 1) * 0.35
|
|
62
|
+
const ph = hash(i, 2) * Math.PI * 2
|
|
63
|
+
const t = this.time * speed + ph
|
|
64
|
+
out.push([
|
|
65
|
+
0.08 + 0.84 * (0.5 + 0.5 * Math.sin(t * 1.3 + hash(i, 3) * 6)),
|
|
66
|
+
0.08 + 0.84 * (0.5 + 0.5 * Math.cos(t * 0.9 + hash(i, 4) * 6)),
|
|
67
|
+
0.06 + 0.3 * (0.5 + 0.5 * Math.sin(t * 2.1 + hash(i, 5) * 6)),
|
|
68
|
+
])
|
|
69
|
+
}
|
|
70
|
+
return out
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
get params(): RelightParams {
|
|
74
|
+
return {
|
|
75
|
+
light: this.light,
|
|
76
|
+
color: hexToRgb(this.color),
|
|
77
|
+
intensity: this.intensity,
|
|
78
|
+
falloff: this.falloff,
|
|
79
|
+
ambient: this.ambient,
|
|
80
|
+
depthScale: this.depthScale,
|
|
81
|
+
shadows: this.shadows,
|
|
82
|
+
motes: this.motes,
|
|
83
|
+
moteIntensity: this.intensity * 0.18,
|
|
84
|
+
showLights: this.showLights,
|
|
85
|
+
view: this.view,
|
|
86
|
+
exposure: this.exposure,
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** the pointer put the light here (0..1 over the image); stops the orbit */
|
|
91
|
+
moveLight(x: number, y: number): void {
|
|
92
|
+
this.orbit = false
|
|
93
|
+
this.lightX = Math.min(1, Math.max(0, x))
|
|
94
|
+
this.lightY = Math.min(1, Math.max(0, y))
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** the wheel: nearer or farther from the surface */
|
|
98
|
+
nudgeHeight(delta: number): void {
|
|
99
|
+
this.lightZ = Math.min(1.5, Math.max(0.02, this.lightZ * (delta > 0 ? 1.1 : 1 / 1.1)))
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
tick(dt: number): void {
|
|
103
|
+
this.time += dt
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
at(t: number): void {
|
|
107
|
+
this.time = t
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
set<K extends keyof this & string>(k: K, value: this[K]): void {
|
|
111
|
+
this[k] = value
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
// the relight widget: the photo lit again by a point light you move over it. Drag = left/right,
|
|
2
|
+
// up/down; wheel = nearer/farther; the bar holds colour, power, shadows, orbit, motes and the map
|
|
3
|
+
// views. `window.relightAt(t)` draws instant t (sk shot --step), `window.relightTour(t)` the
|
|
4
|
+
// scripted tour a demo video steps through; both wait for the maps first.
|
|
5
|
+
import { observer } from 'mobx-react-lite'
|
|
6
|
+
import { autorun, when } from 'mobx'
|
|
7
|
+
import { useEffect, useMemo, useRef, useState } from 'react'
|
|
8
|
+
import type { GeneratedImage } from 'src/cli/serve/web/api.ts'
|
|
9
|
+
import { RelightGl, type RelightMaps, type RelightView } from 'src/cli/serve/web/widgets/relightGl.ts'
|
|
10
|
+
import { RelightSt } from 'src/cli/serve/web/widgets/RelightSt.ts'
|
|
11
|
+
import { resolveWidgetRoles, type RelightRoles, type RelightWidget } from 'src/vars/outputWidgets.ts'
|
|
12
|
+
|
|
13
|
+
const ROLE_KEYS: (keyof RelightRoles)[] = ['image', 'depth', 'normal', 'albedo', 'roughness', 'metalness', 'specular']
|
|
14
|
+
|
|
15
|
+
function loadImage(url: string): Promise<HTMLImageElement> {
|
|
16
|
+
const img = new Image()
|
|
17
|
+
img.src = url
|
|
18
|
+
return img.decode().then(() => img)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** the scripted tour of a demo video: orbit, then motes, then the light comes close */
|
|
22
|
+
function tour(st: RelightSt, t: number): void {
|
|
23
|
+
st.set('orbit', t < 9)
|
|
24
|
+
st.set('particles', t >= 4)
|
|
25
|
+
st.set('lightZ', t < 9 ? 0.4 : Math.max(0.06, 0.4 - (t - 9) * 0.09))
|
|
26
|
+
if (t >= 9) {
|
|
27
|
+
const k = (t - 9) / 4
|
|
28
|
+
st.set('lightX', 0.15 + 0.7 * Math.min(1, k))
|
|
29
|
+
st.set('lightY', 0.4 + 0.1 * Math.sin(k * 3))
|
|
30
|
+
}
|
|
31
|
+
st.at(t)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const VIEWS: [RelightView, string, keyof RelightRoles | null][] = [
|
|
35
|
+
['lit', 'lit', null],
|
|
36
|
+
['albedo', 'albedo', 'albedo'],
|
|
37
|
+
['normal', 'normals', null],
|
|
38
|
+
['depth', 'depth', 'depth'],
|
|
39
|
+
['roughness', 'roughness', 'roughness'],
|
|
40
|
+
['metalness', 'metalness', 'metalness'],
|
|
41
|
+
['specular', 'specular', 'specular'],
|
|
42
|
+
['shadow', 'shadows', 'depth'],
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
function Slider(p: {
|
|
46
|
+
label: string
|
|
47
|
+
min: number
|
|
48
|
+
max: number
|
|
49
|
+
step: number
|
|
50
|
+
value: number
|
|
51
|
+
onChange: (v: number) => void
|
|
52
|
+
}) {
|
|
53
|
+
return (
|
|
54
|
+
<label className="relight-slider">
|
|
55
|
+
<span>{p.label}</span>
|
|
56
|
+
<input
|
|
57
|
+
type="range"
|
|
58
|
+
min={p.min}
|
|
59
|
+
max={p.max}
|
|
60
|
+
step={p.step}
|
|
61
|
+
value={p.value}
|
|
62
|
+
onChange={(e) => p.onChange(Number(e.target.value))}
|
|
63
|
+
/>
|
|
64
|
+
</label>
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function Toggle(p: { label: string; on: boolean; onChange: (v: boolean) => void; tip: string }) {
|
|
69
|
+
return (
|
|
70
|
+
<button type="button" className={p.on ? 'seg on' : 'seg'} data-tip={p.tip} onClick={() => p.onChange(!p.on)}>
|
|
71
|
+
{p.label}
|
|
72
|
+
</button>
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export const RelightWidgetView = observer(function RelightWidgetView(p: {
|
|
77
|
+
widget: RelightWidget
|
|
78
|
+
images: GeneratedImage[]
|
|
79
|
+
}) {
|
|
80
|
+
const st = useMemo(() => new RelightSt(), [])
|
|
81
|
+
const canvasRef = useRef<HTMLCanvasElement>(null)
|
|
82
|
+
const glRef = useRef<RelightGl | null>(null)
|
|
83
|
+
const [error, setError] = useState<string | null>(null)
|
|
84
|
+
const [aspect, setAspect] = useState(1)
|
|
85
|
+
const [present, setPresent] = useState<Set<keyof RelightRoles>>(new Set())
|
|
86
|
+
|
|
87
|
+
const roles = resolveWidgetRoles(p.widget, p.images)
|
|
88
|
+
const urlOf = (role: keyof RelightRoles): string | null => {
|
|
89
|
+
const ix = roles.get(role)
|
|
90
|
+
return ix == null ? null : (p.images[ix]?.url ?? null)
|
|
91
|
+
}
|
|
92
|
+
const urlKey = ROLE_KEYS.map((r) => urlOf(r) ?? '').join('|')
|
|
93
|
+
|
|
94
|
+
// load the maps whenever the run's urls change
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
const canvas = canvasRef.current
|
|
97
|
+
const imageUrl = urlOf('image')
|
|
98
|
+
if (canvas == null || imageUrl == null) return
|
|
99
|
+
let live = true
|
|
100
|
+
try {
|
|
101
|
+
glRef.current ??= new RelightGl(canvas)
|
|
102
|
+
} catch (e) {
|
|
103
|
+
setError(e instanceof Error ? e.message : String(e))
|
|
104
|
+
return
|
|
105
|
+
}
|
|
106
|
+
st.set('ready', false)
|
|
107
|
+
const loads = ROLE_KEYS.map((role) => {
|
|
108
|
+
const url = urlOf(role)
|
|
109
|
+
return url == null ? Promise.resolve(null) : loadImage(url).then((img) => [role, img] as const)
|
|
110
|
+
})
|
|
111
|
+
void Promise.all(loads).then((pairs) => {
|
|
112
|
+
if (!live) return
|
|
113
|
+
const maps: RelightMaps = { image: new Image() }
|
|
114
|
+
const got = new Set<keyof RelightRoles>()
|
|
115
|
+
let w = 1
|
|
116
|
+
let h = 1
|
|
117
|
+
for (const pair of pairs) {
|
|
118
|
+
if (pair == null) continue
|
|
119
|
+
const [role, img] = pair
|
|
120
|
+
maps[role] = img
|
|
121
|
+
got.add(role)
|
|
122
|
+
if (role === 'image') {
|
|
123
|
+
w = img.naturalWidth
|
|
124
|
+
h = img.naturalHeight
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
setAspect(w / Math.max(1, h))
|
|
128
|
+
setPresent(got)
|
|
129
|
+
glRef.current?.setMaps(maps, w / Math.max(1, h), w, h)
|
|
130
|
+
st.set('ready', true)
|
|
131
|
+
})
|
|
132
|
+
return () => {
|
|
133
|
+
live = false
|
|
134
|
+
}
|
|
135
|
+
}, [urlKey])
|
|
136
|
+
|
|
137
|
+
// redraw on every parameter change; animate while the orbit or the motes run
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
// never before the maps: the placeholder textures would flash a flat colour
|
|
140
|
+
const draw = (): void => {
|
|
141
|
+
if (st.ready) glRef.current?.draw(st.params)
|
|
142
|
+
}
|
|
143
|
+
const stopDraw = autorun(draw)
|
|
144
|
+
let raf = 0
|
|
145
|
+
let last = performance.now()
|
|
146
|
+
const loop = (now: number): void => {
|
|
147
|
+
if (st.animating) st.tick((now - last) / 1000)
|
|
148
|
+
last = now
|
|
149
|
+
raf = requestAnimationFrame(loop)
|
|
150
|
+
}
|
|
151
|
+
raf = requestAnimationFrame(loop)
|
|
152
|
+
const onResize = (): void => draw()
|
|
153
|
+
window.addEventListener('resize', onResize)
|
|
154
|
+
// a stepped capture waits for the maps, then draws the instant it is given
|
|
155
|
+
const whenReady = (): Promise<void> => when(() => st.ready, { timeout: 10_000 })
|
|
156
|
+
Object.assign(window, {
|
|
157
|
+
relightAt: async (t: number) => {
|
|
158
|
+
await whenReady()
|
|
159
|
+
st.at(t)
|
|
160
|
+
draw()
|
|
161
|
+
},
|
|
162
|
+
relightTour: async (t: number) => {
|
|
163
|
+
await whenReady()
|
|
164
|
+
tour(st, t)
|
|
165
|
+
draw()
|
|
166
|
+
},
|
|
167
|
+
relight: st,
|
|
168
|
+
})
|
|
169
|
+
return () => {
|
|
170
|
+
stopDraw()
|
|
171
|
+
cancelAnimationFrame(raf)
|
|
172
|
+
window.removeEventListener('resize', onResize)
|
|
173
|
+
}
|
|
174
|
+
}, [st])
|
|
175
|
+
|
|
176
|
+
const dragging = useRef(false)
|
|
177
|
+
const pointAt = (e: React.PointerEvent<HTMLCanvasElement>): void => {
|
|
178
|
+
const r = e.currentTarget.getBoundingClientRect()
|
|
179
|
+
st.moveLight((e.clientX - r.left) / r.width, (e.clientY - r.top) / r.height)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (urlOf('image') == null)
|
|
183
|
+
return <div className="relight-missing">relight: no output carries the `{p.widget.roles.image}` tag</div>
|
|
184
|
+
if (error != null) return <div className="relight-missing">{error}</div>
|
|
185
|
+
return (
|
|
186
|
+
<div className="relight">
|
|
187
|
+
<div className="relight-stage">
|
|
188
|
+
<canvas
|
|
189
|
+
ref={canvasRef}
|
|
190
|
+
className="relight-canvas"
|
|
191
|
+
style={{ aspectRatio: String(aspect) }}
|
|
192
|
+
onPointerDown={(e) => {
|
|
193
|
+
dragging.current = true
|
|
194
|
+
e.currentTarget.setPointerCapture(e.pointerId)
|
|
195
|
+
pointAt(e)
|
|
196
|
+
}}
|
|
197
|
+
onPointerMove={(e) => {
|
|
198
|
+
if (dragging.current) pointAt(e)
|
|
199
|
+
}}
|
|
200
|
+
onPointerUp={() => (dragging.current = false)}
|
|
201
|
+
onWheel={(e) => st.nudgeHeight(e.deltaY)}
|
|
202
|
+
/>
|
|
203
|
+
</div>
|
|
204
|
+
<div className="relight-bar">
|
|
205
|
+
<div className="relight-row">
|
|
206
|
+
<span className="relight-title">{p.widget.title ?? 'relight'}</span>
|
|
207
|
+
<div className="relight-segs">
|
|
208
|
+
{VIEWS.filter(([, , need]) => need == null || present.has(need)).map(([view, label]) => (
|
|
209
|
+
<button
|
|
210
|
+
key={view}
|
|
211
|
+
type="button"
|
|
212
|
+
className={st.view === view ? 'seg on' : 'seg'}
|
|
213
|
+
onClick={() => st.set('view', view)}
|
|
214
|
+
>
|
|
215
|
+
{label}
|
|
216
|
+
</button>
|
|
217
|
+
))}
|
|
218
|
+
</div>
|
|
219
|
+
</div>
|
|
220
|
+
<div className="relight-row">
|
|
221
|
+
<label className="relight-color" data-tip="light colour">
|
|
222
|
+
<input type="color" value={st.color} onChange={(e) => st.set('color', e.target.value)} />
|
|
223
|
+
</label>
|
|
224
|
+
<Slider
|
|
225
|
+
label="power"
|
|
226
|
+
min={0}
|
|
227
|
+
max={8}
|
|
228
|
+
step={0.05}
|
|
229
|
+
value={st.intensity}
|
|
230
|
+
onChange={(v) => st.set('intensity', v)}
|
|
231
|
+
/>
|
|
232
|
+
<Slider
|
|
233
|
+
label="height"
|
|
234
|
+
min={0.02}
|
|
235
|
+
max={1.5}
|
|
236
|
+
step={0.01}
|
|
237
|
+
value={st.lightZ}
|
|
238
|
+
onChange={(v) => st.set('lightZ', v)}
|
|
239
|
+
/>
|
|
240
|
+
<Slider
|
|
241
|
+
label="reach"
|
|
242
|
+
min={0.05}
|
|
243
|
+
max={3}
|
|
244
|
+
step={0.01}
|
|
245
|
+
value={st.falloff}
|
|
246
|
+
onChange={(v) => st.set('falloff', v)}
|
|
247
|
+
/>
|
|
248
|
+
<Slider
|
|
249
|
+
label="photo"
|
|
250
|
+
min={0}
|
|
251
|
+
max={1}
|
|
252
|
+
step={0.01}
|
|
253
|
+
value={st.ambient}
|
|
254
|
+
onChange={(v) => st.set('ambient', v)}
|
|
255
|
+
/>
|
|
256
|
+
<Slider
|
|
257
|
+
label="relief"
|
|
258
|
+
min={0}
|
|
259
|
+
max={1.5}
|
|
260
|
+
step={0.01}
|
|
261
|
+
value={st.depthScale}
|
|
262
|
+
onChange={(v) => st.set('depthScale', v)}
|
|
263
|
+
/>
|
|
264
|
+
</div>
|
|
265
|
+
<div className="relight-row">
|
|
266
|
+
<Toggle
|
|
267
|
+
label="shadows"
|
|
268
|
+
on={st.shadows}
|
|
269
|
+
onChange={(v) => st.set('shadows', v)}
|
|
270
|
+
tip="cast through the depth map"
|
|
271
|
+
/>
|
|
272
|
+
<Toggle
|
|
273
|
+
label="orbit"
|
|
274
|
+
on={st.orbit}
|
|
275
|
+
onChange={(v) => st.set('orbit', v)}
|
|
276
|
+
tip="the light circles the image"
|
|
277
|
+
/>
|
|
278
|
+
<Toggle
|
|
279
|
+
label="motes"
|
|
280
|
+
on={st.particles}
|
|
281
|
+
onChange={(v) => st.set('particles', v)}
|
|
282
|
+
tip="small drifting lights"
|
|
283
|
+
/>
|
|
284
|
+
{st.particles && (
|
|
285
|
+
<Slider
|
|
286
|
+
label="count"
|
|
287
|
+
min={1}
|
|
288
|
+
max={16}
|
|
289
|
+
step={1}
|
|
290
|
+
value={st.moteCount}
|
|
291
|
+
onChange={(v) => st.set('moteCount', v)}
|
|
292
|
+
/>
|
|
293
|
+
)}
|
|
294
|
+
<Toggle
|
|
295
|
+
label="glow"
|
|
296
|
+
on={st.showLights}
|
|
297
|
+
onChange={(v) => st.set('showLights', v)}
|
|
298
|
+
tip="draw the lights themselves"
|
|
299
|
+
/>
|
|
300
|
+
<span className="relight-hint">drag: move the light · wheel: nearer / farther</span>
|
|
301
|
+
</div>
|
|
302
|
+
</div>
|
|
303
|
+
</div>
|
|
304
|
+
)
|
|
305
|
+
})
|