Block visuals
This commit is contained in:
@@ -76,7 +76,8 @@ ctx.components.register_block("copper_tools:shaft", {
|
||||
changes often doesn't resend the others, and setting a field to what it already is sends nothing.
|
||||
- `undefined` removes a field. All of them go when the block is broken or replaced.
|
||||
- It's saved with the world. `get_client_data` and `set_client_data` copy the value, so change it by setting it again.
|
||||
- Client scripts read it with `ctx.world.get_client_data<T>(x, y, z, field)`, see [GUI](../gui/index.md).
|
||||
- Client scripts read it with `ctx.world.get_client_data<T>(x, y, z, field)`, see [GUI](../gui/index.md). A block's
|
||||
[visual](visuals.md) reads it with `block.get_client_data`, and its `update` is called when a field changes.
|
||||
|
||||
Like NeoForge's `getUpdateTag`, but per field. Keep it to what clients need to draw the block: everything in it is sent
|
||||
to every player near it.
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
# 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.
|
||||
|
||||
## Registering
|
||||
|
||||
```ts
|
||||
import type { ClientContext } from "bworld/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.
|
||||
|
||||
## 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
|
||||
|
||||
Instances are worked out on the CPU each frame for now, a little for each one in view. A few hundred are fine.
|
||||
Reference in New Issue
Block a user