Better block models !

This commit is contained in:
2026-10-03 15:57:47 -03:00
parent ecc0295f6d
commit 24fe7cdede
23 changed files with 678 additions and 122 deletions
+120
View File
@@ -0,0 +1,120 @@
import { assert, assertAlmostEquals, assertEquals } from "@std/assert";
import { BUILTIN_MODELS, type ModelJson } from "$/common/block_models.ts";
import {
block_shape,
clip_shape,
collision_shape_at,
EMPTY_SHAPE,
FULL_SHAPE,
model_shape,
type Shape,
} from "$/common/block_shapes.ts";
import { block_from_json, validate_block } from "$/common/mod_data.ts";
import { move_body } from "$/common/physics.ts";
import { box_overlaps_block } from "$/common/utils.ts";
const slab: ModelJson = {
id: "test:slab",
elements: [{ from: [0, 0, 0], to: [16, 8, 16], faces: { top: { texture: "#side" } } }],
};
const SLAB: Shape = [[0, 0, 0, 1, 0.5, 1]];
Deno.test("a model's shape is the boxes of its elements", () => {
assertEquals(model_shape(BUILTIN_MODELS["engine:cube"]), FULL_SHAPE);
assertEquals(model_shape(slab), SLAB);
const stairs: ModelJson = {
id: "test:stairs",
elements: [
{ from: [0, 0, 0], to: [16, 8, 16], faces: {} },
{ from: [0, 8, 8], to: [16, 16, 16], faces: {} },
],
};
assertEquals(model_shape(stairs), [[0, 0, 0, 1, 0.5, 1], [0, 0.5, 0.5, 1, 1, 1]]);
// turned a quarter clockwise seen from above, the back half goes from south to west
assertEquals(model_shape(stairs, 90)[1], [0, 0.5, 0, 0.5, 1, 1]);
});
Deno.test("models made of planes get one box around them, so plants can still be pointed at", () => {
assertEquals(model_shape(BUILTIN_MODELS["engine:crop"]), [[0, 0, 0, 1, 1, 1]]);
const [cross] = model_shape(BUILTIN_MODELS["engine:cross"]);
assertEquals(cross[1], 0);
assertEquals(cross[4], 1);
assert(cross[0] < 0.1 && cross[3] > 0.9);
});
Deno.test("a block's shape follows its model, or its own shape in pixels, turned with the variant", () => {
const { block } = block_from_json({
id: "test:half",
textures: "test:stone",
shape: [[0, 0, 0, 16, 8, 8]],
states: [{ name: "facing", bits: 2, default: 0 }],
variants: { "facing=1": { y: 90 } },
});
assertEquals(block_shape(block, { facing: 0 }), [[0, 0, 0, 1, 0.5, 0.5]]);
assertEquals(block_shape(block, { facing: 1 }), [[0.5, 0, 0, 1, 0.5, 1]]);
const { block: plant } = block_from_json({
id: "test:plant",
textures: { cross: "test:flower" },
model: "engine:cross",
collision: false,
});
assertEquals(collision_shape_at(plant, 0), EMPTY_SHAPE);
});
Deno.test("shapes must be boxes in pixels", () => {
const block = { id: "test:a", textures: "test:a" };
assertEquals(validate_block({ ...block, shape: [[0, 0, 0, 16, 8, 16]] }), []);
assertEquals(validate_block({ ...block, shape: [[0, 0, 0, 16, 8]] }).length, 1);
assertEquals(validate_block({ ...block, shape: [[0, 8, 0, 16, 0, 16]] }).length, 1);
assertEquals(validate_block({ ...block, shape: [] }).length, 1);
});
Deno.test("bodies land on a slab's top and walk under nothing where its other half is empty", () => {
const slab_at_origin = (x: number, y: number, z: number) => y === -1 || (x === 0 && y === 0 && z === 0 && SLAB);
const falling = { x: 0.5, y: 2, z: 0.5, vx: 0, vy: 0, vz: 0, width: 0.6, height: 1.8 };
for (let i = 0; i < 40; i++) move_body(falling, -32, slab_at_origin);
assertAlmostEquals(falling.y, 0.5, 1e-6);
// walking into it steps up onto it, it's under the step height
const walking = { x: -1, y: 0, z: 0.5, vx: 4, vy: 0, vz: 0, width: 0.6, height: 1.8 };
for (let i = 0; i < 10; i++) {
walking.vx = 4;
move_body(walking, -32, slab_at_origin, 0.6, true);
}
assert(walking.x > 0.5);
assertAlmostEquals(walking.y, 0.5, 1e-6);
// a slab on the ceiling leaves room for its empty bottom half
const upper: Shape = [[0, 0.5, 0, 1, 1, 1]];
const ceiling = (x: number, y: number, z: number) => y === -1 || (x === 0 && y === 1 && z === 0 && upper);
const jumping = { x: 0.5, y: 0, z: 0.5, vx: 0, vy: 10, vz: 0, width: 0.6, height: 1 };
move_body(jumping, 0, ceiling);
assertAlmostEquals(jumping.y, 0.5, 1e-6);
});
Deno.test("a ray goes into a block only where its boxes are", () => {
// level with the slab's empty top half, it misses
assertEquals(clip_shape(SLAB, 0, 0, 0, [-1, 0.75, 0.5], [1, 0, 0], 5), undefined);
// looking down at it, it hits the top 0.5 below the eye, on the top face
const hit = clip_shape(SLAB, 0, 0, 0, [0.5, 1, 0.5], [0, -1, 0], 5)!;
assertAlmostEquals(hit.distance, 0.5);
assertEquals([hit.axis, hit.sign], [1, -1]);
// out of reach
assertEquals(clip_shape(SLAB, 0, 0, 0, [0.5, 10, 0.5], [0, -1, 0], 5), undefined);
});
Deno.test("only a placed block's boxes are in the way of whoever stands there", () => {
const player = { x: 0.5, y: 0.6, z: 0.5, width: 0.6, height: 1.8 };
assert(box_overlaps_block(player, 0, 0, 0));
assert(!box_overlaps_block(player, 0, 0, 0, SLAB));
});
Deno.test("the outline is the edges of the shape's boxes, a little outside them", async () => {
const { shape_edges } = await import("$/client/rendering/level_renderer.ts");
const cube = shape_edges(FULL_SHAPE);
assertEquals(cube.length, 12);
assert(cube.every((edge) => edge.every((v) => v < 0 || v > 1)));
assertEquals(shape_edges(SLAB).length, 12);
// edges two boxes share are drawn once
assertEquals(shape_edges([...SLAB, ...SLAB]).length, 12);
});
+7 -7
View File
@@ -12,7 +12,7 @@ function setup(ceiling = Infinity, world?: { solid: Blocks; water: Blocks }) {
const sent: { y: number; swimming?: boolean }[] = [];
const client = {
level: {
has_collision: world?.solid ?? ((_x: number, y: number) => y < 0 || y >= ceiling),
collision_shape: world?.solid ?? ((_x: number, y: number) => y < 0 || y >= ceiling),
is_liquid: world?.water ?? (() => false),
},
screen: undefined,
@@ -190,9 +190,9 @@ Deno.test("sprinting starts going forward and keeps going until it stops, sneaks
// running into a wall stops it, running along one doesn't
for (const [yaw, stops] of [[0, true], [Math.PI / 2, false]] as const) {
const { player } = setup();
const level = player.level as unknown as { has_collision: (x: number, y: number, z: number) => boolean };
const level = player.level as unknown as { collision_shape: (x: number, y: number, z: number) => boolean };
// a wall on the -z side
level.has_collision = (_x, y, z) => y < 0 || z < -2;
level.collision_shape = (_x, y, z) => y < 0 || z < -2;
player.set_position(0.5, 0, -1);
player.yaw = yaw;
run(player, 1, () => false);
@@ -223,8 +223,8 @@ Deno.test("sneaking doesn't walk off edges, from any direction, but still jumps
for (const [key, yaw] of [["KeyW", 0], ["KeyW", Math.PI / 4], ["KeyD", 0], ["KeyS", 1], ["KeyA", 2.5]] as const) {
// a 3x3 platform with nothing around it
const { player } = setup();
const level = player.level as unknown as { has_collision: (x: number, y: number, z: number) => boolean };
level.has_collision = (x, y, z) => y === -1 && x >= 0 && x < 3 && z >= 0 && z < 3;
const level = player.level as unknown as { collision_shape: (x: number, y: number, z: number) => boolean };
level.collision_shape = (x, y, z) => y === -1 && x >= 0 && x < 3 && z >= 0 && z < 3;
player.set_position(1.5, 0, 1.5);
player.yaw = yaw;
run(player, 1, () => false);
@@ -244,8 +244,8 @@ Deno.test("sneaking doesn't walk off edges, from any direction, but still jumps
Deno.test("stepping from the edge of a block onto the one diagonal to it doesn't fall between them", () => {
// floor only at (0, 0) and (1, 1)
const { player } = setup();
const level = player.level as unknown as { has_collision: (x: number, y: number, z: number) => boolean };
level.has_collision = (x, y, z) => y === -1 && ((x === 0 && z === 0) || (x === 1 && z === 1));
const level = player.level as unknown as { collision_shape: (x: number, y: number, z: number) => boolean };
level.collision_shape = (x, y, z) => y === -1 && ((x === 0 && z === 0) || (x === 1 && z === 1));
// on the +x edge of the first block, hanging over it, then walking diagonally to the other
player.set_position(1, 0, 0.3);
run(player, 1, () => false);