oda-engine 0.0.0-stage → 0.1.1
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 +151 -2
- package/bin/nowhere.mjs +352 -0
- package/lib/account.mjs +57 -0
- package/lib/agentModules.mjs +57 -0
- package/lib/devServer.mjs +351 -0
- package/lib/headless.mjs +231 -0
- package/lib/headlessPreview.mjs +212 -0
- package/lib/keys.mjs +517 -0
- package/lib/make.mjs +855 -0
- package/lib/mcp.mjs +537 -0
- package/lib/open.mjs +22 -0
- package/lib/preview.mjs +87 -0
- package/lib/previewRpc.mjs +134 -0
- package/lib/project.mjs +359 -0
- package/lib/publishFlow.mjs +131 -0
- package/lib/resources.mjs +50 -0
- package/lib/rules.mjs +60 -0
- package/lib/scaffold.mjs +42 -0
- package/lib/setup.mjs +59 -0
- package/lib/tools.mjs +668 -0
- package/package.json +51 -4
- package/template/game.config.json +9 -0
- package/template/scene.json +4 -0
- package/template/src/main.ts +11 -0
- package/vendor/DOCS.md +447 -0
- package/vendor/agent/appRun.mjs +520 -0
- package/vendor/agent/assets/brief.mjs +242 -0
- package/vendor/agent/assets/collider.mjs +531 -0
- package/vendor/agent/assets/environments/index.mjs +12 -0
- package/vendor/agent/assets/environments/worldlabs.mjs +161 -0
- package/vendor/agent/assets/fetch.mjs +168 -0
- package/vendor/agent/assets/footprint.mjs +13 -0
- package/vendor/agent/assets/gltf.mjs +547 -0
- package/vendor/agent/assets/hdr.mjs +269 -0
- package/vendor/agent/assets/imageOps.mjs +171 -0
- package/vendor/agent/assets/images/codex.mjs +307 -0
- package/vendor/agent/assets/images/fal.mjs +75 -0
- package/vendor/agent/assets/images/gemini.mjs +84 -0
- package/vendor/agent/assets/images/index.mjs +25 -0
- package/vendor/agent/assets/images/openai.mjs +75 -0
- package/vendor/agent/assets/images/stability.mjs +71 -0
- package/vendor/agent/assets/images/views.mjs +31 -0
- package/vendor/agent/assets/import.mjs +1352 -0
- package/vendor/agent/assets/inspect.mjs +465 -0
- package/vendor/agent/assets/jobs.mjs +196 -0
- package/vendor/agent/assets/ktx2.mjs +290 -0
- package/vendor/agent/assets/ledger.mjs +466 -0
- package/vendor/agent/assets/net.mjs +254 -0
- package/vendor/agent/assets/prices.json +39 -0
- package/vendor/agent/assets/providers/fal.mjs +115 -0
- package/vendor/agent/assets/providers/index.mjs +22 -0
- package/vendor/agent/assets/providers/meshy.mjs +119 -0
- package/vendor/agent/assets/providers/tripo.mjs +188 -0
- package/vendor/agent/assets/sources/ambientcg.mjs +129 -0
- package/vendor/agent/assets/sources/index.mjs +23 -0
- package/vendor/agent/assets/sources/polyhaven.mjs +237 -0
- package/vendor/agent/assets/sources/polypizza.mjs +121 -0
- package/vendor/agent/assets/splat.mjs +631 -0
- package/vendor/agent/assets/targets.mjs +122 -0
- package/vendor/agent/assets/tile.mjs +261 -0
- package/vendor/agent/assets/unzip.mjs +180 -0
- package/vendor/agent/blender/cleanup.py +360 -0
- package/vendor/agent/blender/convert.py +167 -0
- package/vendor/agent/blender/find.mjs +192 -0
- package/vendor/agent/blender/nw_export.py +104 -0
- package/vendor/agent/blender/nw_rubric.py +426 -0
- package/vendor/agent/blender/run.mjs +193 -0
- package/vendor/agent/blender/runner.py +252 -0
- package/vendor/agent/blender/sheet.py +743 -0
- package/vendor/agent/depth.mjs +134 -0
- package/vendor/agent/director/board.mjs +451 -0
- package/vendor/agent/director/contracts.mjs +663 -0
- package/vendor/agent/director/director.mjs +2365 -0
- package/vendor/agent/director/estimate.mjs +200 -0
- package/vendor/agent/director/gate.mjs +802 -0
- package/vendor/agent/director/limits.json +16 -0
- package/vendor/agent/director/methods.mjs +457 -0
- package/vendor/agent/director/policy.json +33 -0
- package/vendor/agent/director/prompts.mjs +244 -0
- package/vendor/agent/director/reading.mjs +341 -0
- package/vendor/agent/director/router.mjs +184 -0
- package/vendor/agent/director/seams.mjs +243 -0
- package/vendor/agent/director/session.mjs +643 -0
- package/vendor/agent/director/store.mjs +119 -0
- package/vendor/agent/director/taskGate.mjs +110 -0
- package/vendor/agent/director/teamLedger.mjs +118 -0
- package/vendor/agent/director/triage.mjs +63 -0
- package/vendor/agent/downloads.cjs +363 -0
- package/vendor/agent/flow.mjs +78 -0
- package/vendor/agent/guard.mjs +613 -0
- package/vendor/agent/host.mjs +1736 -0
- package/vendor/agent/language.mjs +58 -0
- package/vendor/agent/ledger.mjs +360 -0
- package/vendor/agent/locale.cjs +186 -0
- package/vendor/agent/loop/capacity.mjs +74 -0
- package/vendor/agent/loop/compose.mjs +127 -0
- package/vendor/agent/loop/gate.mjs +205 -0
- package/vendor/agent/loop/judge.mjs +189 -0
- package/vendor/agent/loop/levels.mjs +273 -0
- package/vendor/agent/loop/state.mjs +358 -0
- package/vendor/agent/notes.cjs +278 -0
- package/vendor/agent/pace.mjs +258 -0
- package/vendor/agent/panels.mjs +431 -0
- package/vendor/agent/prompt.mjs +342 -0
- package/vendor/agent/reference/board.mjs +410 -0
- package/vendor/agent/reference/compose.mjs +364 -0
- package/vendor/agent/reference/decode.mjs +56 -0
- package/vendor/agent/reference/facts.mjs +1874 -0
- package/vendor/agent/reference/png.mjs +107 -0
- package/vendor/agent/reference/records.mjs +63 -0
- package/vendor/agent/reference/web.mjs +252 -0
- package/vendor/agent/roles/artist.mjs +69 -0
- package/vendor/agent/roles/builder.mjs +79 -0
- package/vendor/agent/roles/checks.mjs +223 -0
- package/vendor/agent/roles/common.mjs +188 -0
- package/vendor/agent/roles/critic.mjs +171 -0
- package/vendor/agent/roles/index.mjs +39 -0
- package/vendor/agent/roles/modeller.mjs +142 -0
- package/vendor/agent/roles/qa.mjs +58 -0
- package/vendor/agent/roles/rubrics.mjs +79 -0
- package/vendor/agent/roles/sound.mjs +69 -0
- package/vendor/agent/rounds.cjs +333 -0
- package/vendor/agent/shared.mjs +284 -0
- package/vendor/agent/stopGate.mjs +574 -0
- package/vendor/agent/subagents.mjs +129 -0
- package/vendor/agent/tools/assets.mjs +499 -0
- package/vendor/agent/tools/blender.mjs +754 -0
- package/vendor/agent/tools/cards.mjs +479 -0
- package/vendor/agent/tools/choices.mjs +479 -0
- package/vendor/agent/tools/compose.mjs +74 -0
- package/vendor/agent/tools/environment.mjs +488 -0
- package/vendor/agent/tools/eyes.mjs +733 -0
- package/vendor/agent/tools/generate.mjs +338 -0
- package/vendor/agent/tools/guide.mjs +46 -0
- package/vendor/agent/tools/harness.mjs +193 -0
- package/vendor/agent/tools/image.mjs +643 -0
- package/vendor/agent/tools/index.mjs +162 -0
- package/vendor/agent/tools/look.mjs +578 -0
- package/vendor/agent/tools/loop.mjs +858 -0
- package/vendor/agent/tools/others.mjs +123 -0
- package/vendor/agent/tools/pictures.mjs +535 -0
- package/vendor/agent/tools/reference.mjs +366 -0
- package/vendor/agent/transcripts.mjs +118 -0
- package/vendor/agent-loop.mjs +602 -0
- package/vendor/agent-plugin/.claude-plugin/plugin.json +5 -0
- package/vendor/agent-plugin/skills/art-direction/SKILL.md +82 -0
- package/vendor/agent-plugin/skills/asset-legality/SKILL.md +101 -0
- package/vendor/agent-plugin/skills/asset-sources/SKILL.md +111 -0
- package/vendor/agent-plugin/skills/blender-modeling/SKILL.md +97 -0
- package/vendor/agent-plugin/skills/look-dev/SKILL.md +104 -0
- package/vendor/agent-plugin/skills/playtesting/SKILL.md +74 -0
- package/vendor/agent-plugin/skills/reference-loop/SKILL.md +102 -0
- package/vendor/agent-plugin/skills/sound-design/SKILL.md +60 -0
- package/vendor/agent-plugin/skills/style/SKILL.md +35 -0
- package/vendor/agent-plugin/skills/tsl-r185/SKILL.md +107 -0
- package/vendor/auth.cjs +570 -0
- package/vendor/build.cjs +953 -0
- package/vendor/catalog.json +455 -0
- package/vendor/connectors.cjs +688 -0
- package/vendor/eyes.mjs +733 -0
- package/vendor/gameExports.json +1390 -0
- package/vendor/git.cjs +222 -0
- package/vendor/models.json +57 -0
- package/vendor/publish.cjs +1018 -0
- package/vendor/refBoard.cjs +301 -0
- package/vendor/schema/dist/assets.d.ts +335 -0
- package/vendor/schema/dist/assets.js +439 -0
- package/vendor/schema/dist/brief.d.ts +357 -0
- package/vendor/schema/dist/brief.js +302 -0
- package/vendor/schema/dist/common.d.ts +9 -0
- package/vendor/schema/dist/common.js +13 -0
- package/vendor/schema/dist/config.d.ts +406 -0
- package/vendor/schema/dist/config.js +355 -0
- package/vendor/schema/dist/contract.d.ts +599 -0
- package/vendor/schema/dist/contract.js +1146 -0
- package/vendor/schema/dist/index.d.ts +14 -0
- package/vendor/schema/dist/index.js +21 -0
- package/vendor/schema/dist/look.d.ts +463 -0
- package/vendor/schema/dist/look.js +681 -0
- package/vendor/schema/dist/lookStats.d.ts +230 -0
- package/vendor/schema/dist/lookStats.js +768 -0
- package/vendor/schema/dist/pilot.d.ts +32 -0
- package/vendor/schema/dist/pilot.js +44 -0
- package/vendor/schema/dist/publish.d.ts +214 -0
- package/vendor/schema/dist/publish.js +291 -0
- package/vendor/schema/dist/scene.d.ts +75 -0
- package/vendor/schema/dist/scene.js +120 -0
- package/vendor/schema/dist/skyLight.d.ts +141 -0
- package/vendor/schema/dist/skyLight.js +436 -0
- package/vendor/schema/dist/style.d.ts +131 -0
- package/vendor/schema/dist/style.js +155 -0
- package/vendor/schema/package.json +1 -0
- package/vendor/selectionPrompt.mjs +51 -0
- package/vendor/useTools.mjs +195 -0
- package/vendor/userMessage.mjs +26 -0
- package/web-dist/assets/GaussianSplatUtils-CptQkDVX.js +1 -0
- package/web-dist/assets/KTX2Loader-C53LJqss.js +2 -0
- package/web-dist/assets/SPZLoader-BQ3tjaTv.js +1 -0
- package/web-dist/assets/gaussianSplat-_9tcWegB.js +1 -0
- package/web-dist/assets/index-Baka-ZO7.js +693 -0
- package/web-dist/assets/index-CcjyyQco.css +1 -0
- package/web-dist/assets/meshopt_decoder.module-BvwdjiD3.js +1 -0
- package/web-dist/assets/zstddec.module-CfFeQW7d.js +1 -0
- package/web-dist/index.html +34 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// A small Radiance (.hdr, RGBE) reader, for one number pair: the colours of
|
|
2
|
+
// an HDRI's upper and lower halves. An HDRI sky in game.config.json carries
|
|
3
|
+
// them (sky.top, sky.bottom) because everything that only knows gradients
|
|
4
|
+
// reads those: the SDK's character glow, the universe's globe, the splat
|
|
5
|
+
// backdrop, and the look's own sky until the HDRI has loaded.
|
|
6
|
+
//
|
|
7
|
+
// The halves are averaged over their solid angle (rows weighted by the cosine
|
|
8
|
+
// of their latitude), brought into display range with Reinhard (x / (1 + x))
|
|
9
|
+
// and written as sRGB hex.
|
|
10
|
+
|
|
11
|
+
const MAX_SIDE = 32768
|
|
12
|
+
|
|
13
|
+
function header(buf) {
|
|
14
|
+
let p = 0
|
|
15
|
+
const line = () => {
|
|
16
|
+
const end = buf.indexOf(0x0a, p)
|
|
17
|
+
if (end < 0) throw new Error('the header never ends')
|
|
18
|
+
const s = buf.subarray(p, end).toString('latin1')
|
|
19
|
+
p = end + 1
|
|
20
|
+
return s
|
|
21
|
+
}
|
|
22
|
+
const first = line()
|
|
23
|
+
if (!/^#\?(RADIANCE|RGBE)/.test(first)) throw new Error('it is not a Radiance HDR file')
|
|
24
|
+
let format = null
|
|
25
|
+
for (let n = 0; n < 128; n++) {
|
|
26
|
+
const l = line()
|
|
27
|
+
if (l === '') break
|
|
28
|
+
const m = /^FORMAT=(.*)$/.exec(l)
|
|
29
|
+
if (m) format = m[1].trim()
|
|
30
|
+
}
|
|
31
|
+
if (format && format !== '32-bit_rle_rgbe') throw new Error(`its pixels are ${format}; only 32-bit_rle_rgbe is read`)
|
|
32
|
+
const size = /^-Y\s+(\d+)\s+\+X\s+(\d+)$/.exec(line().trim())
|
|
33
|
+
if (!size) throw new Error('its picture is not stored top row first (-Y H +X W)')
|
|
34
|
+
const height = Number(size[1])
|
|
35
|
+
const width = Number(size[2])
|
|
36
|
+
if (!(width > 0 && height > 0 && width <= MAX_SIDE && height <= MAX_SIDE)) throw new Error('it has an impossible size')
|
|
37
|
+
return { width, height, offset: p }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Decodes every scanline into RGBE bytes, width * 4 per row. Calls
|
|
41
|
+
* `row(y, rgbe)` for each one as it goes, so a big file is never held
|
|
42
|
+
* decoded whole. */
|
|
43
|
+
function scanlines(buf, { width, height, offset }, row) {
|
|
44
|
+
let p = offset
|
|
45
|
+
const line = new Uint8Array(width * 4)
|
|
46
|
+
for (let y = 0; y < height; y++) {
|
|
47
|
+
if (p + 4 > buf.length) throw new Error('it ends early')
|
|
48
|
+
const rle = width >= 8 && width < 0x8000 && buf[p] === 2 && buf[p + 1] === 2 && (buf[p + 2] << 8 | buf[p + 3]) === width
|
|
49
|
+
if (!rle) {
|
|
50
|
+
// Flat (or old-style) pixels: four bytes each.
|
|
51
|
+
if (p + width * 4 > buf.length) throw new Error('it ends early')
|
|
52
|
+
line.set(buf.subarray(p, p + width * 4))
|
|
53
|
+
p += width * 4
|
|
54
|
+
} else {
|
|
55
|
+
p += 4
|
|
56
|
+
for (let c = 0; c < 4; c++) {
|
|
57
|
+
let x = 0
|
|
58
|
+
while (x < width) {
|
|
59
|
+
if (p >= buf.length) throw new Error('it ends early')
|
|
60
|
+
let count = buf[p++]
|
|
61
|
+
if (count > 128) {
|
|
62
|
+
count -= 128
|
|
63
|
+
if (x + count > width) throw new Error('a run passes the end of its line')
|
|
64
|
+
const v = buf[p++]
|
|
65
|
+
for (let i = 0; i < count; i++) line[(x++) * 4 + c] = v
|
|
66
|
+
} else {
|
|
67
|
+
if (count === 0 || x + count > width || p + count > buf.length) throw new Error('a run passes the end of its line')
|
|
68
|
+
for (let i = 0; i < count; i++) line[(x++) * 4 + c] = buf[p++]
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
row(y, line)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const toSrgb = (c) => {
|
|
78
|
+
const v = Math.max(0, Math.min(1, c))
|
|
79
|
+
return v <= 0.0031308 ? v * 12.92 : 1.055 * v ** (1 / 2.4) - 0.055
|
|
80
|
+
}
|
|
81
|
+
const hex = ([r, g, b]) => `#${[r, g, b].map((c) => Math.round(toSrgb(c / (1 + c)) * 255).toString(16).padStart(2, '0')).join('')}`
|
|
82
|
+
|
|
83
|
+
/** Linear RGB sums of the two halves, weighted by solid angle. */
|
|
84
|
+
function halves(width, height, eachRow) {
|
|
85
|
+
const top = [0, 0, 0]
|
|
86
|
+
const bottom = [0, 0, 0]
|
|
87
|
+
let wTop = 0
|
|
88
|
+
let wBottom = 0
|
|
89
|
+
// Every pixel of a row is the same weight; a stride keeps a 16k file quick.
|
|
90
|
+
const stride = Math.max(1, Math.floor(width / 512))
|
|
91
|
+
eachRow((y, rgb) => {
|
|
92
|
+
const lat = Math.PI / 2 - ((y + 0.5) / height) * Math.PI
|
|
93
|
+
const w = Math.cos(lat)
|
|
94
|
+
const into = y < height / 2 ? top : bottom
|
|
95
|
+
let n = 0
|
|
96
|
+
const s = [0, 0, 0]
|
|
97
|
+
for (let x = 0; x < width; x += stride) {
|
|
98
|
+
const [r, g, b] = rgb(x)
|
|
99
|
+
s[0] += r
|
|
100
|
+
s[1] += g
|
|
101
|
+
s[2] += b
|
|
102
|
+
n++
|
|
103
|
+
}
|
|
104
|
+
for (let c = 0; c < 3; c++) into[c] += (s[c] / n) * w
|
|
105
|
+
if (into === top) wTop += w
|
|
106
|
+
else wBottom += w
|
|
107
|
+
})
|
|
108
|
+
return { top: top.map((v) => v / (wTop || 1)), bottom: bottom.map((v) => v / (wBottom || 1)) }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The two halves of a Radiance file, as { width, height, top, bottom } with
|
|
112
|
+
* the colours as #rrggbb. Throws with a readable reason. */
|
|
113
|
+
export function measureHdr(buf) {
|
|
114
|
+
const h = header(buf)
|
|
115
|
+
const avg = halves(h.width, h.height, (visit) =>
|
|
116
|
+
scanlines(buf, h, (y, line) =>
|
|
117
|
+
visit(y, (x) => {
|
|
118
|
+
const e = line[x * 4 + 3]
|
|
119
|
+
if (e === 0) return [0, 0, 0]
|
|
120
|
+
const f = 2 ** (e - 136)
|
|
121
|
+
return [line[x * 4] * f, line[x * 4 + 1] * f, line[x * 4 + 2] * f]
|
|
122
|
+
}),
|
|
123
|
+
),
|
|
124
|
+
)
|
|
125
|
+
return { width: h.width, height: h.height, top: hex(avg.top), bottom: hex(avg.bottom), linear: avg }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Whether an OpenEXR file's header reads, every attribute inside the file:
|
|
129
|
+
* { ok: true } or { ok: false, error }. three's EXRLoader reads a name to
|
|
130
|
+
* its terminating zero without looking for the end of the buffer, so a
|
|
131
|
+
* damaged file would keep it reading for ever; this is read first. */
|
|
132
|
+
export function exrHeader(buf) {
|
|
133
|
+
if (!buf || buf.length < 8 || buf.readUInt32LE(0) !== 20000630) return { ok: false, error: 'it is not an OpenEXR file' }
|
|
134
|
+
const zeroAt = (from) => {
|
|
135
|
+
const end = buf.indexOf(0, from)
|
|
136
|
+
return end < 0 ? -1 : end
|
|
137
|
+
}
|
|
138
|
+
const seen = new Set()
|
|
139
|
+
let p = 8
|
|
140
|
+
for (let n = 0; n < 256; n++) {
|
|
141
|
+
const nameEnd = zeroAt(p)
|
|
142
|
+
if (nameEnd < 0) return { ok: false, error: 'its header never ends' }
|
|
143
|
+
const name = buf.subarray(p, nameEnd).toString('latin1')
|
|
144
|
+
p = nameEnd + 1
|
|
145
|
+
if (name === '') {
|
|
146
|
+
const missing = ['channels', 'compression', 'dataWindow'].filter((k) => !seen.has(k))
|
|
147
|
+
return missing.length ? { ok: false, error: `its header has no ${missing.join(', ')}` } : { ok: true }
|
|
148
|
+
}
|
|
149
|
+
const typeEnd = zeroAt(p)
|
|
150
|
+
if (typeEnd < 0 || typeEnd + 5 > buf.length) return { ok: false, error: 'its header is cut short' }
|
|
151
|
+
const size = buf.readInt32LE(typeEnd + 1)
|
|
152
|
+
p = typeEnd + 5
|
|
153
|
+
if (size < 0 || p + size > buf.length) return { ok: false, error: `its ${name} attribute runs past the end of the file` }
|
|
154
|
+
p += size
|
|
155
|
+
seen.add(name)
|
|
156
|
+
}
|
|
157
|
+
return { ok: false, error: 'its header has more attributes than a picture has' }
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** The same for decoded float pixels (an EXR through three's EXRLoader):
|
|
161
|
+
* `data` is RGBA, `rowsTopFirst` false when row 0 is the bottom. */
|
|
162
|
+
export function measureFloat(data, width, height, { channels = 4, rowsTopFirst = true } = {}) {
|
|
163
|
+
const avg = halves(width, height, (visit) => {
|
|
164
|
+
for (let y = 0; y < height; y++) {
|
|
165
|
+
const row = rowsTopFirst ? y : height - 1 - y
|
|
166
|
+
visit(y, (x) => {
|
|
167
|
+
const i = (row * width + x) * channels
|
|
168
|
+
return [data[i], data[i + 1], data[i + 2]]
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
})
|
|
172
|
+
return { width, height, top: hex(avg.top), bottom: hex(avg.bottom), linear: avg }
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** One channel of a scanline, run-length coded as Radiance does it: runs of
|
|
176
|
+
* four or more equal bytes as (128 + length, value), the rest as up to 128
|
|
177
|
+
* bytes after their count. */
|
|
178
|
+
function rleChannel(values, out) {
|
|
179
|
+
const n = values.length
|
|
180
|
+
let x = 0
|
|
181
|
+
while (x < n) {
|
|
182
|
+
let start = x
|
|
183
|
+
let run = 0
|
|
184
|
+
while (start < n) {
|
|
185
|
+
run = 1
|
|
186
|
+
while (start + run < n && run < 127 && values[start + run] === values[start]) run++
|
|
187
|
+
if (run >= 4) break
|
|
188
|
+
start += run
|
|
189
|
+
}
|
|
190
|
+
if (start >= n) run = 0
|
|
191
|
+
while (x < start) {
|
|
192
|
+
const count = Math.min(128, start - x)
|
|
193
|
+
out.push(count)
|
|
194
|
+
for (let i = 0; i < count; i++) out.push(values[x + i])
|
|
195
|
+
x += count
|
|
196
|
+
}
|
|
197
|
+
if (run >= 4) {
|
|
198
|
+
out.push(128 + run, values[start])
|
|
199
|
+
x = start + run
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** sRGB (0 to 255) as linear light. */
|
|
205
|
+
const toLinear = (v) => {
|
|
206
|
+
const c = v / 255
|
|
207
|
+
return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* An 8-bit sRGB picture (RGBA, top row first) as a Radiance file, run-length
|
|
212
|
+
* coded, its pixels in linear light. What it holds is what the picture
|
|
213
|
+
* holds: nothing brighter than white, so as a sky it lights the way an
|
|
214
|
+
* overcast one does, and the sun's own light is the config's sun.
|
|
215
|
+
*/
|
|
216
|
+
export function encodeHdr(pic) {
|
|
217
|
+
const { width: w, height: h, data } = pic
|
|
218
|
+
const head = Buffer.from(`#?RADIANCE\nFORMAT=32-bit_rle_rgbe\n\n-Y ${h} +X ${w}\n`, 'latin1')
|
|
219
|
+
const lut = Float64Array.from({ length: 256 }, (_, v) => toLinear(v))
|
|
220
|
+
const rle = w >= 8 && w < 0x8000
|
|
221
|
+
const out = []
|
|
222
|
+
const row = [new Uint8Array(w), new Uint8Array(w), new Uint8Array(w), new Uint8Array(w)]
|
|
223
|
+
for (let y = 0; y < h; y++) {
|
|
224
|
+
for (let x = 0; x < w; x++) {
|
|
225
|
+
const i = (y * w + x) * 4
|
|
226
|
+
const r = lut[data[i]]
|
|
227
|
+
const g = lut[data[i + 1]]
|
|
228
|
+
const b = lut[data[i + 2]]
|
|
229
|
+
const m = Math.max(r, g, b)
|
|
230
|
+
if (m < 1e-32) {
|
|
231
|
+
row[0][x] = row[1][x] = row[2][x] = row[3][x] = 0
|
|
232
|
+
continue
|
|
233
|
+
}
|
|
234
|
+
// m = f * 2^e with f in [0.5, 1), as C's frexp: each mantissa under 256.
|
|
235
|
+
const e = Math.floor(Math.log2(m)) + 1
|
|
236
|
+
const f = 256 / 2 ** e
|
|
237
|
+
row[0][x] = Math.min(255, Math.floor(r * f))
|
|
238
|
+
row[1][x] = Math.min(255, Math.floor(g * f))
|
|
239
|
+
row[2][x] = Math.min(255, Math.floor(b * f))
|
|
240
|
+
row[3][x] = e + 128
|
|
241
|
+
}
|
|
242
|
+
if (rle) {
|
|
243
|
+
out.push(2, 2, w >> 8, w & 255)
|
|
244
|
+
for (let c = 0; c < 4; c++) rleChannel(row[c], out)
|
|
245
|
+
} else {
|
|
246
|
+
for (let x = 0; x < w; x++) out.push(row[0][x], row[1][x], row[2][x], row[3][x])
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return Buffer.concat([head, Buffer.from(out)])
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** A Radiance file of solid colours, top half one and bottom half another,
|
|
253
|
+
* for checks. Flat pixels, no run-length coding. */
|
|
254
|
+
export function writeTestHdr(width, height, topRgb, bottomRgb) {
|
|
255
|
+
const head = Buffer.from(`#?RADIANCE\nFORMAT=32-bit_rle_rgbe\n\n-Y ${height} +X ${width}\n`, 'latin1')
|
|
256
|
+
const rgbe = ([r, g, b]) => {
|
|
257
|
+
const m = Math.max(r, g, b)
|
|
258
|
+
if (m < 1e-32) return [0, 0, 0, 0]
|
|
259
|
+
const e = Math.ceil(Math.log2(m))
|
|
260
|
+
const f = 256 / 2 ** e
|
|
261
|
+
return [Math.min(255, Math.floor(r * f)), Math.min(255, Math.floor(g * f)), Math.min(255, Math.floor(b * f)), e + 128]
|
|
262
|
+
}
|
|
263
|
+
const px = Buffer.alloc(width * height * 4)
|
|
264
|
+
for (let y = 0; y < height; y++) {
|
|
265
|
+
const v = rgbe(y < height / 2 ? topRgb : bottomRgb)
|
|
266
|
+
for (let x = 0; x < width; x++) px.set(v, (y * width + x) * 4)
|
|
267
|
+
}
|
|
268
|
+
return Buffer.concat([head, px])
|
|
269
|
+
}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// Resizing and re-encoding pictures for asset_import, with whatever this host
|
|
2
|
+
// has, best first:
|
|
3
|
+
// 1. the studio's image-resize rpc (Electron's nativeImage, in main),
|
|
4
|
+
// 2. sharp, when it resolves (the CLI may have it; optional),
|
|
5
|
+
// 3. nothing: the file is kept as it is and the answer says "not resized".
|
|
6
|
+
// KTX2 is written where the sides a planet goes to decode it (asset_import's
|
|
7
|
+
// decoders: @nowhere/schema DECODERS_LIVE, less what the last Publish found
|
|
8
|
+
// a side without), with Basis Universal's WebAssembly encoder
|
|
9
|
+
// (ktx2.mjs: ETC1S for a colour map that holds up in it, else UASTC; UASTC
|
|
10
|
+
// for normal and data maps; mipmaps), or KTX-Software's `ktx` (4.3 or later)
|
|
11
|
+
// or `toktx` when that package is not installed.
|
|
12
|
+
//
|
|
13
|
+
// The rpc reads and writes files only inside the project or its .nowhere/,
|
|
14
|
+
// so asset_import does its work in .nowhere/import/.
|
|
15
|
+
|
|
16
|
+
import { execFile } from 'node:child_process'
|
|
17
|
+
import fsp from 'node:fs/promises'
|
|
18
|
+
import path from 'node:path'
|
|
19
|
+
import { decodeImage } from '../reference/decode.mjs'
|
|
20
|
+
import { ktx2Encoder, ktx2For } from './ktx2.mjs'
|
|
21
|
+
|
|
22
|
+
/** Width and height from a PNG, JPEG or WebP header, or null. */
|
|
23
|
+
export function imageSize(buf) {
|
|
24
|
+
if (!buf || buf.length < 24) return null
|
|
25
|
+
// PNG: IHDR is the first chunk.
|
|
26
|
+
if (buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47) {
|
|
27
|
+
return { width: buf.readUInt32BE(16), height: buf.readUInt32BE(20), format: 'png' }
|
|
28
|
+
}
|
|
29
|
+
// JPEG: walk the markers to the first start-of-frame.
|
|
30
|
+
if (buf[0] === 0xff && buf[1] === 0xd8) {
|
|
31
|
+
let p = 2
|
|
32
|
+
while (p + 9 < buf.length) {
|
|
33
|
+
if (buf[p] !== 0xff) {
|
|
34
|
+
p++
|
|
35
|
+
continue
|
|
36
|
+
}
|
|
37
|
+
const marker = buf[p + 1]
|
|
38
|
+
if (marker === 0xd8 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) {
|
|
39
|
+
p += 2
|
|
40
|
+
continue
|
|
41
|
+
}
|
|
42
|
+
const len = buf.readUInt16BE(p + 2)
|
|
43
|
+
if ((marker >= 0xc0 && marker <= 0xc3) || (marker >= 0xc5 && marker <= 0xc7) || (marker >= 0xc9 && marker <= 0xcb) || (marker >= 0xcd && marker <= 0xcf)) {
|
|
44
|
+
return { width: buf.readUInt16BE(p + 7), height: buf.readUInt16BE(p + 5), format: 'jpg' }
|
|
45
|
+
}
|
|
46
|
+
p += 2 + len
|
|
47
|
+
}
|
|
48
|
+
return null
|
|
49
|
+
}
|
|
50
|
+
// WebP: RIFF, then VP8 (lossy), VP8L (lossless) or VP8X (extended).
|
|
51
|
+
if (buf.toString('latin1', 0, 4) === 'RIFF' && buf.toString('latin1', 8, 12) === 'WEBP') {
|
|
52
|
+
const chunk = buf.toString('latin1', 12, 16)
|
|
53
|
+
if (chunk === 'VP8 ' && buf.length >= 30) return { width: buf.readUInt16LE(26) & 0x3fff, height: buf.readUInt16LE(28) & 0x3fff, format: 'webp' }
|
|
54
|
+
if (chunk === 'VP8L' && buf.length >= 25) {
|
|
55
|
+
const b = buf.readUInt32LE(21)
|
|
56
|
+
return { width: (b & 0x3fff) + 1, height: ((b >> 14) & 0x3fff) + 1, format: 'webp' }
|
|
57
|
+
}
|
|
58
|
+
if (chunk === 'VP8X' && buf.length >= 30) return { width: 1 + buf.readUIntLE(24, 3), height: 1 + buf.readUIntLE(27, 3), format: 'webp' }
|
|
59
|
+
}
|
|
60
|
+
return null
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const run = (cmd, args, timeoutMs = 120_000) =>
|
|
64
|
+
new Promise((resolve) => {
|
|
65
|
+
execFile(cmd, args, { timeout: timeoutMs, windowsHide: true, maxBuffer: 4 << 20 }, (err, stdout, stderr) => {
|
|
66
|
+
resolve({ code: err ? (typeof err.code === 'number' ? err.code : 1) : 0, stdout: String(stdout ?? ''), stderr: String(stderr ?? ''), error: err && typeof err.code !== 'number' ? err.message : null })
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
/** Whether a command answers on PATH. */
|
|
71
|
+
export async function onPath(cmd, args = ['--version'], runner = run) {
|
|
72
|
+
const r = await runner(cmd, args, 10_000)
|
|
73
|
+
return r.code === 0 || (r.error === null && /version/i.test(r.stdout + r.stderr))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let sharpModule
|
|
77
|
+
async function loadSharp() {
|
|
78
|
+
if (sharpModule !== undefined) return sharpModule
|
|
79
|
+
try {
|
|
80
|
+
sharpModule = (await import('sharp')).default
|
|
81
|
+
} catch {
|
|
82
|
+
sharpModule = null
|
|
83
|
+
}
|
|
84
|
+
return sharpModule
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* @param {{ rpc?: Function, sharp?: false | Function, run?: Function }} deps
|
|
89
|
+
* rpc: the host's (studio only); sharp: false to skip it, or a stand-in;
|
|
90
|
+
* run: how commands are started (checks replace it).
|
|
91
|
+
*/
|
|
92
|
+
export function createImageOps({ rpc, sharp, run: runner = run } = {}) {
|
|
93
|
+
/** Fits a picture inside maxSize x maxSize and writes it as png or jpg.
|
|
94
|
+
* Never enlarges. Answers { width, height, bytes, resized, how }. */
|
|
95
|
+
async function resize(fromAbs, toAbs, { maxSize, format = 'png', quality = 90 }) {
|
|
96
|
+
const buf = await fsp.readFile(fromAbs)
|
|
97
|
+
const size = imageSize(buf)
|
|
98
|
+
const sameFormat = size?.format === format
|
|
99
|
+
// Already small enough and in the format asked for: kept byte for byte.
|
|
100
|
+
if (size && Math.max(size.width, size.height) <= maxSize && sameFormat) {
|
|
101
|
+
if (path.resolve(fromAbs) !== path.resolve(toAbs)) await fsp.copyFile(fromAbs, toAbs)
|
|
102
|
+
return { width: size.width, height: size.height, bytes: buf.length, resized: false, how: 'kept (already within size)' }
|
|
103
|
+
}
|
|
104
|
+
if (typeof rpc === 'function') {
|
|
105
|
+
try {
|
|
106
|
+
const r = await rpc('image-resize', { from: fromAbs, to: toAbs, maxSize, format, quality })
|
|
107
|
+
if (r?.ok) return { width: r.width, height: r.height, bytes: r.bytes, resized: true, how: 'the studio' }
|
|
108
|
+
} catch {
|
|
109
|
+
// the next way
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const s = sharp === false ? null : (sharp ?? (await loadSharp()))
|
|
113
|
+
if (s) {
|
|
114
|
+
try {
|
|
115
|
+
let pipeline = s(buf).resize({ width: maxSize, height: maxSize, fit: 'inside', withoutEnlargement: true })
|
|
116
|
+
pipeline = format === 'jpg' ? pipeline.jpeg({ quality, mozjpeg: true }) : pipeline.png({ compressionLevel: 9 })
|
|
117
|
+
const out = await pipeline.toBuffer({ resolveWithObject: true })
|
|
118
|
+
await fsp.writeFile(toAbs, out.data)
|
|
119
|
+
return { width: out.info.width, height: out.info.height, bytes: out.data.length, resized: true, how: 'sharp' }
|
|
120
|
+
} catch {
|
|
121
|
+
// the next way
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// Nothing here can resize or convert. The file is kept only when it is
|
|
125
|
+
// already the format asked for: a WebP must never be written under a
|
|
126
|
+
// .png name.
|
|
127
|
+
if (size && !sameFormat) {
|
|
128
|
+
return { error: `it is ${size.format}, and nothing here converts it to ${format} (no image tool)`, width: size.width, height: size.height, bytes: buf.length, resized: false, how: 'not converted' }
|
|
129
|
+
}
|
|
130
|
+
if (path.resolve(fromAbs) !== path.resolve(toAbs)) await fsp.copyFile(fromAbs, toAbs)
|
|
131
|
+
return { width: size?.width ?? null, height: size?.height ?? null, bytes: buf.length, resized: false, how: 'not resized (no image tool here)' }
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
let ktxTool
|
|
135
|
+
/** 'wasm' (the encoder package), else `ktx` (KTX-Software 4.3+), else
|
|
136
|
+
* `toktx`, else null. */
|
|
137
|
+
async function ktxCommand() {
|
|
138
|
+
if (ktxTool !== undefined) return ktxTool
|
|
139
|
+
if ((await ktx2Encoder()).ok) ktxTool = 'wasm'
|
|
140
|
+
else if (await onPath('ktx', ['--version'], runner)) ktxTool = 'ktx'
|
|
141
|
+
else if (await onPath('toktx', ['--version'], runner)) ktxTool = 'toktx'
|
|
142
|
+
else ktxTool = null
|
|
143
|
+
return ktxTool
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Encodes a PNG or JPEG as KTX2, or answers why not. role: what the map
|
|
147
|
+
* is (color, normal, rough, ...), which decides its codec. */
|
|
148
|
+
async function ktx2(fromAbs, toAbs, { normal = false, role = normal ? 'normal' : 'color' } = {}) {
|
|
149
|
+
const tool = await ktxCommand()
|
|
150
|
+
if (!tool) return { ok: false, error: 'no KTX2 encoder (the ktx2-encoder package, or KTX-Software on PATH)' }
|
|
151
|
+
if (tool === 'wasm') {
|
|
152
|
+
const pic = await decodeImage(await fsp.readFile(fromAbs), { mime: /\.jpe?g$/i.test(fromAbs) ? 'image/jpeg' : 'image/png', maxEdge: 8192, rpc })
|
|
153
|
+
if (!pic.ok) return { ok: false, error: pic.error }
|
|
154
|
+
const r = await ktx2For(pic, { role })
|
|
155
|
+
if (!r.ok) return { ok: false, error: r.error }
|
|
156
|
+
await fsp.writeFile(toAbs, r.bytes)
|
|
157
|
+
return { ok: true, bytes: r.bytes.byteLength, tool, mode: r.mode, why: r.why, measured: r.measured }
|
|
158
|
+
}
|
|
159
|
+
const args =
|
|
160
|
+
tool === 'ktx'
|
|
161
|
+
? ['create', '--format', normal ? 'R8G8B8A8_UNORM' : 'R8G8B8A8_SRGB', '--encode', normal ? 'uastc' : 'basis-lz', '--generate-mipmap', ...(normal ? ['--assign-tf', 'linear'] : []), fromAbs, toAbs]
|
|
162
|
+
: ['--t2', '--genmipmap', ...(normal ? ['--encode', 'uastc', '--assign_oetf', 'linear'] : ['--encode', 'etc1s', '--assign_oetf', 'srgb']), toAbs, fromAbs]
|
|
163
|
+
const r = await runner(tool, args, 300_000)
|
|
164
|
+
if (r.code !== 0) return { ok: false, error: `${tool} failed: ${(r.stderr || r.stdout || r.error || '').trim().slice(0, 300)}` }
|
|
165
|
+
const st = await fsp.stat(toAbs).catch(() => null)
|
|
166
|
+
if (!st?.size) return { ok: false, error: `${tool} wrote nothing` }
|
|
167
|
+
return { ok: true, bytes: st.size, tool }
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return { resize, ktx2, ktxCommand }
|
|
171
|
+
}
|