Allow for textures that aren't 16x16
This commit is contained in:
+42
-11
@@ -2,6 +2,7 @@
|
||||
import { assert, assertEquals, assertRejects, assertStringIncludes, assertThrows } from "@std/assert";
|
||||
import { zipSync } from "fflate";
|
||||
import { atlas_layout } from "$/client/atlas.ts";
|
||||
import { MAX_TEXTURE_SIZE } from "$/common/constants.ts";
|
||||
import { read_bmod, write_bmod } from "$/common/bmod.ts";
|
||||
import { load_and_check } from "$/tools/check_mods.ts";
|
||||
import { pack_mod } from "$/tools/pack_mod.ts";
|
||||
@@ -79,16 +80,19 @@ Deno.test("broken .bmod files are turned away, saying why", () => {
|
||||
Error,
|
||||
"scripts.server scripts/server.js isn't in the file",
|
||||
);
|
||||
// a texture that isn't 16x16
|
||||
const ui = Deno.readFileSync("assets/sprites/gui.png");
|
||||
// a texture bigger than MAX_TEXTURE_SIZE. only its header is read
|
||||
const big = new Uint8Array(24);
|
||||
big.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
||||
new DataView(big.buffer).setUint32(16, MAX_TEXTURE_SIZE * 2);
|
||||
new DataView(big.buffer).setUint32(20, 16);
|
||||
assertThrows(
|
||||
() =>
|
||||
read_bmod(
|
||||
zip({ "manifest.json": text(manifest), "data.json": text({}), "textures/big.png": ui }),
|
||||
zip({ "manifest.json": text(manifest), "data.json": text({}), "textures/big.png": big }),
|
||||
"e.bmod",
|
||||
),
|
||||
Error,
|
||||
"textures must be 16×16",
|
||||
`textures can be at most ${MAX_TEXTURE_SIZE}×${MAX_TEXTURE_SIZE}`,
|
||||
);
|
||||
// too much once unzipped, without unzipping it all
|
||||
const huge = new Uint8Array(65 * 1024 * 1024);
|
||||
@@ -193,16 +197,43 @@ Deno.test("a mod with its own deno.json is bundled with it", async () => {
|
||||
Deno.removeSync(sources, { recursive: true });
|
||||
});
|
||||
|
||||
Deno.test("the atlas puts the missing texture first and the rest in order", () => {
|
||||
const layout = atlas_layout(["b:two", "a:one", "engine:break_0", "a:one"]);
|
||||
const sizes = (entries: [string, number, number][]) =>
|
||||
new Map(entries.map(([id, width, height]) => [id, { width, height }]));
|
||||
|
||||
Deno.test("the atlas lays out same sized textures by id, with the missing texture among them", () => {
|
||||
const layout = atlas_layout(sizes([["b:two", 16, 16], ["a:one", 16, 16], ["engine:break_0", 16, 16]]));
|
||||
assertEquals(layout.size, 32);
|
||||
assertEquals(layout.regions, {
|
||||
"engine:missing": { x: 0, y: 0 },
|
||||
"a:one": { x: 1, y: 0 },
|
||||
"b:two": { x: 0, y: 1 },
|
||||
"engine:break_0": { x: 1, y: 1 },
|
||||
"a:one": { x: 0, y: 0, width: 16, height: 16 },
|
||||
"b:two": { x: 16, y: 0, width: 16, height: 16 },
|
||||
"engine:break_0": { x: 0, y: 16, width: 16, height: 16 },
|
||||
"engine:missing": { x: 16, y: 16, width: 16, height: 16 },
|
||||
});
|
||||
assertEquals(atlas_layout(Array.from({ length: 100 }, (_, i) => `m:t${i}`)).size, 256);
|
||||
const many = atlas_layout(sizes(Array.from({ length: 100 }, (_, i) => [`m:t${i}`, 16, 16])));
|
||||
assertEquals(many.size, 256);
|
||||
});
|
||||
|
||||
Deno.test("the atlas fits textures of any size, tallest first, without overlapping", () => {
|
||||
const layout = atlas_layout(
|
||||
sizes([["a:block", 16, 16], ["a:zombie", 64, 64], ["a:boat", 128, 32], ["a:x", 8, 24]]),
|
||||
);
|
||||
assertEquals(layout.size, 128);
|
||||
assertEquals(layout.regions["a:zombie"], { x: 0, y: 0, width: 64, height: 64 });
|
||||
assertEquals(layout.regions["a:boat"], { x: 0, y: 64, width: 128, height: 32 });
|
||||
assertEquals(layout.regions["a:x"], { x: 0, y: 96, width: 8, height: 24 });
|
||||
|
||||
const big = atlas_layout(
|
||||
sizes(Array.from({ length: 40 }, (_, i) => [`m:t${i}`, 16 + (i % 5) * 12, 8 + (i % 7) * 9])),
|
||||
);
|
||||
const regions = Object.values(big.regions);
|
||||
for (const [i, a] of regions.entries()) {
|
||||
assert(a.x >= 0 && a.y >= 0 && a.x + a.width <= big.size && a.y + a.height <= big.size);
|
||||
for (const b of regions.slice(i + 1)) {
|
||||
const apart = a.x + a.width <= b.x || b.x + b.width <= a.x || a.y + a.height <= b.y ||
|
||||
b.y + b.height <= a.y;
|
||||
assert(apart, `${JSON.stringify(a)} overlaps ${JSON.stringify(b)}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// write_bmod without anything optional still makes a file read_bmod takes
|
||||
|
||||
@@ -3,10 +3,10 @@ import { assert, assertEquals } from "@std/assert";
|
||||
import { extrude_item, type ItemQuad } from "$/client/rendering/item_model.ts";
|
||||
import { TEXTURE_SIZE } from "$/common/constants.ts";
|
||||
|
||||
function mask(pixels: [number, number][]) {
|
||||
const mask = new Uint8Array(TEXTURE_SIZE * TEXTURE_SIZE);
|
||||
for (const [x, y] of pixels) mask[y * TEXTURE_SIZE + x] = 1;
|
||||
return mask;
|
||||
function mask(pixels: [number, number][], width = TEXTURE_SIZE, height = TEXTURE_SIZE) {
|
||||
const mask = new Uint8Array(width * height);
|
||||
for (const [x, y] of pixels) mask[y * width + x] = 1;
|
||||
return { width, height, mask };
|
||||
}
|
||||
|
||||
// each quad's corners go counter clockwise seen from outside, or it's culled from the side it should show
|
||||
@@ -32,7 +32,11 @@ Deno.test("only edges next to nothing get a side, and see-through pixels get non
|
||||
// two pixels side by side share an edge, so 6 sides instead of 8
|
||||
assertEquals(extrude_item(mask([[0, 0], [1, 0]])).length, 2 + 6);
|
||||
// a full sprite only has its border
|
||||
const full = extrude_item(new Uint8Array(TEXTURE_SIZE * TEXTURE_SIZE).fill(1));
|
||||
const full = extrude_item({
|
||||
width: TEXTURE_SIZE,
|
||||
height: TEXTURE_SIZE,
|
||||
mask: new Uint8Array(TEXTURE_SIZE * TEXTURE_SIZE).fill(1),
|
||||
});
|
||||
assertEquals(full.length, 2 + 4 * TEXTURE_SIZE);
|
||||
// nothing to see, only the (see-through) front and back
|
||||
assertEquals(extrude_item(mask([])).length, 2);
|
||||
@@ -47,3 +51,17 @@ Deno.test("side faces show their pixel's color, and the model is one pixel thick
|
||||
for (const [, , z] of side.corners) assertEquals(Math.abs(z), 0.5 / TEXTURE_SIZE);
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("a bigger sprite has smaller pixels, and is as thick as a 16 pixel one", () => {
|
||||
const [front, , ...sides] = extrude_item(mask([[31, 0]], 32, 64));
|
||||
// the front covers the whole sprite
|
||||
assert(front.uvs.some(([u, v]) => u > 31 && v > 63));
|
||||
// the top right pixel: 1/32 across and 1/64 down
|
||||
for (const side of sides) {
|
||||
for (const [x, y, z] of side.corners) {
|
||||
assert(x >= 0.5 - 1 / 32 - 1e-9 && x <= 0.5 + 1e-9, `x ${x}`);
|
||||
assert(y >= 1 - 1 / 64 - 1e-9 && y <= 1 + 1e-9, `y ${y}`);
|
||||
assertEquals(Math.abs(z), 0.5 / TEXTURE_SIZE);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { assert, assertEquals, assertThrows } from "@std/assert";
|
||||
import { check_mods } from "$/tools/check_mods.ts";
|
||||
import { MAX_TEXTURE_SIZE } from "$/common/constants.ts";
|
||||
import { create_mod } from "$/tools/new_mod.ts";
|
||||
|
||||
function temp_mods_dir() {
|
||||
@@ -92,7 +93,11 @@ Deno.test("check-mods finds broken mods", async () => {
|
||||
},
|
||||
});
|
||||
// wrong texture size
|
||||
Deno.copyFileSync("assets/sprites/gui.png", `${mod}/textures/huge.png`);
|
||||
const huge = new Uint8Array(24);
|
||||
huge.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
||||
new DataView(huge.buffer).setUint32(16, MAX_TEXTURE_SIZE + 1);
|
||||
new DataView(huge.buffer).setUint32(20, 16);
|
||||
Deno.writeFileSync(`${mod}/textures/huge.png`, huge);
|
||||
// a script that doesn't typecheck
|
||||
Deno.writeTextFileSync(`${mod}/scripts/client.ts`, "export function setup(ctx: number) { ctx.nope(); }\n");
|
||||
// a dependency that isn't installed, and no longer depending on bworld
|
||||
|
||||
@@ -53,7 +53,7 @@ async function mesh_chunks(radius: number) {
|
||||
type: "init",
|
||||
blocks_registry: JSON.parse(JSON.stringify(blocks)),
|
||||
models: {},
|
||||
textures_info: { "engine:missing": { x: 0, y: 0 } },
|
||||
textures_info: { "engine:missing": { x: 0, y: 0, width: 16, height: 16 } },
|
||||
image: { width: 1024, height: 1024 },
|
||||
});
|
||||
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ async function mesh_block(id: string, x: number, middle: Colors, west: Colors |
|
||||
type: "init",
|
||||
blocks_registry: JSON.parse(JSON.stringify(blocks)),
|
||||
models: {},
|
||||
textures_info: { "engine:missing": { x: 0, y: 0 } },
|
||||
textures_info: { "engine:missing": { x: 0, y: 0, width: 16, height: 16 } },
|
||||
image: { width: 1024, height: 1024 },
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user