Fluid rendering in graphics

This commit is contained in:
2026-10-28 11:49:56 -03:00
parent 8fc08ab41e
commit bcd0690ef5
7 changed files with 59 additions and 20 deletions
+6 -2
View File
@@ -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);
}
+8
View File
@@ -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<FluidJson>("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) {
+29
View File
@@ -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
+2 -10
View File
@@ -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<FluidJson>("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 },
+3
View File
@@ -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;
+3
View File
@@ -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
+8 -8
View File
@@ -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