@i7r.tech/moveby 0.0.21-ywc → 0.21.2-ywc
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 +31 -0
- package/bin/moveby.js +431 -10
- package/bin/tooling/wasm_engine_bg.wasm +0 -0
- package/dist/engine/wasm_engine.d.ts +44 -16
- package/dist/engine/wasm_engine.js +60 -27
- package/dist/engine/wasm_engine_bg.wasm +0 -0
- package/dist/index.js +437 -225
- package/dist/passes/backdrop.d.ts +6 -1
- package/dist/passes/field.d.ts +4 -1
- package/dist/passes/road.d.ts +6 -2
- package/dist/passes/scenery.d.ts +3 -1
- package/dist/passes/sky.d.ts +1 -0
- package/dist/passes/text.d.ts +1 -0
- package/dist/scene.d.ts +16 -5
- package/dist/utils.d.ts +17 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -56,6 +56,37 @@ const scene = createScene(canvas, track, [backdrop(), road(), scenery({ atlas: "
|
|
|
56
56
|
await scene.ready; // rejects with a SCENE_ATLAS MovebyError if the fetch or the decode fails
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
+
## A sky image
|
|
60
|
+
|
|
61
|
+
`backdrop()` takes a ground colour and a sky. The sky is a colour, an image (`TexImageSource`), a URL,
|
|
62
|
+
or an object that holds one of the two:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
const scene = createScene(canvas, track, [
|
|
66
|
+
backdrop({ ground: [0.25, 0.22, 0.16], sky: { image: "/hills.png", color: [0.5, 0.8, 0.95], share: 0.4 } }),
|
|
67
|
+
road(),
|
|
68
|
+
]);
|
|
69
|
+
await scene.ready; // a URL: SCENE_ATLAS if the fetch or the decode fails
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The image stands on the horizon and moves sideways with the heading of the road.
|
|
73
|
+
|
|
74
|
+
- `share` is the part of the canvas height that the image takes, above 0 and at most 1. The default is
|
|
75
|
+
0.5. The width follows the image's shape.
|
|
76
|
+
- `color` is the colour under the image. The default is white. It shows above the image, beside it,
|
|
77
|
+
through its transparent parts, and until a URL image arrives.
|
|
78
|
+
- No edge shows while the image is drawn at least 1.25 canvas widths wide. So its width divided by its
|
|
79
|
+
height must be at least `1.25 × canvasWidth / (share × canvasHeight)`. At the default share that is
|
|
80
|
+
about 4.2 : 1 on a 1000 × 600 canvas, and 1.4 : 1 on a 390 × 700 canvas. A narrower image is not
|
|
81
|
+
stretched: the colour shows beside it, on a bend or, if it is narrower than the canvas, always.
|
|
82
|
+
- The image is filtered `LINEAR` and has no mipmaps. Pixel art goes soft, and an image much larger than
|
|
83
|
+
its drawn size shimmers when it moves.
|
|
84
|
+
- Transparency blends with premultiplied alpha. The library premultiplies a URL, an `<img>`, a canvas and
|
|
85
|
+
`ImageData`. The browser cannot do it for an `ImageBitmap` at the upload: make your own one with
|
|
86
|
+
`createImageBitmap(source, { premultiplyAlpha: "premultiply" })`.
|
|
87
|
+
- An image larger than the device's largest texture is a `SCENE_IMAGE_SIZE` error: `createScene` throws
|
|
88
|
+
it for an image, and `scene.ready` rejects with it for a URL.
|
|
89
|
+
|
|
59
90
|
## Errors
|
|
60
91
|
|
|
61
92
|
Every fault the library raises is a `MovebyError` with a stable `code` and a readable message.
|
package/bin/moveby.js
CHANGED
|
@@ -7278,7 +7278,30 @@ var atlas_schema_default = {
|
|
|
7278
7278
|
cols: { type: "integer", minimum: 1, maximum: 65536 },
|
|
7279
7279
|
rows: { type: "integer", minimum: 1, maximum: 65536 },
|
|
7280
7280
|
pxWidth: { type: "integer", minimum: 1, maximum: 4294967295 },
|
|
7281
|
-
pxHeight: { type: "integer", minimum: 1, maximum: 4294967295 }
|
|
7281
|
+
pxHeight: { type: "integer", minimum: 1, maximum: 4294967295 },
|
|
7282
|
+
cells: {
|
|
7283
|
+
type: "array",
|
|
7284
|
+
items: { $ref: "#/$defs/cell" },
|
|
7285
|
+
description: "The images that the build packs into this atlas, one per entry. With this key, `moveby build` writes the atlas image itself, `pxWidth` by `pxHeight` pixels, beside the track: `sprites.png` for `sprites`, `fields.png` for `fields`. A cell with no entry is transparent. A cell is a whole number of pixels: `pxWidth` is a multiple of `cols`, and `pxHeight` of `rows`. Neither side of an atlas that the build writes is past 16384 pixels."
|
|
7286
|
+
}
|
|
7287
|
+
}
|
|
7288
|
+
},
|
|
7289
|
+
cell: {
|
|
7290
|
+
description: "One image in one cell of an atlas.",
|
|
7291
|
+
type: "object",
|
|
7292
|
+
additionalProperties: false,
|
|
7293
|
+
required: ["cell", "image"],
|
|
7294
|
+
properties: {
|
|
7295
|
+
cell: {
|
|
7296
|
+
type: "integer",
|
|
7297
|
+
minimum: 0,
|
|
7298
|
+
description: "The number of the cell, counted from 0, row by row from the top left. It is the number that `asset()` and road.json take. Each cell has one entry at most."
|
|
7299
|
+
},
|
|
7300
|
+
image: {
|
|
7301
|
+
type: "string",
|
|
7302
|
+
minLength: 1,
|
|
7303
|
+
description: "A PNG file with 8 bits per channel, not interlaced. The path is relative to the folder of this file. The image scales to fit the cell, keeps its aspect, and sits at the bottom centre. An image of `fields` has the aspect of its cell, because the ground is opaque."
|
|
7304
|
+
}
|
|
7282
7305
|
}
|
|
7283
7306
|
}
|
|
7284
7307
|
}
|
|
@@ -7392,8 +7415,38 @@ function readAtlas(json) {
|
|
|
7392
7415
|
throw new AtlasError("ATLAS_SCHEMA", fault.path, fault.message);
|
|
7393
7416
|
}
|
|
7394
7417
|
const { sprites, fields, font } = json;
|
|
7418
|
+
checkCells("sprites", sprites);
|
|
7419
|
+
checkCells("fields", fields);
|
|
7395
7420
|
return { sprites, fields, font };
|
|
7396
7421
|
}
|
|
7422
|
+
var MAX_SHEET_SIDE = 16384;
|
|
7423
|
+
function checkCells(name, grid) {
|
|
7424
|
+
if (grid?.cells === undefined)
|
|
7425
|
+
return;
|
|
7426
|
+
const { cols, rows, pxWidth, pxHeight, cells } = grid;
|
|
7427
|
+
for (const [key, side] of Object.entries({ pxWidth, pxHeight })) {
|
|
7428
|
+
if (side > MAX_SHEET_SIDE) {
|
|
7429
|
+
throw new AtlasError("ATLAS_CELLS", `/${name}/${key}`, `${key} ${side} is past ${MAX_SHEET_SIDE}, the longest side of an atlas that the build writes`);
|
|
7430
|
+
}
|
|
7431
|
+
}
|
|
7432
|
+
const entryOfCell = new Map;
|
|
7433
|
+
for (const [index, { cell }] of cells.entries()) {
|
|
7434
|
+
const fail = (message) => {
|
|
7435
|
+
throw new AtlasError("ATLAS_CELLS", `/${name}/cells/${index}`, message);
|
|
7436
|
+
};
|
|
7437
|
+
if (pxWidth % cols !== 0)
|
|
7438
|
+
fail(`pxWidth ${pxWidth} is not a multiple of cols ${cols}`);
|
|
7439
|
+
if (pxHeight % rows !== 0)
|
|
7440
|
+
fail(`pxHeight ${pxHeight} is not a multiple of rows ${rows}`);
|
|
7441
|
+
if (cell >= cols * rows) {
|
|
7442
|
+
fail(`names cell ${cell}: a grid of ${cols} × ${rows} has the cells 0 to ${cols * rows - 1}`);
|
|
7443
|
+
}
|
|
7444
|
+
const first = entryOfCell.get(cell);
|
|
7445
|
+
if (first !== undefined)
|
|
7446
|
+
fail(`names cell ${cell}, which entry ${first} already holds`);
|
|
7447
|
+
entryOfCell.set(cell, index);
|
|
7448
|
+
}
|
|
7449
|
+
}
|
|
7397
7450
|
function readFont(json) {
|
|
7398
7451
|
const fault = shapeFault(validateFont, json, "does not match font.schema.json");
|
|
7399
7452
|
if (fault) {
|
|
@@ -7417,6 +7470,317 @@ function encodeTrack(road, parts) {
|
|
|
7417
7470
|
return encode_track_bytes(road.route, road.laneRecords, road.fieldRecords, fields.cols, fields.rows, fields.pxWidth, fields.pxHeight, gridNumbers(parts.sprites), parts.font ?? new Float32Array);
|
|
7418
7471
|
}
|
|
7419
7472
|
|
|
7473
|
+
// ../moveby-build/src/png.ts
|
|
7474
|
+
import { deflateSync, inflateSync } from "node:zlib";
|
|
7475
|
+
|
|
7476
|
+
class PngError extends Error {
|
|
7477
|
+
code = "ATLAS_IMAGE";
|
|
7478
|
+
constructor(message) {
|
|
7479
|
+
super(message);
|
|
7480
|
+
this.name = "PngError";
|
|
7481
|
+
}
|
|
7482
|
+
}
|
|
7483
|
+
var SIGNATURE = Uint8Array.of(137, 80, 78, 71, 13, 10, 26, 10);
|
|
7484
|
+
var GREY = 0;
|
|
7485
|
+
var TRUECOLOUR = 2;
|
|
7486
|
+
var INDEXED = 3;
|
|
7487
|
+
var GREY_ALPHA = 4;
|
|
7488
|
+
var TRUECOLOUR_ALPHA = 6;
|
|
7489
|
+
var BYTES_PER_PIXEL = new Map([
|
|
7490
|
+
[GREY, 1],
|
|
7491
|
+
[TRUECOLOUR, 3],
|
|
7492
|
+
[INDEXED, 1],
|
|
7493
|
+
[GREY_ALPHA, 2],
|
|
7494
|
+
[TRUECOLOUR_ALPHA, 4]
|
|
7495
|
+
]);
|
|
7496
|
+
function decodePng(bytes) {
|
|
7497
|
+
const { header, palette, transparency, data } = readChunks(bytes);
|
|
7498
|
+
const { width, height, colourType, bytesPerPixel } = readHeader(header);
|
|
7499
|
+
const rowBytes = width * bytesPerPixel;
|
|
7500
|
+
const filtered = inflate(Buffer.concat(data));
|
|
7501
|
+
if (filtered.length !== height * (rowBytes + 1)) {
|
|
7502
|
+
throw new PngError(`holds ${filtered.length} bytes of image data, not the ${height * (rowBytes + 1)} that its size needs`);
|
|
7503
|
+
}
|
|
7504
|
+
const samples = unfilter(filtered, height, rowBytes, bytesPerPixel);
|
|
7505
|
+
return { width, height, rgba: toRgba(samples, colourType, palette, transparency) };
|
|
7506
|
+
}
|
|
7507
|
+
function readChunks(bytes) {
|
|
7508
|
+
if (SIGNATURE.some((byte, at2) => bytes[at2] !== byte)) {
|
|
7509
|
+
throw new PngError("is not a PNG file");
|
|
7510
|
+
}
|
|
7511
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
7512
|
+
let header;
|
|
7513
|
+
let palette;
|
|
7514
|
+
let transparency;
|
|
7515
|
+
const data = [];
|
|
7516
|
+
let at = SIGNATURE.length;
|
|
7517
|
+
for (;; ) {
|
|
7518
|
+
if (at + 12 > bytes.length)
|
|
7519
|
+
throw new PngError("ends before its IEND chunk");
|
|
7520
|
+
const length = view.getUint32(at);
|
|
7521
|
+
const type = String.fromCharCode(...bytes.subarray(at + 4, at + 8));
|
|
7522
|
+
const end = at + 8 + length;
|
|
7523
|
+
if (end + 4 > bytes.length)
|
|
7524
|
+
throw new PngError(`ends inside its ${type} chunk`);
|
|
7525
|
+
const body = bytes.subarray(at + 8, end);
|
|
7526
|
+
if (header === undefined) {
|
|
7527
|
+
if (type !== "IHDR")
|
|
7528
|
+
throw new PngError(`starts with a ${type} chunk, not IHDR`);
|
|
7529
|
+
header = body;
|
|
7530
|
+
} else if (type === "IEND") {
|
|
7531
|
+
return { header, palette, transparency, data };
|
|
7532
|
+
} else if (type === "PLTE") {
|
|
7533
|
+
palette = body;
|
|
7534
|
+
} else if (type === "tRNS") {
|
|
7535
|
+
transparency = body;
|
|
7536
|
+
} else if (type === "IDAT") {
|
|
7537
|
+
data.push(body);
|
|
7538
|
+
}
|
|
7539
|
+
at = end + 4;
|
|
7540
|
+
}
|
|
7541
|
+
}
|
|
7542
|
+
function readHeader(header) {
|
|
7543
|
+
if (header.length !== 13)
|
|
7544
|
+
throw new PngError("has an IHDR chunk of the wrong length");
|
|
7545
|
+
const view = new DataView(header.buffer, header.byteOffset, header.byteLength);
|
|
7546
|
+
const width = view.getUint32(0);
|
|
7547
|
+
const height = view.getUint32(4);
|
|
7548
|
+
const bitDepth = header[8];
|
|
7549
|
+
const colourType = header[9];
|
|
7550
|
+
const compression = header[10];
|
|
7551
|
+
const filtering = header[11];
|
|
7552
|
+
const interlace = header[12];
|
|
7553
|
+
if (width === 0 || height === 0)
|
|
7554
|
+
throw new PngError("has no pixels");
|
|
7555
|
+
if (bitDepth !== 8)
|
|
7556
|
+
throw new PngError(`has ${bitDepth} bits per sample: only 8 is read`);
|
|
7557
|
+
const bytesPerPixel = BYTES_PER_PIXEL.get(colourType);
|
|
7558
|
+
if (bytesPerPixel === undefined)
|
|
7559
|
+
throw new PngError(`has the unknown colour type ${colourType}`);
|
|
7560
|
+
if (compression !== 0 || filtering !== 0) {
|
|
7561
|
+
throw new PngError("names a compression or filter method that PNG does not define");
|
|
7562
|
+
}
|
|
7563
|
+
if (interlace !== 0)
|
|
7564
|
+
throw new PngError("is interlaced: save it without interlace");
|
|
7565
|
+
return { width, height, colourType, bytesPerPixel };
|
|
7566
|
+
}
|
|
7567
|
+
function inflate(data) {
|
|
7568
|
+
try {
|
|
7569
|
+
return inflateSync(data);
|
|
7570
|
+
} catch (error) {
|
|
7571
|
+
throw new PngError(`has broken image data: ${error instanceof Error ? error.message : String(error)}`);
|
|
7572
|
+
}
|
|
7573
|
+
}
|
|
7574
|
+
function unfilter(filtered, height, rowBytes, bytesPerPixel) {
|
|
7575
|
+
const samples = new Uint8Array(height * rowBytes);
|
|
7576
|
+
for (let row = 0;row < height; row++) {
|
|
7577
|
+
const filter = filtered[row * (rowBytes + 1)];
|
|
7578
|
+
for (let column = 0;column < rowBytes; column++) {
|
|
7579
|
+
const at = row * rowBytes + column;
|
|
7580
|
+
const hasLeft = column >= bytesPerPixel;
|
|
7581
|
+
const left = hasLeft ? samples[at - bytesPerPixel] : 0;
|
|
7582
|
+
const up = row > 0 ? samples[at - rowBytes] : 0;
|
|
7583
|
+
const upLeft = hasLeft && row > 0 ? samples[at - rowBytes - bytesPerPixel] : 0;
|
|
7584
|
+
samples[at] = filtered[at + row + 1] + predict(filter, left, up, upLeft);
|
|
7585
|
+
}
|
|
7586
|
+
}
|
|
7587
|
+
return samples;
|
|
7588
|
+
}
|
|
7589
|
+
function predict(filter, left, up, upLeft) {
|
|
7590
|
+
switch (filter) {
|
|
7591
|
+
case 0:
|
|
7592
|
+
return 0;
|
|
7593
|
+
case 1:
|
|
7594
|
+
return left;
|
|
7595
|
+
case 2:
|
|
7596
|
+
return up;
|
|
7597
|
+
case 3:
|
|
7598
|
+
return left + up >> 1;
|
|
7599
|
+
case 4:
|
|
7600
|
+
return paeth(left, up, upLeft);
|
|
7601
|
+
default:
|
|
7602
|
+
throw new PngError(`has a row with the unknown filter ${filter}`);
|
|
7603
|
+
}
|
|
7604
|
+
}
|
|
7605
|
+
function paeth(left, up, upLeft) {
|
|
7606
|
+
const estimate = left + up - upLeft;
|
|
7607
|
+
const toLeft = Math.abs(estimate - left);
|
|
7608
|
+
const toUp = Math.abs(estimate - up);
|
|
7609
|
+
const toUpLeft = Math.abs(estimate - upLeft);
|
|
7610
|
+
if (toLeft <= toUp && toLeft <= toUpLeft)
|
|
7611
|
+
return left;
|
|
7612
|
+
return toUp <= toUpLeft ? up : upLeft;
|
|
7613
|
+
}
|
|
7614
|
+
function toRgba(samples, colourType, palette, transparency) {
|
|
7615
|
+
if (colourType === TRUECOLOUR_ALPHA)
|
|
7616
|
+
return samples;
|
|
7617
|
+
if (colourType === INDEXED && palette === undefined) {
|
|
7618
|
+
throw new PngError("uses a palette and has no PLTE chunk");
|
|
7619
|
+
}
|
|
7620
|
+
const bytesPerPixel = BYTES_PER_PIXEL.get(colourType);
|
|
7621
|
+
const rgba = new Uint8Array(samples.length / bytesPerPixel * 4);
|
|
7622
|
+
for (let from = 0, to = 0;from < samples.length; from += bytesPerPixel, to += 4) {
|
|
7623
|
+
const first = samples[from];
|
|
7624
|
+
switch (colourType) {
|
|
7625
|
+
case GREY:
|
|
7626
|
+
rgba.fill(first, to, to + 3);
|
|
7627
|
+
rgba[to + 3] = transparency !== undefined && first === transparency[1] ? 0 : 255;
|
|
7628
|
+
break;
|
|
7629
|
+
case GREY_ALPHA:
|
|
7630
|
+
rgba.fill(first, to, to + 3);
|
|
7631
|
+
rgba[to + 3] = samples[from + 1];
|
|
7632
|
+
break;
|
|
7633
|
+
case TRUECOLOUR: {
|
|
7634
|
+
const green = samples[from + 1];
|
|
7635
|
+
const blue = samples[from + 2];
|
|
7636
|
+
const keyed = transparency !== undefined && first === transparency[1] && green === transparency[3] && blue === transparency[5];
|
|
7637
|
+
rgba[to] = first;
|
|
7638
|
+
rgba[to + 1] = green;
|
|
7639
|
+
rgba[to + 2] = blue;
|
|
7640
|
+
rgba[to + 3] = keyed ? 0 : 255;
|
|
7641
|
+
break;
|
|
7642
|
+
}
|
|
7643
|
+
default: {
|
|
7644
|
+
if (first * 3 + 3 > palette.length) {
|
|
7645
|
+
throw new PngError(`names palette entry ${first}, which the palette does not have`);
|
|
7646
|
+
}
|
|
7647
|
+
rgba.set(palette.subarray(first * 3, first * 3 + 3), to);
|
|
7648
|
+
rgba[to + 3] = transparency?.[first] ?? 255;
|
|
7649
|
+
}
|
|
7650
|
+
}
|
|
7651
|
+
}
|
|
7652
|
+
return rgba;
|
|
7653
|
+
}
|
|
7654
|
+
function encodePng(width, height, rgba) {
|
|
7655
|
+
const header = Buffer.alloc(13);
|
|
7656
|
+
header.writeUInt32BE(width, 0);
|
|
7657
|
+
header.writeUInt32BE(height, 4);
|
|
7658
|
+
header[8] = 8;
|
|
7659
|
+
header[9] = TRUECOLOUR_ALPHA;
|
|
7660
|
+
const rowBytes = width * 4;
|
|
7661
|
+
const filtered = Buffer.alloc(height * (rowBytes + 1));
|
|
7662
|
+
for (let row = 0;row < height; row++) {
|
|
7663
|
+
filtered.set(rgba.subarray(row * rowBytes, (row + 1) * rowBytes), row * (rowBytes + 1) + 1);
|
|
7664
|
+
}
|
|
7665
|
+
return Buffer.concat([
|
|
7666
|
+
SIGNATURE,
|
|
7667
|
+
chunk("IHDR", header),
|
|
7668
|
+
chunk("IDAT", deflateSync(filtered, { level: 9 })),
|
|
7669
|
+
chunk("IEND", new Uint8Array)
|
|
7670
|
+
]);
|
|
7671
|
+
}
|
|
7672
|
+
function chunk(type, data) {
|
|
7673
|
+
const bytes = Buffer.alloc(12 + data.length);
|
|
7674
|
+
bytes.writeUInt32BE(data.length, 0);
|
|
7675
|
+
bytes.write(type, 4, "latin1");
|
|
7676
|
+
bytes.set(data, 8);
|
|
7677
|
+
bytes.writeUInt32BE(crc32(bytes.subarray(4, 8 + data.length)), 8 + data.length);
|
|
7678
|
+
return bytes;
|
|
7679
|
+
}
|
|
7680
|
+
var CRC_TABLE = Uint32Array.from({ length: 256 }, (_, byte) => {
|
|
7681
|
+
let value = byte;
|
|
7682
|
+
for (let bit = 0;bit < 8; bit++) {
|
|
7683
|
+
value = value & 1 ? 3988292384 ^ value >>> 1 : value >>> 1;
|
|
7684
|
+
}
|
|
7685
|
+
return value >>> 0;
|
|
7686
|
+
});
|
|
7687
|
+
function crc32(bytes) {
|
|
7688
|
+
let crc = 4294967295;
|
|
7689
|
+
for (const byte of bytes) {
|
|
7690
|
+
crc = CRC_TABLE[(crc ^ byte) & 255] ^ crc >>> 8;
|
|
7691
|
+
}
|
|
7692
|
+
return (crc ^ 4294967295) >>> 0;
|
|
7693
|
+
}
|
|
7694
|
+
|
|
7695
|
+
// ../moveby-build/src/pack.ts
|
|
7696
|
+
function fitRect(cellWidth, cellHeight, imageWidth, imageHeight) {
|
|
7697
|
+
const scale = Math.min(cellWidth / imageWidth, cellHeight / imageHeight);
|
|
7698
|
+
const width = Math.max(1, Math.round(imageWidth * scale));
|
|
7699
|
+
const height = Math.max(1, Math.round(imageHeight * scale));
|
|
7700
|
+
return { x: Math.floor((cellWidth - width) / 2), y: cellHeight - height, width, height };
|
|
7701
|
+
}
|
|
7702
|
+
function packGrid(name, grid, images) {
|
|
7703
|
+
const sheet = new Uint8Array(grid.pxWidth * grid.pxHeight * 4);
|
|
7704
|
+
const cellWidth = grid.pxWidth / grid.cols;
|
|
7705
|
+
const cellHeight = grid.pxHeight / grid.rows;
|
|
7706
|
+
for (const [index, { cell, image }] of (grid.cells ?? []).entries()) {
|
|
7707
|
+
const fail = (message) => {
|
|
7708
|
+
throw new AtlasError("ATLAS_IMAGE", `/${name}/cells/${index}/image`, message);
|
|
7709
|
+
};
|
|
7710
|
+
const picture = readImage(image, images.get(image), fail);
|
|
7711
|
+
if (name === "fields" && picture.width * cellHeight !== picture.height * cellWidth) {
|
|
7712
|
+
fail(`the file ${image} is ${picture.width} × ${picture.height}: a field tile has the aspect of its cell, ${cellWidth} × ${cellHeight}`);
|
|
7713
|
+
}
|
|
7714
|
+
const rect = fitRect(cellWidth, cellHeight, picture.width, picture.height);
|
|
7715
|
+
const smaller = rect.width < picture.width || rect.height < picture.height;
|
|
7716
|
+
const scaled = smaller ? shrink(picture, rect.width, rect.height) : enlarge(picture, rect.width, rect.height);
|
|
7717
|
+
const left = cell % grid.cols * cellWidth + rect.x;
|
|
7718
|
+
const top = Math.floor(cell / grid.cols) * cellHeight + rect.y;
|
|
7719
|
+
for (let row = 0;row < rect.height; row++) {
|
|
7720
|
+
sheet.set(scaled.subarray(row * rect.width * 4, (row + 1) * rect.width * 4), ((top + row) * grid.pxWidth + left) * 4);
|
|
7721
|
+
}
|
|
7722
|
+
}
|
|
7723
|
+
return sheet;
|
|
7724
|
+
}
|
|
7725
|
+
function readImage(image, bytes, fail) {
|
|
7726
|
+
if (bytes === undefined)
|
|
7727
|
+
return fail(`the file ${image} was not read`);
|
|
7728
|
+
try {
|
|
7729
|
+
return decodePng(bytes);
|
|
7730
|
+
} catch (error) {
|
|
7731
|
+
if (!(error instanceof PngError))
|
|
7732
|
+
throw error;
|
|
7733
|
+
return fail(`the file ${image} ${error.message}`);
|
|
7734
|
+
}
|
|
7735
|
+
}
|
|
7736
|
+
function shrink(source, width, height) {
|
|
7737
|
+
const result = new Uint8Array(width * height * 4);
|
|
7738
|
+
for (let y = 0;y < height; y++) {
|
|
7739
|
+
const top = y * source.height / height;
|
|
7740
|
+
const bottom = (y + 1) * source.height / height;
|
|
7741
|
+
for (let x = 0;x < width; x++) {
|
|
7742
|
+
const left = x * source.width / width;
|
|
7743
|
+
const right = (x + 1) * source.width / width;
|
|
7744
|
+
let red = 0;
|
|
7745
|
+
let green = 0;
|
|
7746
|
+
let blue = 0;
|
|
7747
|
+
let alpha = 0;
|
|
7748
|
+
for (let row = Math.floor(top);row < bottom; row++) {
|
|
7749
|
+
const rowShare = Math.min(bottom, row + 1) - Math.max(top, row);
|
|
7750
|
+
for (let column = Math.floor(left);column < right; column++) {
|
|
7751
|
+
const area = rowShare * (Math.min(right, column + 1) - Math.max(left, column));
|
|
7752
|
+
const at = (row * source.width + column) * 4;
|
|
7753
|
+
const weight = area * source.rgba[at + 3];
|
|
7754
|
+
red += weight * source.rgba[at];
|
|
7755
|
+
green += weight * source.rgba[at + 1];
|
|
7756
|
+
blue += weight * source.rgba[at + 2];
|
|
7757
|
+
alpha += weight;
|
|
7758
|
+
}
|
|
7759
|
+
}
|
|
7760
|
+
if (alpha === 0)
|
|
7761
|
+
continue;
|
|
7762
|
+
const to = (y * width + x) * 4;
|
|
7763
|
+
result[to] = Math.round(red / alpha);
|
|
7764
|
+
result[to + 1] = Math.round(green / alpha);
|
|
7765
|
+
result[to + 2] = Math.round(blue / alpha);
|
|
7766
|
+
result[to + 3] = Math.round(alpha / ((right - left) * (bottom - top)));
|
|
7767
|
+
}
|
|
7768
|
+
}
|
|
7769
|
+
return result;
|
|
7770
|
+
}
|
|
7771
|
+
function enlarge(source, width, height) {
|
|
7772
|
+
const result = new Uint8Array(width * height * 4);
|
|
7773
|
+
for (let y = 0;y < height; y++) {
|
|
7774
|
+
const row = Math.floor((y + 0.5) * source.height / height);
|
|
7775
|
+
for (let x = 0;x < width; x++) {
|
|
7776
|
+
const column = Math.floor((x + 0.5) * source.width / width);
|
|
7777
|
+
const at = (row * source.width + column) * 4;
|
|
7778
|
+
result.set(source.rgba.subarray(at, at + 4), (y * width + x) * 4);
|
|
7779
|
+
}
|
|
7780
|
+
}
|
|
7781
|
+
return result;
|
|
7782
|
+
}
|
|
7783
|
+
|
|
7420
7784
|
// ../moveby-build/src/road.ts
|
|
7421
7785
|
var import__20202 = __toESM(require_2020(), 1);
|
|
7422
7786
|
// ../moveby-build/road.schema.json
|
|
@@ -8158,6 +8522,7 @@ function reasonOf(error) {
|
|
|
8158
8522
|
}
|
|
8159
8523
|
|
|
8160
8524
|
// ../moveby-build/src/build.ts
|
|
8525
|
+
var SHEET_FILES = { sprites: "sprites.png", fields: "fields.png" };
|
|
8161
8526
|
var ASSET_GROUP2 = "moveby.asset.";
|
|
8162
8527
|
async function build(inputs, compiler) {
|
|
8163
8528
|
const scene = await compileScene(inputs.scene, compiler);
|
|
@@ -8189,12 +8554,14 @@ async function build(inputs, compiler) {
|
|
|
8189
8554
|
} catch (error) {
|
|
8190
8555
|
throw new InputFault(engineRefusal(error, inputs));
|
|
8191
8556
|
}
|
|
8557
|
+
const sheets = atlasFile ? packSheets(atlasFile, atlas, inputs.images ?? new Map) : {};
|
|
8192
8558
|
return {
|
|
8193
8559
|
ok: true,
|
|
8194
8560
|
files: {
|
|
8195
8561
|
"track.bin": track,
|
|
8196
8562
|
"scene.wasm": scene.scene.bytes,
|
|
8197
|
-
"track.d.ts": scene.scene.types
|
|
8563
|
+
"track.d.ts": scene.scene.types,
|
|
8564
|
+
...sheets
|
|
8198
8565
|
},
|
|
8199
8566
|
read
|
|
8200
8567
|
};
|
|
@@ -8204,6 +8571,17 @@ async function build(inputs, compiler) {
|
|
|
8204
8571
|
return { ok: false, diagnostics: [error.diagnostic], read };
|
|
8205
8572
|
}
|
|
8206
8573
|
}
|
|
8574
|
+
function packSheets(atlasFile, atlas, images) {
|
|
8575
|
+
const sheets = {};
|
|
8576
|
+
for (const name of Object.keys(SHEET_FILES)) {
|
|
8577
|
+
const grid = atlas[name];
|
|
8578
|
+
if (grid?.cells === undefined)
|
|
8579
|
+
continue;
|
|
8580
|
+
const rgba = readPart(atlasFile, () => packGrid(name, grid, images));
|
|
8581
|
+
sheets[SHEET_FILES[name]] = encodePng(grid.pxWidth, grid.pxHeight, rgba);
|
|
8582
|
+
}
|
|
8583
|
+
return sheets;
|
|
8584
|
+
}
|
|
8207
8585
|
function missingPart(needs) {
|
|
8208
8586
|
const { scene, road, roadJson, atlas, files } = needs;
|
|
8209
8587
|
const need = (what, key, file, where) => ({
|
|
@@ -8294,9 +8672,10 @@ function diagnosticLine(diagnostic) {
|
|
|
8294
8672
|
// ../moveby-build/src/shell.ts
|
|
8295
8673
|
async function buildOnce(command, io, compiler) {
|
|
8296
8674
|
const started = performance.now();
|
|
8675
|
+
const folder = command.out ?? dirname(command.paths.scene);
|
|
8297
8676
|
let read;
|
|
8298
8677
|
try {
|
|
8299
|
-
read = await readInputs(command.paths, io.cwd);
|
|
8678
|
+
read = await readInputs(command.paths, folder, io.cwd);
|
|
8300
8679
|
} catch (error) {
|
|
8301
8680
|
if (!(error instanceof UsageError))
|
|
8302
8681
|
throw error;
|
|
@@ -8309,11 +8688,10 @@ async function buildOnce(command, io, compiler) {
|
|
|
8309
8688
|
const sceneFiles = result.read.flatMap((uri) => uri.startsWith("file:") ? [fileURLToPath(uri)] : []);
|
|
8310
8689
|
if (!result.ok)
|
|
8311
8690
|
return { code: 1, read: [...read.files, ...sceneFiles] };
|
|
8312
|
-
const folder = command.out ?? dirname(command.paths.scene);
|
|
8313
8691
|
await mkdir(folder, { recursive: true });
|
|
8314
|
-
|
|
8315
|
-
|
|
8316
|
-
|
|
8692
|
+
for (const [name, content] of Object.entries(result.files)) {
|
|
8693
|
+
await writeAtomically(join(folder, name), content);
|
|
8694
|
+
}
|
|
8317
8695
|
return { code: 0, read: [...read.files, ...sceneFiles] };
|
|
8318
8696
|
}
|
|
8319
8697
|
|
|
@@ -8326,7 +8704,7 @@ class UsageError extends Error {
|
|
|
8326
8704
|
this.files = files;
|
|
8327
8705
|
}
|
|
8328
8706
|
}
|
|
8329
|
-
async function readInputs(paths, cwd) {
|
|
8707
|
+
async function readInputs(paths, folder, cwd) {
|
|
8330
8708
|
const named = [paths.scene, paths.road, paths.atlas].filter((path) => path !== undefined);
|
|
8331
8709
|
const texts = new Map;
|
|
8332
8710
|
for (const path of named) {
|
|
@@ -8352,6 +8730,8 @@ async function readInputs(paths, cwd) {
|
|
|
8352
8730
|
}), 1, [...named, fontFile]);
|
|
8353
8731
|
}
|
|
8354
8732
|
}
|
|
8733
|
+
const beforeImages = [...named, ...fontFile === undefined ? [] : [fontFile]];
|
|
8734
|
+
const { images, files: imageFiles } = paths.atlas === undefined ? { images: new Map, files: [] } : await readImages(texts.get(paths.atlas), paths.atlas, folder, cwd, beforeImages);
|
|
8355
8735
|
return {
|
|
8356
8736
|
inputs: {
|
|
8357
8737
|
scene: {
|
|
@@ -8361,11 +8741,52 @@ async function readInputs(paths, cwd) {
|
|
|
8361
8741
|
},
|
|
8362
8742
|
road: file(paths.road),
|
|
8363
8743
|
atlas: paths.atlas === undefined ? undefined : file(paths.atlas),
|
|
8364
|
-
font
|
|
8744
|
+
font,
|
|
8745
|
+
images
|
|
8365
8746
|
},
|
|
8366
|
-
files: [...
|
|
8747
|
+
files: [...beforeImages, ...imageFiles]
|
|
8367
8748
|
};
|
|
8368
8749
|
}
|
|
8750
|
+
async function readImages(atlasText, atlasPath, folder, cwd, before) {
|
|
8751
|
+
const { written, named } = cellsOf(atlasText);
|
|
8752
|
+
const files = named.map((image) => resolve(dirname(atlasPath), image));
|
|
8753
|
+
const outputs = new Set(written.map((sheet) => join(folder, SHEET_FILES[sheet]).toLowerCase()));
|
|
8754
|
+
const images = new Map;
|
|
8755
|
+
for (const [index, image] of named.entries()) {
|
|
8756
|
+
const path = files[index];
|
|
8757
|
+
const fault = (message) => new UsageError(diagnosticLine({ code: "ATLAS_IMAGE", file: shown(path, cwd), message }), 1, [...before, ...files]);
|
|
8758
|
+
if (outputs.has(path.toLowerCase())) {
|
|
8759
|
+
throw fault("the build writes this file: give the source image another name, or use --out");
|
|
8760
|
+
}
|
|
8761
|
+
try {
|
|
8762
|
+
images.set(image, await readFile(path));
|
|
8763
|
+
} catch (error) {
|
|
8764
|
+
throw fault(`cannot read the file: ${reasonOf2(error)}`);
|
|
8765
|
+
}
|
|
8766
|
+
}
|
|
8767
|
+
return { images, files };
|
|
8768
|
+
}
|
|
8769
|
+
function cellsOf(atlasText) {
|
|
8770
|
+
let atlas;
|
|
8771
|
+
try {
|
|
8772
|
+
atlas = JSON.parse(atlasText);
|
|
8773
|
+
} catch {
|
|
8774
|
+
return { written: [], named: [] };
|
|
8775
|
+
}
|
|
8776
|
+
const written = [];
|
|
8777
|
+
const named = [];
|
|
8778
|
+
for (const sheet of Object.keys(SHEET_FILES)) {
|
|
8779
|
+
const cells = atlas?.[sheet]?.cells;
|
|
8780
|
+
if (!Array.isArray(cells))
|
|
8781
|
+
continue;
|
|
8782
|
+
written.push(sheet);
|
|
8783
|
+
for (const entry of cells) {
|
|
8784
|
+
if (typeof entry?.image === "string" && entry.image !== "")
|
|
8785
|
+
named.push(entry.image);
|
|
8786
|
+
}
|
|
8787
|
+
}
|
|
8788
|
+
return { written, named };
|
|
8789
|
+
}
|
|
8369
8790
|
function fontPathOf(atlasText) {
|
|
8370
8791
|
try {
|
|
8371
8792
|
const font = JSON.parse(atlasText).font;
|
|
Binary file
|