diff --git a/tests/bmod_test.ts b/tests/bmod_test.ts index ac3fb65..bf2d712 100644 --- a/tests/bmod_test.ts +++ b/tests/bmod_test.ts @@ -1,7 +1,7 @@ // .bmod files: packing mods, reading them back, and the server loading a folder of them import { assert, assertEquals, assertRejects, assertStringIncludes, assertThrows } from "@std/assert"; import { zipSync } from "fflate"; -import { atlas_layout } from "$/client/atlas.ts"; +import { ATLAS_BORDER, atlas_layout, extrude_borders } 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"; @@ -203,12 +203,13 @@ const sizes = (entries: [string, number, number][]) => 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.size, 64); + // each inside a pixel of border assertEquals(layout.regions, { - "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 }, + "a:one": { x: 1, y: 1, width: 16, height: 16 }, + "b:two": { x: 19, y: 1, width: 16, height: 16 }, + "engine:break_0": { x: 37, y: 1, width: 16, height: 16 }, + "engine:missing": { x: 1, y: 19, width: 16, height: 16 }, }); const many = atlas_layout(sizes(Array.from({ length: 100 }, (_, i) => [`m:t${i}`, 16, 16]))); assertEquals(many.size, 256); @@ -218,25 +219,42 @@ Deno.test("the atlas fits textures of any size, tallest first, without overlappi 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 }); + assertEquals(layout.size, 256); + assertEquals(layout.regions["a:zombie"], { x: 1, y: 1, width: 64, height: 64 }); + assertEquals(layout.regions["a:boat"], { x: 67, y: 1, width: 128, height: 32 }); + assertEquals(layout.regions["a:x"], { x: 197, y: 1, 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); + // with their borders + const b = ATLAS_BORDER; 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)}`); + assert(a.x >= b && a.y >= b && a.x + a.width + b <= big.size && a.y + a.height + b <= big.size); + for (const c of regions.slice(i + 1)) { + const apart = a.x + a.width + 2 * b <= c.x || c.x + c.width + 2 * b <= a.x || + a.y + a.height + 2 * b <= c.y || c.y + c.height + 2 * b <= a.y; + assert(apart, `${JSON.stringify(a)} overlaps ${JSON.stringify(c)}`); } } }); +Deno.test("each texture's border in the atlas repeats its edge, so lookups at the edge never reach the next one", () => { + // a 2×2 texture at 1 1 in a 4×4 atlas, pixels numbered 1 to 4 in the red channel + const pixels = new Uint8ClampedArray(4 * 4 * 4); + const red = (x: number, y: number) => pixels[(y * 4 + x) * 4]; + [[1, 1, 1], [2, 1, 2], [1, 2, 3], [2, 2, 4]].forEach(([x, y, n]) => pixels[(y * 4 + x) * 4] = n); + extrude_borders(pixels, 4, { "a:t": { x: 1, y: 1, width: 2, height: 2 } }); + const rows = [0, 1, 2, 3].map((y) => [0, 1, 2, 3].map((x) => red(x, y))); + assertEquals(rows, [ + [1, 1, 2, 2], + [1, 1, 2, 2], + [3, 3, 4, 4], + [3, 3, 4, 4], + ]); +}); + // write_bmod without anything optional still makes a file read_bmod takes Deno.test("a mod with only data packs and reads", () => { const bmod = {