This commit is contained in:
2026-10-03 16:28:09 -03:00
parent 24fe7cdede
commit 91655c28c5
53 changed files with 1752 additions and 114 deletions
+95 -8
View File
@@ -3,6 +3,7 @@
// into quads in block space (0 to 1) that the chunk mesher and the item renderers draw
import type { BlockRegistry, BlockTextures } from "./everything_registry.ts";
import { tint_without_biome } from "./worldgen/biome_colors.ts";
import { get_state_value } from "./utils.ts";
export type ModelFace = "top" | "bottom" | "north" | "south" | "west" | "east";
export const MODEL_FACES: ModelFace[] = ["top", "bottom", "north", "south", "west", "east"];
@@ -51,6 +52,30 @@ export interface BlockVariant {
// what the player points at and bumps into, as boxes in pixels: [x1, y1, z1, x2, y2, z2]. the model's boxes when
// not set, see common/block_shapes.ts
shape?: number[][];
// what entities bump into, when it isn't the shape. can go up to 24 pixels high, like a fence's
collision_shape?: number[][];
}
// one piece of a block made of pieces, like minecraft's multipart blockstates: a fence is a post, and a bar for each
// side it connects on. every part whose "when" matches is drawn, and adds its shape
export interface BlockPart {
// a condition on the block's states like a variant's key, "north=1". always when not set
when?: string;
model: string;
// the block's textures when not set
textures?: BlockTextures;
y?: number;
shape?: number[][];
collision_shape?: number[][];
}
// a model with everything it's drawn with, a variant or a part
export interface PlacedModel {
model: string;
textures: BlockTextures;
y: number;
shape?: number[][];
collision_shape?: number[][];
}
export const DEFAULT_MODEL = "engine:cube";
@@ -147,14 +172,12 @@ export function texture_variables(reference: string, textures: BlockTextures | u
// the block's model, textures, turn and shape for a value with state bits. each comes from the first variant whose conditions
// match that sets it, so "lit=1" can change the textures and "facing=1" the turn of the same block
export function block_variant(
block: BlockRegistry,
states?: Record<string, number>,
): Required<Omit<BlockVariant, "shape">> & Pick<BlockVariant, "shape"> {
export function block_variant(block: BlockRegistry, states?: Record<string, number>): PlacedModel {
let model: string | undefined;
let textures: BlockTextures | undefined;
let y: number | undefined;
let shape: number[][] | undefined;
let collision_shape: number[][] | undefined;
if (block.variants && states) {
for (const [condition, override] of Object.entries(block.variants)) {
if (!variant_matches(condition, states)) continue;
@@ -162,6 +185,7 @@ export function block_variant(
textures ??= override.textures;
y ??= override.y;
shape ??= override.shape;
collision_shape ??= override.collision_shape;
}
}
return {
@@ -169,9 +193,55 @@ export function block_variant(
textures: textures ?? block.textures,
y: y ?? 0,
shape: shape ?? block.shape,
collision_shape: collision_shape ?? block.collision_shape,
};
}
// what a block is drawn with at a value with state bits: every matching part for a block made of parts, or its variant.
// parts take the variant's textures when they don't set their own
export function block_parts(block: BlockRegistry, states?: Record<string, number>): PlacedModel[] {
const variant = block_variant(block, states);
if (!block.parts) return [variant];
return block.parts
.filter((part) => variant_matches(part.when ?? "", states ?? {}))
.map((part) => ({
model: part.model,
textures: part.textures ?? variant.textures,
y: part.y ?? 0,
shape: part.shape,
collision_shape: part.collision_shape,
}));
}
// whether it's a full cube in every state, so it hides what's behind it from light like minecraft's solid render
// shape. slabs, fences and pots aren't, even with collision
export function is_full_cube(block: BlockRegistry) {
const cube = (model: string | undefined) => (model ?? DEFAULT_MODEL) === DEFAULT_MODEL;
return !block.parts && !block.shape && cube(block.model) &&
Object.values(block.variants ?? {}).every((variant) => !variant.shape && cube(variant.model ?? block.model));
}
// whether how the block looks depends on its states, so it has to be worked out per value and not per block
export function looks_by_state(block: BlockRegistry) {
return block.variants !== undefined || block.parts !== undefined;
}
// the states of a value with state bits, by name
export function states_of(block: BlockRegistry, value: number): Record<string, number> {
const states: Record<string, number> = {};
for (const state of block.states ?? []) states[state.name] = get_state_value(value, block, state.name)!;
return states;
}
// every quad of the block at a value with state bits, its parts' quads together
export function bake_block(block: BlockRegistry, value: number, models: Record<string, ModelJson>): BakedQuad[] {
const states = looks_by_state(block) ? states_of(block, value) : undefined;
return block_parts(block, states).flatMap((part) => {
const model = find_model(part.model, models) ?? BUILTIN_MODELS[DEFAULT_MODEL];
return bake_model(model, part.textures, part.y);
});
}
// "age=7" or "age=7,facing=2", "" matches everything
export function variant_matches(condition: string, states: Record<string, number>) {
if (condition === "") return true;
@@ -181,10 +251,12 @@ export function variant_matches(condition: string, states: Record<string, number
});
}
// for inventories and dropped items: blocks with a full cube draw as a cube, anything else as a flat sprite
// of its first texture, like minecraft's items for plants
// for inventories and dropped items: blocks with a full cube or an item_model draw as that model, anything else as a
// flat sprite of its first texture, like minecraft's items for plants
export function block_item_texture(block: BlockRegistry): string | undefined {
if ((block.model ?? DEFAULT_MODEL) === DEFAULT_MODEL) return undefined;
if (block.item_model !== undefined || (!block.parts && (block.model ?? DEFAULT_MODEL) === DEFAULT_MODEL)) {
return undefined;
}
const textures = block.textures;
if (typeof textures === "string") return textures;
return Object.values(textures)[0] ?? "engine:missing";
@@ -197,12 +269,27 @@ export function cube_face_texture(block: BlockRegistry, face: "top" | "bottom" |
// the color to draw a face of a block's item in, as r g b from 0 to 1. items aren't in a biome, so a biome "tint"
// gets the default grass or foliage color, like minecraft's grass block item
export function cube_face_tint(block: BlockRegistry, face: "top" | "bottom" | "front" | "side"): number[] {
const variables = texture_variables(`#${face}`, block.textures, BUILTIN_MODELS[DEFAULT_MODEL]);
return item_tint(block, texture_variables(`#${face}`, block.textures, BUILTIN_MODELS[DEFAULT_MODEL]));
}
// the color to draw a part of a block's item in that got its texture through these variables, see cube_face_tint
export function item_tint(block: BlockRegistry, variables: string[]): number[] {
if (!is_tinted(block, variables)) return [1, 1, 1];
const color = tint_without_biome(block.tint!.color);
return [color >> 16, (color >> 8) & 0xff, color & 0xff].map((c) => c / 255);
}
// the quads of a block's item_model with the block's textures, for its item in inventories, hands and on the ground.
// undefined for blocks without one
export function block_item_quads(block: BlockRegistry, models: Record<string, ModelJson>): BakedQuad[] | undefined {
if (block.item_model === undefined) return undefined;
const model = find_model(block.item_model, models);
return model ? bake_model(model, block.textures) : undefined;
}
// how much darker each side of an item's model is drawn, like the mesher's: top, bottom, south, north, west, east
export const ITEM_FACE_SHADE = [1.0, 0.5, 0.8, 0.8, 0.6, 0.6];
// whether a face that got its texture through these variables is drawn in the block's tint
export function is_tinted(block: BlockRegistry, variables: string[]) {
if (!block.tint) return false;