From bcd0690ef50162d9388f6f1b900e0e861c46bc4e Mon Sep 17 00:00:00 2001 From: Paula Date: Wed, 28 Oct 2026 11:49:56 -0300 Subject: [PATCH] Fluid rendering in graphics --- client/gui/mod_graphics.ts | 8 ++++++-- client/rendering/item_renderer.ts | 8 ++++++++ client/rendering/render_utils.ts | 29 +++++++++++++++++++++++++++++ client/rendering/visuals.ts | 12 ++---------- common/mod_api/client.ts | 3 +++ docs/modding/gui/index.md | 3 +++ docs/modding/resources/fluids.md | 16 ++++++++-------- 7 files changed, 59 insertions(+), 20 deletions(-) diff --git a/client/gui/mod_graphics.ts b/client/gui/mod_graphics.ts index 42daa32..489f58f 100644 --- a/client/gui/mod_graphics.ts +++ b/client/gui/mod_graphics.ts @@ -7,7 +7,7 @@ import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts"; import { ItemStack } from "$/common/inventory.ts"; import type { Color, Graphics } from "$/common/mod_api/client.ts"; -import type { KeyCode } from "$/common/mod_api/shared.ts"; +import type { FluidVariant, KeyCode } from "$/common/mod_api/shared.ts"; import { point_inside_rec } from "$/common/utils.ts"; import { AssetManager } from "$/client/assets.ts"; import { InputManager, type KeyCode as InputKey } from "$/client/input_manager.ts"; @@ -21,7 +21,7 @@ import { measure_text, Texture, } from "$/client/renderer/mod.ts"; -import { draw_item } from "$/client/rendering/render_utils.ts"; +import { draw_fluid, draw_item } from "$/client/rendering/render_utils.ts"; import { get_sprite_region } from "$/client/sprites.ts"; import { draw_tooltip, item_tooltip, type TooltipLine } from "./tooltip.ts"; import { TEXT_HEIGHT, TEXT_SCALE, TextInput } from "./widgets.ts"; @@ -164,6 +164,10 @@ export class ModGraphics implements Graphics { draw_item(stack, this.#x(x), this.#y(y), count !== 1 && stack.max_amount !== 1); } + fluid(fluid: FluidVariant, x: number, y: number, filled = 1) { + draw_fluid(fluid, this.#x(x), this.#y(y), filled); + } + clip(x: number, y: number, w: number, h: number, draw: () => void) { this.#clip_canvas(this.#x(x), this.#y(y), w * S, h * S, draw); } diff --git a/client/rendering/item_renderer.ts b/client/rendering/item_renderer.ts index 7077273..c798d4e 100644 --- a/client/rendering/item_renderer.ts +++ b/client/rendering/item_renderer.ts @@ -9,6 +9,7 @@ import { get_sprite_region } from "$/client/sprites.ts"; import type { ItemEntity } from "$/client/entity/item_entity.ts"; import { contained_fluid, type ItemData, ItemStack } from "$/common/inventory.ts"; import type { FluidVariant } from "$/common/mod_api/shared.ts"; +import type { FluidJson } from "$/common/mod_data.ts"; import { light_color, push_vertex, Texture } from "$/client/renderer/mod.ts"; import { bake_model, @@ -404,6 +405,13 @@ export function fluid_rgb(fluid: FluidVariant): Vec3 { return color_rgb(tint_without_biome(fluid_rules.color(fluid) as TintColor)); } +// a fluid's texture and its color by its rules, for drawing it in a box or a slot. throws for a fluid no mod defines +export function fluid_look(fluid: FluidVariant): { texture: string; tint: Vec3 } { + const json = EverythingRegistry.get("fluids", fluid?.id); + if (!json) throw new Error(`there's no fluid ${fluid?.id}`); + return { texture: json.texture ?? "engine:missing", tint: fluid_rgb(structuredClone(fluid)) }; +} + // 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) { diff --git a/client/rendering/render_utils.ts b/client/rendering/render_utils.ts index 57e1ef1..d414225 100644 --- a/client/rendering/render_utils.ts +++ b/client/rendering/render_utils.ts @@ -7,6 +7,7 @@ import { get_sprite_region } from "$/client/sprites.ts"; import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts"; import { AssetManager } from "$/client/assets.ts"; import { ItemStack } from "$/common/inventory.ts"; +import type { FluidVariant } from "$/common/mod_api/shared.ts"; import { item_texture } from "$/common/utils.ts"; import { cube_face_texture, @@ -17,6 +18,7 @@ import { } from "$/common/block_models.ts"; import { display_matrix, + fluid_look, item_block, item_layers, item_model_of, @@ -160,6 +162,33 @@ function draw_item_item(item: ItemStack, item_info: ItemRegistry, x: number, y: for (const layer of item_layers(item)) draw(layer.texture, layer.color); } +// a fluid as a slot shows it, its texture tinted in its color over the slot's inside where an item's sprite goes. +// filled (0 to 1) is how much of it there is from the bottom up, like a tank's level: the texture is cut, not squashed +export function draw_fluid(fluid: FluidVariant, x: number, y: number, filled = 1) { + const { texture, tint: [r, g, b] } = fluid_look(fluid); + // nothing for NaN too, like amount / capacity before there's a capacity + const done = filled > 0 ? Math.min(1, filled) : 0; + if (done === 0) return; + const region = get_sprite_region(texture); + const size = SLOT_SIZE - 6; + draw_texture_region( + AssetManager.instance.get("bworld:textures"), + region.x, + region.y + region.height * (1 - done), + region.width, + region.height * done, + x + 3, + y + 3 + size * (1 - done), + size, + size * done, + false, + false, + r, + g, + b, + ); +} + // from a model's points to a slot, as screen x and y and depth toward the viewer. with a gui transform, the transform // starts from the model's 16 pixels across the slot's middle, y up. without one it's the cube icon's corners (see // draw_item_block), so models are drawn at the same size and angle: seen from above the north west corner, west on the diff --git a/client/rendering/visuals.ts b/client/rendering/visuals.ts index c225a43..32800bc 100644 --- a/client/rendering/visuals.ts +++ b/client/rendering/visuals.ts @@ -9,7 +9,6 @@ import { mat4, vec3 } from "gl-matrix"; import type { FluidInstance, - FluidVariant, Instances, ItemStack as ApiItemStack, ModelRef, @@ -30,7 +29,7 @@ import { states_of, } from "$/common/block_models.ts"; import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "$/common/constants.ts"; -import type { FluidJson, ModelFace } from "$/common/mod_data.ts"; +import type { ModelFace } from "$/common/mod_data.ts"; import { ItemStack } from "$/common/inventory.ts"; import { chunk_key, item_texture } from "$/common/utils.ts"; import { @@ -43,7 +42,7 @@ import { write_buffer, } from "$/client/renderer/mod.ts"; import { get_sprite_region } from "$/client/sprites.ts"; -import { fluid_rgb, ground_transform, item_layers, push_item, sprite_u, sprite_v } from "./item_renderer.ts"; +import { fluid_look, ground_transform, item_layers, push_item, sprite_u, sprite_v } from "./item_renderer.ts"; export interface VisualRegistration { mod: string; @@ -466,13 +465,6 @@ function item_quads(item: ItemStack): InstanceQuad[] { return quads; } -// a fluid's texture and its color by its rules, for a box of it -function fluid_look(fluid: FluidVariant) { - const json = EverythingRegistry.get("fluids", fluid?.id); - if (!json) throw new Error(`there's no fluid ${fluid?.id}`); - return { texture: json.texture ?? "engine:missing", tint: fluid_rgb(structuredClone(fluid)) }; -} - // a box's faces, by the axis they're across and whether they're at its high end or its low one const BOX_FACES: { face: ModelFace; axis: number; high: boolean }[] = [ { face: "west", axis: 0, high: false }, diff --git a/common/mod_api/client.ts b/common/mod_api/client.ts index 7ef012a..0dbd2f7 100644 --- a/common/mod_api/client.ts +++ b/common/mod_api/client.ts @@ -254,6 +254,9 @@ export interface Graphics { direction?: "right" | "up", ): void; item(id: Id, x: number, y: number, count?: number): void; + // a fluid as a slot shows it, its texture in its color, at a slot's top left like item. filled (0 to 1, 1 when not + // set) is how much of the slot it fills from the bottom up, like amount / capacity for a machine's tank + fluid(fluid: FluidVariant, x: number, y: number, filled?: number): void; clip(x: number, y: number, w: number, h: number, draw: () => void): void; button(label: string, x: number, y: number, w: number, h: number): boolean; text_input(id: string, x: number, y: number, w: number): string; diff --git a/docs/modding/gui/index.md b/docs/modding/gui/index.md index acda5ea..a5a6bdb 100644 --- a/docs/modding/gui/index.md +++ b/docs/modding/gui/index.md @@ -127,6 +127,9 @@ interface Graphics { direction?: "right" | "up", ): void; item(id: string, x: number, y: number, count?: number): void; // an item as a slot shows it + // a fluid as a slot shows it, like { id: "bworld:juice", data: { item: "bworld:apple" } }. filled is how much of + // the slot it fills from the bottom up, 0 to 1, like a tank's amount / capacity + fluid(fluid: FluidVariant, x: number, y: number, filled?: number): void; clip(x: number, y: number, w: number, h: number, draw: () => void): void; // only draws inside the box button(label: string, x: number, y: number, w: number, h: number): boolean; // true the frame it's clicked diff --git a/docs/modding/resources/fluids.md b/docs/modding/resources/fluids.md index 12cbc82..9f6df2b 100644 --- a/docs/modding/resources/fluids.md +++ b/docs/modding/resources/fluids.md @@ -23,17 +23,17 @@ here has no block, so it can't be placed. } ``` -| Field | Required | What it does | -| ----------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------- | -| `id` | yes | The fluid's id, in the mod's [namespace](../concepts/registries.md#identifiers). | -| `name` | no | What players see. `copper_tools:maple_syrup` is "Maple Syrup" when not set, like an item's. | -| `color` | yes | Its color, like `"#a0522d"`, for whatever shows it. | -| `texture` | no | What a [box of it](../blocks/visuals.md#fluids) is drawn with, tinted by its color, like NeoForge's still texture. See below. | -| `base_sell_value` | no | What 1000 mB is worth sold in a [container](#containers), a whole number of 0 or more. None when not set. | +| Field | Required | What it does | +| ----------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `id` | yes | The fluid's id, in the mod's [namespace](../concepts/registries.md#identifiers). | +| `name` | no | What players see. `copper_tools:maple_syrup` is "Maple Syrup" when not set, like an item's. | +| `color` | yes | Its color, like `"#a0522d"`, for whatever shows it. | +| `texture` | no | What a [box of it](../blocks/visuals.md#fluids) and [a slot of it](../gui/index.md#graphics) are drawn with, tinted by its color, like NeoForge's still texture. See below. | +| `base_sell_value` | no | What 1000 mB is worth sold in a [container](#containers), a whole number of 0 or more. None when not set. | Like a fluid container's layer, the texture is multiplied by the fluid's color, so a light gray one comes out in the fluid's own color, and its alpha is how much shows through. The base game's `bworld:water_still` is such a texture, -animated, and any fluid can use it. Without one, a box of the fluid is drawn with the missing texture. +animated, and any fluid can use it. Without one, the fluid is drawn with the missing texture. ## Fluids With Data