Files
bworld/common/block_shapes.ts
T
2026-10-03 15:57:47 -03:00

137 lines
5.7 KiB
TypeScript

// a block's shape, like minecraft's VoxelShape: the boxes it's made of. it's what the player points at and what the
// cracks are drawn on when it breaks (minecraft's getShape), and what entities bump into when it has collision
// (getCollisionShape). blocks get theirs from the boxes of their model, or set "shape" like minecraft's Block.box
import { block_variant, element_rotation, find_model, type ModelJson } from "./block_models.ts";
import { type BlockRegistry, EverythingRegistry } from "./everything_registry.ts";
import { get_state_value } from "./utils.ts";
// min x y z then max x y z, in block space (0 to 1)
export type Box = readonly [number, number, number, number, number, number];
export type Shape = readonly Box[];
export const FULL_SHAPE: Shape = [[0, 0, 0, 1, 1, 1]];
export const EMPTY_SHAPE: Shape = [];
// thinner than this on some axis is a flat plane, like the cross model's, not a box
const FLAT = 1e-4;
// the boxes of a model's elements, turned like the model is. elements turned by their own rotation take the box
// around them. flat planes (plants) aren't boxes, so a model made only of planes gets one box around all of them
export function model_shape(model: ModelJson, y_rotation = 0): Shape {
const boxes: Box[] = [];
let bounds: Box | undefined;
for (const element of model.elements) {
const rotate = element_rotation(element);
const min = [Infinity, Infinity, Infinity];
const max = [-Infinity, -Infinity, -Infinity];
for (let corner = 0; corner < 8; corner++) {
const point = rotate(
[0, 1, 2].map((axis) => ((corner >> axis) & 1 ? element.to[axis] : element.from[axis]) / 16),
);
for (let axis = 0; axis < 3; axis++) {
min[axis] = Math.min(min[axis], point[axis]);
max[axis] = Math.max(max[axis], point[axis]);
}
}
const box = clamp([...min, ...max]);
bounds = bounds ? clamp(bounds.map((v, i) => (i < 3 ? Math.min : Math.max)(v, box[i]))) : box;
if ([0, 1, 2].every((axis) => box[axis + 3] - box[axis] > FLAT)) boxes.push(box);
}
if (boxes.length === 0 && bounds) boxes.push(bounds);
return boxes.map((box) => turn(box, y_rotation));
}
// boxes in pixels, like a block's "shape" ([x1, y1, z1, x2, y2, z2], 0 to 16), turned like the model is
export function pixel_shape(boxes: number[][], y_rotation = 0): Shape {
return boxes.map((box) => turn(clamp(box.map((v) => v / 16)), y_rotation));
}
// what the block is at a value with state bits: its "shape" when it (or a variant) sets one, otherwise its model's
export function block_shape(block: BlockRegistry, states?: Record<string, number>): Shape {
const variant = block_variant(block, states);
if (variant.shape) return pixel_shape(variant.shape, variant.y);
const model = find_model(variant.model, Object.fromEntries(EverythingRegistry.entries<ModelJson>("models")));
return model ? model_shape(model, variant.y) : FULL_SHAPE;
}
// shapes by block, then by value with state bits, worked out the first time they're asked for
const cache = new WeakMap<BlockRegistry, Map<number, Shape>>();
// the block's shape at a value from the world, see block_shape
export function shape_at(block: BlockRegistry, value: number): Shape {
let shapes = cache.get(block);
if (!shapes) cache.set(block, shapes = new Map());
const key = block.variants ? value : 0;
let shape = shapes.get(key);
if (!shape) {
let states: Record<string, number> | undefined;
if (block.variants && block.states) {
states = {};
for (const state of block.states) states[state.name] = get_state_value(value, block, state.name)!;
}
shape = block_shape(block, states);
shapes.set(key, shape);
}
return shape;
}
// what entities bump into, like getCollisionShape: the shape, or nothing for blocks with "collision": false
export function collision_shape_at(block: BlockRegistry, value: number): Shape {
return block.has_collision ? shape_at(block, value) : EMPTY_SHAPE;
}
// how far along the ray (origin plus direction times distance) it first goes into one of the boxes of a block at
// x, y, z, and the side it went in by as the axis and which way along it the ray was going. undefined if it misses
export function clip_shape(
shape: Shape,
x: number,
y: number,
z: number,
origin: number[],
direction: number[],
max_distance: number,
): { distance: number; axis: number; sign: number } | undefined {
const offset = [x, y, z];
let closest: { distance: number; axis: number; sign: number } | undefined;
for (const box of shape) {
let enter = 0;
let enter_axis = -1;
let leave = closest?.distance ?? max_distance;
for (let axis = 0; axis < 3 && enter <= leave; axis++) {
const min = offset[axis] + box[axis];
const max = offset[axis] + box[axis + 3];
if (Math.abs(direction[axis]) < 1e-9) {
if (origin[axis] < min || origin[axis] > max) enter = Infinity;
continue;
}
const a = (min - origin[axis]) / direction[axis];
const b = (max - origin[axis]) / direction[axis];
if (Math.min(a, b) >= enter) {
enter = Math.min(a, b);
enter_axis = axis;
}
leave = Math.min(leave, Math.max(a, b));
}
if (enter <= leave && enter_axis >= 0) {
closest = { distance: enter, axis: enter_axis, sign: Math.sign(direction[enter_axis]) };
}
}
return closest;
}
function clamp(box: number[]): Box {
const c = box.map((v) => Math.min(Math.max(v, 0), 1));
return [c[0], c[1], c[2], c[3], c[4], c[5]];
}
// clockwise seen from above by 0, 90, 180 or 270 degrees, the same as bake_model turns quads
function turn(box: Box, y_rotation: number): Box {
const turns = ((Math.round(y_rotation / 90) % 4) + 4) % 4;
let [x1, y1, z1, x2, y2, z2] = box;
for (let t = 0; t < turns; t++) {
// a point (x, z) goes to (1 - z, x)
[x1, z1, x2, z2] = [1 - z2, x1, 1 - z1, x2];
}
return [x1, y1, z1, x2, y2, z2];
}