159 lines
7.4 KiB
Markdown
159 lines
7.4 KiB
Markdown
# Visuals
|
||
|
||
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
|
||
|
||
```ts
|
||
import type { ClientContext } from "cubi/client";
|
||
|
||
export function setup(ctx: ClientContext) {
|
||
ctx.render.register_visual("copper_tools:shaft", (block, instances) => {
|
||
const shaft = instances.rotating();
|
||
const turn = () => shaft.set_rotation("y", block.get_client_data<number>("speed") ?? 0);
|
||
turn();
|
||
return { update: turn };
|
||
}, { replaces_model: true });
|
||
}
|
||
```
|
||
|
||
- Visuals can only be registered during `setup`, and a block has at most one.
|
||
- With `replaces_model: true`, the block's model is left out of the terrain, for a visual that draws all of the block,
|
||
like the shaft above. Without it, the model is drawn as usual and the visual adds to it, like a press's head over its
|
||
casing. The model is still what the block's item, its outline and the cracks on it while it's broken are drawn with.
|
||
- `block` is `{ id, x, y, z, states, get_client_data }`. `get_client_data` reads a field of its
|
||
[client data](tiledata.md#client-data) as it is now, which is how the server tells a visual what to draw.
|
||
|
||
## When Visuals Are Made
|
||
|
||
| Handler | Called when |
|
||
| ---------------- | -------------------------------------------------------------------------------------- |
|
||
| the function | The block's chunk is shown, or the block is placed or its states change. |
|
||
| `update()` | A field of the block's client data changes. |
|
||
| `on_frame(time)` | Before each frame its chunk is in view. See [Moving Every Frame](#moving-every-frame). |
|
||
| `on_delete()` | The block or its chunk goes, the player leaves, or its states change. |
|
||
|
||
Changing a block's states makes its visual again, so `block.states` never changes. Instances go with their visual, so
|
||
`on_delete` is only needed for something else the visual keeps.
|
||
|
||
A visual that throws is logged with its mod and block. One whose function threw isn't made again until the block
|
||
changes, and an `on_frame` that throws isn't called again.
|
||
|
||
## Instances
|
||
|
||
A visual draws models as instances, lit by the block's light and shaded by the way each face points:
|
||
|
||
```ts
|
||
instances.rotating(); // the block's own model, the way its states draw it
|
||
instances.rotating("copper_tools:shaft_half"); // a model from models/
|
||
instances.transformed({ model: "copper_tools:shaft_half", x: 90, y: 180 }); // turned like a block variant
|
||
```
|
||
|
||
A **rotating** instance turns around an axis through the middle of the block at a steady speed:
|
||
|
||
```ts
|
||
shaft.set_rotation("x", 32); // 32 rpm around x
|
||
shaft.set_rotation("x", -32, 22.5); // the other way, 22.5 degrees along at time 0
|
||
```
|
||
|
||
It's at `time × rpm × 0.3 + offset` degrees, counterclockwise seen from the axis's positive end when `rpm` is positive.
|
||
It needs nothing per frame, only `set_rotation` again when the speed changes. Every instance on a client turns by the
|
||
same time, so cogs set up to mesh stay meshed.
|
||
|
||
A **transformed** instance goes wherever a matrix puts it, set again whenever it moves. The matrix is 16 numbers in
|
||
column major order, like gl-matrix's `mat4`, from the model's space (0 to 1 across the block) to the block's, where 0 0
|
||
0 is its corner:
|
||
|
||
```ts
|
||
const head = instances.transformed("copper_tools:press_head");
|
||
head.set_transform([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, -0.5, 0, 1]); // half a block down
|
||
```
|
||
|
||
`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
|
||
import { mat4 } from "gl-matrix";
|
||
|
||
const stack = block.get_client_data<ItemStack>("stack");
|
||
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
|
||
`on_frame`. `time` is in ticks, with the part of the next one, the same time rotating instances turn by:
|
||
|
||
```ts
|
||
ctx.render.register_visual("copper_tools:crank", (block, instances) => {
|
||
const handle = instances.rotating("copper_tools:crank_handle");
|
||
let angle = 0;
|
||
let speed = 0;
|
||
let last: number | undefined;
|
||
return {
|
||
on_frame(time) {
|
||
const ticks = last === undefined ? 0 : time - last;
|
||
last = time;
|
||
// catches up with the speed the server says, a quarter of the way each tick
|
||
const target = block.get_client_data<number>("speed") ?? 0;
|
||
speed += (target - speed) * Math.min(1, ticks / 4);
|
||
angle += speed * 0.3 * ticks;
|
||
handle.set_rotation("y", 0, angle);
|
||
},
|
||
};
|
||
});
|
||
```
|
||
|
||
Only visuals in chunks in view get `on_frame`, so keep anything that has to happen out of view in the server script.
|
||
|
||
## Cost
|
||
|
||
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.
|