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
+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