Fluid and item visuals

This commit is contained in:
2026-10-25 18:46:48 -03:00
parent 08df7f1b6b
commit 233c518def
15 changed files with 549 additions and 122 deletions
+52 -3
View File
@@ -1,8 +1,9 @@
# Visuals
A visual draws a block with code, for blocks that move: a shaft that turns, a crank's handle, a press coming down. A
client script registers one for a block, and every block of that kind in a shown chunk gets its own. It's bworld's
version of Minecraft's block entity renderers, shaped like the Flywheel visuals Create draws its machines with.
A visual draws a block with code, for blocks that move or show what's in them: a shaft that turns, a press coming down,
the items and fluid in a machine. A client script registers one for a block, and every block of that kind in a shown
chunk gets its own. It's bworld's version of Minecraft's block entity renderers, shaped like the Flywheel visuals Create
draws its machines with.
## Registering
@@ -73,6 +74,50 @@ head.set_transform([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, -0.5, 0, 1]); // half
`delete()` stops drawing an instance for good.
### Items
An **item** instance draws one of an item, whatever the stack's count, the way it looks in a slot: its model, its block,
or its sprite with its [layers](../items/index.md#layers) in their colors, like a bottle with its juice. It's the size
it's drawn on the ground (half a block across, a block's item a quarter), with its middle at the middle of the block,
and it's placed by a matrix like a transformed model:
```ts
const stack = block.get_client_data<ItemStack>("item");
if (stack) {
const item = instances.item(stack);
const lying = mat4.fromTranslation(mat4.create(), [0.5, 0.2, 0.5]);
mat4.rotateX(lying, lying, Math.PI / 2);
mat4.translate(lying, lying, [-0.5, -0.5, -0.5]);
item.set_transform(lying); // flat, a bit above the bottom of the block
}
```
The server tells the visual what to draw through [client data](tiledata.md#client-data), like the stack above. To draw a
stack as more than one, like a basin with a copy for every 8 items, make an instance per copy. To change what an
instance draws, delete it and make another.
### Fluids
A **fluid** instance draws a box of a [fluid](../resources/fluids.md), its `texture` tinted in its color for its data,
like what's in a tank:
```ts
const tank = block.get_client_data<{ fluid: FluidVariant; amount: number }>("tank");
if (tank) {
const juice = instances.fluid(tank.fluid);
juice.set_box([2, 2, 2], [14, 2 + 12 * tank.amount / 1000, 14]); // filling the 12 pixels inside its walls
}
```
`set_box(from, to)` is in pixels, like a model's elements: 0 to 16 across the block, or past it for a box that reaches
into the blocks around, like a tank's. The texture is cut to fit, never stretched, and repeats once a block. Nothing is
drawn until a box is set, and it can be set again whenever the fluid goes up or down. Only the box's outside is drawn,
so it can't be seen from inside it.
A fluid's box shows what's behind it, as much as its texture's alpha lets through. Boxes are drawn after the rest of the
visuals, the farthest first. Translucent terrain like water is drawn after them, so water behind a box isn't seen
through it.
## Moving Every Frame
A visual whose instances move in a way the time can't say, like a crank's handle slowing to a stop, moves them in
@@ -105,3 +150,7 @@ Only visuals in chunks in view get `on_frame`, so keep anything that has to happ
Every instance of a model is drawn in one draw call, and the GPU turns rotating instances by the time, so a block that
keeps turning costs nothing per frame. Setting an instance's rotation or transform sends just that instance to the GPU
again. What costs CPU time is `on_frame`, so use a rotating instance wherever the movement is steady.
Items that look the same are one model: every bottle of strawberry juice in every visual is one draw call, and one of
melon juice another. A fluid's box is a model of its own, so each is a draw call, and setting its box sends its few
faces to the GPU again.