diff --git a/client/level/client_level.ts b/client/level/client_level.ts index 393e6da..6c286fa 100644 --- a/client/level/client_level.ts +++ b/client/level/client_level.ts @@ -1,7 +1,7 @@ import { block_value, chunk_key, default_block_value } from "$/common/utils.ts"; import { AIR_ID } from "$/common/protocol.ts"; import { decode_level_chunk } from "$/common/chunk_streaming.ts"; -import { foliage_color, grass_color } from "$/common/worldgen/biome_colors.ts"; +import { foliage_color, grass_color, water_color } from "$/common/worldgen/biome_colors.ts"; import { block_light_emission, block_light_opacity, @@ -45,7 +45,7 @@ export interface Chunk { max_y: number; // only for translucent meshes that have to be sorted again as the camera moves translucent_sort?: TranslucentSort; - // each column's biome grass color, x then z, then each column's foliage color + // each column's biome grass color, x then z, then each column's foliage color, then water color biome_colors: Uint32Array; // from its last mesh, see get_light. undefined until it's been meshed light?: Uint8Array; @@ -171,10 +171,11 @@ export class ClientLevel { if (!this.#in_storage_range(x, z)) { return; } - const biome_colors = new Uint32Array(CHUNK_AREA * 2); + const biome_colors = new Uint32Array(CHUNK_AREA * 3); biomes.forEach((biome, column) => { biome_colors[column] = grass_color(biome); biome_colors[CHUNK_AREA + column] = foliage_color(biome); + biome_colors[CHUNK_AREA * 2 + column] = water_color(biome); }); const chunk = this.get_chunk(x, z); diff --git a/client/workers/chunk_worker.ts b/client/workers/chunk_worker.ts index 812c328..0acc778 100644 --- a/client/workers/chunk_worker.ts +++ b/client/workers/chunk_worker.ts @@ -113,7 +113,7 @@ interface MeshQuad { sprite: SpriteRegion; // 4 weights per corner light_weights: number[]; - // 0xRRGGBB it's drawn in, GRASS or FOLIAGE for the biome's (see "tint"), or white + // 0xRRGGBB it's drawn in, GRASS, FOLIAGE or WATER for the biome's (see "tint"), or white tint: number; } @@ -207,17 +207,18 @@ function post(message: FromChunkWorker, transfer: Transferable[]) { self.postMessage(message, transfer); } -// like minecraft's biome blend at its default radius: each column's grass and foliage color is the average of the 5x5 +// like minecraft's biome blend at its default radius: each column's grass, foliage and water color is the average of the 5x5 // columns around it, so biome edges fade instead of being a line. neighbors that aren't there are left out const BLEND_RADIUS = 2; -// the middle chunk's blended colors, x then z, grass then foliage -const blended = new Uint32Array(CHUNK_AREA * 2); +// the middle chunk's blended colors, x then z, grass then foliage then water +const blended = new Uint32Array(CHUNK_AREA * 3); // what MeshQuad.tint is for the biome's colors, they aren't colors since those only go up to 0xffffff const GRASS = -1; const FOLIAGE = -2; +const WATER = -3; function blend_biome_colors(colors: (Uint32Array | null)[]) { - for (const offset of [0, CHUNK_AREA]) { + for (const offset of [0, CHUNK_AREA, CHUNK_AREA * 2]) { for (let z = 0; z < CHUNK_SIZE; z++) { for (let x = 0; x < CHUNK_SIZE; x++) { let r = 0; @@ -253,6 +254,7 @@ function quad_tint(block: BlockRegistry, variables: string[]) { const color = block.tint!.color; if (color === "grass") return GRASS; if (color === "foliage") return FOLIAGE; + if (color === "water") return WATER; return parseInt(color.slice(1), 16); } @@ -610,6 +612,8 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array ? blended[z * CHUNK_SIZE + x] : quad.tint === FOLIAGE ? blended[CHUNK_AREA + z * CHUNK_SIZE + x] + : quad.tint === WATER + ? blended[CHUNK_AREA * 2 + z * CHUNK_SIZE + x] : quad.tint; if (layer_id !== TRANSLUCENT) { diff --git a/common/mod_data.ts b/common/mod_data.ts index e7966b9..ea741aa 100644 --- a/common/mod_data.ts +++ b/common/mod_data.ts @@ -624,7 +624,7 @@ export function validate_block(json: unknown): Problems { if (json.tint !== undefined) { const tint = json.tint; if (!is_object(tint) || !is_tint_color(tint.color)) { - problems.push('tint.color must be "grass", "foliage" or a color like "#80a755"'); + problems.push('tint.color must be "grass", "foliage", "water" or a color like "#80a755"'); } else if ( tint.textures !== undefined && !(Array.isArray(tint.textures) && tint.textures.every((name) => typeof name === "string")) diff --git a/common/worldgen/biome_colors.ts b/common/worldgen/biome_colors.ts index 5d4c944..7f1ab3c 100644 --- a/common/worldgen/biome_colors.ts +++ b/common/worldgen/biome_colors.ts @@ -1,5 +1,5 @@ -// what color each biome tints grass and leaves, as 0xRRGGBB, minecraft's grass and foliage colors. blocks pick them -// with "tint" +// what color each biome tints grass, leaves and water, as 0xRRGGBB, minecraft's grass, foliage and water colors. +// blocks pick them with "tint" export const BIOME_GRASS_COLORS: Record = { "bworld:deep_ocean": 0x8eb971, "bworld:deep_frozen_ocean": 0x80b497, @@ -58,9 +58,39 @@ export const BIOME_FOLIAGE_COLORS: Record = { "bworld:frozen_peaks": 0x60a17b, }; +export const BIOME_WATER_COLORS: Record = { + "bworld:deep_ocean": 0x3f76e4, + "bworld:deep_frozen_ocean": 0x3938c9, + "bworld:deep_lukewarm_ocean": 0x45adf2, + "bworld:ocean": 0x3f76e4, + "bworld:frozen_ocean": 0x3938c9, + "bworld:warm_ocean": 0x43d5ee, + "bworld:river": 0x3f76e4, + "bworld:frozen_river": 0x3938c9, + "bworld:beach": 0x3f76e4, + "bworld:snowy_beach": 0x3d57d6, + "bworld:stony_shore": 0x3f76e4, + "bworld:plains": 0x3f76e4, + "bworld:meadow": 0x0e4ecf, + "bworld:forest": 0x3f76e4, + "bworld:dark_forest": 0x3f76e4, + "bworld:swamp": 0x617b64, + "bworld:taiga": 0x3f76e4, + "bworld:snowy_plains": 0x3f76e4, + "bworld:snowy_taiga": 0x3d57d6, + "bworld:savanna": 0x3f76e4, + "bworld:jungle": 0x3f76e4, + "bworld:desert": 0x3f76e4, + "bworld:snowy_slopes": 0x3f76e4, + "bworld:stony_peaks": 0x3f76e4, + "bworld:jagged_peaks": 0x3f76e4, + "bworld:frozen_peaks": 0x3f76e4, +}; + // biomes mods add without a color, and items, which aren't in any biome export const DEFAULT_GRASS_COLOR = BIOME_GRASS_COLORS["bworld:plains"]; export const DEFAULT_FOLIAGE_COLOR = BIOME_FOLIAGE_COLORS["bworld:plains"]; +export const DEFAULT_WATER_COLOR = BIOME_WATER_COLORS["bworld:plains"]; export function grass_color(biome: string | undefined) { return (biome !== undefined ? BIOME_GRASS_COLORS[biome] : undefined) ?? DEFAULT_GRASS_COLOR; @@ -70,16 +100,22 @@ export function foliage_color(biome: string | undefined) { return (biome !== undefined ? BIOME_FOLIAGE_COLORS[biome] : undefined) ?? DEFAULT_FOLIAGE_COLOR; } -// a block's "tint" color: the biome's grass or foliage color, or a fixed one like "#80a755" -export type TintColor = "grass" | "foliage" | `#${string}`; - -export function is_tint_color(value: unknown): value is TintColor { - return value === "grass" || value === "foliage" || (typeof value === "string" && /^#[0-9a-fA-F]{6}$/.test(value)); +export function water_color(biome: string | undefined) { + return (biome !== undefined ? BIOME_WATER_COLORS[biome] : undefined) ?? DEFAULT_WATER_COLOR; } -// the color outside any biome, for items: the default grass or foliage color, or the fixed one +// a block's "tint" color: the biome's grass, foliage or water color, or a fixed one like "#80a755" +export type TintColor = "grass" | "foliage" | "water" | `#${string}`; + +export function is_tint_color(value: unknown): value is TintColor { + return value === "grass" || value === "foliage" || value === "water" || + (typeof value === "string" && /^#[0-9a-fA-F]{6}$/.test(value)); +} + +// the color outside any biome, for items: the default grass, foliage or water color, or the fixed one export function tint_without_biome(color: TintColor) { if (color === "grass") return DEFAULT_GRASS_COLOR; if (color === "foliage") return DEFAULT_FOLIAGE_COLOR; + if (color === "water") return DEFAULT_WATER_COLOR; return parseInt(color.slice(1), 16); } diff --git a/docs/modding/blocks/index.md b/docs/modding/blocks/index.md index 99b7c7c..a292996 100644 --- a/docs/modding/blocks/index.md +++ b/docs/modding/blocks/index.md @@ -154,6 +154,7 @@ still going when the server stops don't carry on after it starts again until som "tint": { "color": "grass", "textures": ["top"] } ``` -`color` is `"grass"` or `"foliage"` for the biome's color, blended across biome borders like Minecraft's grass and -leaves, or a fixed color like `"#80a755"`. `textures` lists the texture variables to tint, like only the grass block's -top. Without it every texture is tinted. In inventories, where there's no biome, the block uses plains' color. +`color` is `"grass"`, `"foliage"` or `"water"` for the biome's color, blended across biome borders like Minecraft's +grass, leaves and water, or a fixed color like `"#80a755"`. `textures` lists the texture variables to tint, like only +the grass block's top. Without it every texture is tinted. In inventories, where there's no biome, the block uses +plains' color. diff --git a/mods/bworld/blocks/water.json b/mods/bworld/blocks/water.json index fec05fe..c2206ba 100644 --- a/mods/bworld/blocks/water.json +++ b/mods/bworld/blocks/water.json @@ -3,6 +3,7 @@ "block": { "id": "bworld:water", "textures": "bworld:water", + "tint": { "color": "water" }, "render_layer": "translucent", "light_opacity": 1, "alpha": 0.8, diff --git a/mods/bworld/textures/water.png b/mods/bworld/textures/water.png index 4566ef4..f95f384 100644 Binary files a/mods/bworld/textures/water.png and b/mods/bworld/textures/water.png differ diff --git a/tests/tint_test.ts b/tests/tint_test.ts index fe6ce4b..654203e 100644 --- a/tests/tint_test.ts +++ b/tests/tint_test.ts @@ -1,13 +1,13 @@ -// blocks with a "tint" are drawn in their biome's grass or foliage color, blended across biome edges, or a fixed one +// 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_HEIGHT, CHUNK_SIZE } from "$/common/constants.ts"; import { generate_raw_chunk } from "$/common/generation.ts"; -import { BIOME_FOLIAGE_COLORS, BIOME_GRASS_COLORS } from "$/common/worldgen/biome_colors.ts"; +import { BIOME_FOLIAGE_COLORS, BIOME_GRASS_COLORS, BIOME_WATER_COLORS } from "$/common/worldgen/biome_colors.ts"; import { type LayerMesh, TERRAIN_VERTEX_BYTES } from "$/client/workers/chunk_messages.ts"; import { test_game } from "./helpers.ts"; -type Colors = { grass: number; foliage: number }; +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 @@ -30,16 +30,17 @@ async function mesh_block(id: string, x: number, middle: Colors, west: Colors | const empty = () => new Uint32Array(CHUNK_AREA * CHUNK_HEIGHT); const chunks = [empty(), empty(), empty(), empty(), chunk, empty(), empty(), empty(), empty()]; const colors = chunks.map(() => null as Uint32Array | null); - const biome_colors = ({ grass, foliage }: Colors) => { - const array = new Uint32Array(CHUNK_AREA * 2); + const biome_colors = ({ grass, foliage, water = 0 }: Colors) => { + const array = new Uint32Array(CHUNK_AREA * 3); array.fill(grass, 0, CHUNK_AREA); - array.fill(foliage, CHUNK_AREA); + array.fill(foliage, CHUNK_AREA, CHUNK_AREA * 2); + array.fill(water, CHUNK_AREA * 2); return array; }; colors[4] = biome_colors(middle); if (west) colors[3] = biome_colors(west); - const meshed = await new Promise<{ solid: LayerMesh; cutout: LayerMesh }>((resolve) => { + const meshed = await new Promise<{ solid: LayerMesh; cutout: LayerMesh; translucent: LayerMesh }>((resolve) => { worker.onmessage = (e) => resolve(e.data); worker.postMessage({ type: "mesh", @@ -55,7 +56,7 @@ async function mesh_block(id: string, x: number, middle: Colors, west: Colors | const top: number[][] = []; const sides: number[][] = []; - for (const mesh of [meshed.solid, meshed.cutout]) { + 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; @@ -101,7 +102,17 @@ Deno.test("oak leaves are the biome's foliage color on every side, birch a fixed assertEquals(spruce.top, [[255, 255, 255]]); }); -Deno.test("every column the overworld generates has a biome with grass and foliage colors", async () => { +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 = {}; @@ -112,6 +123,7 @@ Deno.test("every column the overworld generates has a biome with grass and folia for (const biome of biomes) { assert(biome in BIOME_GRASS_COLORS, `${biome} has no grass color`); assert(biome in BIOME_FOLIAGE_COLORS, `${biome} has no foliage color`); + assert(biome in BIOME_WATER_COLORS, `${biome} has no water color`); } } });