Water tint

This commit is contained in:
2026-10-07 11:29:58 -03:00
parent b830196829
commit 40d5a4f5af
8 changed files with 84 additions and 29 deletions
+4 -3
View File
@@ -1,7 +1,7 @@
import { block_value, chunk_key, default_block_value } from "$/common/utils.ts"; import { block_value, chunk_key, default_block_value } from "$/common/utils.ts";
import { AIR_ID } from "$/common/protocol.ts"; import { AIR_ID } from "$/common/protocol.ts";
import { decode_level_chunk } from "$/common/chunk_streaming.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 { import {
block_light_emission, block_light_emission,
block_light_opacity, block_light_opacity,
@@ -45,7 +45,7 @@ export interface Chunk {
max_y: number; max_y: number;
// only for translucent meshes that have to be sorted again as the camera moves // only for translucent meshes that have to be sorted again as the camera moves
translucent_sort?: TranslucentSort; 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; biome_colors: Uint32Array;
// from its last mesh, see get_light. undefined until it's been meshed // from its last mesh, see get_light. undefined until it's been meshed
light?: Uint8Array; light?: Uint8Array;
@@ -171,10 +171,11 @@ export class ClientLevel {
if (!this.#in_storage_range(x, z)) { if (!this.#in_storage_range(x, z)) {
return; return;
} }
const biome_colors = new Uint32Array(CHUNK_AREA * 2); const biome_colors = new Uint32Array(CHUNK_AREA * 3);
biomes.forEach((biome, column) => { biomes.forEach((biome, column) => {
biome_colors[column] = grass_color(biome); biome_colors[column] = grass_color(biome);
biome_colors[CHUNK_AREA + column] = foliage_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); const chunk = this.get_chunk(x, z);
+9 -5
View File
@@ -113,7 +113,7 @@ interface MeshQuad {
sprite: SpriteRegion; sprite: SpriteRegion;
// 4 weights per corner // 4 weights per corner
light_weights: number[]; 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; tint: number;
} }
@@ -207,17 +207,18 @@ function post(message: FromChunkWorker, transfer: Transferable[]) {
self.postMessage(message, transfer); 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 // columns around it, so biome edges fade instead of being a line. neighbors that aren't there are left out
const BLEND_RADIUS = 2; const BLEND_RADIUS = 2;
// the middle chunk's blended colors, x then z, grass then foliage // the middle chunk's blended colors, x then z, grass then foliage then water
const blended = new Uint32Array(CHUNK_AREA * 2); 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 // what MeshQuad.tint is for the biome's colors, they aren't colors since those only go up to 0xffffff
const GRASS = -1; const GRASS = -1;
const FOLIAGE = -2; const FOLIAGE = -2;
const WATER = -3;
function blend_biome_colors(colors: (Uint32Array | null)[]) { 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 z = 0; z < CHUNK_SIZE; z++) {
for (let x = 0; x < CHUNK_SIZE; x++) { for (let x = 0; x < CHUNK_SIZE; x++) {
let r = 0; let r = 0;
@@ -253,6 +254,7 @@ function quad_tint(block: BlockRegistry, variables: string[]) {
const color = block.tint!.color; const color = block.tint!.color;
if (color === "grass") return GRASS; if (color === "grass") return GRASS;
if (color === "foliage") return FOLIAGE; if (color === "foliage") return FOLIAGE;
if (color === "water") return WATER;
return parseInt(color.slice(1), 16); 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] ? blended[z * CHUNK_SIZE + x]
: quad.tint === FOLIAGE : quad.tint === FOLIAGE
? blended[CHUNK_AREA + z * CHUNK_SIZE + x] ? blended[CHUNK_AREA + z * CHUNK_SIZE + x]
: quad.tint === WATER
? blended[CHUNK_AREA * 2 + z * CHUNK_SIZE + x]
: quad.tint; : quad.tint;
if (layer_id !== TRANSLUCENT) { if (layer_id !== TRANSLUCENT) {
+1 -1
View File
@@ -624,7 +624,7 @@ export function validate_block(json: unknown): Problems {
if (json.tint !== undefined) { if (json.tint !== undefined) {
const tint = json.tint; const tint = json.tint;
if (!is_object(tint) || !is_tint_color(tint.color)) { 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 ( } else if (
tint.textures !== undefined && tint.textures !== undefined &&
!(Array.isArray(tint.textures) && tint.textures.every((name) => typeof name === "string")) !(Array.isArray(tint.textures) && tint.textures.every((name) => typeof name === "string"))
+44 -8
View File
@@ -1,5 +1,5 @@
// what color each biome tints grass and leaves, as 0xRRGGBB, minecraft's grass and foliage colors. blocks pick them // what color each biome tints grass, leaves and water, as 0xRRGGBB, minecraft's grass, foliage and water colors.
// with "tint" // blocks pick them with "tint"
export const BIOME_GRASS_COLORS: Record<string, number> = { export const BIOME_GRASS_COLORS: Record<string, number> = {
"bworld:deep_ocean": 0x8eb971, "bworld:deep_ocean": 0x8eb971,
"bworld:deep_frozen_ocean": 0x80b497, "bworld:deep_frozen_ocean": 0x80b497,
@@ -58,9 +58,39 @@ export const BIOME_FOLIAGE_COLORS: Record<string, number> = {
"bworld:frozen_peaks": 0x60a17b, "bworld:frozen_peaks": 0x60a17b,
}; };
export const BIOME_WATER_COLORS: Record<string, number> = {
"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 // 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_GRASS_COLOR = BIOME_GRASS_COLORS["bworld:plains"];
export const DEFAULT_FOLIAGE_COLOR = BIOME_FOLIAGE_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) { export function grass_color(biome: string | undefined) {
return (biome !== undefined ? BIOME_GRASS_COLORS[biome] : undefined) ?? DEFAULT_GRASS_COLOR; 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; 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 function water_color(biome: string | undefined) {
export type TintColor = "grass" | "foliage" | `#${string}`; return (biome !== undefined ? BIOME_WATER_COLORS[biome] : undefined) ?? DEFAULT_WATER_COLOR;
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));
} }
// 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) { export function tint_without_biome(color: TintColor) {
if (color === "grass") return DEFAULT_GRASS_COLOR; if (color === "grass") return DEFAULT_GRASS_COLOR;
if (color === "foliage") return DEFAULT_FOLIAGE_COLOR; if (color === "foliage") return DEFAULT_FOLIAGE_COLOR;
if (color === "water") return DEFAULT_WATER_COLOR;
return parseInt(color.slice(1), 16); return parseInt(color.slice(1), 16);
} }
+4 -3
View File
@@ -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"] } "tint": { "color": "grass", "textures": ["top"] }
``` ```
`color` is `"grass"` or `"foliage"` for the biome's color, blended across biome borders like Minecraft's grass and `color` is `"grass"`, `"foliage"` or `"water"` for the biome's color, blended across biome borders like Minecraft's
leaves, or a fixed color like `"#80a755"`. `textures` lists the texture variables to tint, like only the grass block's grass, leaves and water, or a fixed color like `"#80a755"`. `textures` lists the texture variables to tint, like only
top. Without it every texture is tinted. In inventories, where there's no biome, the block uses plains' color. the grass block's top. Without it every texture is tinted. In inventories, where there's no biome, the block uses
plains' color.
+1
View File
@@ -3,6 +3,7 @@
"block": { "block": {
"id": "bworld:water", "id": "bworld:water",
"textures": "bworld:water", "textures": "bworld:water",
"tint": { "color": "water" },
"render_layer": "translucent", "render_layer": "translucent",
"light_opacity": 1, "light_opacity": 1,
"alpha": 0.8, "alpha": 0.8,
Binary file not shown.

Before

Width:  |  Height:  |  Size: 119 B

After

Width:  |  Height:  |  Size: 262 B

+21 -9
View File
@@ -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 { assert, assertEquals } from "@std/assert";
import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts"; import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "$/common/constants.ts"; import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "$/common/constants.ts";
import { generate_raw_chunk } from "$/common/generation.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 { type LayerMesh, TERRAIN_VERTEX_BYTES } from "$/client/workers/chunk_messages.ts";
import { test_game } from "./helpers.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 // 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 // -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 empty = () => new Uint32Array(CHUNK_AREA * CHUNK_HEIGHT);
const chunks = [empty(), empty(), empty(), empty(), chunk, empty(), empty(), empty(), empty()]; const chunks = [empty(), empty(), empty(), empty(), chunk, empty(), empty(), empty(), empty()];
const colors = chunks.map(() => null as Uint32Array | null); const colors = chunks.map(() => null as Uint32Array | null);
const biome_colors = ({ grass, foliage }: Colors) => { const biome_colors = ({ grass, foliage, water = 0 }: Colors) => {
const array = new Uint32Array(CHUNK_AREA * 2); const array = new Uint32Array(CHUNK_AREA * 3);
array.fill(grass, 0, CHUNK_AREA); 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; return array;
}; };
colors[4] = biome_colors(middle); colors[4] = biome_colors(middle);
if (west) colors[3] = biome_colors(west); 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.onmessage = (e) => resolve(e.data);
worker.postMessage({ worker.postMessage({
type: "mesh", type: "mesh",
@@ -55,7 +56,7 @@ async function mesh_block(id: string, x: number, middle: Colors, west: Colors |
const top: number[][] = []; const top: number[][] = [];
const sides: 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); const f32 = new Float32Array(mesh.vertices.buffer);
for (let quad = 0; quad < mesh.quad_count; quad++) { for (let quad = 0; quad < mesh.quad_count; quad++) {
const byte = quad * 4 * TERRAIN_VERTEX_BYTES; 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]]); 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 { game } = await test_game("mods");
const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks"); const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
const block_ids: Record<string, number> = {}; const block_ids: Record<string, number> = {};
@@ -112,6 +123,7 @@ Deno.test("every column the overworld generates has a biome with grass and folia
for (const biome of biomes) { for (const biome of biomes) {
assert(biome in BIOME_GRASS_COLORS, `${biome} has no grass color`); 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_FOLIAGE_COLORS, `${biome} has no foliage color`);
assert(biome in BIOME_WATER_COLORS, `${biome} has no water color`);
} }
} }
}); });