comfy-ts 2.23.0 → 2.24.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-DolRCMHj.js → ServeApp-fsHtW5zG.js} +3 -3
- package/dist/{ServeApp-DolRCMHj.js.map → ServeApp-fsHtW5zG.js.map} +1 -1
- package/dist/cli.js +5 -5
- package/dist/{exampleAssets-Duneoq6r.js → exampleAssets-DZynSkZm.js} +79 -15
- package/dist/exampleAssets-DZynSkZm.js.map +1 -0
- package/dist/{findDefinedWorkflow-DjoJA8y7.js → findDefinedWorkflow-qyqFea7q.js} +2 -2
- package/dist/{findDefinedWorkflow-DjoJA8y7.js.map → findDefinedWorkflow-qyqFea7q.js.map} +1 -1
- package/dist/index.cjs +80 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +53 -19
- package/dist/index.d.ts +53 -19
- package/dist/index.js +80 -15
- package/dist/index.js.map +1 -1
- package/dist/{run-run-Cr0L9lCK.js → run-run-Cn2JzKlE.js} +5 -5
- package/dist/{run-run-Cr0L9lCK.js.map → run-run-Cn2JzKlE.js.map} +1 -1
- package/dist/{run-serve---qA7o_m.js → run-serve-BhGegaCI.js} +4 -4
- package/dist/{run-serve---qA7o_m.js.map → run-serve-BhGegaCI.js.map} +1 -1
- package/dist/{run-tui-DxXiWo5J.js → run-tui-CrM4ijkH.js} +4 -4
- package/dist/{run-tui-DxXiWo5J.js.map → run-tui-CrM4ijkH.js.map} +1 -1
- package/dist/{schemaDrift-BTxdABHi.js → schemaDrift-BGYJZGMD.js} +3 -2
- package/dist/{schemaDrift-BTxdABHi.js.map → schemaDrift-BGYJZGMD.js.map} +1 -1
- package/dist/web.d.ts +53 -19
- package/dist/web.js +80 -15
- package/dist/web.js.map +1 -1
- package/examples/README.md +1 -0
- package/examples/depth-explorer/models.ts +6 -56
- package/examples/depth-explorer/serve.ts +12 -133
- package/examples/depth-explorer/web/App.tsx +20 -91
- package/examples/depth-explorer/web/index.html +1 -0
- package/examples/depth-explorer/web/state.ts +26 -109
- package/examples/depth-explorer/web/styles.css +1 -286
- package/examples/explorer-kit/catalog.ts +80 -0
- package/examples/explorer-kit/server.ts +167 -0
- package/examples/explorer-kit/web/base.css +289 -0
- package/examples/explorer-kit/web/runs.ts +160 -0
- package/examples/explorer-kit/web/ui.tsx +125 -0
- package/examples/rvion/17-relight.cflow.ts +2 -1
- package/examples/rvion/19-flythrough.cflow.ts +312 -0
- package/examples/rvion/20-grow-splat.cflow.ts +88 -0
- package/examples/rvion/21-photo-to-model.cflow.ts +191 -0
- package/examples/rvion/flythroughChain.ts +171 -0
- package/examples/rvion/flythroughColmap.ts +157 -0
- package/examples/rvion/flythroughFuse.ts +199 -0
- package/examples/rvion/flythroughGuide.ts +300 -0
- package/examples/rvion/flythroughPalette.ts +310 -0
- package/examples/rvion/flythroughPath.ts +177 -0
- package/examples/rvion/flythroughSplat.ts +73 -0
- package/examples/rvion/grow-splat/align.ts +114 -0
- package/examples/rvion/grow-splat/grow.ts +198 -0
- package/examples/rvion/grow-splat/ply.ts +166 -0
- package/examples/rvion/grow-splat/raster.ts +207 -0
- package/examples/rvion/grow-splat/sharp.ts +45 -0
- package/examples/rvion/splatDataset.ts +80 -0
- package/examples/rvion/splatEval.ts +380 -0
- package/examples/upscale-explorer/models.ts +341 -0
- package/examples/upscale-explorer/serve.ts +19 -0
- package/examples/upscale-explorer/web/App.tsx +250 -0
- package/examples/upscale-explorer/web/compare.tsx +285 -0
- package/examples/upscale-explorer/web/index.html +15 -0
- package/examples/upscale-explorer/web/main.tsx +12 -0
- package/examples/upscale-explorer/web/state.ts +279 -0
- package/examples/upscale-explorer/web/styles.css +241 -0
- package/examples/upscale-explorer/web/tsconfig.json +16 -0
- package/examples/upscale-explorer/web/viewport.tsx +130 -0
- package/examples/world-explorer/web/main.tsx +2 -2
- package/package.json +4 -3
- package/src/coreExports.ts +5 -0
- package/src/host/ComfyHost.ts +6 -0
- package/src/runner/ComfyExecution.ts +22 -5
- package/src/runner/ComfyWorkflow.ts +22 -0
- package/src/runner/ComfyWsApi.ts +1 -1
- package/src/runner/comfyOrgApiKey.ts +44 -0
- package/src/sdk-generator/ComfyUIObjectInfoTypes.ts +2 -0
- package/src/state.ts +2 -0
- package/src/vars/DefinedWorkflow.ts +1 -0
- package/dist/exampleAssets-Duneoq6r.js.map +0 -1
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// 3D gaussians in the binary ply layout of 3DGS (x y z, f_dc_0..2, opacity, scale_0..2, rot_0..3), plus the
|
|
2
|
+
// camera elements SHARP writes after the vertices (intrinsic, image_size): the world viewer frames a splat with them
|
|
3
|
+
import { readFileSync, writeFileSync } from 'node:fs'
|
|
4
|
+
|
|
5
|
+
/** the fields of one gaussian, in the order a 3DGS ply stores them */
|
|
6
|
+
export const FIELDS = [
|
|
7
|
+
'x', 'y', 'z',
|
|
8
|
+
'f_dc_0', 'f_dc_1', 'f_dc_2',
|
|
9
|
+
'opacity',
|
|
10
|
+
'scale_0', 'scale_1', 'scale_2',
|
|
11
|
+
'rot_0', 'rot_1', 'rot_2', 'rot_3',
|
|
12
|
+
] as const
|
|
13
|
+
export const STRIDE = FIELDS.length
|
|
14
|
+
/** field offsets inside one gaussian's row */
|
|
15
|
+
export const F = { x: 0, dc: 3, opacity: 6, scale: 7, rot: 10 } as const
|
|
16
|
+
|
|
17
|
+
/** the pinhole a splat was made for: focal and principal point in pixels, image size */
|
|
18
|
+
export type SplatCamera = { f: number; cx: number; cy: number; width: number; height: number }
|
|
19
|
+
|
|
20
|
+
/** n gaussians, STRIDE floats each (raw ply values: sh dc color, opacity logit, log scales, quaternion w x y z) */
|
|
21
|
+
export type Splat = { n: number; data: Float32Array; camera: SplatCamera | null }
|
|
22
|
+
|
|
23
|
+
const SIZES: Record<string, number> = { char: 1, uchar: 1, int8: 1, uint8: 1, short: 2, ushort: 2, int: 4, uint: 4, float: 4, float32: 4, double: 8 }
|
|
24
|
+
|
|
25
|
+
type Prop = { name: string; type: string }
|
|
26
|
+
type Element = { name: string; count: number; props: Prop[] }
|
|
27
|
+
|
|
28
|
+
function readValue(view: DataView, at: number, type: string): number {
|
|
29
|
+
switch (type) {
|
|
30
|
+
case 'float':
|
|
31
|
+
case 'float32':
|
|
32
|
+
return view.getFloat32(at, true)
|
|
33
|
+
case 'double':
|
|
34
|
+
return view.getFloat64(at, true)
|
|
35
|
+
case 'uint':
|
|
36
|
+
return view.getUint32(at, true)
|
|
37
|
+
case 'int':
|
|
38
|
+
return view.getInt32(at, true)
|
|
39
|
+
case 'uchar':
|
|
40
|
+
case 'uint8':
|
|
41
|
+
return view.getUint8(at)
|
|
42
|
+
case 'char':
|
|
43
|
+
case 'int8':
|
|
44
|
+
return view.getInt8(at)
|
|
45
|
+
case 'ushort':
|
|
46
|
+
return view.getUint16(at, true)
|
|
47
|
+
case 'short':
|
|
48
|
+
return view.getInt16(at, true)
|
|
49
|
+
default:
|
|
50
|
+
throw new Error(`ply: unknown property type '${type}'`)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function parseSplat(bytes: Uint8Array): Splat {
|
|
55
|
+
const marker = 'end_header\n'
|
|
56
|
+
const head = Buffer.from(bytes.subarray(0, Math.min(bytes.length, 16384))).toString('latin1')
|
|
57
|
+
const end = head.indexOf(marker)
|
|
58
|
+
if (!head.startsWith('ply\n') || end < 0) throw new Error('ply: no header')
|
|
59
|
+
const lines = head.slice(0, end).split('\n')
|
|
60
|
+
if (!lines.some((l) => l.trim() === 'format binary_little_endian 1.0'))
|
|
61
|
+
throw new Error('ply: only binary_little_endian is read')
|
|
62
|
+
const elements: Element[] = []
|
|
63
|
+
for (const line of lines) {
|
|
64
|
+
const w = line.trim().split(/\s+/)
|
|
65
|
+
if (w[0] === 'element') elements.push({ name: String(w[1]), count: Number(w[2]), props: [] })
|
|
66
|
+
else if (w[0] === 'property') {
|
|
67
|
+
const el = elements[elements.length - 1]
|
|
68
|
+
if (el == null) throw new Error('ply: property before any element')
|
|
69
|
+
if (w[1] === 'list') throw new Error('ply: list properties are not read')
|
|
70
|
+
el.props.push({ type: String(w[1]), name: String(w[2]) })
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
74
|
+
let at = end + marker.length
|
|
75
|
+
let splat: { n: number; data: Float32Array } | null = null
|
|
76
|
+
const scalars = new Map<string, number[]>()
|
|
77
|
+
for (const el of elements) {
|
|
78
|
+
const offsets: number[] = []
|
|
79
|
+
let stride = 0
|
|
80
|
+
for (const p of el.props) {
|
|
81
|
+
const size = SIZES[p.type]
|
|
82
|
+
if (size == null) throw new Error(`ply: unknown property type '${p.type}'`)
|
|
83
|
+
offsets.push(stride)
|
|
84
|
+
stride += size
|
|
85
|
+
}
|
|
86
|
+
if (el.name === 'vertex') {
|
|
87
|
+
const src = FIELDS.map((name) => {
|
|
88
|
+
const i = el.props.findIndex((p) => p.name === name)
|
|
89
|
+
if (i < 0) throw new Error(`ply: vertex has no '${name}': not a 3D gaussian splat`)
|
|
90
|
+
const prop = el.props[i]
|
|
91
|
+
if (prop?.type !== 'float' && prop?.type !== 'float32') throw new Error(`ply: '${name}' is not a float`)
|
|
92
|
+
return offsets[i] ?? 0
|
|
93
|
+
})
|
|
94
|
+
const data = new Float32Array(el.count * STRIDE)
|
|
95
|
+
for (let g = 0; g < el.count; g++) {
|
|
96
|
+
const row = at + g * stride
|
|
97
|
+
for (let k = 0; k < STRIDE; k++) data[g * STRIDE + k] = view.getFloat32(row + (src[k] ?? 0), true)
|
|
98
|
+
}
|
|
99
|
+
splat = { n: el.count, data }
|
|
100
|
+
} else if (el.props.length === 1) {
|
|
101
|
+
const prop = el.props[0]
|
|
102
|
+
if (prop != null) {
|
|
103
|
+
const values: number[] = []
|
|
104
|
+
for (let i = 0; i < el.count; i++) values.push(readValue(view, at + i * stride, prop.type))
|
|
105
|
+
scalars.set(el.name, values)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
at += el.count * stride
|
|
109
|
+
}
|
|
110
|
+
if (splat == null) throw new Error('ply: no vertex element')
|
|
111
|
+
if (at > bytes.length) throw new Error(`ply: truncated (${bytes.length} bytes, the header needs ${at})`)
|
|
112
|
+
const intr = scalars.get('intrinsic')
|
|
113
|
+
const size = scalars.get('image_size')
|
|
114
|
+
const camera =
|
|
115
|
+
intr != null && intr.length === 9 && size != null && size.length === 2
|
|
116
|
+
? { f: intr[0] ?? 0, cx: intr[2] ?? 0, cy: intr[5] ?? 0, width: size[0] ?? 0, height: size[1] ?? 0 }
|
|
117
|
+
: null
|
|
118
|
+
return { ...splat, camera }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function readSplat(path: string): Splat {
|
|
122
|
+
return parseSplat(readFileSync(path))
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** the binary ply: the gaussians, then the camera as SHARP writes it (identity extrinsic) when there is one */
|
|
126
|
+
export function encodeSplat(s: Splat): Uint8Array {
|
|
127
|
+
if (s.data.length !== s.n * STRIDE) throw new Error(`ply: ${s.n} gaussians need ${s.n * STRIDE} floats, got ${s.data.length}`)
|
|
128
|
+
const head = ['ply', 'format binary_little_endian 1.0', `element vertex ${s.n}`, ...FIELDS.map((f) => `property float ${f}`)]
|
|
129
|
+
const c = s.camera
|
|
130
|
+
if (c != null)
|
|
131
|
+
head.push('element extrinsic 16', 'property float extrinsic', 'element intrinsic 9', 'property float intrinsic', 'element image_size 2', 'property uint image_size')
|
|
132
|
+
head.push('end_header', '')
|
|
133
|
+
const header = Buffer.from(head.join('\n'), 'latin1')
|
|
134
|
+
const tail = c == null ? 0 : 16 * 4 + 9 * 4 + 2 * 4
|
|
135
|
+
const out = new Uint8Array(header.length + s.data.byteLength + tail)
|
|
136
|
+
out.set(header, 0)
|
|
137
|
+
const view = new DataView(out.buffer)
|
|
138
|
+
let at = header.length
|
|
139
|
+
for (let i = 0; i < s.data.length; i++, at += 4) view.setFloat32(at, s.data[i] ?? 0, true)
|
|
140
|
+
if (c != null) {
|
|
141
|
+
for (let i = 0; i < 16; i++, at += 4) view.setFloat32(at, i % 5 === 0 ? 1 : 0, true)
|
|
142
|
+
for (const v of [c.f, 0, c.cx, 0, c.f, c.cy, 0, 0, 1]) {
|
|
143
|
+
view.setFloat32(at, v, true)
|
|
144
|
+
at += 4
|
|
145
|
+
}
|
|
146
|
+
view.setUint32(at, c.width, true)
|
|
147
|
+
view.setUint32(at + 4, c.height, true)
|
|
148
|
+
}
|
|
149
|
+
return out
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function writeSplat(path: string, s: Splat): void {
|
|
153
|
+
writeFileSync(path, encodeSplat(s))
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** one splat after the other; the first one's camera stays */
|
|
157
|
+
export function concatSplats(parts: Splat[]): Splat {
|
|
158
|
+
const n = parts.reduce((a, p) => a + p.n, 0)
|
|
159
|
+
const data = new Float32Array(n * STRIDE)
|
|
160
|
+
let at = 0
|
|
161
|
+
for (const p of parts) {
|
|
162
|
+
data.set(p.data, at)
|
|
163
|
+
at += p.data.length
|
|
164
|
+
}
|
|
165
|
+
return { n, data, camera: parts[0]?.camera ?? null }
|
|
166
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// a CPU gaussian splat rasterizer (EWA: each gaussian a projected ellipse, sorted near to far, blended front to back)
|
|
2
|
+
// with the three maps the grow loop needs from one camera: color, coverage (alpha) and depth in metres.
|
|
3
|
+
// axes are SHARP's (OpenCV): x right, y down, z forward; a pose is camera to world
|
|
4
|
+
import { F, STRIDE, type Splat, type SplatCamera } from './ply.ts'
|
|
5
|
+
|
|
6
|
+
export type Vec3 = [number, number, number]
|
|
7
|
+
/** a 3x3 matrix, row major */
|
|
8
|
+
export type Mat3 = [number, number, number, number, number, number, number, number, number]
|
|
9
|
+
/** where a camera stands (C) and how it is turned (R: its x, y, z axes as the columns, in world axes) */
|
|
10
|
+
export type Pose = { R: Mat3; C: Vec3 }
|
|
11
|
+
|
|
12
|
+
export const IDENTITY: Mat3 = [1, 0, 0, 0, 1, 0, 0, 0, 1]
|
|
13
|
+
|
|
14
|
+
export function mul3(a: Mat3, b: Mat3): Mat3 {
|
|
15
|
+
const o: number[] = []
|
|
16
|
+
for (let r = 0; r < 3; r++)
|
|
17
|
+
for (let c = 0; c < 3; c++) o.push(a[r * 3]! * b[c]! + a[r * 3 + 1]! * b[3 + c]! + a[r * 3 + 2]! * b[6 + c]!)
|
|
18
|
+
return o as Mat3
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function transpose3(a: Mat3): Mat3 {
|
|
22
|
+
return [a[0], a[3], a[6], a[1], a[4], a[7], a[2], a[5], a[8]]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function apply3(m: Mat3, v: Vec3): Vec3 {
|
|
26
|
+
return [
|
|
27
|
+
m[0] * v[0] + m[1] * v[1] + m[2] * v[2],
|
|
28
|
+
m[3] * v[0] + m[4] * v[1] + m[5] * v[2],
|
|
29
|
+
m[6] * v[0] + m[7] * v[1] + m[8] * v[2],
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** the turn of a camera: yaw to its left (degrees, about the down axis), then pitch up */
|
|
34
|
+
export function turn(yawLeft: number, pitchUp = 0): Mat3 {
|
|
35
|
+
const y = (yawLeft * Math.PI) / 180
|
|
36
|
+
const p = (pitchUp * Math.PI) / 180
|
|
37
|
+
const yaw: Mat3 = [Math.cos(y), 0, -Math.sin(y), 0, 1, 0, Math.sin(y), 0, Math.cos(y)]
|
|
38
|
+
const pitch: Mat3 = [1, 0, 0, 0, Math.cos(p), -Math.sin(p), 0, Math.sin(p), Math.cos(p)]
|
|
39
|
+
return mul3(yaw, pitch)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function quatToMat(w: number, x: number, y: number, z: number): Mat3 {
|
|
43
|
+
const n = Math.hypot(w, x, y, z) || 1
|
|
44
|
+
w /= n
|
|
45
|
+
x /= n
|
|
46
|
+
y /= n
|
|
47
|
+
z /= n
|
|
48
|
+
return [
|
|
49
|
+
1 - 2 * (y * y + z * z), 2 * (x * y - w * z), 2 * (x * z + w * y),
|
|
50
|
+
2 * (x * y + w * z), 1 - 2 * (x * x + z * z), 2 * (y * z - w * x),
|
|
51
|
+
2 * (x * z - w * y), 2 * (y * z + w * x), 1 - 2 * (x * x + y * y),
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function matToQuat(m: Mat3): [number, number, number, number] {
|
|
56
|
+
const t = m[0] + m[4] + m[8]
|
|
57
|
+
if (t > 0) {
|
|
58
|
+
const s = Math.sqrt(t + 1) * 2
|
|
59
|
+
return [0.25 * s, (m[7] - m[5]) / s, (m[2] - m[6]) / s, (m[3] - m[1]) / s]
|
|
60
|
+
}
|
|
61
|
+
if (m[0] > m[4] && m[0] > m[8]) {
|
|
62
|
+
const s = Math.sqrt(1 + m[0] - m[4] - m[8]) * 2
|
|
63
|
+
return [(m[7] - m[5]) / s, 0.25 * s, (m[1] + m[3]) / s, (m[2] + m[6]) / s]
|
|
64
|
+
}
|
|
65
|
+
if (m[4] > m[8]) {
|
|
66
|
+
const s = Math.sqrt(1 + m[4] - m[0] - m[8]) * 2
|
|
67
|
+
return [(m[2] - m[6]) / s, (m[1] + m[3]) / s, 0.25 * s, (m[5] + m[7]) / s]
|
|
68
|
+
}
|
|
69
|
+
const s = Math.sqrt(1 + m[8] - m[0] - m[4]) * 2
|
|
70
|
+
return [(m[3] - m[1]) / s, (m[2] + m[6]) / s, (m[5] + m[7]) / s, 0.25 * s]
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const SH_C0 = 0.28209479177387814
|
|
74
|
+
|
|
75
|
+
export type Render = {
|
|
76
|
+
width: number
|
|
77
|
+
height: number
|
|
78
|
+
/** rgb 0..1 over black, 3 floats a pixel */
|
|
79
|
+
color: Float32Array
|
|
80
|
+
/** how much of each pixel the gaussians cover, 0..1 */
|
|
81
|
+
alpha: Float32Array
|
|
82
|
+
/** the blended distance along the camera's z, metres; 0 where nothing is */
|
|
83
|
+
depth: Float32Array
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type RenderOptions = {
|
|
87
|
+
/** gaussians nearer than this to the camera plane are skipped (metres) */
|
|
88
|
+
near?: number
|
|
89
|
+
/** an ellipse never reaches past this many pixels from its centre */
|
|
90
|
+
maxRadius?: number
|
|
91
|
+
/** rgb 0..1 a gaussian, in place of its sh dc colour (a view dependent colour evaluated for this camera) */
|
|
92
|
+
colors?: Float32Array
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function render(s: Splat, cam: SplatCamera, pose: Pose, o: RenderOptions = {}): Render {
|
|
96
|
+
const W = cam.width
|
|
97
|
+
const H = cam.height
|
|
98
|
+
const near = o.near ?? 0.05
|
|
99
|
+
const maxRadius = o.maxRadius ?? 400
|
|
100
|
+
const toCam = transpose3(pose.R)
|
|
101
|
+
const d = s.data
|
|
102
|
+
// the projected ellipses, kept for the visible ones only
|
|
103
|
+
const u0 = new Float32Array(s.n)
|
|
104
|
+
const v0 = new Float32Array(s.n)
|
|
105
|
+
const depthOf = new Float32Array(s.n)
|
|
106
|
+
const conic = new Float32Array(s.n * 3)
|
|
107
|
+
const radius = new Uint16Array(s.n)
|
|
108
|
+
const visible: number[] = []
|
|
109
|
+
for (let g = 0; g < s.n; g++) {
|
|
110
|
+
const b = g * STRIDE
|
|
111
|
+
const px = d[b]! - pose.C[0]
|
|
112
|
+
const py = d[b + 1]! - pose.C[1]
|
|
113
|
+
const pz = d[b + 2]! - pose.C[2]
|
|
114
|
+
const x = toCam[0] * px + toCam[1] * py + toCam[2] * pz
|
|
115
|
+
const y = toCam[3] * px + toCam[4] * py + toCam[5] * pz
|
|
116
|
+
const z = toCam[6] * px + toCam[7] * py + toCam[8] * pz
|
|
117
|
+
if (z < near) continue
|
|
118
|
+
const u = (cam.f * x) / z + cam.cx
|
|
119
|
+
const v = (cam.f * y) / z + cam.cy
|
|
120
|
+
// M = toCam · Rg · diag(scale); the camera covariance is M Mᵀ, the image one J M (J M)ᵀ
|
|
121
|
+
const rg = quatToMat(d[b + F.rot]!, d[b + F.rot + 1]!, d[b + F.rot + 2]!, d[b + F.rot + 3]!)
|
|
122
|
+
const sx = Math.exp(d[b + F.scale]!)
|
|
123
|
+
const sy = Math.exp(d[b + F.scale + 1]!)
|
|
124
|
+
const sz = Math.exp(d[b + F.scale + 2]!)
|
|
125
|
+
const m = mul3(toCam, rg)
|
|
126
|
+
const fz = cam.f / z
|
|
127
|
+
const fxz = (cam.f * x) / (z * z)
|
|
128
|
+
const fyz = (cam.f * y) / (z * z)
|
|
129
|
+
// rows of J M, each column scaled by its axis' size
|
|
130
|
+
const a0 = (fz * m[0] - fxz * m[6]) * sx
|
|
131
|
+
const a1 = (fz * m[1] - fxz * m[7]) * sy
|
|
132
|
+
const a2 = (fz * m[2] - fxz * m[8]) * sz
|
|
133
|
+
const c0 = (fz * m[3] - fyz * m[6]) * sx
|
|
134
|
+
const c1 = (fz * m[4] - fyz * m[7]) * sy
|
|
135
|
+
const c2 = (fz * m[5] - fyz * m[8]) * sz
|
|
136
|
+
// the 0.3 px² low pass of the 3DGS rasterizer: no ellipse thinner than a pixel
|
|
137
|
+
const sxx = a0 * a0 + a1 * a1 + a2 * a2 + 0.3
|
|
138
|
+
const sxy = a0 * c0 + a1 * c1 + a2 * c2
|
|
139
|
+
const syy = c0 * c0 + c1 * c1 + c2 * c2 + 0.3
|
|
140
|
+
const det = sxx * syy - sxy * sxy
|
|
141
|
+
if (det <= 0) continue
|
|
142
|
+
const mid = 0.5 * (sxx + syy)
|
|
143
|
+
const lmax = mid + Math.sqrt(Math.max(0.1, mid * mid - det))
|
|
144
|
+
const r = Math.min(maxRadius, Math.ceil(3 * Math.sqrt(lmax)))
|
|
145
|
+
if (u + r < 0 || u - r >= W || v + r < 0 || v - r >= H) continue
|
|
146
|
+
u0[g] = u
|
|
147
|
+
v0[g] = v
|
|
148
|
+
depthOf[g] = z
|
|
149
|
+
conic[g * 3] = syy / det
|
|
150
|
+
conic[g * 3 + 1] = -sxy / det
|
|
151
|
+
conic[g * 3 + 2] = sxx / det
|
|
152
|
+
radius[g] = r
|
|
153
|
+
visible.push(g)
|
|
154
|
+
}
|
|
155
|
+
visible.sort((a, b) => depthOf[a]! - depthOf[b]!)
|
|
156
|
+
|
|
157
|
+
const color = new Float32Array(W * H * 3)
|
|
158
|
+
const T = new Float32Array(W * H).fill(1)
|
|
159
|
+
const depthSum = new Float32Array(W * H)
|
|
160
|
+
for (const g of visible) {
|
|
161
|
+
const b = g * STRIDE
|
|
162
|
+
const op = 1 / (1 + Math.exp(-d[b + F.opacity]!))
|
|
163
|
+
if (op < 1 / 255) continue
|
|
164
|
+
const cr = o.colors != null ? o.colors[g * 3]! : Math.min(1, Math.max(0, 0.5 + SH_C0 * d[b + F.dc]!))
|
|
165
|
+
const cg = o.colors != null ? o.colors[g * 3 + 1]! : Math.min(1, Math.max(0, 0.5 + SH_C0 * d[b + F.dc + 1]!))
|
|
166
|
+
const cb = o.colors != null ? o.colors[g * 3 + 2]! : Math.min(1, Math.max(0, 0.5 + SH_C0 * d[b + F.dc + 2]!))
|
|
167
|
+
const u = u0[g]!
|
|
168
|
+
const v = v0[g]!
|
|
169
|
+
const z = depthOf[g]!
|
|
170
|
+
const ca = conic[g * 3]!
|
|
171
|
+
const cbc = conic[g * 3 + 1]!
|
|
172
|
+
const cc = conic[g * 3 + 2]!
|
|
173
|
+
const r = radius[g]!
|
|
174
|
+
const xa = Math.max(0, Math.floor(u - r))
|
|
175
|
+
const xb = Math.min(W - 1, Math.ceil(u + r))
|
|
176
|
+
const ya = Math.max(0, Math.floor(v - r))
|
|
177
|
+
const yb = Math.min(H - 1, Math.ceil(v + r))
|
|
178
|
+
for (let py = ya; py <= yb; py++) {
|
|
179
|
+
const dy = py + 0.5 - v
|
|
180
|
+
const row = py * W
|
|
181
|
+
for (let px = xa; px <= xb; px++) {
|
|
182
|
+
const i = row + px
|
|
183
|
+
const t = T[i]!
|
|
184
|
+
if (t < 1e-4) continue
|
|
185
|
+
const dx = px + 0.5 - u
|
|
186
|
+
const power = -0.5 * (ca * dx * dx + cc * dy * dy) - cbc * dx * dy
|
|
187
|
+
if (power > 0 || power < -4.5) continue
|
|
188
|
+
const al = Math.min(0.99, op * Math.exp(power))
|
|
189
|
+
if (al < 1 / 255) continue
|
|
190
|
+
const w = t * al
|
|
191
|
+
color[i * 3] = color[i * 3]! + w * cr
|
|
192
|
+
color[i * 3 + 1] = color[i * 3 + 1]! + w * cg
|
|
193
|
+
color[i * 3 + 2] = color[i * 3 + 2]! + w * cb
|
|
194
|
+
depthSum[i] = depthSum[i]! + w * z
|
|
195
|
+
T[i] = t * (1 - al)
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
const alpha = new Float32Array(W * H)
|
|
200
|
+
const depth = new Float32Array(W * H)
|
|
201
|
+
for (let i = 0; i < W * H; i++) {
|
|
202
|
+
const a = 1 - T[i]!
|
|
203
|
+
alpha[i] = a
|
|
204
|
+
depth[i] = a > 1e-3 ? depthSum[i]! / a : 0
|
|
205
|
+
}
|
|
206
|
+
return { width: W, height: H, color, alpha, depth }
|
|
207
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// lift one picture to 3D gaussians with Apple's SHARP (github.com/apple/ml-sharp), which runs outside ComfyUI:
|
|
2
|
+
// locally when its `sharp` cli is on the PATH, or on a box over ssh. The box is named by the environment only:
|
|
3
|
+
// SHARP_SSH=<ssh destination> SHARP_ROOT=<SHARP's folder on it, holding .venv> SHARP_WORK=<a folder for files>
|
|
4
|
+
// on a Windows box each step runs in its own ssh call: a chained cmd.exe line can fail without a word
|
|
5
|
+
import { spawnSync } from 'node:child_process'
|
|
6
|
+
import { existsSync, mkdirSync } from 'node:fs'
|
|
7
|
+
import { basename, dirname, join } from 'pathe'
|
|
8
|
+
|
|
9
|
+
function run(cmd: string, args: string[]): string {
|
|
10
|
+
const r = spawnSync(cmd, args, { encoding: 'utf8' })
|
|
11
|
+
if (r.error != null) throw new Error(`${cmd} failed to start: ${r.error.message}`)
|
|
12
|
+
if (r.status !== 0) throw new Error(`${cmd} ${args.join(' ')} exited ${r.status}:\n${r.stderr}${r.stdout}`.trim())
|
|
13
|
+
return `${r.stdout}${r.stderr}`
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** SHARP on `png` writes `outPly`; SHARP reads no focal from a png, so it assumes 30 mm (f = width × 30 / 36) */
|
|
17
|
+
export function sharpPredict(png: string, outPly: string): void {
|
|
18
|
+
mkdirSync(dirname(outPly), { recursive: true })
|
|
19
|
+
const stem = basename(png).replace(/\.[^.]+$/, '')
|
|
20
|
+
const ssh = process.env.SHARP_SSH
|
|
21
|
+
if (ssh == null || ssh === '') {
|
|
22
|
+
const outDir = join(dirname(outPly), `.sharp-${stem}`)
|
|
23
|
+
run('sharp', ['predict', '-i', png, '-o', outDir])
|
|
24
|
+
run('mv', [join(outDir, `${stem}.ply`), outPly])
|
|
25
|
+
return
|
|
26
|
+
}
|
|
27
|
+
const root = process.env.SHARP_ROOT
|
|
28
|
+
const work = process.env.SHARP_WORK
|
|
29
|
+
if (root == null || root === '' || work == null || work === '')
|
|
30
|
+
throw new Error('SHARP_SSH is set: SHARP_ROOT (the SHARP folder on the box) and SHARP_WORK (a folder for files) must be too')
|
|
31
|
+
const win = /^[A-Za-z]:\\/.test(work)
|
|
32
|
+
const sep = win ? '\\' : '/'
|
|
33
|
+
const remoteIn = `${work}${sep}in${sep}${stem}.png`
|
|
34
|
+
const remoteOut = `${work}${sep}out`
|
|
35
|
+
const fwd = (p: string) => p.replaceAll('\\', '/')
|
|
36
|
+
for (const dir of [work, `${work}${sep}in`, remoteOut]) {
|
|
37
|
+
// mkdir fails when the folder exists, on both shells: the scp below is what proves the folder is there
|
|
38
|
+
spawnSync('ssh', [ssh, win ? `if not exist "${dir}" mkdir "${dir}"` : `mkdir -p '${dir}'`], { encoding: 'utf8' })
|
|
39
|
+
}
|
|
40
|
+
run('scp', ['-q', png, `${ssh}:${fwd(remoteIn)}`])
|
|
41
|
+
const exe = win ? `${root}\\.venv\\Scripts\\sharp.exe` : `${root}/.venv/bin/sharp`
|
|
42
|
+
run('ssh', [ssh, `"${exe}" predict -i "${remoteIn}" -o "${remoteOut}"`])
|
|
43
|
+
run('scp', ['-q', `${ssh}:${fwd(`${remoteOut}${sep}${stem}.ply`)}`, outPly])
|
|
44
|
+
if (!existsSync(outPly)) throw new Error(`SHARP gave no ${outPly}`)
|
|
45
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// the training set of a splat, picked from a full fly-through export (flythroughColmap.ts with every 1): one
|
|
2
|
+
// fixed held-out split for every variant, so variants are scored on the same frames. The base set is the
|
|
3
|
+
// one the first splat trained on (every 4th frame); every 8th of it is held out, and every 8th (shifted) is
|
|
4
|
+
// the training check. A variant then picks its training frames: denser (every 1 or 2), far enough from
|
|
5
|
+
// any held-out frame that it never trains on a near copy of one, and above a camera match score.
|
|
6
|
+
//
|
|
7
|
+
// run: bun examples/rvion/splatDataset.ts <full colmap dir> <out dir> [--every n] [--gap n] [--min-score s] [--clips 0,1,2]
|
|
8
|
+
// writes <out>/sparse/0/{cameras,images,points3D}.txt (images.txt: the training frames only) and <out>/split.json
|
|
9
|
+
import { copyFileSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
10
|
+
import { join } from 'pathe'
|
|
11
|
+
|
|
12
|
+
export type Frame = { clip: number; frame: number; score: number; name: string }
|
|
13
|
+
|
|
14
|
+
/** the frames of a full export, in time order, with their camera match (retime.json) */
|
|
15
|
+
export function exportFrames(colmapDir: string): Frame[] {
|
|
16
|
+
const timing = JSON.parse(readFileSync(join(colmapDir, 'retime.json'), 'utf8')) as { clip: number; frame: number; score: number }[]
|
|
17
|
+
return timing.map((t) => ({ ...t, name: `c${t.clip}-f${String(t.frame).padStart(3, '0')}.png` }))
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type Split = { heldout: string[]; trainCheck: string[]; train: string[] }
|
|
21
|
+
|
|
22
|
+
export type SplitRule = {
|
|
23
|
+
/** a training frame every n frames of each clip */
|
|
24
|
+
every: number
|
|
25
|
+
/** no training frame within this many frames of a held-out one, in its clip */
|
|
26
|
+
gap: number
|
|
27
|
+
/** no training frame whose camera match is under this */
|
|
28
|
+
minScore: number
|
|
29
|
+
/** the clips that train; all when absent */
|
|
30
|
+
clips?: number[]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** the base set is every 4th frame (a later clip's frame 0 is the last clip's end, kept once) */
|
|
34
|
+
const inBase = (f: Frame) => f.frame % 4 === 0 && (f.clip === 0 || f.frame > 0)
|
|
35
|
+
|
|
36
|
+
export function splitFrames(frames: Frame[], rule: SplitRule): Split {
|
|
37
|
+
const base = frames.filter(inBase)
|
|
38
|
+
const heldout = base.filter((_, i) => i % 8 === 4)
|
|
39
|
+
const held = new Set(heldout.map((f) => f.name))
|
|
40
|
+
const near = (f: Frame) => heldout.some((h) => h.clip === f.clip && Math.abs(h.frame - f.frame) <= rule.gap)
|
|
41
|
+
const train = frames.filter(
|
|
42
|
+
(f) =>
|
|
43
|
+
!held.has(f.name) &&
|
|
44
|
+
f.frame % rule.every === 0 &&
|
|
45
|
+
(f.clip === 0 || f.frame > 0) &&
|
|
46
|
+
!near(f) &&
|
|
47
|
+
f.score >= rule.minScore &&
|
|
48
|
+
(rule.clips == null || rule.clips.includes(f.clip)),
|
|
49
|
+
)
|
|
50
|
+
const trained = new Set(train.map((f) => f.name))
|
|
51
|
+
const trainCheck = base.filter((f, i) => i % 8 === 0 && trained.has(f.name))
|
|
52
|
+
return { heldout: heldout.map((f) => f.name), trainCheck: trainCheck.map((f) => f.name), train: train.map((f) => f.name) }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (import.meta.main) {
|
|
56
|
+
const args = process.argv.slice(2)
|
|
57
|
+
const flag = (name: string) => {
|
|
58
|
+
const i = args.indexOf(name)
|
|
59
|
+
return i < 0 ? undefined : args.splice(i, 2)[1]
|
|
60
|
+
}
|
|
61
|
+
const every = Number(flag('--every') ?? 4)
|
|
62
|
+
const gap = Number(flag('--gap') ?? 3)
|
|
63
|
+
const minScore = Number(flag('--min-score') ?? Number.NEGATIVE_INFINITY)
|
|
64
|
+
const clips = flag('--clips')?.split(',').map(Number)
|
|
65
|
+
const [full, out] = args
|
|
66
|
+
if (full == null || out == null) throw new Error('usage: bun examples/rvion/splatDataset.ts <full colmap dir> <out dir> [--every n] [--gap n] [--min-score s] [--clips 0,1,2]')
|
|
67
|
+
const split = splitFrames(exportFrames(full), { every, gap, minScore, clips })
|
|
68
|
+
const sparse = join(out, 'sparse', '0')
|
|
69
|
+
mkdirSync(sparse, { recursive: true })
|
|
70
|
+
copyFileSync(join(full, 'sparse', '0', 'cameras.txt'), join(sparse, 'cameras.txt'))
|
|
71
|
+
copyFileSync(join(full, 'sparse', '0', 'points3D.txt'), join(sparse, 'points3D.txt'))
|
|
72
|
+
const keep = new Set(split.train)
|
|
73
|
+
const lines = readFileSync(join(full, 'sparse', '0', 'images.txt'), 'utf8')
|
|
74
|
+
.split('\n')
|
|
75
|
+
.filter((l) => l.startsWith('#') || keep.has(l.trim().split(/\s+/)[9] ?? ''))
|
|
76
|
+
// COLMAP pairs each image line with a line of its 2D points: none here, so an empty one
|
|
77
|
+
writeFileSync(join(sparse, 'images.txt'), `${lines.flatMap((l) => (l.startsWith('#') ? [l] : [l, ''])).join('\n')}\n`)
|
|
78
|
+
writeFileSync(join(out, 'split.json'), JSON.stringify(split))
|
|
79
|
+
console.log(`🟢 ${split.train.length} training frames, ${split.heldout.length} held out, ${split.trainCheck.length} training checks: ${out}`)
|
|
80
|
+
}
|