From 8f8030c02e22bc1b72e43148a84eeb33b054604c Mon Sep 17 00:00:00 2001 From: Paula Date: Wed, 7 Oct 2026 11:43:19 -0300 Subject: [PATCH] Water tweaks --- client/level/client_level.ts | 12 +++++++++++- client/rendering/game_renderer.ts | 27 ++++++++++++++++++++++++++- client/workers/chunk_worker.ts | 15 +++++++++++++++ tests/liquid_test.ts | 13 +++++++++++-- 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/client/level/client_level.ts b/client/level/client_level.ts index 6c286fa..9f45283 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, water_color } from "$/common/worldgen/biome_colors.ts"; +import { DEFAULT_WATER_COLOR, foliage_color, grass_color, water_color } from "$/common/worldgen/biome_colors.ts"; import { block_light_emission, block_light_opacity, @@ -323,6 +323,16 @@ export class ClientLevel { return block ? collision_shape_at(block, value) : true; } + // the biome's water color at a column as 0xRRGGBB, see "tint". plains' in unloaded chunks + water_color(x: number, z: number) { + const chunk_x = Math.floor(x / CHUNK_SIZE); + const chunk_z = Math.floor(z / CHUNK_SIZE); + const chunk = this.get_chunk(chunk_x, chunk_z); + if (!chunk) return DEFAULT_WATER_COLOR; + return chunk + .biome_colors[CHUNK_AREA * 2 + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE)]; + } + // water, see is_liquid_block. unloaded chunks aren't is_liquid(x: number, y: number, z: number) { const block = this.get_block(x, y, z); diff --git a/client/rendering/game_renderer.ts b/client/rendering/game_renderer.ts index ba5df6d..a2d9d74 100644 --- a/client/rendering/game_renderer.ts +++ b/client/rendering/game_renderer.ts @@ -1,5 +1,12 @@ import type { Client } from "$/client/client.ts"; -import { begin_mode_3d, clear_background, end_mode_3d, update_lightmap } from "$/client/renderer/mod.ts"; +import { + begin_mode_3d, + canvas, + clear_background, + draw_rect, + end_mode_3d, + update_lightmap, +} from "$/client/renderer/mod.ts"; import { daylight } from "$/common/time.ts"; import { Hud } from "$/client/gui/hud.ts"; import { DebugOverlay } from "$/client/gui/debug_overlay.ts"; @@ -13,6 +20,8 @@ const DAY_SKY = [0.69, 0.8, 1]; const NIGHT_SKY = [0.02, 0.03, 0.08]; // minecraft's darkest sky: moonlight still lights things a little const NIGHT_SKY_LIGHT = 0.2; +// how much of the screen is the water's color with the camera under water +const UNDERWATER_ALPHA = 0.4; // draws a frame, like minecraft's GameRenderer: the level from the camera, then the hud and screens on top export class GameRenderer { @@ -35,6 +44,21 @@ export class GameRenderer { } } + // with the camera in water, the screen and the hand are washed in the biome's water color, like minecraft's + // underwater overlay + #render_underwater(client: Client) { + const { x, y, z } = client.camera; + const [bx, by, bz] = [Math.floor(x), Math.floor(y), Math.floor(z)]; + if (!client.level.is_liquid(bx, by, bz)) return; + const color = client.level.water_color(bx, bz); + draw_rect(0, 0, canvas.width, canvas.height, [ + (color >> 16) / 255, + ((color >> 8) & 0xff) / 255, + (color & 0xff) / 255, + UNDERWATER_ALPHA, + ]); + } + // partial_tick is how far this frame is between the last tick and the next, entities are drawn in between render(client: Client, partial_tick: number) { const camera = client.camera; @@ -53,6 +77,7 @@ export class GameRenderer { profiler.section("nameplates", () => render_nameplates(client.level, camera, client.player, partial_tick)); end_mode_3d(); profiler.section("hand", () => this.hand.render(client.player, partial_tick)); + this.#render_underwater(client); profiler.section("hud", () => { this.hud.render(client); diff --git a/client/workers/chunk_worker.ts b/client/workers/chunk_worker.ts index 0acc778..32c8670 100644 --- a/client/workers/chunk_worker.ts +++ b/client/workers/chunk_worker.ts @@ -585,6 +585,20 @@ function liquid_mesh_quad( }; } +// the liquid's surface seen from below, so it shows from under water: the top quad facing down, lit and shaded like +// the top. its corners go the other way round, the lighting the mesher just worked out with them +const UNDERSIDE_CORNERS = [0, 3, 2, 1]; + +function underside(top: MeshQuad): MeshQuad { + for (const light of [corner_sky, corner_block, corner_ao]) light.set(UNDERSIDE_CORNERS.map((k) => light[k])); + return { + ...top, + face: 1, + positions: UNDERSIDE_CORNERS.flatMap((k) => top.positions.slice(k * 3, k * 3 + 3)), + uvs: UNDERSIDE_CORNERS.flatMap((k) => top.uvs.slice(k * 2, k * 2 + 2)), + }; +} + // region has to be filled and lit first // chunks are the 3x3 chunks' blocks with their states, the middle one's for models that change with them and the // ones around it for liquids, whose surface follows their neighbors' @@ -666,6 +680,7 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array // like minecraft's liquids, no ambient occlusion corner_ao.fill(1); emit(quad, layer_id, x, y, z, alpha); + if (face === 0) emit(underside(quad), layer_id, x, y, z, alpha); } continue; } diff --git a/tests/liquid_test.ts b/tests/liquid_test.ts index f95d666..565d379 100644 --- a/tests/liquid_test.ts +++ b/tests/liquid_test.ts @@ -202,6 +202,12 @@ async function mesh(blocks: [number, number, number, string, number?][]) { return quads; } +// the y of a quad's normal from its corners' winding, counterclockwise is the front +function facing_y(corners: number[][]) { + const [a, b, c] = corners; + return (b[2] - a[2]) * (c[0] - a[0]) - (b[0] - a[0]) * (c[2] - a[2]); +} + Deno.test("the chunk worker draws water sloping down from a source, across a chunk's edge too", async () => { // three rows of it: a source at x -1, in the chunk to the -x side, then flowing water at x 0 and 1, on stone const blocks: [number, number, number, string, number?][] = []; @@ -210,9 +216,12 @@ Deno.test("the chunk worker draws water sloping down from a source, across a chu blocks.push([0, 9, z, "bworld:stone"], [1, 9, z, "bworld:stone"]); } const quads = await mesh(blocks); - // the tops of the middle chunk's water, by the x of their corners + // the tops of the middle chunk's water, by the x of their corners. each is drawn facing up and, to be seen from + // under water, facing down const tops = quads.filter((corners) => corners.every(([, y]) => y > 10)); - assertEquals(tops.length, 6); + assertEquals(tops.length, 12); + assertEquals(tops.filter((corners) => facing_y(corners) > 0).length, 6); + assertEquals(tops.filter((corners) => facing_y(corners) < 0).length, 6); const height_at = (x: number) => Math.max(...tops.flat().filter(([cx]) => Math.abs(cx - x) < 1e-6).map(([, y]) => y - 10)); // full where it meets the source, then down