oda-engine 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/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 +90 -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-CB77cP9U.js +1 -0
- package/web-dist/assets/KTX2Loader-6OIWUF7s.js +2 -0
- package/web-dist/assets/SPZLoader-zYj0Alfv.js +1 -0
- package/web-dist/assets/gaussianSplat-CJTI1sEh.js +1 -0
- package/web-dist/assets/index-CcjyyQco.css +1 -0
- package/web-dist/assets/index-DMyJcoZJ.js +693 -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,290 @@
|
|
|
1
|
+
// KTX2 for the textures asset_import writes, made with Basis Universal's own
|
|
2
|
+
// encoder compiled to WebAssembly (the npm package ktx2-encoder carries it),
|
|
3
|
+
// so nothing is downloaded or installed on the creator's machine. The same
|
|
4
|
+
// module also transcodes, which is how each texture is measured: it is
|
|
5
|
+
// encoded, decoded back and compared with what went in.
|
|
6
|
+
//
|
|
7
|
+
// Which codec each map gets:
|
|
8
|
+
// colour ETC1S when it holds up, measured against the picture (the
|
|
9
|
+
// colour difference, the noise and the fine detail it keeps),
|
|
10
|
+
// UASTC when it does not; ETC1S is about a tenth of the bytes
|
|
11
|
+
// but can smear painted strokes and fine grain, which is why it
|
|
12
|
+
// is measured, never assumed
|
|
13
|
+
// normal, UASTC, linear (a normal map with the encoder's normal-map
|
|
14
|
+
// rough settings): the light reads them directly, and ETC1S's shared
|
|
15
|
+
// endpoints show there as banding and blotches
|
|
16
|
+
// ao, metal, ETC1S, linear, when it holds up (the noise and the fine
|
|
17
|
+
// displace- detail; there is no colour to differ), else UASTC. Measured
|
|
18
|
+
// ment, on ambientCG and Poly Haven scans: a smooth displacement, an
|
|
19
|
+
// opacity opacity or a metal mask that is mostly 0 and 1 holds up at a
|
|
20
|
+
// fifth of UASTC's bytes or less; a grainy ao or displacement
|
|
21
|
+
// does not
|
|
22
|
+
// Mipmaps always. UASTC is zstd-supercompressed, which three's KTX2Loader
|
|
23
|
+
// reads, without the encoder's RDO pass. RDO was measured on laputa-v2's
|
|
24
|
+
// painted maps and on the scans: at its default it saved 6 to 13% of the
|
|
25
|
+
// bytes of a scanned colour or rough map (rock_wall_10 at 2k, Bricks060 at
|
|
26
|
+
// 1k) for two and a half to three times the encode time and 1 to 3% of the
|
|
27
|
+
// fine detail, and nothing on normal maps; at twice the default, 16 to 23%
|
|
28
|
+
// for 7 to 9% of the detail. Not worth it as a default.
|
|
29
|
+
//
|
|
30
|
+
// Basis 2.5 caps one texture at 12 megapixels, so a 4096 x 4096 map cannot
|
|
31
|
+
// be encoded here; the answer says so and the map stays PNG or JPEG.
|
|
32
|
+
//
|
|
33
|
+
// Which way up: three uploads a PNG or a JPEG bottom row first (a texture's
|
|
34
|
+
// flipY, true by default), so a picture's top is at v = 1, and it cannot do
|
|
35
|
+
// that to a compressed texture. A .ktx2 a planet carries on its own (a
|
|
36
|
+
// material's map, a texture its code loads) is therefore stored bottom row
|
|
37
|
+
// first (ktx2For's flipY), and draws on the same UVs exactly as the PNG it
|
|
38
|
+
// replaces. A texture inside a GLB is stored top row first, as glTF says and
|
|
39
|
+
// as three's GLTFLoader draws every texture of a model (flipY false).
|
|
40
|
+
|
|
41
|
+
import path from 'node:path'
|
|
42
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
43
|
+
import { deltaE2000, srgbToLab } from '../../schema/dist/index.js'
|
|
44
|
+
|
|
45
|
+
/** When ETC1S is good enough for a colour map: all three must hold. The
|
|
46
|
+
* colour difference is the one a visitor could just see (CIEDE2000 1), and
|
|
47
|
+
* detail is the share of the fine grain kept. A Poly Haven scan at its best
|
|
48
|
+
* ETC1S quality measured 2.55 and 0.72, a laputa-v2 painted stone 1.92 and
|
|
49
|
+
* 0.73: both stay UASTC. Flat and smooth pictures pass. */
|
|
50
|
+
export const ETC1S_BAR = { psnr: 36, deltaE: 1, detail: 0.85 }
|
|
51
|
+
/** The settings each codec is run with: ETC1S at its best quality (a few
|
|
52
|
+
* percent more bytes than its default for a little more of the detail). */
|
|
53
|
+
export const ETC1S_QUALITY = 255
|
|
54
|
+
export const UASTC_RDO = 0
|
|
55
|
+
|
|
56
|
+
const MAX_TEXELS = 12 * 1024 * 1024
|
|
57
|
+
|
|
58
|
+
let loaded
|
|
59
|
+
/** The encoder module, loaded once: { ok: true, basis, encodeWithModule } or
|
|
60
|
+
* { ok: false, error }. Its own messages are silenced: printed, they would
|
|
61
|
+
* land in the CLI's MCP stream, which is the server's stdout. */
|
|
62
|
+
export function ktx2Encoder() {
|
|
63
|
+
loaded ??= (async () => {
|
|
64
|
+
try {
|
|
65
|
+
// The package's own entry names the folder its wasm build is in; the
|
|
66
|
+
// build and its encode loop are taken from there by path, since its
|
|
67
|
+
// exports offer only the entry, whose module prints to stdout.
|
|
68
|
+
const entry = fileURLToPath(import.meta.resolve('ktx2-encoder'))
|
|
69
|
+
const dist = path.resolve(path.dirname(entry), '..')
|
|
70
|
+
const { default: BASIS } = await import(pathToFileURL(path.join(dist, 'basis', 'basis_encoder.js')).href)
|
|
71
|
+
const { encodeWithModule } = await import(pathToFileURL(path.join(dist, 'encodeCore.js')).href)
|
|
72
|
+
const basis = await BASIS({ print: () => {}, printErr: () => {} })
|
|
73
|
+
basis.initializeBasis()
|
|
74
|
+
return { ok: true, basis, encodeWithModule }
|
|
75
|
+
} catch (err) {
|
|
76
|
+
return { ok: false, error: `the KTX2 encoder (npm ktx2-encoder) could not be loaded: ${String(err?.message ?? err).split('\n')[0]}` }
|
|
77
|
+
}
|
|
78
|
+
})()
|
|
79
|
+
return loaded
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The role a map plays decides its codec family: colour may be ETC1S, and
|
|
83
|
+
* so may the data maps the light does not read directly. */
|
|
84
|
+
const COLOUR_ROLES = new Set(['color', 'card', 'panorama'])
|
|
85
|
+
export const isColourRole = (role) => !role || COLOUR_ROLES.has(role)
|
|
86
|
+
const ETC1S_DATA_ROLES = new Set(['ao', 'metal', 'displacement', 'opacity'])
|
|
87
|
+
export const mayBeEtc1s = (role) => isColourRole(role) || ETC1S_DATA_ROLES.has(role)
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* One RGBA picture as KTX2 in one codec. rgba: { data, width, height }.
|
|
91
|
+
* Answers the bytes; throws with the encoder's reason.
|
|
92
|
+
*/
|
|
93
|
+
export async function encodeKtx2(rgba, { mode, srgb = true, normal = false } = {}) {
|
|
94
|
+
const enc = await ktx2Encoder()
|
|
95
|
+
if (!enc.ok) throw new Error(enc.error)
|
|
96
|
+
if (rgba.width * rgba.height > MAX_TEXELS) throw new Error(`${rgba.width}x${rgba.height} is over the 12 megapixels Basis encodes in one texture`)
|
|
97
|
+
const options = {
|
|
98
|
+
isUASTC: mode === 'uastc',
|
|
99
|
+
generateMipmap: true,
|
|
100
|
+
isSetKTX2SRGBTransferFunc: srgb,
|
|
101
|
+
// Data is weighed as numbers, not as what an eye sees.
|
|
102
|
+
isPerceptual: srgb,
|
|
103
|
+
isNormalMap: normal,
|
|
104
|
+
// The picture is already decoded: the encoder's decoder hands it over.
|
|
105
|
+
imageDecoder: async () => ({ data: rgba.data, width: rgba.width, height: rgba.height }),
|
|
106
|
+
...(mode === 'uastc' ? { needSupercompression: true, ...(UASTC_RDO > 0 ? { enableRDO: true, rdoQualityLevel: UASTC_RDO } : {}) } : { qualityLevel: ETC1S_QUALITY, compressionLevel: 2 }),
|
|
107
|
+
}
|
|
108
|
+
return enc.encodeWithModule(enc.basis, new Uint8Array(1), options)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** The same RGBA picture upside down (a new buffer): what a KTX2 that is
|
|
112
|
+
* stored bottom row first holds, and how it is read back to be looked at. */
|
|
113
|
+
export function flipRows(rgba) {
|
|
114
|
+
const { width: w, height: h } = rgba
|
|
115
|
+
const row = w * 4
|
|
116
|
+
const data = new Uint8Array(rgba.data.length)
|
|
117
|
+
for (let y = 0; y < h; y++) data.set(rgba.data.subarray(y * row, (y + 1) * row), (h - 1 - y) * row)
|
|
118
|
+
return { ...rgba, data }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** A KTX2's first image back as RGBA: { data, width, height, mode, levels }. */
|
|
122
|
+
export async function decodeKtx2(bytes) {
|
|
123
|
+
const enc = await ktx2Encoder()
|
|
124
|
+
if (!enc.ok) throw new Error(enc.error)
|
|
125
|
+
const file = new enc.basis.KTX2File(new Uint8Array(bytes))
|
|
126
|
+
try {
|
|
127
|
+
if (!file.isValid()) throw new Error('not a KTX2 file the transcoder reads')
|
|
128
|
+
if (!file.startTranscoding()) throw new Error('the transcoder could not start on it')
|
|
129
|
+
const RGBA32 = 13
|
|
130
|
+
const size = file.getImageTranscodedSizeInBytes(0, 0, 0, RGBA32)
|
|
131
|
+
const data = new Uint8Array(size)
|
|
132
|
+
if (!file.transcodeImage(data, 0, 0, 0, RGBA32, 0, -1, -1)) throw new Error('the transcoder could not decode it')
|
|
133
|
+
return { data, width: file.getWidth(), height: file.getHeight(), mode: file.isUASTC() ? 'uastc' : 'etc1s', levels: file.getLevels() }
|
|
134
|
+
} finally {
|
|
135
|
+
file.close()
|
|
136
|
+
file.delete()
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// ---------------------------------------------------------------- measuring
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* How far `b` is from `a` (same size, RGBA): PSNR over the colour channels
|
|
144
|
+
* (dB), the mean CIEDE2000 difference over a sample of pixels (colour maps
|
|
145
|
+
* only; null for data), how much of the fine detail is kept (the
|
|
146
|
+
* correlation of the two pictures' high-pass, 1 when it is all there), and
|
|
147
|
+
* the alpha channel's PSNR when the picture has transparency. The colour
|
|
148
|
+
* measures are weighted by the picture's alpha, as it is seen over what is
|
|
149
|
+
* behind it: what a transparent pixel holds is never seen.
|
|
150
|
+
*/
|
|
151
|
+
export function compareRgba(a, b, { colour = true, samples = 65536 } = {}) {
|
|
152
|
+
const { width: w, height: h } = a
|
|
153
|
+
const n = w * h
|
|
154
|
+
const weight = (p) => a.data[p * 4 + 3] / 255
|
|
155
|
+
let se = 0
|
|
156
|
+
let counted = 0
|
|
157
|
+
let seA = 0
|
|
158
|
+
let alpha = false
|
|
159
|
+
for (let p = 0, i = 0; p < n; p++, i += 4) {
|
|
160
|
+
const dA = a.data[i + 3] - b.data[i + 3]
|
|
161
|
+
seA += dA * dA
|
|
162
|
+
if (a.data[i + 3] < 255) alpha = true
|
|
163
|
+
const k = weight(p)
|
|
164
|
+
if (k === 0) continue
|
|
165
|
+
counted += k
|
|
166
|
+
for (let c = 0; c < 3; c++) {
|
|
167
|
+
const d = a.data[i + c] - b.data[i + c]
|
|
168
|
+
se += k * d * d
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
const psnrOf = (mse) => (mse === 0 ? 99 : Math.round(10 * Math.log10((255 * 255) / mse) * 100) / 100)
|
|
172
|
+
const psnr = counted ? psnrOf(se / (counted * 3)) : 99
|
|
173
|
+
let deltaE = null
|
|
174
|
+
if (colour) {
|
|
175
|
+
const step = Math.max(1, Math.floor(Math.sqrt(n / samples)))
|
|
176
|
+
let sum = 0
|
|
177
|
+
let count = 0
|
|
178
|
+
for (let y = 0; y < h; y += step) {
|
|
179
|
+
for (let x = 0; x < w; x += step) {
|
|
180
|
+
const p = y * w + x
|
|
181
|
+
const k = weight(p)
|
|
182
|
+
if (k === 0) continue
|
|
183
|
+
const i = p * 4
|
|
184
|
+
sum += k * deltaE2000(srgbToLab(a.data[i], a.data[i + 1], a.data[i + 2]), srgbToLab(b.data[i], b.data[i + 1], b.data[i + 2]))
|
|
185
|
+
count += k
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
deltaE = count ? Math.round((sum / count) * 100) / 100 : 0
|
|
189
|
+
}
|
|
190
|
+
// The high-pass of each: a pixel's luma less its 3 x 3 neighbourhood's.
|
|
191
|
+
const luma = (img) => {
|
|
192
|
+
const out = new Float32Array(n)
|
|
193
|
+
for (let p = 0, i = 0; p < n; p++, i += 4) out[p] = 0.2126 * img.data[i] + 0.7152 * img.data[i + 1] + 0.0722 * img.data[i + 2]
|
|
194
|
+
return out
|
|
195
|
+
}
|
|
196
|
+
const la = luma(a)
|
|
197
|
+
const lb = luma(b)
|
|
198
|
+
let sab = 0
|
|
199
|
+
let saa = 0
|
|
200
|
+
let sbb = 0
|
|
201
|
+
for (let y = 1; y < h - 1; y++) {
|
|
202
|
+
for (let x = 1; x < w - 1; x++) {
|
|
203
|
+
const p = y * w + x
|
|
204
|
+
const k = weight(p)
|
|
205
|
+
if (k === 0) continue
|
|
206
|
+
const box = (l) => (l[p - w - 1] + l[p - w] + l[p - w + 1] + l[p - 1] + l[p] + l[p + 1] + l[p + w - 1] + l[p + w] + l[p + w + 1]) / 9
|
|
207
|
+
const ha = la[p] - box(la)
|
|
208
|
+
const hb = lb[p] - box(lb)
|
|
209
|
+
sab += k * ha * hb
|
|
210
|
+
saa += k * ha * ha
|
|
211
|
+
sbb += k * hb * hb
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// A picture with next to no fine grain (a smooth gradient, a flat colour:
|
|
215
|
+
// under one level of luma on average) has none to lose.
|
|
216
|
+
const fine = counted ? Math.sqrt(saa / counted) : 0
|
|
217
|
+
const detail = fine < 1 || sbb === 0 ? 1 : Math.round((sab / Math.sqrt(saa * sbb)) * 1000) / 1000
|
|
218
|
+
return { psnr, deltaE, detail, ...(alpha ? { alphaPsnr: psnrOf(seA / n) } : {}) }
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** Whether ETC1S held up, against ETC1S_BAR (a data map has no colour
|
|
222
|
+
* difference, so its noise and its detail decide). */
|
|
223
|
+
export function etc1sHolds(m) {
|
|
224
|
+
return m.psnr >= ETC1S_BAR.psnr && (m.deltaE === null || m.deltaE <= ETC1S_BAR.deltaE) && m.detail >= ETC1S_BAR.detail && (m.alphaPsnr === undefined || m.alphaPsnr >= ETC1S_BAR.psnr)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** A KTX2 file's size and codec from its header: { width, height, mode }
|
|
228
|
+
* (BasisLZ supercompression is ETC1S; anything else read here is UASTC). */
|
|
229
|
+
export function ktx2Header(buf) {
|
|
230
|
+
const id = [0xab, 0x4b, 0x54, 0x58, 0x20, 0x32, 0x30, 0xbb, 0x0d, 0x0a, 0x1a, 0x0a]
|
|
231
|
+
if (!buf || buf.length < 48 || id.some((b, i) => buf[i] !== b)) return null
|
|
232
|
+
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength)
|
|
233
|
+
return { width: view.getUint32(20, true), height: view.getUint32(24, true), mode: view.getUint32(44, true) === 1 ? 'etc1s' : 'uastc' }
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* The bytes a texture takes on the GPU, mipmaps included (4/3): as RGBA8
|
|
238
|
+
* (what a PNG or JPEG becomes), and as what the KTX2 transcodes to. ETC1S
|
|
239
|
+
* becomes ETC1 or BC1 at half a byte a pixel where the device has them
|
|
240
|
+
* (phones, Apple), BC7 at one byte on a desktop with BC alone; UASTC becomes
|
|
241
|
+
* ASTC or BC7, one byte. An alpha channel takes ETC1S to one byte too.
|
|
242
|
+
*/
|
|
243
|
+
export function gpuBytes(width, height, mode = null, { alpha = false } = {}) {
|
|
244
|
+
const px = width * height * (4 / 3)
|
|
245
|
+
const rgba8 = Math.round(px * 4)
|
|
246
|
+
if (!mode) return { rgba8, low: rgba8, high: rgba8 }
|
|
247
|
+
if (mode === 'uastc') return { rgba8, low: Math.round(px), high: Math.round(px) }
|
|
248
|
+
return { rgba8, low: Math.round(px * (alpha ? 1 : 0.5)), high: Math.round(px) }
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* The KTX2 for one map, its codec chosen for its role, with what was
|
|
253
|
+
* measured: { ok, bytes, mode, width, height, measured: { etc1s?, uastc? },
|
|
254
|
+
* why, ms } or { ok: false, error }. rgba is the map as it is written.
|
|
255
|
+
* flipY: stored bottom row first, for a .ktx2 that travels on its own (see
|
|
256
|
+
* the top of this file); measured the same, since what is compared is what
|
|
257
|
+
* is encoded.
|
|
258
|
+
*/
|
|
259
|
+
export async function ktx2For(picture, { role = 'color', alpha = false, flipY = false } = {}) {
|
|
260
|
+
const started = Date.now()
|
|
261
|
+
const rgba = flipY ? flipRows(picture) : picture
|
|
262
|
+
const colour = isColourRole(role)
|
|
263
|
+
const measured = {}
|
|
264
|
+
try {
|
|
265
|
+
if (mayBeEtc1s(role)) {
|
|
266
|
+
const etc1s = await encodeKtx2(rgba, { mode: 'etc1s', srgb: colour })
|
|
267
|
+
const back = await decodeKtx2(etc1s)
|
|
268
|
+
const m = { ...compareRgba(rgba, back, { colour }), bytes: etc1s.byteLength }
|
|
269
|
+
measured.etc1s = m
|
|
270
|
+
if (etc1sHolds(m)) {
|
|
271
|
+
return { ok: true, bytes: etc1s, mode: 'etc1s', width: rgba.width, height: rgba.height, measured, why: `ETC1S${colour ? '' : `, linear, for the ${role} map,`} held up (${describe(m)})`, ms: Date.now() - started, gpu: gpuBytes(rgba.width, rgba.height, 'etc1s', { alpha }) }
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
const uastc = await encodeKtx2(rgba, { mode: 'uastc', srgb: colour, normal: role === 'normal' })
|
|
275
|
+
const back = await decodeKtx2(uastc)
|
|
276
|
+
measured.uastc = { ...compareRgba(rgba, back, { colour }), bytes: uastc.byteLength }
|
|
277
|
+
const why = measured.etc1s
|
|
278
|
+
? `ETC1S did not hold up (${describe(measured.etc1s)}), so UASTC${colour ? '' : ', linear,'} (${describe(measured.uastc)})`
|
|
279
|
+
: `UASTC, linear, for the ${role} map (${describe(measured.uastc)})`
|
|
280
|
+
return { ok: true, bytes: uastc, mode: 'uastc', width: rgba.width, height: rgba.height, measured, why, ms: Date.now() - started, gpu: gpuBytes(rgba.width, rgba.height, 'uastc', { alpha }) }
|
|
281
|
+
} catch (err) {
|
|
282
|
+
return { ok: false, error: String(err?.message ?? err).split('\n')[0], measured }
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** A measurement as a few words. */
|
|
287
|
+
export function describe(m) {
|
|
288
|
+
if (!m) return 'not measured'
|
|
289
|
+
return [`PSNR ${m.psnr} dB`, m.deltaE !== null && m.deltaE !== undefined ? `mean dE2000 ${m.deltaE}` : null, `detail kept ${m.detail}`, m.alphaPsnr !== undefined ? `alpha PSNR ${m.alphaPsnr} dB` : null].filter(Boolean).join(', ')
|
|
290
|
+
}
|