Compare commits

...
3 Commits
Author SHA1 Message Date
paula 0376b1ab1f Basic sprinkler 2026-10-10 19:11:32 -03:00
paula 88a3c9c92d Test for the fix 2026-10-10 19:07:58 -03:00
paula 48f5b80776 Fix old rendering hack 2026-10-10 19:07:51 -03:00
7 changed files with 454 additions and 42 deletions
+45 -8
View File
@@ -17,6 +17,10 @@ export interface TextureSize {
height: number;
}
// pixels around each texture, copies of its edge, so a face whose uvs reach the texture's edge never shows a bit of
// the next texture over, however the lookup rounds. see extrude_borders
export const ATLAS_BORDER = 1;
// shelves, tallest first so each row wastes little, and by id when they're the same size so the same textures always
// give the same atlas. engine:missing is 16×16 and always there
export function atlas_layout(sizes: Map<string, TextureSize>): AtlasLayout {
@@ -24,8 +28,11 @@ export function atlas_layout(sizes: Map<string, TextureSize>): AtlasLayout {
entries.push(["engine:missing", { width: TEXTURE_SIZE, height: TEXTURE_SIZE }]);
entries.sort(([a, sa], [b, sb]) => sb.height - sa.height || sb.width - sa.width || a.localeCompare(b));
const area = entries.reduce((sum, [, { width, height }]) => sum + width * height, 0);
const widest = Math.max(...entries.map(([, { width }]) => width));
const area = entries.reduce(
(sum, [, { width, height }]) => sum + (width + 2 * ATLAS_BORDER) * (height + 2 * ATLAS_BORDER),
0,
);
const widest = Math.max(...entries.map(([, { width }]) => width + 2 * ATLAS_BORDER));
let size = 2 ** Math.ceil(Math.log2(Math.max(Math.sqrt(area), widest)));
while (true) {
const regions = pack_shelves(entries, size);
@@ -34,21 +41,22 @@ export function atlas_layout(sizes: Map<string, TextureSize>): AtlasLayout {
}
}
// undefined if they don't fit in a size × size square
// undefined if they don't fit in a size × size square. regions are the textures themselves, inside their border
function pack_shelves(entries: [string, TextureSize][], size: number) {
const regions: Record<string, SpriteRegion> = {};
let x = 0;
let y = 0;
let shelf = 0;
for (const [id, { width, height }] of entries) {
if (x + width > size) {
const [outer_width, outer_height] = [width + 2 * ATLAS_BORDER, height + 2 * ATLAS_BORDER];
if (x + outer_width > size) {
x = 0;
y += shelf;
shelf = 0;
}
regions[id] = { x, y, width, height };
x += width;
shelf = Math.max(shelf, height);
regions[id] = { x: x + ATLAS_BORDER, y: y + ATLAS_BORDER, width, height };
x += outer_width;
shelf = Math.max(shelf, outer_height);
}
return y + shelf <= size ? regions : undefined;
}
@@ -110,9 +118,12 @@ export async function build_atlas(textures: Map<string, Uint8Array>, texture_jso
regions[id] ??= missing;
}
// which pixels of each texture can be seen, for the 3d models of dropped items (see item_model.ts)
const image_data = ctx.getImageData(0, 0, size, size);
const pixels = image_data.data;
extrude_borders(pixels, size, regions);
ctx.putImageData(image_data, 0, 0);
// which pixels of each texture can be seen, for the 3d models of dropped items (see item_model.ts)
built_atlas = image_data;
opaque.clear();
for (const [id, region] of Object.entries(regions)) {
@@ -129,6 +140,32 @@ export async function build_atlas(textures: Map<string, Uint8Array>, texture_jso
return { image: canvas.transferToImageBitmap(), regions, animated };
}
// fills each region's border with copies of its edge pixels, the corners with its corners. pixels is a size × size
// rgba atlas. an animated texture's border keeps its first frame's edge, only rounding ever shows it
export function extrude_borders(pixels: Uint8ClampedArray, size: number, regions: Record<string, SpriteRegion>) {
const copy = (from_x: number, from_y: number, to_x: number, to_y: number) => {
if (to_x < 0 || to_y < 0 || to_x >= size || to_y >= size) return;
const from = (from_y * size + from_x) * 4;
const to = (to_y * size + to_x) * 4;
for (let c = 0; c < 4; c++) pixels[to + c] = pixels[from + c];
};
// the missing texture fills in for unknown ids too, once is enough
for (const { x, y, width, height } of new Set(Object.values(regions))) {
for (let b = 1; b <= ATLAS_BORDER; b++) {
for (let px = -b; px < width + b; px++) {
const from_x = x + Math.min(Math.max(px, 0), width - 1);
copy(from_x, y, x + px, y - b);
copy(from_x, y + height - 1, x + px, y + height - 1 + b);
}
for (let py = -b; py < height + b; py++) {
const from_y = y + Math.min(Math.max(py, 0), height - 1);
copy(x, from_y, x - b, y + py);
copy(x + width - 1, from_y, x + width - 1 + b, y + py);
}
}
}
}
// every frame of an animated texture stays in memory, to copy into the atlas when it's that frame's turn
function image_pixels(image: ImageBitmap) {
const ctx = new OffscreenCanvas(image.width, image.height).getContext("2d")!;
+8 -11
View File
@@ -26,8 +26,6 @@ import type { Vec3 } from "./player_renderer.ts";
// how big items on the ground are drawn, minecraft's ground transform
const BLOCK_SCALE = 0.25;
const ITEM_SCALE = 0.5;
// keeps texture lookups off the sprite's edge
const UV_PAD = 0.5;
// each face's corners in drawing order (counter clockwise from outside) on a unit cube, with its shade.
// top, bottom, front, back, left, right, like the chunk mesher
@@ -254,22 +252,21 @@ export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransf
}
}
// where a model's uv in pixels (0-16) is in the atlas, kept off the sprite's edge
// where a model's uv in pixels (0-16) is in the atlas. the atlas's border around each sprite (see ATLAS_BORDER) keeps
// lookups that round past its edge on its own pixels
export function sprite_u(atlas: Texture, region: SpriteRegion, pixels: number) {
const t = Math.min(Math.max(pixels / TEXTURE_SIZE * region.width, UV_PAD), region.width - UV_PAD);
return (region.x + t) / atlas.width;
return (region.x + pixels / TEXTURE_SIZE * region.width) / atlas.width;
}
export function sprite_v(atlas: Texture, region: SpriteRegion, pixels: number) {
const t = Math.min(Math.max(pixels / TEXTURE_SIZE * region.height, UV_PAD), region.height - UV_PAD);
return (region.y + t) / atlas.height;
return (region.y + pixels / TEXTURE_SIZE * region.height) / atlas.height;
}
function push_textured_quad(atlas: Texture, region: SpriteRegion, corners: number[][], color: number[]) {
const u0 = (region.x + UV_PAD) / atlas.width;
const v0 = (region.y + UV_PAD) / atlas.height;
const u1 = (region.x + region.width - UV_PAD) / atlas.width;
const v1 = (region.y + region.height - UV_PAD) / atlas.height;
const u0 = region.x / atlas.width;
const v0 = region.y / atlas.height;
const u1 = (region.x + region.width) / atlas.width;
const v1 = (region.y + region.height) / atlas.height;
for (const i of [0, 1, 2, 0, 2, 3]) {
const [px, py, pz] = corners[i];
+4 -8
View File
@@ -57,9 +57,6 @@ import { liquid_height, liquid_textures } from "$/common/everything_registry.ts"
type TexturesInfo = Record<string, SpriteRegion>;
// keeps texture lookups off the sprite's edge
const UV_PAD = 0.5;
// same order as FACE_NORMALS in translucent_sort.ts, and FACE_CORNERS and CORNER_UVS in block_models.ts
// minecraft's shading by direction, so faces stay apart even in flat light
const FACE_SHADE = [1.0, 0.5, 0.8, 0.8, 0.6, 0.6];
@@ -437,15 +434,14 @@ function push_quad(
buffer.count += 1;
}
// model uvs (0 to TEXTURE_SIZE across the sprite, whatever its size) to atlas coordinates, kept off the sprite's edge
// model uvs (0 to TEXTURE_SIZE across the sprite, whatever its size) to atlas coordinates. the atlas's border around
// each sprite (see ATLAS_BORDER) keeps lookups that round past its edge on its own pixels
function atlas_u(sprite: SpriteRegion, u: number) {
const px = u * sprite.width / TEXTURE_SIZE;
return (sprite.x + Math.min(Math.max(px, UV_PAD), sprite.width - UV_PAD)) / image.width;
return (sprite.x + u * sprite.width / TEXTURE_SIZE) / image.width;
}
function atlas_v(sprite: SpriteRegion, v: number) {
const px = v * sprite.height / TEXTURE_SIZE;
return (sprite.y + Math.min(Math.max(px, UV_PAD), sprite.height - UV_PAD)) / image.height;
return (sprite.y + v * sprite.height / TEXTURE_SIZE) / image.height;
}
// the model quads for a block value, baked the first time it's seen
+16
View File
@@ -0,0 +1,16 @@
{
"format_version": 1,
"block": {
"id": "bworld:basic_sprinkler",
"name": "Basic Sprinkler",
"textures": "bworld:basic_sprinkler",
"model": "bworld:basic_sprinkler",
"item_model": "bworld:basic_sprinkler",
"render_layer": "cutout",
"mining": {
"toughness": 1.5,
"tool": "pickaxe"
},
"drops": "bworld:basic_sprinkler"
}
}
+348
View File
@@ -0,0 +1,348 @@
{
"format_version": 1,
"model": {
"id": "bworld:basic_sprinkler",
"elements": [
{
"from": [
7,
0,
7
],
"to": [
9,
3,
9
],
"faces": {
"north": {
"texture": "#texture",
"uv": [
4,
6,
6,
9
]
},
"east": {
"texture": "#texture",
"uv": [
6,
6,
8,
9
]
},
"south": {
"texture": "#texture",
"uv": [
8,
0,
10,
3
]
},
"west": {
"texture": "#texture",
"uv": [
8,
3,
10,
6
]
},
"top": {
"texture": "#texture",
"uv": [
2,
9,
4,
11
]
},
"bottom": {
"texture": "#texture",
"uv": [
4,
9,
6,
11
]
}
}
},
{
"from": [
7,
4,
7
],
"to": [
9,
5,
9
],
"faces": {
"north": {
"texture": "#texture",
"uv": [
10,
6,
12,
7
]
},
"east": {
"texture": "#texture",
"uv": [
10,
7,
12,
8
]
},
"south": {
"texture": "#texture",
"uv": [
10,
8,
12,
9
]
},
"west": {
"texture": "#texture",
"uv": [
10,
9,
12,
10
]
},
"top": {
"texture": "#texture",
"uv": [
6,
9,
8,
11
]
},
"bottom": {
"texture": "#texture",
"uv": [
8,
9,
10,
11
]
}
}
},
{
"from": [
4,
3,
7
],
"to": [
12,
4,
9
],
"faces": {
"north": {
"texture": "#texture",
"uv": [
0,
4,
8,
5
]
},
"east": {
"texture": "#texture",
"uv": [
10,
10,
12,
11
]
},
"south": {
"texture": "#texture",
"uv": [
0,
5,
8,
6
]
},
"west": {
"texture": "#texture",
"uv": [
2,
11,
4,
12
]
},
"top": {
"texture": "#texture",
"uv": [
0,
0,
8,
2
]
},
"bottom": {
"texture": "#texture",
"uv": [
0,
2,
8,
4
]
}
}
},
{
"from": [
7,
3,
4
],
"to": [
9,
4,
7
],
"faces": {
"north": {
"texture": "#texture",
"uv": [
4,
11,
6,
12
]
},
"east": {
"texture": "#texture",
"uv": [
10,
2,
13,
3
]
},
"south": {
"texture": "#texture",
"uv": [
6,
11,
8,
12
]
},
"west": {
"texture": "#texture",
"uv": [
10,
3,
13,
4
]
},
"top": {
"texture": "#texture",
"uv": [
8,
6,
10,
9
]
},
"bottom": {
"texture": "#texture",
"uv": [
0,
9,
2,
12
]
}
}
},
{
"from": [
7,
3,
9
],
"to": [
9,
4,
12
],
"faces": {
"north": {
"texture": "#texture",
"uv": [
10,
4,
12,
5
]
},
"east": {
"texture": "#texture",
"uv": [
10,
0,
13,
1
]
},
"south": {
"texture": "#texture",
"uv": [
10,
5,
12,
6
]
},
"west": {
"texture": "#texture",
"uv": [
10,
1,
13,
2
]
},
"top": {
"texture": "#texture",
"uv": [
0,
6,
2,
9
]
},
"bottom": {
"texture": "#texture",
"uv": [
2,
6,
4,
9
]
}
}
}
]
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 421 B

+33 -15
View File
@@ -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 = {