// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // blocks with a "tint" are drawn in their biome's grass or foliage color, blended across biome edges, or a fixed one. water is its biome's water color import { assert, assertEquals } from "@std/assert"; import { BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts"; import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts"; import { DEFAULT_SHAPE } from "@/common/world_shape.ts"; import { generate_raw_chunk } from "@/common/generation.ts"; import { get_biome } from "@/common/worldgen/biomes.ts"; import { TERRAIN_VERTEX_BYTES } from "@/client/workers/chunk_messages.ts"; import { chunk_worker, mesh_column, test_game } from "./helpers.ts"; type Colors = { grass: number; foliage: number; water?: number }; // one block at x, 10, 8 of the middle chunk, meshed with these biome colors for the middle chunk and the one to its // -x side (the others aren't there). returns the colors of the quads on its top and on its sides async function mesh_block(id: string, x: number, middle: Colors, west: Colors | null) { await test_game("mods"); const blocks = EverythingRegistry.get_registry("blocks"); const block_ids: Record = {}; blocks.forEach((b, nid) => block_ids[b.id] = nid); const worker = chunk_worker(DEFAULT_SHAPE); const chunk = new Uint32Array(DEFAULT_SHAPE.volume); chunk[10 * CHUNK_AREA + 8 * CHUNK_SIZE + x] = block_ids[id]; const empty = () => new Uint32Array(DEFAULT_SHAPE.volume); const chunks = [empty(), empty(), empty(), empty(), chunk, empty(), empty(), empty(), empty()]; const colors = chunks.map(() => null as Uint32Array | null); // the colors of each chunk's one biome const biome_colors = ({ grass, foliage, water = 0 }: Colors) => Uint32Array.of(grass, foliage, water); colors[4] = biome_colors(middle); if (west) colors[3] = biome_colors(west); const meshed = await mesh_column(worker, DEFAULT_SHAPE, chunks, { biome_colors: colors }); worker.terminate(); const top: number[][] = []; const sides: number[][] = []; for (const mesh of [meshed.solid, meshed.cutout, meshed.translucent]) { const f32 = new Float32Array(mesh.vertices.buffer); for (let quad = 0; quad < mesh.quad_count; quad++) { const byte = quad * 4 * TERRAIN_VERTEX_BYTES; const ys = [0, 1, 2, 3].map((k) => f32[(byte + k * TERRAIN_VERTEX_BYTES) / 4 + 1]); const rgb = [...mesh.vertices.subarray(byte + 16, byte + 19)]; if (ys.every((y) => y === 11)) top.push(rgb); else if (!ys.every((y) => y === 10)) sides.push(rgb); } } return { top, sides }; } // the vertex color of an unlit face: the color times the face's directional shade, 1 on top and 0.8 north and south const shaded = (color: number, shade: number) => [color >> 16, (color >> 8) & 0xff, color & 0xff].map((c) => Math.round(shade * c)); Deno.test("the grass block's top is its biome's grass color, and only the top", async () => { const { top, sides } = await mesh_block("bworld:grass_block", 8, { grass: 0xff8000, foliage: 0x0000ff }, null); assertEquals(top, [[255, 128, 0]]); assertEquals(sides.length, 4); for (const [r, g, b] of sides) assert(r === g && g === b, "sides aren't tinted"); }); Deno.test("biome colors blend over the 5x5 columns around, across chunk edges", async () => { // at x 0 the two columns to its -x are in the other chunk: 10 of the 25 are blue, 15 red const { top } = await mesh_block("bworld:grass_block", 0, { grass: 0xff0000, foliage: 0 }, { grass: 0x0000ff, foliage: 0, }); assertEquals(top, [[153, 0, 102]]); }); Deno.test("oak leaves are the biome's foliage color on every side, birch a fixed green, spruce isn't tinted", async () => { const colors = { grass: 0xff0000, foliage: 0x00ff80 }; const oak = await mesh_block("bworld:oak_leaves", 8, colors, null); assertEquals(oak.top, [[0, 255, 128]]); assert(oak.sides.some((rgb) => JSON.stringify(rgb) === JSON.stringify(shaded(0x00ff80, 0.8)))); const birch = await mesh_block("bworld:birch_leaves", 8, colors, null); assertEquals(birch.top, [shaded(0x80a755, 1)]); const spruce = await mesh_block("bworld:spruce_leaves", 8, colors, null); assertEquals(spruce.top, [[255, 255, 255]]); }); Deno.test("water is its biome's water color", async () => { const { sides } = await mesh_block( "bworld:water", 8, { grass: 0xff0000, foliage: 0x00ff00, water: 0x2040ff }, null, ); assert(sides.some((rgb) => JSON.stringify(rgb) === JSON.stringify(shaded(0x2040ff, 0.8)))); }); Deno.test("every column the overworld generates has a biome with grass, foliage and water colors", async () => { const { game } = await test_game("mods"); const blocks = EverythingRegistry.get_registry("blocks"); const block_ids: Record = {}; blocks.forEach((b, nid) => block_ids[b.id] = nid); for (const [x, z] of [[0, 0], [30, -12], [-80, 45]]) { const { biomes } = generate_raw_chunk(x, z, game.world.seed, block_ids, game.world.worldgen).column; for (const biome of biomes.palette) { const { colors } = get_biome(biome)!; assert(colors.grass !== undefined, `${biome} has no grass color`); assert(colors.foliage !== undefined, `${biome} has no foliage color`); assert(colors.water !== undefined, `${biome} has no water color`); } } });