nanoodle 0.13.1 → 0.14.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 +7 -2
- package/package.json +1 -1
- package/src/client.mjs +30 -12
- package/src/gltf-scene.mjs +80 -0
- package/src/graph.mjs +2 -0
- package/src/io.mjs +18 -0
- package/src/local-media.mjs +4 -3
- package/src/nodes.mjs +18 -11
- package/src/sprite-cpu.mjs +89 -0
- package/src/sprite-model.mjs +287 -0
- package/src/sprite-projection.mjs +77 -0
- package/src/sprite-slices.mjs +357 -0
- package/src/sprite-stack.mjs +179 -0
- package/templates/starter-graph.json +1 -1
package/README.md
CHANGED
|
@@ -115,7 +115,7 @@ unrecoverable.
|
|
|
115
115
|
```js
|
|
116
116
|
wf.inputs // [{ key: "Text", nodeId: "n1", field: "text", kind: "textarea", optional: false, def: "..." }]
|
|
117
117
|
wf.outputs // [{ key: "Image", nodeId: "n3", type: "image", ports: [{ name: "image", type: "image" }] }]
|
|
118
|
-
wf.settings // [{ key: "n3.model", kind: "model", def: "nano-banana-2-
|
|
118
|
+
wf.settings // [{ key: "n3.model", kind: "model", def: "nano-banana-2-fast" }, ...]
|
|
119
119
|
```
|
|
120
120
|
|
|
121
121
|
Input keys are flexible (case-insensitive): the node’s custom name (`"Text"`),
|
|
@@ -272,11 +272,16 @@ decisions, joins, retries, and an existing gate.
|
|
|
272
272
|
|
|
273
273
|
| runs | node types |
|
|
274
274
|
|---|---|
|
|
275
|
-
| local | text, upload (image/audio/video), mupload (3D
|
|
275
|
+
| local | text, upload (image/audio/video), mupload (3D asset), choice, join, comment |
|
|
276
|
+
| sprite rendering | slice-3d, sprite-stack-render |
|
|
276
277
|
| local media† | resize, vframes, combine, soundtrack, trim, extractaudio |
|
|
277
278
|
| NanoGPT | llm (incl. vision + audio input), image, edit, inpaint*, vision, decide†, tvideo, ivideo, model3d‡, vedit, lipsync, music, remix, tts, transcribe, cleanvoice§ |
|
|
278
279
|
| custom URL | endpoint (POST to a URL in the graph; no NanoGPT key) |
|
|
279
280
|
|
|
281
|
+
`slice-3d` takes a `model` wire and an optional `paletteImage`. It reads static GLB or self-contained glTF 2 triangle meshes and uncompressed Gaussian PLY splats (ASCII or binary little-endian). Use the original Gaussian PLY for splats. Auto up is Y for meshes and Z for splats. The default is 48 layers of 48 × 48 pixels, 16 colors and an outline. Set `palette` to 0 to preserve source colors.
|
|
282
|
+
|
|
283
|
+
The `sheet` output is a transparent PNG with square tiles ordered from ground at left to top at right. The `layout` text output gives the bounds, axis and sampling dimensions. The `image` output previews the stack at 35° yaw. Wire `sheet` into `sprite-stack-render.sheet` to change yaw, pitch, layer spacing or pixel scale. Enable `turntable` to add a local `video` output with configurable duration, fps, start yaw and end yaw. In the editor and `nanoodle/browser`, Canvas 2D draws the view and MediaRecorder exports MP4 or WebM. Node.js runs the same graph headlessly. Its CPU renderer reads ordinary PNG sheets directly and uses FFmpeg on PATH for additional image codecs and MP4 turntables.
|
|
284
|
+
|
|
280
285
|
† `decide` asks a NanoGPT decision model one typed question (`POST /api/v1/decisions`): pick the best of the wired `img1…` images, choose a label, score on a scale, or yes/no. Text-only decisions need nothing extra; wired images are shrunk to the model's limits with ffmpeg. A yes/no gate that answers no is not a failure: that node settles as `gated`, everything downstream is `skipped` unbilled, and the run succeeds (see [Gates](#gates-decide-said-no)).
|
|
281
286
|
|
|
282
287
|
‡ `model3d` turns an image and/or a prompt into a GLB (`POST /api/generate-video`, then `GET /api/video/status`). The output port is `model`. A blank model id uses `tripo3d/v2.5` (image only). Polling stops at 25 minutes.
|
package/package.json
CHANGED
package/src/client.mjs
CHANGED
|
@@ -25,6 +25,16 @@ export function httpError(status, bodyText, headers) {
|
|
|
25
25
|
return new NanoodleError(status + ": " + String(bodyText).slice(0, 160), { code: "http", status });
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
async function paidResponseError(response, paymentId, stage) {
|
|
29
|
+
const requestId = response.headers?.get?.("x-request-id");
|
|
30
|
+
const reference = /^[a-z0-9][a-z0-9._:-]{0,127}$/i.test(requestId || "") ? requestId : null;
|
|
31
|
+
await response.text().catch(() => {});
|
|
32
|
+
return new NanoodleError(
|
|
33
|
+
`NanoGPT x402 ${stage} failed after payment was sent (HTTP ${response.status}${reference ? `, request ${reference}` : ""}). Check the payment status before paying again.`,
|
|
34
|
+
{ code: "x402", status: response.status, paymentId, stage, requestId: reference },
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
28
38
|
/**
|
|
29
39
|
* Cost extraction (mirrors the app's costFromJson). USD priority:
|
|
30
40
|
* j.cost (>0) → x_nanogpt_pricing.(costUsd|cost|amount) → metadata.cost → j.cost even when 0.
|
|
@@ -142,12 +152,7 @@ export class NanoClient {
|
|
|
142
152
|
if (settled.response) return settled.response; // complete replayed the stored request
|
|
143
153
|
if (signal && signal.aborted) throw abortError(signal.reason);
|
|
144
154
|
const r2 = await this.fetch(url, { ...init, headers: { ...init.headers, "x-x402-payment-id": settled.paymentId } });
|
|
145
|
-
if (r2.
|
|
146
|
-
throw new NanoodleError(
|
|
147
|
-
"payment " + settled.paymentId + " settled, but the API still answered 402 on retry — " +
|
|
148
|
-
"check " + (settled.statusUrl || "the payment status") + " before paying again",
|
|
149
|
-
{ code: "x402", status: 402, paymentId: settled.paymentId });
|
|
150
|
-
}
|
|
155
|
+
if (!r2.ok) throw await paidResponseError(r2, settled.paymentId, "replay");
|
|
151
156
|
return r2;
|
|
152
157
|
}
|
|
153
158
|
|
|
@@ -210,7 +215,7 @@ export class NanoClient {
|
|
|
210
215
|
}
|
|
211
216
|
return { paymentId: invoice.paymentId, statusUrl: invoice.statusUrl };
|
|
212
217
|
}
|
|
213
|
-
if (cr.status !== 402) throw
|
|
218
|
+
if (cr.status !== 402) throw await paidResponseError(cr, invoice.paymentId, "completion");
|
|
214
219
|
await cr.text().catch(() => {}); // drain the "not verified yet" body
|
|
215
220
|
if (Date.now() >= deadline) {
|
|
216
221
|
throw new NanoodleError(
|
|
@@ -311,8 +316,12 @@ export class NanoClient {
|
|
|
311
316
|
return j;
|
|
312
317
|
}
|
|
313
318
|
|
|
314
|
-
/** POST /v1/images/generations (NOTE: not /api/v1). Returns data: / https URL(s).
|
|
315
|
-
|
|
319
|
+
/** POST /v1/images/generations (NOTE: not /api/v1). Returns data: / https URL(s).
|
|
320
|
+
* With multi + items, returns { urls, layers } instead: layers is the per-item
|
|
321
|
+
* `layer` metadata aligned by index with urls (layer-decomposition models return
|
|
322
|
+
* { z_index, name, description, bounding_box } per item), or null when no item
|
|
323
|
+
* carries any. Items with neither b64_json nor url drop out of both arrays. */
|
|
324
|
+
async image({ prompt, model, size, imageDataUrl, maskDataUrl, extra, n = 1, multi = false, items = false }, { onCost, signal } = {}) {
|
|
316
325
|
const body = { model, size: size || "1024x1024", n, response_format: "b64_json" };
|
|
317
326
|
if (prompt) body.prompt = prompt; // omit when blank — upscalers run with no instruction
|
|
318
327
|
if (imageDataUrl) body.imageDataUrl = imageDataUrl; // string OR array (edit multi-reference)
|
|
@@ -321,12 +330,21 @@ export class NanoClient {
|
|
|
321
330
|
const r = await this._postJson("/v1/images/generations", body, signal);
|
|
322
331
|
if (!r.ok) throw httpError(r.status, await r.text(), r.headers);
|
|
323
332
|
const j = await r.json();
|
|
324
|
-
const
|
|
325
|
-
.map((d) =>
|
|
333
|
+
const pairs = (j.data || [])
|
|
334
|
+
.map((d) => {
|
|
335
|
+
const url = d.b64_json ? "data:" + b64ImageMime(d.b64_json) + ";base64," + d.b64_json : d.url;
|
|
336
|
+
return url ? { url, layer: d.layer != null ? d.layer : null } : null;
|
|
337
|
+
})
|
|
326
338
|
.filter(Boolean);
|
|
339
|
+
const urls = pairs.map((p) => p.url);
|
|
327
340
|
if (!urls.length) throw new NanoodleError("no image in response");
|
|
328
341
|
if (onCost) onCost(costWithHeaders(j, r));
|
|
329
|
-
|
|
342
|
+
if (!multi) return urls[0];
|
|
343
|
+
if (items) {
|
|
344
|
+
const layers = pairs.map((p) => p.layer);
|
|
345
|
+
return { urls, layers: layers.some((l) => l != null) ? layers : null };
|
|
346
|
+
}
|
|
347
|
+
return urls;
|
|
330
348
|
}
|
|
331
349
|
|
|
332
350
|
/** POST /api/generate-video then poll GET /api/video/status?requestId= until COMPLETED. */
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// The editor also embeds these helpers in its standalone GLB viewer.
|
|
2
|
+
export function ident() {
|
|
3
|
+
return [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
|
|
4
|
+
}
|
|
5
|
+
export function mul(a, b) {
|
|
6
|
+
var o = new Array(16),
|
|
7
|
+
c,
|
|
8
|
+
r;
|
|
9
|
+
for (c = 0; c < 4; c++)
|
|
10
|
+
for (r = 0; r < 4; r++)
|
|
11
|
+
o[c * 4 + r] =
|
|
12
|
+
a[r] * b[c * 4] + a[4 + r] * b[c * 4 + 1] + a[8 + r] * b[c * 4 + 2] + a[12 + r] * b[c * 4 + 3];
|
|
13
|
+
return o;
|
|
14
|
+
}
|
|
15
|
+
export function trs(t, r, s) {
|
|
16
|
+
var x = r[0],
|
|
17
|
+
y = r[1],
|
|
18
|
+
z = r[2],
|
|
19
|
+
w = r[3];
|
|
20
|
+
var sx = s[0],
|
|
21
|
+
sy = s[1],
|
|
22
|
+
sz = s[2];
|
|
23
|
+
var x2 = x + x,
|
|
24
|
+
y2 = y + y,
|
|
25
|
+
z2 = z + z;
|
|
26
|
+
var xx = x * x2,
|
|
27
|
+
xy = x * y2,
|
|
28
|
+
xz = x * z2,
|
|
29
|
+
yy = y * y2,
|
|
30
|
+
yz = y * z2,
|
|
31
|
+
zz = z * z2;
|
|
32
|
+
var wx = w * x2,
|
|
33
|
+
wy = w * y2,
|
|
34
|
+
wz = w * z2;
|
|
35
|
+
return [
|
|
36
|
+
(1 - (yy + zz)) * sx,
|
|
37
|
+
(xy + wz) * sx,
|
|
38
|
+
(xz - wy) * sx,
|
|
39
|
+
0,
|
|
40
|
+
(xy - wz) * sy,
|
|
41
|
+
(1 - (xx + zz)) * sy,
|
|
42
|
+
(yz + wx) * sy,
|
|
43
|
+
0,
|
|
44
|
+
(xz + wy) * sz,
|
|
45
|
+
(yz - wx) * sz,
|
|
46
|
+
(1 - (xx + yy)) * sz,
|
|
47
|
+
0,
|
|
48
|
+
t[0],
|
|
49
|
+
t[1],
|
|
50
|
+
t[2],
|
|
51
|
+
1,
|
|
52
|
+
];
|
|
53
|
+
}
|
|
54
|
+
export function xform(m, x, y, z, dir) {
|
|
55
|
+
var w = dir ? 0 : 1;
|
|
56
|
+
return [
|
|
57
|
+
m[0] * x + m[4] * y + m[8] * z + m[12] * w,
|
|
58
|
+
m[1] * x + m[5] * y + m[9] * z + m[13] * w,
|
|
59
|
+
m[2] * x + m[6] * y + m[10] * z + m[14] * w,
|
|
60
|
+
];
|
|
61
|
+
}
|
|
62
|
+
export function unpackGlb(bytes) {
|
|
63
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
64
|
+
if (bytes.length < 20 || view.getUint32(4, true) !== 2 || view.getUint32(8, true) !== bytes.length)
|
|
65
|
+
throw new Error("Invalid GLB v2 header or length.");
|
|
66
|
+
let json,
|
|
67
|
+
bin = new Uint8Array(0);
|
|
68
|
+
for (let p = 12; p < bytes.length; ) {
|
|
69
|
+
if (p + 8 > bytes.length) throw new Error("Truncated GLB chunk header.");
|
|
70
|
+
const length = view.getUint32(p, true),
|
|
71
|
+
type = view.getUint32(p + 4, true);
|
|
72
|
+
p += 8;
|
|
73
|
+
if (p + length > bytes.length) throw new Error("Truncated GLB chunk.");
|
|
74
|
+
const chunk = bytes.subarray(p, p + length);
|
|
75
|
+
if (type === 0x4e4f534a) json = JSON.parse(new TextDecoder().decode(chunk));
|
|
76
|
+
if (type === 0x004e4942) bin = chunk;
|
|
77
|
+
p += length;
|
|
78
|
+
}
|
|
79
|
+
return { json, bin };
|
|
80
|
+
}
|
package/src/graph.mjs
CHANGED
|
@@ -40,6 +40,8 @@ export const NODE_TYPES = {
|
|
|
40
40
|
upload: { title: "Image input", inputs: [], outputs: [{ name: "image", type: "image" }], local: true },
|
|
41
41
|
aupload: { title: "Audio input", inputs: [], outputs: [{ name: "audio", type: "audio" }], local: true },
|
|
42
42
|
vupload: { title: "Video input", inputs: [], outputs: [{ name: "video", type: "video" }], local: true },
|
|
43
|
+
"slice-3d": { title: "Slice 3D", inputs: ["model", "paletteImage"], outputs: [{ name: "sheet", type: "image" }, { name: "layout", type: "text" }, { name: "image", type: "image" }], local: true },
|
|
44
|
+
"sprite-stack-render": { title: "Sprite stack render", inputs: ["sheet"], outputs: [{ name: "image", type: "image" }, { name: "video", type: "video" }], local: true },
|
|
43
45
|
mupload: { title: "3D input", inputs: [], outputs: [{ name: "model", type: "model3d" }], local: true },
|
|
44
46
|
choice: { title: "Choice", inputs: [], outputs: [{ name: "text", type: "text" }], local: true },
|
|
45
47
|
join: { title: "Join", inputs: ["a", "b"], outputs: [{ name: "text", type: "text" }], local: true },
|
package/src/io.mjs
CHANGED
|
@@ -284,6 +284,24 @@ export const SETTING_SPECS = {
|
|
|
284
284
|
{ f: "seed", label: "Seed", kind: "number" },
|
|
285
285
|
],
|
|
286
286
|
// local media knobs (play.html SETTING_SPECS) — shape-affecting fields for vframes/combine
|
|
287
|
+
"slice-3d": [
|
|
288
|
+
{ f: "slices", label: "Slices", kind: "number", min: 8, max: 128, def: "48" },
|
|
289
|
+
{ f: "resolution", label: "Tile resolution", kind: "number", min: 16, max: 128, def: "48" },
|
|
290
|
+
{ f: "palette", label: "Palette colors", kind: "number", min: 0, max: 64, def: "16" },
|
|
291
|
+
{ f: "outline", label: "Outline", kind: "boolean", def: true },
|
|
292
|
+
{ f: "upAxis", label: "Up axis", kind: "select", options: ["auto", "x", "y", "z"], def: "auto" },
|
|
293
|
+
],
|
|
294
|
+
"sprite-stack-render": [
|
|
295
|
+
{ f: "yaw", label: "Yaw (degrees)", kind: "number", def: "35" },
|
|
296
|
+
{ f: "pitch", label: "Pitch (degrees)", kind: "number", min: 0, max: 90, def: "30" },
|
|
297
|
+
{ f: "spacing", label: "Layer spacing", kind: "number", min: 0, max: 4, def: "1" },
|
|
298
|
+
{ f: "scale", label: "Pixel scale", kind: "number", min: 1, max: 8, def: "3" },
|
|
299
|
+
{ f: "turntable", label: "Record turntable", kind: "boolean", def: false },
|
|
300
|
+
{ f: "duration", label: "Duration (s)", kind: "number", min: 0.25, max: 30, def: "3" },
|
|
301
|
+
{ f: "fps", label: "Frames per second", kind: "number", min: 1, max: 60, def: "24" },
|
|
302
|
+
{ f: "yawStart", label: "Start yaw", kind: "number", def: "0" },
|
|
303
|
+
{ f: "yawEnd", label: "End yaw", kind: "number", def: "360" },
|
|
304
|
+
],
|
|
287
305
|
resize: [
|
|
288
306
|
{ f: "mode", label: "Mode", kind: "select", options: ["fit", "fill", "exact"], def: "fit" },
|
|
289
307
|
{ f: "width", label: "Width", kind: "number" },
|
package/src/local-media.mjs
CHANGED
|
@@ -218,7 +218,7 @@ function pngChunk(type, data) {
|
|
|
218
218
|
}
|
|
219
219
|
|
|
220
220
|
/** Decode 8-bit RGB/RGBA/gray/gray+alpha PNG → { w, h, rgba:Uint8ClampedArray }. */
|
|
221
|
-
async function decodePng(bytes) {
|
|
221
|
+
async function decodePng(bytes, allowSpriteStrip = false) {
|
|
222
222
|
const u8 = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
223
223
|
if (u8.length < 8 || u8[0] !== 0x89 || u8[1] !== 0x50) {
|
|
224
224
|
throw new NanoodleError("couldn't read that image to resize");
|
|
@@ -251,7 +251,8 @@ async function decodePng(bytes) {
|
|
|
251
251
|
if (!(w > 0) || !(h > 0) || bitDepth !== 8) {
|
|
252
252
|
throw new NanoodleError("couldn't read that image to resize");
|
|
253
253
|
}
|
|
254
|
-
|
|
254
|
+
const maxWidth = allowSpriteStrip && h <= 128 ? 128 * 128 : MAX_IMAGE_DIM;
|
|
255
|
+
if (w > maxWidth || h > MAX_IMAGE_DIM) {
|
|
255
256
|
throw new NanoodleError(
|
|
256
257
|
`image is too large to resize in-process (${w}×${h}; max ${MAX_IMAGE_DIM}px) — use smaller source dimensions`);
|
|
257
258
|
}
|
|
@@ -1095,4 +1096,4 @@ export async function muxSoundtrack(videoUrl, audioUrl, loop = false, { fetch: f
|
|
|
1095
1096
|
export { MAX_FRAMES, MAX_IMAGE_DIM, MP4CAT };
|
|
1096
1097
|
// PNG codec internals — used by the nanoodle repo's dual-engine parity harness
|
|
1097
1098
|
// (pixel-level mask comparison + headless canvas shim). Not part of the stable API.
|
|
1098
|
-
export { decodePng, encodePngRgba };
|
|
1099
|
+
export { decodePng, encodePngRgba, withTemp, runProc };
|
package/src/nodes.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { slice3d, renderSpriteStack } from "./sprite-stack.mjs";
|
|
1
2
|
import { imageModelOpts } from "./image-options.mjs";
|
|
2
3
|
import { NanoodleError } from "./errors.mjs";
|
|
3
4
|
import { catItem, chatModelCan, pricingAdvertisesRefs } from "./catalog.mjs";
|
|
@@ -86,14 +87,20 @@ function guardInputRoles(model, inp) {
|
|
|
86
87
|
}
|
|
87
88
|
|
|
88
89
|
/**
|
|
89
|
-
* edit/inpaint
|
|
90
|
-
*
|
|
91
|
-
*
|
|
90
|
+
* edit/inpaint surface EVERY image a model returns. `image` stays the first — the
|
|
91
|
+
* long-standing single-output contract, unchanged for existing graphs — `images`
|
|
92
|
+
* carries them all in response order (mirroring the image node's variations
|
|
93
|
+
* output), and when response items carry layer metadata (layer-decomposition
|
|
94
|
+
* models), `layers` carries it aligned by index with `images`. Fixed-batch
|
|
95
|
+
* models (midjourney fixed_image_count: 4) surface their variants the same way;
|
|
96
|
+
* nothing a model returns is dropped silently anymore.
|
|
92
97
|
*/
|
|
93
|
-
function
|
|
94
|
-
if (
|
|
95
|
-
|
|
96
|
-
|
|
98
|
+
function multiOut(res) {
|
|
99
|
+
if (typeof res === "string") return { image: res, images: [res] }; // a host-injected ctx.image that ignores multi/items → already one url
|
|
100
|
+
const urls = Array.isArray(res) ? res : res.urls; // …or one that honors multi but not `items` → bare urls
|
|
101
|
+
const out = { image: urls[0], images: urls };
|
|
102
|
+
if (!Array.isArray(res) && res.layers) out.layers = res.layers;
|
|
103
|
+
return out;
|
|
97
104
|
}
|
|
98
105
|
|
|
99
106
|
function promptOf(n, inp, errMsg) {
|
|
@@ -496,6 +503,8 @@ function guardRefsSize(imgs) {
|
|
|
496
503
|
* ctx = { chat, image, video, audio, transcribe, progress } (cost/poll wired by the engine).
|
|
497
504
|
*/
|
|
498
505
|
export const RUNNERS = {
|
|
506
|
+
"slice-3d": slice3d,
|
|
507
|
+
"sprite-stack-render": renderSpriteStack,
|
|
499
508
|
async text(n) { return { text: n.fields.text || "" }; },
|
|
500
509
|
|
|
501
510
|
async upload(n) {
|
|
@@ -693,8 +702,7 @@ export const RUNNERS = {
|
|
|
693
702
|
imgs = await Promise.all(imgs.map((u) => fitImage(u, ctx, "source image")));
|
|
694
703
|
guardRefsSize(imgs);
|
|
695
704
|
const src = imgs.length > 1 ? imgs : imgs[0]; // array → multi-image composite; string → single edit
|
|
696
|
-
|
|
697
|
-
return { image: keptFirst(urls, ctx) };
|
|
705
|
+
return multiOut(await ctx.image({ prompt, model: mdl(n), size: n.fields.size || "1024x1024", imageDataUrl: src, extra: imgExtra(n), multi: true, items: true }));
|
|
698
706
|
},
|
|
699
707
|
|
|
700
708
|
async inpaint(n, inp, ctx) {
|
|
@@ -707,8 +715,7 @@ export const RUNNERS = {
|
|
|
707
715
|
// ctx.maskToSource lets a browser host inject its canvas compositor (handles JPEG/WebP
|
|
708
716
|
// sources the pure-PNG path can't, where ffmpeg isn't an option).
|
|
709
717
|
const mask = await (ctx.maskToSource || maskToSource)(rawMask, source, mediaOpts(ctx));
|
|
710
|
-
|
|
711
|
-
return { image: keptFirst(urls, ctx) };
|
|
718
|
+
return multiOut(await ctx.image({ prompt, model: mdl(n), size: n.fields.size || "1024x1024", imageDataUrl: source, maskDataUrl: mask, extra: imgExtra(n), multi: true, items: true }));
|
|
712
719
|
},
|
|
713
720
|
|
|
714
721
|
async tvideo(n, inp, ctx) {
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { bytesToDataUrl, sniffMime, extForMime } from "./media.mjs";
|
|
2
|
+
import { decodePng, encodePngRgba, runProc, withTemp } from "./local-media.mjs";
|
|
3
|
+
import { softwareFrame, turntablePlan } from "./sprite-projection.mjs";
|
|
4
|
+
|
|
5
|
+
export async function decodeSpriteImage(blob, signal) {
|
|
6
|
+
const bytes = new Uint8Array(await blob.arrayBuffer());
|
|
7
|
+
if (bytes[0] === 137 && bytes[1] === 80) {
|
|
8
|
+
try {
|
|
9
|
+
const { w, h, rgba } = await decodePng(bytes, true);
|
|
10
|
+
return { width: w, height: h, data: rgba };
|
|
11
|
+
} catch (error) {
|
|
12
|
+
if (signal?.aborted) throw error;
|
|
13
|
+
// FFmpeg handles indexed, interlaced and higher-bit-depth PNGs.
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return withTemp(async (dir) => {
|
|
17
|
+
const { writeFile, readFile } = await import("node:fs/promises");
|
|
18
|
+
const input = dir + "/source." + extForMime(sniffMime(bytes)),
|
|
19
|
+
output = dir + "/image.png";
|
|
20
|
+
await writeFile(input, bytes);
|
|
21
|
+
await runProc(
|
|
22
|
+
"ffmpeg",
|
|
23
|
+
[
|
|
24
|
+
"-nostdin",
|
|
25
|
+
"-v",
|
|
26
|
+
"error",
|
|
27
|
+
"-protocol_whitelist",
|
|
28
|
+
"file",
|
|
29
|
+
"-f",
|
|
30
|
+
"image2",
|
|
31
|
+
"-i",
|
|
32
|
+
input,
|
|
33
|
+
"-frames:v",
|
|
34
|
+
"1",
|
|
35
|
+
"-pix_fmt",
|
|
36
|
+
"rgba",
|
|
37
|
+
"-c:v",
|
|
38
|
+
"png",
|
|
39
|
+
output,
|
|
40
|
+
],
|
|
41
|
+
{ signal },
|
|
42
|
+
);
|
|
43
|
+
const { w, h, rgba } = await decodePng(new Uint8Array(await readFile(output)), true);
|
|
44
|
+
return { width: w, height: h, data: rgba };
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function frameUrl(frame) {
|
|
49
|
+
return bytesToDataUrl(await encodePngRgba(frame.width, frame.height, frame.data), "image/png");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function encodeTurntable(sheet, fields, signal) {
|
|
53
|
+
const { duration, from, to, count } = turntablePlan(fields);
|
|
54
|
+
return withTemp(async (dir) => {
|
|
55
|
+
const { writeFile, readFile } = await import("node:fs/promises");
|
|
56
|
+
for (let i = 0; i < count; i++) {
|
|
57
|
+
signal?.throwIfAborted();
|
|
58
|
+
const frame = softwareFrame(sheet, fields, from + ((to - from) * i) / (count - 1));
|
|
59
|
+
const png = await encodePngRgba(frame.width, frame.height, frame.data);
|
|
60
|
+
await writeFile(dir + "/" + String(i).padStart(5, "0") + ".png", png);
|
|
61
|
+
}
|
|
62
|
+
const args = [
|
|
63
|
+
"-nostdin",
|
|
64
|
+
"-v",
|
|
65
|
+
"error",
|
|
66
|
+
"-framerate",
|
|
67
|
+
String(count / duration),
|
|
68
|
+
"-i",
|
|
69
|
+
dir + "/%05d.png",
|
|
70
|
+
"-an",
|
|
71
|
+
];
|
|
72
|
+
const out = dir + "/turntable.mp4";
|
|
73
|
+
try {
|
|
74
|
+
await runProc(
|
|
75
|
+
"ffmpeg",
|
|
76
|
+
[...args, "-c:v", "libx264", "-pix_fmt", "yuv420p", "-movflags", "+faststart", out],
|
|
77
|
+
{ signal },
|
|
78
|
+
);
|
|
79
|
+
} catch (error) {
|
|
80
|
+
if (!/unknown encoder.*libx264|encoder.*libx264.*not found/i.test(error.message)) throw error;
|
|
81
|
+
await runProc(
|
|
82
|
+
"ffmpeg",
|
|
83
|
+
[...args, "-y", "-c:v", "mpeg4", "-q:v", "3", "-pix_fmt", "yuv420p", "-movflags", "+faststart", out],
|
|
84
|
+
{ signal },
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
return bytesToDataUrl(new Uint8Array(await readFile(out)), "video/mp4");
|
|
88
|
+
});
|
|
89
|
+
}
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { dataUrlBytes } from "./media.mjs";
|
|
2
|
+
|
|
3
|
+
import { ident, mul, trs, xform, unpackGlb } from "./gltf-scene.mjs";
|
|
4
|
+
|
|
5
|
+
const decodeText = (bytes) => new TextDecoder().decode(bytes);
|
|
6
|
+
const components = { SCALAR: 1, VEC2: 2, VEC3: 3, VEC4: 4 };
|
|
7
|
+
const glTypes = {
|
|
8
|
+
5120: [1, "getInt8", 127],
|
|
9
|
+
5121: [1, "getUint8", 255],
|
|
10
|
+
5122: [2, "getInt16", 32767],
|
|
11
|
+
5123: [2, "getUint16", 65535],
|
|
12
|
+
5125: [4, "getUint32", 4294967295],
|
|
13
|
+
5126: [4, "getFloat32", 1],
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function unpack(bytes) {
|
|
17
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
18
|
+
if (bytes.length < 4 || view.getUint32(0, true) !== 0x46546c67) {
|
|
19
|
+
try {
|
|
20
|
+
return { json: JSON.parse(decodeText(bytes)) };
|
|
21
|
+
} catch {
|
|
22
|
+
throw new Error("Expected a GLB, a self-contained glTF 2 mesh, or a Gaussian PLY.");
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return unpackGlb(bytes);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function embedded(uri) {
|
|
29
|
+
if (!uri?.startsWith("data:"))
|
|
30
|
+
throw new Error("External glTF buffers and images require a self-contained .gltf or .glb export.");
|
|
31
|
+
return dataUrlBytes(uri);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function triangleModel(bytes, imagePixels) {
|
|
35
|
+
const { json, bin } = unpack(bytes);
|
|
36
|
+
if (json?.asset?.version !== "2.0") throw new Error("Expected glTF 2.0.");
|
|
37
|
+
if (json.extensionsRequired?.length)
|
|
38
|
+
throw new Error("Unsupported required glTF extensions: " + json.extensionsRequired.join(", "));
|
|
39
|
+
const buffers = (json.buffers || []).map((b, i) => {
|
|
40
|
+
const data = b.uri ? embedded(b.uri).bytes : i === 0 && bin;
|
|
41
|
+
if (!data || data.length < b.byteLength) throw new Error("Missing or truncated glTF buffer.");
|
|
42
|
+
return data;
|
|
43
|
+
});
|
|
44
|
+
function bufferView(index) {
|
|
45
|
+
const bv = json.bufferViews?.[index],
|
|
46
|
+
buffer = buffers[bv?.buffer],
|
|
47
|
+
start = bv?.byteOffset ?? 0;
|
|
48
|
+
if (
|
|
49
|
+
!buffer ||
|
|
50
|
+
!Number.isSafeInteger(bv.byteLength) ||
|
|
51
|
+
start < 0 ||
|
|
52
|
+
bv.byteLength < 0 ||
|
|
53
|
+
start + bv.byteLength > buffer.length
|
|
54
|
+
)
|
|
55
|
+
throw new Error("Invalid glTF buffer view.");
|
|
56
|
+
if (bv.extensions?.EXT_meshopt_compression)
|
|
57
|
+
throw new Error("Meshopt-compressed buffers are unsupported.");
|
|
58
|
+
return buffer.subarray(start, start + bv.byteLength);
|
|
59
|
+
}
|
|
60
|
+
function accessor(index, shapes, count) {
|
|
61
|
+
const a = json.accessors?.[index],
|
|
62
|
+
spec = glTypes[a?.componentType];
|
|
63
|
+
if (!a || !spec || !shapes.includes(a.type) || a.sparse || a.bufferView == null)
|
|
64
|
+
throw new Error("Unsupported glTF accessor type or sparse encoding.");
|
|
65
|
+
const n = components[a.type],
|
|
66
|
+
[size, get, max] = spec,
|
|
67
|
+
buf = bufferView(a.bufferView);
|
|
68
|
+
const stride = json.bufferViews[a.bufferView].byteStride ?? n * size,
|
|
69
|
+
offset = a.byteOffset ?? 0;
|
|
70
|
+
if (
|
|
71
|
+
!Number.isSafeInteger(a.count) ||
|
|
72
|
+
a.count < 1 ||
|
|
73
|
+
(count != null && a.count !== count) ||
|
|
74
|
+
offset < 0 ||
|
|
75
|
+
stride < n * size ||
|
|
76
|
+
offset + (a.count - 1) * stride + n * size > buf.length
|
|
77
|
+
)
|
|
78
|
+
throw new Error("Invalid glTF accessor bounds or count.");
|
|
79
|
+
const view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength),
|
|
80
|
+
data = new Float64Array(n * a.count);
|
|
81
|
+
for (let i = 0; i < a.count; i++)
|
|
82
|
+
for (let c = 0; c < n; c++) {
|
|
83
|
+
const v = view[get](offset + i * stride + c * size, true);
|
|
84
|
+
if (!Number.isFinite(v)) throw new Error("Non-finite glTF attribute.");
|
|
85
|
+
data[i * n + c] = a.normalized ? Math.max(-1, v / max) : v;
|
|
86
|
+
}
|
|
87
|
+
return { data, n, count: a.count };
|
|
88
|
+
}
|
|
89
|
+
const textures = new Map();
|
|
90
|
+
async function texture(info) {
|
|
91
|
+
const tex = json.textures?.[info.index],
|
|
92
|
+
img = json.images?.[tex?.source];
|
|
93
|
+
if (!img || info.extensions?.KHR_texture_transform)
|
|
94
|
+
throw new Error("Unsupported base-color texture encoding or transform.");
|
|
95
|
+
if (!textures.has(tex.source)) {
|
|
96
|
+
const image = img.uri ? embedded(img.uri) : { bytes: bufferView(img.bufferView), mime: img.mimeType };
|
|
97
|
+
textures.set(tex.source, await imagePixels(image.bytes, image.mime));
|
|
98
|
+
}
|
|
99
|
+
return { pixels: textures.get(tex.source), sampler: json.samplers?.[tex.sampler] || {} };
|
|
100
|
+
}
|
|
101
|
+
const meshes = [];
|
|
102
|
+
async function emit(index, world) {
|
|
103
|
+
if (!json.meshes?.[index]) throw new Error("Missing glTF mesh.");
|
|
104
|
+
for (const p of json.meshes[index].primitives) {
|
|
105
|
+
const mode = p.mode ?? 4;
|
|
106
|
+
if (![4, 5, 6].includes(mode))
|
|
107
|
+
throw new Error(
|
|
108
|
+
"Slice 3D requires triangles. For a point-cloud preview, supply the original Gaussian PLY.",
|
|
109
|
+
);
|
|
110
|
+
if (p.targets?.length || p.extensions?.KHR_draco_mesh_compression)
|
|
111
|
+
throw new Error("Morph targets and Draco-compressed meshes are unsupported.");
|
|
112
|
+
const pos = accessor(p.attributes.POSITION, ["VEC3"]);
|
|
113
|
+
for (let i = 0; i < pos.data.length; i += 3) {
|
|
114
|
+
const point = xform(world, pos.data[i], pos.data[i + 1], pos.data[i + 2], false);
|
|
115
|
+
if (!point.every(Number.isFinite)) throw new Error("Invalid glTF scene transform.");
|
|
116
|
+
pos.data.set(point, i);
|
|
117
|
+
}
|
|
118
|
+
if (p.indices != null) {
|
|
119
|
+
const indexSpec = json.accessors?.[p.indices];
|
|
120
|
+
if (!indexSpec || ![5121, 5123, 5125].includes(indexSpec.componentType) || indexSpec.normalized)
|
|
121
|
+
throw new Error("glTF triangle indices must be unsigned integers.");
|
|
122
|
+
}
|
|
123
|
+
const idx = p.indices != null ? accessor(p.indices, ["SCALAR"]).data : null;
|
|
124
|
+
if (idx?.some((v) => !Number.isInteger(v) || v < 0 || v >= pos.count))
|
|
125
|
+
throw new Error("Invalid triangle index.");
|
|
126
|
+
const length = idx?.length ?? pos.count;
|
|
127
|
+
if (length < 3 || (mode === 4 && length % 3)) throw new Error("Incomplete triangle primitive.");
|
|
128
|
+
const mat = json.materials?.[p.material] || {},
|
|
129
|
+
pbr = mat.pbrMetallicRoughness || {},
|
|
130
|
+
info = pbr.baseColorTexture;
|
|
131
|
+
const factor = pbr.baseColorFactor ?? [1, 1, 1, 1];
|
|
132
|
+
if (
|
|
133
|
+
!Array.isArray(factor) ||
|
|
134
|
+
factor.length !== 4 ||
|
|
135
|
+
!factor.every((v) => Number.isFinite(v) && v >= 0 && v <= 1)
|
|
136
|
+
)
|
|
137
|
+
throw new Error("Invalid glTF base color factor.");
|
|
138
|
+
if (mat.alphaMode != null && !["OPAQUE", "MASK", "BLEND"].includes(mat.alphaMode))
|
|
139
|
+
throw new Error("Invalid glTF alpha mode.");
|
|
140
|
+
const color =
|
|
141
|
+
p.attributes.COLOR_0 == null ? null : accessor(p.attributes.COLOR_0, ["VEC3", "VEC4"], pos.count);
|
|
142
|
+
const uv = info
|
|
143
|
+
? accessor(p.attributes["TEXCOORD_" + (info.texCoord ?? 0)], ["VEC2"], pos.count)
|
|
144
|
+
: null;
|
|
145
|
+
meshes.push({
|
|
146
|
+
positions: pos.data,
|
|
147
|
+
idx,
|
|
148
|
+
mode,
|
|
149
|
+
length,
|
|
150
|
+
color,
|
|
151
|
+
uv,
|
|
152
|
+
texture: info && (await texture(info)),
|
|
153
|
+
factor,
|
|
154
|
+
alphaMode: mat.alphaMode || "OPAQUE",
|
|
155
|
+
cutoff: mat.alphaCutoff ?? 0.5,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const active = new Set(),
|
|
160
|
+
nodes = json.nodes || [];
|
|
161
|
+
async function walk(i, parent) {
|
|
162
|
+
const node = nodes[i];
|
|
163
|
+
if (!node || active.has(i)) throw new Error("Invalid or cyclic glTF node tree.");
|
|
164
|
+
if (node.skin != null) throw new Error("Skinned meshes require a static mesh export.");
|
|
165
|
+
active.add(i);
|
|
166
|
+
const world = mul(
|
|
167
|
+
parent,
|
|
168
|
+
node.matrix ||
|
|
169
|
+
trs(node.translation || [0, 0, 0], node.rotation || [0, 0, 0, 1], node.scale || [1, 1, 1]),
|
|
170
|
+
);
|
|
171
|
+
if (node.mesh != null) await emit(node.mesh, world);
|
|
172
|
+
for (const child of node.children || []) await walk(child, world);
|
|
173
|
+
active.delete(i);
|
|
174
|
+
}
|
|
175
|
+
if (nodes.length) {
|
|
176
|
+
const children = new Set(nodes.flatMap((n) => n.children || []));
|
|
177
|
+
const roots =
|
|
178
|
+
json.scenes?.[json.scene ?? 0]?.nodes ?? nodes.map((_, i) => i).filter((i) => !children.has(i));
|
|
179
|
+
for (const root of roots) await walk(root, ident());
|
|
180
|
+
} else for (let i = 0; i < (json.meshes || []).length; i++) await emit(i, ident());
|
|
181
|
+
if (!meshes.length) throw new Error("No triangles in the selected glTF scene.");
|
|
182
|
+
return { kind: "mesh", meshes };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const plyTypes = {
|
|
186
|
+
char: glTypes[5120],
|
|
187
|
+
int8: glTypes[5120],
|
|
188
|
+
uchar: glTypes[5121],
|
|
189
|
+
uint8: glTypes[5121],
|
|
190
|
+
short: glTypes[5122],
|
|
191
|
+
int16: glTypes[5122],
|
|
192
|
+
ushort: glTypes[5123],
|
|
193
|
+
uint16: glTypes[5123],
|
|
194
|
+
int: [4, "getInt32"],
|
|
195
|
+
int32: [4, "getInt32"],
|
|
196
|
+
uint: glTypes[5125],
|
|
197
|
+
uint32: glTypes[5125],
|
|
198
|
+
float: glTypes[5126],
|
|
199
|
+
float32: glTypes[5126],
|
|
200
|
+
double: [8, "getFloat64"],
|
|
201
|
+
float64: [8, "getFloat64"],
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
export function gaussianModel(bytes) {
|
|
205
|
+
const head = decodeText(bytes.subarray(0, 65536)),
|
|
206
|
+
end = /^end_header\r?\n/m.exec(head);
|
|
207
|
+
if (!end) throw new Error("Missing PLY header terminator.");
|
|
208
|
+
const header = head.slice(0, end.index + end[0].length),
|
|
209
|
+
offset = new TextEncoder().encode(header).length;
|
|
210
|
+
let format,
|
|
211
|
+
count,
|
|
212
|
+
stride = 0,
|
|
213
|
+
element;
|
|
214
|
+
const props = new Map();
|
|
215
|
+
for (const line of header.trim().split(/\r?\n/)) {
|
|
216
|
+
const [key, a, b, version] = line.trim().split(/\s+/);
|
|
217
|
+
if (key === "format") {
|
|
218
|
+
if (b !== "1.0" || !["ascii", "binary_little_endian"].includes(a))
|
|
219
|
+
throw new Error("Supported Gaussian PLY formats are ASCII and binary little-endian 1.0.");
|
|
220
|
+
format = a;
|
|
221
|
+
}
|
|
222
|
+
if (key === "element") {
|
|
223
|
+
element = a;
|
|
224
|
+
if (a !== "vertex" && Number(b) !== 0)
|
|
225
|
+
throw new Error("Expected an uncompressed vertex-only Gaussian PLY.");
|
|
226
|
+
if (a === "vertex") count = Number(b);
|
|
227
|
+
}
|
|
228
|
+
if (key === "property" && element === "vertex") {
|
|
229
|
+
if (!plyTypes[a] || version || props.has(b))
|
|
230
|
+
throw new Error("Unsupported Gaussian PLY property encoding.");
|
|
231
|
+
props.set(b, { type: plyTypes[a], offset: stride, index: props.size });
|
|
232
|
+
stride += plyTypes[a][0];
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const required = [
|
|
236
|
+
"x",
|
|
237
|
+
"y",
|
|
238
|
+
"z",
|
|
239
|
+
"opacity",
|
|
240
|
+
"scale_0",
|
|
241
|
+
"scale_1",
|
|
242
|
+
"scale_2",
|
|
243
|
+
"rot_0",
|
|
244
|
+
"rot_1",
|
|
245
|
+
"rot_2",
|
|
246
|
+
"rot_3",
|
|
247
|
+
];
|
|
248
|
+
const dc = ["f_dc_0", "f_dc_1", "f_dc_2"],
|
|
249
|
+
rgb = ["red", "green", "blue"];
|
|
250
|
+
const hasDC = dc.every((p) => props.has(p));
|
|
251
|
+
if (!format || !required.every((p) => props.has(p)) || !(hasDC || rgb.every((p) => props.has(p))))
|
|
252
|
+
throw new Error(
|
|
253
|
+
"Expected Gaussian PLY positions, log scales, quaternion, opacity logit, and DC or RGB colors. Ordinary point clouds are unsupported.",
|
|
254
|
+
);
|
|
255
|
+
if (!hasDC && rgb.some((name) => props.get(name).type !== glTypes[5121]))
|
|
256
|
+
throw new Error(
|
|
257
|
+
"Gaussian RGB properties must be unsigned bytes, or supply floating-point DC coefficients.",
|
|
258
|
+
);
|
|
259
|
+
if (!Number.isSafeInteger(count) || count < 1) throw new Error("Invalid Gaussian PLY vertex count.");
|
|
260
|
+
const rows = format === "ascii" ? decodeText(bytes.subarray(offset)).trim().split(/\r?\n/) : null;
|
|
261
|
+
if (rows ? rows.length !== count : count * stride !== bytes.length - offset)
|
|
262
|
+
throw new Error("Gaussian PLY payload does not match its declared vertex count.");
|
|
263
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength),
|
|
264
|
+
splats = [];
|
|
265
|
+
for (let i = 0; i < count; i++) {
|
|
266
|
+
const row = rows && rows[i].trim().split(/\s+/);
|
|
267
|
+
if (row && row.length !== props.size) throw new Error("Invalid ASCII PLY vertex.");
|
|
268
|
+
const read = (key) => {
|
|
269
|
+
const p = props.get(key);
|
|
270
|
+
const v = row ? Number(row[p.index]) : view[p.type[1]](offset + i * stride + p.offset, true);
|
|
271
|
+
if (!Number.isFinite(v)) throw new Error("Non-finite Gaussian PLY attribute.");
|
|
272
|
+
return v;
|
|
273
|
+
};
|
|
274
|
+
const center = ["x", "y", "z"].map(read),
|
|
275
|
+
scales = ["scale_0", "scale_1", "scale_2"].map((p) => Math.exp(read(p)));
|
|
276
|
+
const quaternion = ["rot_0", "rot_1", "rot_2", "rot_3"].map(read),
|
|
277
|
+
norm = Math.hypot(...quaternion);
|
|
278
|
+
if (!norm || !scales.every((s) => Number.isFinite(s) && s > 0))
|
|
279
|
+
throw new Error("Invalid Gaussian scale or rotation.");
|
|
280
|
+
const [w, x, y, z] = quaternion.map((v) => v / norm);
|
|
281
|
+
const rotation = trs([0, 0, 0], [x, y, z, w], [1, 1, 1]);
|
|
282
|
+
const extent = center.map((_, k) => 3 * Math.hypot(...scales.map((s, j) => rotation[j * 4 + k] * s)));
|
|
283
|
+
const color = hasDC ? dc.map((p) => 255 * (0.5 + 0.2820947918 * read(p))) : rgb.map(read);
|
|
284
|
+
splats.push({ center, scales, rotation, extent, color, alpha: 1 / (1 + Math.exp(-read("opacity"))) });
|
|
285
|
+
}
|
|
286
|
+
return { kind: "splat", splats };
|
|
287
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export function number(fields, key, fallback, min, max, integer = false) {
|
|
2
|
+
const value = fields[key] == null || fields[key] === "" ? fallback : Number(fields[key]);
|
|
3
|
+
if (!Number.isFinite(value) || value < min || value > max || (integer && !Number.isInteger(value)))
|
|
4
|
+
throw new Error(
|
|
5
|
+
key + " must be " + (integer ? "an integer " : "") + "between " + min + " and " + max + ".",
|
|
6
|
+
);
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function viewPlan(sheet, fields) {
|
|
11
|
+
const tile = sheet.height,
|
|
12
|
+
layers = sheet.width / tile;
|
|
13
|
+
if (!Number.isInteger(layers) || layers < 8 || layers > 128 || tile < 16 || tile > 128)
|
|
14
|
+
throw new Error("Expected a horizontal Slice 3D sheet with 8–128 square tiles of 16–128 pixels.");
|
|
15
|
+
const pitch = (number(fields, "pitch", 30, 0, 90) * Math.PI) / 180,
|
|
16
|
+
scale = number(fields, "scale", 3, 1, 8);
|
|
17
|
+
const spacing = (number(fields, "spacing", 1, 0, 4) * (tile - 2)) / layers;
|
|
18
|
+
const rise = spacing * Math.cos(pitch),
|
|
19
|
+
squash = Math.max(1 / tile, Math.sin(pitch));
|
|
20
|
+
// Fixed even bounds for every yaw, including both turntable endpoints.
|
|
21
|
+
const floor = tile * Math.SQRT2;
|
|
22
|
+
const w = 2 * Math.ceil((floor * scale + 4) / 2),
|
|
23
|
+
h = 2 * Math.ceil(((floor * squash + rise * (layers - 1)) * scale + 4) / 2);
|
|
24
|
+
return { tile, layers, scale, rise, squash, w, h };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Invert each layer's orthographic transform at output pixel centers. This is
|
|
28
|
+
// nearest-neighbor sampling and source-over alpha, matching the editor canvas.
|
|
29
|
+
export function softwareFrame(sheet, fields, yaw) {
|
|
30
|
+
const { tile, layers, scale, rise, squash, w, h } = viewPlan(sheet, fields);
|
|
31
|
+
const data = new Uint8ClampedArray(w * h * 4);
|
|
32
|
+
const angle = (yaw * Math.PI) / 180,
|
|
33
|
+
c = Math.cos(angle),
|
|
34
|
+
s = Math.sin(angle);
|
|
35
|
+
const radius = (tile * scale * (Math.abs(c) + Math.abs(s))) / 2;
|
|
36
|
+
const left = Math.max(0, Math.floor(w / 2 - radius)),
|
|
37
|
+
right = Math.min(w, Math.ceil(w / 2 + radius));
|
|
38
|
+
for (let l = 0; l < layers; l++) {
|
|
39
|
+
const cy = h / 2 + ((layers - 1) / 2 - l) * rise * scale;
|
|
40
|
+
const top = Math.max(0, Math.floor(cy - radius * squash)),
|
|
41
|
+
bottom = Math.min(h, Math.ceil(cy + radius * squash));
|
|
42
|
+
for (let y = top; y < bottom; y++) {
|
|
43
|
+
const dy = (y + 0.5 - cy) / (scale * squash);
|
|
44
|
+
for (let x = left; x < right; x++) {
|
|
45
|
+
const dx = (x + 0.5 - w / 2) / scale;
|
|
46
|
+
const u = Math.floor(c * dx + s * dy + tile / 2),
|
|
47
|
+
v = Math.floor(-s * dx + c * dy + tile / 2);
|
|
48
|
+
if (u < 0 || v < 0 || u >= tile || v >= tile) continue;
|
|
49
|
+
const src = (v * sheet.width + l * tile + u) * 4,
|
|
50
|
+
dst = (y * w + x) * 4,
|
|
51
|
+
alpha = sheet.data[src + 3];
|
|
52
|
+
if (!alpha) continue;
|
|
53
|
+
if (alpha === 255 || !data[dst + 3]) {
|
|
54
|
+
data.set(sheet.data.subarray(src, src + 4), dst);
|
|
55
|
+
} else {
|
|
56
|
+
const under = data[dst + 3] * (1 - alpha / 255),
|
|
57
|
+
a = alpha + under;
|
|
58
|
+
for (let k = 0; k < 3; k++)
|
|
59
|
+
data[dst + k] = (sheet.data[src + k] * alpha + data[dst + k] * under) / a;
|
|
60
|
+
data[dst + 3] = a;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return { width: w, height: h, data };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function flag(value, fallback) {
|
|
69
|
+
return value == null ? fallback : value === true || value === "true";
|
|
70
|
+
}
|
|
71
|
+
export function turntablePlan(fields) {
|
|
72
|
+
const fps = number(fields, "fps", 24, 1, 60, true),
|
|
73
|
+
duration = number(fields, "duration", 3, 0.25, 30);
|
|
74
|
+
const from = number(fields, "yawStart", 0, -3600, 3600),
|
|
75
|
+
to = number(fields, "yawEnd", 360, -3600, 3600);
|
|
76
|
+
return { fps, duration, from, to, count: Math.max(2, Math.round(fps * duration)) };
|
|
77
|
+
}
|
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
// Grid coordinates are (column, row, layer). Each square tile has the same
|
|
2
|
+
// world-to-pixel scale in both directions; layer thickness is recorded separately.
|
|
3
|
+
function gridFor(model, axis, size, layers) {
|
|
4
|
+
const axes = { x: [1, 2, 0, -1], y: [0, 2, 1, 1], z: [0, 1, 2, -1] }[axis];
|
|
5
|
+
if (!axes) throw new Error("Up axis must be auto, x, y or z.");
|
|
6
|
+
const [u, v, h, sign] = axes,
|
|
7
|
+
min = [Infinity, Infinity, Infinity],
|
|
8
|
+
max = min.map(() => -Infinity);
|
|
9
|
+
function grow(x, y, z) {
|
|
10
|
+
min[0] = Math.min(min[0], x);
|
|
11
|
+
min[1] = Math.min(min[1], y);
|
|
12
|
+
min[2] = Math.min(min[2], z);
|
|
13
|
+
max[0] = Math.max(max[0], x);
|
|
14
|
+
max[1] = Math.max(max[1], y);
|
|
15
|
+
max[2] = Math.max(max[2], z);
|
|
16
|
+
}
|
|
17
|
+
for (const m of model.meshes || []) {
|
|
18
|
+
const p = m.positions;
|
|
19
|
+
for (let i = 0; i < p.length; i += 3) grow(p[i], p[i + 1], p[i + 2]);
|
|
20
|
+
}
|
|
21
|
+
for (const s of model.splats || []) {
|
|
22
|
+
const [x, y, z] = s.center,
|
|
23
|
+
[a, b, c] = s.extent;
|
|
24
|
+
grow(x - a, y - b, z - c);
|
|
25
|
+
grow(x + a, y + b, z + c);
|
|
26
|
+
}
|
|
27
|
+
const span = Math.max(...min.map((n, i) => max[i] - n));
|
|
28
|
+
if (!(span > 0) || !Number.isFinite(span)) throw new Error("Model bounds have no usable extent.");
|
|
29
|
+
const unit = span / (size - 2),
|
|
30
|
+
step = span / layers,
|
|
31
|
+
cu = (max[u] + min[u]) / 2,
|
|
32
|
+
cv = (max[v] + min[v]) / 2;
|
|
33
|
+
return {
|
|
34
|
+
size,
|
|
35
|
+
layers,
|
|
36
|
+
unit,
|
|
37
|
+
step,
|
|
38
|
+
axes,
|
|
39
|
+
min,
|
|
40
|
+
max,
|
|
41
|
+
project(p) {
|
|
42
|
+
return [(p[u] - cu) / unit + size / 2, (sign * (p[v] - cv)) / unit + size / 2, (p[h] - min[h]) / step];
|
|
43
|
+
},
|
|
44
|
+
world(x, y, l) {
|
|
45
|
+
const p = [];
|
|
46
|
+
p[u] = (x - size / 2) * unit + cu;
|
|
47
|
+
p[v] = sign * (y - size / 2) * unit + cv;
|
|
48
|
+
p[h] = min[h] + l * step;
|
|
49
|
+
return p;
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function wrap(t, mode) {
|
|
55
|
+
if (mode === 33071) return Math.min(1, Math.max(0, t));
|
|
56
|
+
if (mode === 33648) return 1 - Math.abs((((t % 2) + 2) % 2) - 1);
|
|
57
|
+
return t - Math.floor(t);
|
|
58
|
+
}
|
|
59
|
+
function srgb(v) {
|
|
60
|
+
return v < 0.0031308 ? 12.92 * v : 1.055 * Math.pow(v, 1 / 2.4) - 0.055;
|
|
61
|
+
}
|
|
62
|
+
function linear(v) {
|
|
63
|
+
return v < 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function sample(mesh, ids, weights) {
|
|
67
|
+
const color = mesh.factor.slice();
|
|
68
|
+
function interpolate(attr, channel) {
|
|
69
|
+
return weights.reduce((sum, w, j) => sum + w * attr.data[ids[j] * attr.n + channel], 0);
|
|
70
|
+
}
|
|
71
|
+
if (mesh.color) for (let k = 0; k < mesh.color.n; k++) color[k] *= interpolate(mesh.color, k);
|
|
72
|
+
if (mesh.texture) {
|
|
73
|
+
const { pixels, sampler } = mesh.texture;
|
|
74
|
+
const x = Math.min(
|
|
75
|
+
pixels.width - 1,
|
|
76
|
+
Math.floor(wrap(interpolate(mesh.uv, 0), sampler.wrapS) * pixels.width),
|
|
77
|
+
);
|
|
78
|
+
const y = Math.min(
|
|
79
|
+
pixels.height - 1,
|
|
80
|
+
Math.floor(wrap(interpolate(mesh.uv, 1), sampler.wrapT) * pixels.height),
|
|
81
|
+
);
|
|
82
|
+
const p = (y * pixels.width + x) * 4;
|
|
83
|
+
for (let k = 0; k < 3; k++) color[k] *= linear(pixels.data[p + k] / 255);
|
|
84
|
+
color[3] *= pixels.data[p + 3] / 255;
|
|
85
|
+
}
|
|
86
|
+
const alpha =
|
|
87
|
+
mesh.alphaMode === "OPAQUE" ? 1 : mesh.alphaMode === "MASK" ? Number(color[3] >= mesh.cutoff) : color[3];
|
|
88
|
+
return [srgb(color[0]) * 255, srgb(color[1]) * 255, srgb(color[2]) * 255, alpha * 255];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Scan a triangle in an axis-aligned projection at cell centers. The dominant
|
|
92
|
+
// projection catches vertical walls and flat caps. A floor projection also
|
|
93
|
+
// supplies signed ray intersections for filling closed interiors.
|
|
94
|
+
function scanTriangle(verts, a, b, width, height, visit) {
|
|
95
|
+
const [p, q, r] = verts;
|
|
96
|
+
const dx = q[a] - p[a],
|
|
97
|
+
dy = q[b] - p[b],
|
|
98
|
+
ex = r[a] - p[a],
|
|
99
|
+
ey = r[b] - p[b];
|
|
100
|
+
const det = dx * ey - dy * ex;
|
|
101
|
+
if (Math.abs(det) < 1e-12) return;
|
|
102
|
+
const startX = Math.max(0, Math.ceil(Math.min(p[a], q[a], r[a]) - 0.5));
|
|
103
|
+
const startY = Math.max(0, Math.ceil(Math.min(p[b], q[b], r[b]) - 0.5));
|
|
104
|
+
const endX = Math.min(width - 1, Math.floor(Math.max(p[a], q[a], r[a]) - 0.5));
|
|
105
|
+
const endY = Math.min(height - 1, Math.floor(Math.max(p[b], q[b], r[b]) - 0.5));
|
|
106
|
+
for (let y = startY; y <= endY; y++)
|
|
107
|
+
for (let x = startX; x <= endX; x++) {
|
|
108
|
+
const px = x + 0.5 - p[a],
|
|
109
|
+
py = y + 0.5 - p[b];
|
|
110
|
+
const v = (px * ey - py * ex) / det,
|
|
111
|
+
w = (dx * py - dy * px) / det,
|
|
112
|
+
u = 1 - v - w;
|
|
113
|
+
if (Math.min(u, v, w) >= -1e-8) visit(x, y, [u, v, w], Math.sign(det));
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function paintMesh(grid, model, put) {
|
|
118
|
+
const { size, layers } = grid,
|
|
119
|
+
dims = [size, size, layers],
|
|
120
|
+
rays = new Array(size * size);
|
|
121
|
+
for (const mesh of model.meshes) {
|
|
122
|
+
const points = [];
|
|
123
|
+
for (let i = 0; i < mesh.positions.length; i += 3)
|
|
124
|
+
points.push(grid.project(mesh.positions.subarray(i, i + 3)));
|
|
125
|
+
for (let i = 0; i + 2 < mesh.length; i += mesh.mode === 4 ? 3 : 1) {
|
|
126
|
+
const corners =
|
|
127
|
+
mesh.mode === 6
|
|
128
|
+
? [0, i + 1, i + 2]
|
|
129
|
+
: mesh.mode === 5 && i % 2
|
|
130
|
+
? [i + 1, i, i + 2]
|
|
131
|
+
: [i, i + 1, i + 2];
|
|
132
|
+
const ids = mesh.idx ? corners.map((k) => mesh.idx[k]) : corners,
|
|
133
|
+
verts = ids.map((k) => points[k]);
|
|
134
|
+
const [p, q, r] = verts,
|
|
135
|
+
planes = [
|
|
136
|
+
[0, 1, 2],
|
|
137
|
+
[1, 2, 0],
|
|
138
|
+
[2, 0, 1],
|
|
139
|
+
];
|
|
140
|
+
const areas = planes.map(([a, b]) =>
|
|
141
|
+
Math.abs((q[a] - p[a]) * (r[b] - p[b]) - (q[b] - p[b]) * (r[a] - p[a])),
|
|
142
|
+
);
|
|
143
|
+
const dominant = areas.indexOf(Math.max(...areas));
|
|
144
|
+
if (!areas[dominant]) continue;
|
|
145
|
+
let painted = false;
|
|
146
|
+
for (const j of dominant === 0 ? [0] : [dominant, 0]) {
|
|
147
|
+
const [a, b, c] = planes[j];
|
|
148
|
+
scanTriangle(verts, a, b, dims[a], dims[b], (x, y, weights, sign) => {
|
|
149
|
+
const depth = weights.reduce((s, w, k) => s + w * verts[k][c], 0);
|
|
150
|
+
const color = sample(mesh, ids, weights);
|
|
151
|
+
if (j === dominant) {
|
|
152
|
+
const cell = [];
|
|
153
|
+
cell[a] = x;
|
|
154
|
+
cell[b] = y;
|
|
155
|
+
cell[c] = Math.min(dims[c] - 1, Math.max(0, Math.floor(depth)));
|
|
156
|
+
put(...cell, color);
|
|
157
|
+
painted = true;
|
|
158
|
+
}
|
|
159
|
+
if (j === 0) (rays[y * size + x] ||= []).push({ depth, sign, color });
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
// A subpixel triangle may cover no cell center in any projection.
|
|
163
|
+
if (!painted) {
|
|
164
|
+
const cell = dims.map((d, k) => Math.min(d - 1, Math.max(0, Math.floor((p[k] + q[k] + r[k]) / 3))));
|
|
165
|
+
put(...cell, sample(mesh, ids, [1 / 3, 1 / 3, 1 / 3]));
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
for (let i = 0; i < rays.length; i++) {
|
|
170
|
+
const ray = rays[i];
|
|
171
|
+
if (!ray || ray.length < 2) continue;
|
|
172
|
+
ray.sort((a, b) => a.depth - b.depth);
|
|
173
|
+
// Collapse shared triangle edges before counting volume crossings.
|
|
174
|
+
const hits = [];
|
|
175
|
+
for (const hit of ray) {
|
|
176
|
+
const prev = hits.at(-1);
|
|
177
|
+
if (prev && Math.abs(prev.depth - hit.depth) < 1e-6) prev.sum += hit.sign;
|
|
178
|
+
else hits.push({ ...hit, sum: hit.sign });
|
|
179
|
+
}
|
|
180
|
+
if (hits.reduce((n, h) => n + Math.sign(h.sum), 0) !== 0) continue;
|
|
181
|
+
let winding = 0;
|
|
182
|
+
for (let k = 0; k + 1 < hits.length; k++) {
|
|
183
|
+
const a = hits[k],
|
|
184
|
+
b = hits[k + 1];
|
|
185
|
+
winding += Math.sign(a.sum);
|
|
186
|
+
if (!winding) continue;
|
|
187
|
+
for (let l = Math.max(0, Math.ceil(a.depth - 0.5)); l < layers && l + 0.5 < b.depth; l++)
|
|
188
|
+
put(i % size, Math.floor(i / size), l, l + 0.5 < (a.depth + b.depth) / 2 ? a.color : b.color, true);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function paintSplats(grid, model, rgba, index) {
|
|
194
|
+
const { size, layers, axes, unit, step } = grid,
|
|
195
|
+
weights = new Float32Array(rgba.length / 4);
|
|
196
|
+
const dims = [size, size, layers];
|
|
197
|
+
for (const s of model.splats) {
|
|
198
|
+
const center = grid.project(s.center);
|
|
199
|
+
const radius = [s.extent[axes[0]] / unit, s.extent[axes[1]] / unit, s.extent[axes[2]] / step];
|
|
200
|
+
const lo = center.map((n, i) => Math.max(0, Math.floor(n - radius[i])));
|
|
201
|
+
const hi = center.map((n, i) => Math.min(dims[i] - 1, Math.floor(n + radius[i])));
|
|
202
|
+
const peak = center.map((n, i) => Math.min(dims[i] - 1, Math.max(0, Math.floor(n))));
|
|
203
|
+
for (let l = lo[2]; l <= hi[2]; l++)
|
|
204
|
+
for (let y = lo[1]; y <= hi[1]; y++)
|
|
205
|
+
for (let x = lo[0]; x <= hi[0]; x++) {
|
|
206
|
+
const d = grid.world(x + 0.5, y + 0.5, l + 0.5).map((v, k) => v - s.center[k]);
|
|
207
|
+
const atCenter = x === peak[0] && y === peak[1] && l === peak[2];
|
|
208
|
+
let distance = 0;
|
|
209
|
+
for (let k = 0; k < 3; k++)
|
|
210
|
+
distance +=
|
|
211
|
+
((d[0] * s.rotation[k * 4] + d[1] * s.rotation[k * 4 + 1] + d[2] * s.rotation[k * 4 + 2]) /
|
|
212
|
+
s.scales[k]) **
|
|
213
|
+
2;
|
|
214
|
+
if (distance > 9 && !atCenter) continue;
|
|
215
|
+
const alpha = s.alpha * Math.exp(-0.5 * (atCenter ? 0 : distance));
|
|
216
|
+
if (alpha < 0.5 / 255) continue;
|
|
217
|
+
const p = index(x, y, l),
|
|
218
|
+
old = weights[p / 4],
|
|
219
|
+
sum = old + alpha;
|
|
220
|
+
for (let k = 0; k < 3; k++) rgba[p + k] = (rgba[p + k] * old + s.color[k] * alpha) / sum;
|
|
221
|
+
rgba[p + 3] += alpha * (255 - rgba[p + 3]);
|
|
222
|
+
weights[p / 4] = sum;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Weighted median-cut over 5-bit RGB bins. Small supplied palettes keep their
|
|
228
|
+
// exact colors, including near-neighbors in the same histogram bin.
|
|
229
|
+
function paletteOf(data, limit) {
|
|
230
|
+
const exact = new Set(),
|
|
231
|
+
hist = new Map();
|
|
232
|
+
for (let p = 0; p < data.length; p += 4) {
|
|
233
|
+
if (!data[p + 3]) continue;
|
|
234
|
+
const r = data[p],
|
|
235
|
+
g = data[p + 1],
|
|
236
|
+
b = data[p + 2],
|
|
237
|
+
key = (r >> 3) * 1024 + (g >> 3) * 32 + (b >> 3);
|
|
238
|
+
if (exact.size <= limit) exact.add(r * 65536 + g * 256 + b);
|
|
239
|
+
const bin = hist.get(key) || [0, 0, 0, 0];
|
|
240
|
+
bin[0] += r;
|
|
241
|
+
bin[1] += g;
|
|
242
|
+
bin[2] += b;
|
|
243
|
+
bin[3]++;
|
|
244
|
+
hist.set(key, bin);
|
|
245
|
+
}
|
|
246
|
+
if (!exact.size) throw new Error("Palette source has no visible pixels.");
|
|
247
|
+
if (exact.size <= limit) return [...exact].map((c) => [c >> 16, (c >> 8) & 255, c & 255]);
|
|
248
|
+
const bins = [...hist.values()].map(([r, g, b, n]) => [r / n, g / n, b / n, n]);
|
|
249
|
+
function box(colors) {
|
|
250
|
+
const low = [255, 255, 255],
|
|
251
|
+
high = [0, 0, 0];
|
|
252
|
+
let count = 0;
|
|
253
|
+
for (const c of colors) {
|
|
254
|
+
for (let k = 0; k < 3; k++) {
|
|
255
|
+
low[k] = Math.min(low[k], c[k]);
|
|
256
|
+
high[k] = Math.max(high[k], c[k]);
|
|
257
|
+
}
|
|
258
|
+
count += c[3];
|
|
259
|
+
}
|
|
260
|
+
const range = high.map((v, k) => v - low[k]),
|
|
261
|
+
span = Math.max(...range);
|
|
262
|
+
return { colors, count, axis: range.indexOf(span), score: colors.length > 1 ? span * count : 0 };
|
|
263
|
+
}
|
|
264
|
+
const boxes = [box(bins)];
|
|
265
|
+
while (boxes.length < limit) {
|
|
266
|
+
boxes.sort((a, b) => b.score - a.score);
|
|
267
|
+
if (!boxes[0].score) break;
|
|
268
|
+
const { colors, count, axis } = boxes.shift();
|
|
269
|
+
colors.sort((a, b) => a[axis] - b[axis]);
|
|
270
|
+
let cut = 0,
|
|
271
|
+
weight = 0;
|
|
272
|
+
do {
|
|
273
|
+
weight += colors[cut++][3];
|
|
274
|
+
} while (cut < colors.length - 1 && weight < count / 2);
|
|
275
|
+
boxes.push(box(colors.slice(0, cut)), box(colors.slice(cut)));
|
|
276
|
+
}
|
|
277
|
+
return boxes.map(({ colors, count }) =>
|
|
278
|
+
[0, 1, 2].map((k) => Math.round(colors.reduce((n, c) => n + c[k] * c[3], 0) / count)),
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export function slicePixels(model, { size, layers, axis, palette, outline }, supplied) {
|
|
283
|
+
if (axis === "auto") axis = model.kind === "splat" ? "z" : "y";
|
|
284
|
+
const grid = gridFor(model, axis, size, layers),
|
|
285
|
+
width = size * layers,
|
|
286
|
+
rgba = new Uint8ClampedArray(width * size * 4);
|
|
287
|
+
const index = (x, y, l) => (y * width + l * size + x) * 4;
|
|
288
|
+
if (model.kind === "splat") paintSplats(grid, model, rgba, index);
|
|
289
|
+
else
|
|
290
|
+
paintMesh(grid, model, (x, y, l, c, onlyEmpty = false) => {
|
|
291
|
+
const p = index(x, y, l);
|
|
292
|
+
if (c[3] && (!onlyEmpty || !rgba[p + 3])) rgba.set(c, p);
|
|
293
|
+
});
|
|
294
|
+
let ink = [24, 24, 24];
|
|
295
|
+
if (palette && (supplied || rgba.some((v, i) => i % 4 === 3 && v))) {
|
|
296
|
+
// Reserve one ink color for generated palettes. An explicit reference
|
|
297
|
+
// palette supplies its own darkest color for the outline.
|
|
298
|
+
const reserveInk = outline && !supplied && palette > 1;
|
|
299
|
+
const colors = paletteOf(supplied?.data || rgba, palette - Number(reserveInk));
|
|
300
|
+
if (!reserveInk) ink = colors.reduce((a, b) => (a[0] + a[1] + a[2] <= b[0] + b[1] + b[2] ? a : b));
|
|
301
|
+
const cache = new Map();
|
|
302
|
+
for (let p = 0; p < rgba.length; p += 4) {
|
|
303
|
+
if (!rgba[p + 3]) continue;
|
|
304
|
+
const key = rgba[p] * 65536 + rgba[p + 1] * 256 + rgba[p + 2];
|
|
305
|
+
let color = cache.get(key);
|
|
306
|
+
if (!color) {
|
|
307
|
+
let best = Infinity;
|
|
308
|
+
for (const c of colors) {
|
|
309
|
+
const d = (c[0] - rgba[p]) ** 2 + (c[1] - rgba[p + 1]) ** 2 + (c[2] - rgba[p + 2]) ** 2;
|
|
310
|
+
if (d < best) {
|
|
311
|
+
best = d;
|
|
312
|
+
color = c;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
cache.set(key, color);
|
|
316
|
+
}
|
|
317
|
+
rgba.set(color, p);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (outline) {
|
|
321
|
+
const before = rgba.slice();
|
|
322
|
+
for (let l = 0; l < layers; l++)
|
|
323
|
+
for (let y = 0; y < size; y++)
|
|
324
|
+
for (let x = 0; x < size; x++) {
|
|
325
|
+
const p = index(x, y, l);
|
|
326
|
+
if (before[p + 3]) continue;
|
|
327
|
+
const alpha = Math.max(
|
|
328
|
+
x > 0 ? before[p - 4 + 3] : 0,
|
|
329
|
+
x + 1 < size ? before[p + 4 + 3] : 0,
|
|
330
|
+
y > 0 ? before[p - width * 4 + 3] : 0,
|
|
331
|
+
y + 1 < size ? before[p + width * 4 + 3] : 0,
|
|
332
|
+
);
|
|
333
|
+
if (alpha) {
|
|
334
|
+
rgba.set(ink, p);
|
|
335
|
+
rgba[p + 3] = alpha;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return {
|
|
340
|
+
rgba,
|
|
341
|
+
width,
|
|
342
|
+
height: size,
|
|
343
|
+
layout: {
|
|
344
|
+
version: 1,
|
|
345
|
+
order: "bottom-to-top",
|
|
346
|
+
columns: layers,
|
|
347
|
+
rows: 1,
|
|
348
|
+
tileWidth: size,
|
|
349
|
+
tileHeight: size,
|
|
350
|
+
upAxis: axis,
|
|
351
|
+
source: model.kind,
|
|
352
|
+
bounds: { min: grid.min, max: grid.max },
|
|
353
|
+
pixelSize: grid.unit,
|
|
354
|
+
layerHeight: grid.step,
|
|
355
|
+
},
|
|
356
|
+
};
|
|
357
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { number, viewPlan, softwareFrame, flag, turntablePlan } from "./sprite-projection.mjs";
|
|
2
|
+
import { decodeSpriteImage, frameUrl, encodeTurntable } from "./sprite-cpu.mjs";
|
|
3
|
+
import { MediaRef, bytesToDataUrl } from "./media.mjs";
|
|
4
|
+
import { gaussianModel, triangleModel } from "./sprite-model.mjs";
|
|
5
|
+
import { slicePixels } from "./sprite-slices.mjs";
|
|
6
|
+
|
|
7
|
+
function canvas(width, height) {
|
|
8
|
+
if (typeof document === "undefined") throw new Error("Sprite stacking requires a browser with Canvas 2D.");
|
|
9
|
+
const el = document.createElement("canvas");
|
|
10
|
+
el.width = width;
|
|
11
|
+
el.height = height;
|
|
12
|
+
return el;
|
|
13
|
+
}
|
|
14
|
+
async function bitmap(blob) {
|
|
15
|
+
if (typeof createImageBitmap === "function") return createImageBitmap(blob);
|
|
16
|
+
const url = URL.createObjectURL(blob),
|
|
17
|
+
img = new Image();
|
|
18
|
+
try {
|
|
19
|
+
img.src = url;
|
|
20
|
+
await img.decode();
|
|
21
|
+
return img;
|
|
22
|
+
} finally {
|
|
23
|
+
URL.revokeObjectURL(url);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async function imagePixels(blob) {
|
|
27
|
+
const img = await bitmap(blob);
|
|
28
|
+
try {
|
|
29
|
+
const ctx = canvas(img.width, img.height).getContext("2d", { willReadFrequently: true });
|
|
30
|
+
ctx.drawImage(img, 0, 0);
|
|
31
|
+
return ctx.getImageData(0, 0, img.width, img.height);
|
|
32
|
+
} finally {
|
|
33
|
+
img.close?.();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async function readMedia(value, ctx) {
|
|
37
|
+
const media = new MediaRef(String(value), { fetch: ctx?.fetch });
|
|
38
|
+
const bytes = await media.bytes({ signal: ctx?.signal });
|
|
39
|
+
return new Blob([bytes], { type: media.mime || "application/octet-stream" });
|
|
40
|
+
}
|
|
41
|
+
function png(el) {
|
|
42
|
+
const result = el.toDataURL("image/png");
|
|
43
|
+
if (!result.startsWith("data:image/png")) throw new Error("The atlas exceeds this browser’s canvas limit.");
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function stackView(sheet, fields) {
|
|
48
|
+
const { tile, layers, scale, rise, squash, w, h } = viewPlan(sheet, fields);
|
|
49
|
+
const el = canvas(w, h),
|
|
50
|
+
ctx = el.getContext("2d");
|
|
51
|
+
function draw(yaw) {
|
|
52
|
+
ctx.resetTransform();
|
|
53
|
+
ctx.clearRect(0, 0, w, h);
|
|
54
|
+
ctx.imageSmoothingEnabled = false;
|
|
55
|
+
const a = (yaw * Math.PI) / 180,
|
|
56
|
+
c = Math.cos(a) * scale,
|
|
57
|
+
s = Math.sin(a) * scale;
|
|
58
|
+
for (let l = 0; l < layers; l++) {
|
|
59
|
+
ctx.setTransform(c, s * squash, -s, c * squash, w / 2, h / 2 + ((layers - 1) / 2 - l) * rise * scale);
|
|
60
|
+
ctx.drawImage(sheet, l * tile, 0, tile, tile, -tile / 2, -tile / 2, tile, tile);
|
|
61
|
+
}
|
|
62
|
+
ctx.resetTransform();
|
|
63
|
+
}
|
|
64
|
+
return { el, draw };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Match the editor's MP4-first canvas export, falling back to WebM where available.
|
|
68
|
+
async function recordTurntable(view, fields, signal) {
|
|
69
|
+
if (typeof MediaRecorder === "undefined" || !view.el.captureStream)
|
|
70
|
+
throw new Error("Turntable export requires MediaRecorder and canvas.captureStream.");
|
|
71
|
+
const mime = [
|
|
72
|
+
"video/mp4;codecs=avc1.42E01E",
|
|
73
|
+
"video/mp4",
|
|
74
|
+
"video/webm;codecs=vp9",
|
|
75
|
+
"video/webm;codecs=vp8",
|
|
76
|
+
"video/webm",
|
|
77
|
+
].find((m) => MediaRecorder.isTypeSupported(m));
|
|
78
|
+
if (!mime) throw new Error("No supported MP4 or WebM recorder in this browser.");
|
|
79
|
+
const { fps, duration, from, to, count: frames } = turntablePlan(fields);
|
|
80
|
+
view.draw(from);
|
|
81
|
+
let stream = view.el.captureStream(0);
|
|
82
|
+
if (typeof stream.getVideoTracks()[0]?.requestFrame !== "function") {
|
|
83
|
+
stream.getTracks().forEach((track) => track.stop());
|
|
84
|
+
stream = view.el.captureStream(fps);
|
|
85
|
+
}
|
|
86
|
+
const [track] = stream.getVideoTracks();
|
|
87
|
+
let rec;
|
|
88
|
+
try {
|
|
89
|
+
rec = new MediaRecorder(stream, { mimeType: mime });
|
|
90
|
+
const chunks = [];
|
|
91
|
+
const stopped = new Promise((resolve, reject) => {
|
|
92
|
+
rec.ondataavailable = (e) => {
|
|
93
|
+
if (e.data.size) chunks.push(e.data);
|
|
94
|
+
};
|
|
95
|
+
rec.onerror = (e) => reject(e.error || new Error("Turntable recording failed."));
|
|
96
|
+
rec.onstop = resolve;
|
|
97
|
+
});
|
|
98
|
+
const start = performance.now();
|
|
99
|
+
async function animate() {
|
|
100
|
+
rec.start();
|
|
101
|
+
for (let frame = 0; frame < frames; frame++) {
|
|
102
|
+
signal?.throwIfAborted();
|
|
103
|
+
view.draw(from + ((to - from) * frame) / (frames - 1));
|
|
104
|
+
track.requestFrame?.();
|
|
105
|
+
const deadline = start + ((frame + 1) * duration * 1000) / frames;
|
|
106
|
+
await new Promise((resolve) => setTimeout(resolve, Math.max(0, deadline - performance.now())));
|
|
107
|
+
if (rec.state === "inactive") break;
|
|
108
|
+
}
|
|
109
|
+
if (rec.state !== "inactive") rec.stop();
|
|
110
|
+
}
|
|
111
|
+
await Promise.all([animate(), stopped]);
|
|
112
|
+
signal?.throwIfAborted();
|
|
113
|
+
const blob = new Blob(chunks, { type: rec.mimeType.split(";")[0] });
|
|
114
|
+
if (!blob.size) throw new Error("The browser produced an empty turntable recording.");
|
|
115
|
+
return bytesToDataUrl(new Uint8Array(await blob.arrayBuffer()), blob.type);
|
|
116
|
+
} finally {
|
|
117
|
+
if (rec && rec.state !== "inactive") rec.stop();
|
|
118
|
+
stream.getTracks().forEach((t) => t.stop());
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export async function slice3d(node, inp, ctx) {
|
|
123
|
+
if (!inp.model) throw new Error("Wire a 3D model into Slice 3D.");
|
|
124
|
+
const fields = node.fields || {},
|
|
125
|
+
size = number(fields, "resolution", 48, 16, 128, true),
|
|
126
|
+
layers = number(fields, "slices", 48, 8, 128, true);
|
|
127
|
+
const pixels =
|
|
128
|
+
typeof document === "undefined" ? (blob) => decodeSpriteImage(blob, ctx?.signal) : imagePixels;
|
|
129
|
+
const modelBlob = await readMedia(inp.model, ctx),
|
|
130
|
+
bytes = new Uint8Array(await modelBlob.arrayBuffer());
|
|
131
|
+
const model =
|
|
132
|
+
bytes[0] === 112 && bytes[1] === 108 && bytes[2] === 121
|
|
133
|
+
? gaussianModel(bytes)
|
|
134
|
+
: await triangleModel(bytes, (raw, mime) => pixels(new Blob([raw], { type: mime })));
|
|
135
|
+
const palette = number(fields, "palette", 16, 0, 64, true);
|
|
136
|
+
const supplied = palette && inp.paletteImage ? await pixels(await readMedia(inp.paletteImage, ctx)) : null;
|
|
137
|
+
ctx?.signal?.throwIfAborted();
|
|
138
|
+
const sliced = slicePixels(
|
|
139
|
+
model,
|
|
140
|
+
{ size, layers, axis: fields.upAxis || "auto", outline: flag(fields.outline, true), palette },
|
|
141
|
+
supplied,
|
|
142
|
+
);
|
|
143
|
+
if (typeof document === "undefined") {
|
|
144
|
+
const sheet = { width: sliced.width, height: sliced.height, data: sliced.rgba };
|
|
145
|
+
return {
|
|
146
|
+
sheet: await frameUrl(sheet),
|
|
147
|
+
layout: JSON.stringify(sliced.layout),
|
|
148
|
+
image: await frameUrl(softwareFrame(sheet, {}, 35)),
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
const el = canvas(size * layers, size);
|
|
152
|
+
el.getContext("2d").putImageData(new ImageData(sliced.rgba, sliced.width, sliced.height), 0, 0);
|
|
153
|
+
const view = stackView(el, {});
|
|
154
|
+
view.draw(35);
|
|
155
|
+
return { sheet: png(el), layout: JSON.stringify(sliced.layout), image: png(view.el) };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export async function renderSpriteStack(node, inp, ctx) {
|
|
159
|
+
if (!inp.sheet) throw new Error("Wire the Slice 3D sheet into Sprite stack render.");
|
|
160
|
+
const fields = node.fields || {},
|
|
161
|
+
blob = await readMedia(inp.sheet, ctx);
|
|
162
|
+
const yaw = number(fields, "yaw", 35, -3600, 3600);
|
|
163
|
+
if (typeof document === "undefined") {
|
|
164
|
+
const sheet = await decodeSpriteImage(blob, ctx?.signal);
|
|
165
|
+
const image = await frameUrl(softwareFrame(sheet, fields, yaw));
|
|
166
|
+
if (!flag(fields.turntable, false)) return { image };
|
|
167
|
+
return { image, video: await encodeTurntable(sheet, fields, ctx?.signal) };
|
|
168
|
+
}
|
|
169
|
+
const sheet = await bitmap(blob);
|
|
170
|
+
try {
|
|
171
|
+
const view = stackView(sheet, fields);
|
|
172
|
+
view.draw(yaw);
|
|
173
|
+
const image = png(view.el);
|
|
174
|
+
if (!flag(fields.turntable, false)) return { image };
|
|
175
|
+
return { image, video: await recordTurntable(view, fields, ctx?.signal) };
|
|
176
|
+
} finally {
|
|
177
|
+
sheet.close?.();
|
|
178
|
+
}
|
|
179
|
+
}
|