Improve a buuuunch of textures and some reworks

This commit is contained in:
2026-09-27 18:05:20 -03:00
parent 9f47c709f1
commit b7ded817d9
186 changed files with 1486 additions and 124 deletions
+33
View File
@@ -2,6 +2,7 @@
// a block picks one with "model" and fills in its texture variables with "textures". baking turns a model
// 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";
export type ModelFace = "top" | "bottom" | "north" | "south" | "west" | "east";
export const MODEL_FACES: ModelFace[] = ["top", "bottom", "north", "south", "west", "east"];
@@ -126,6 +127,20 @@ export function resolve_texture(reference: string, textures: BlockTextures | und
return reference.startsWith("#") ? "engine:missing" : reference;
}
// the variables a texture reference goes through before it's a texture id, "#front" on a cube is front then side
export function texture_variables(reference: string, textures: BlockTextures | undefined, model?: ModelJson) {
const variables: string[] = [];
for (let depth = 0; depth < 8 && reference.startsWith("#"); depth++) {
const name = reference.slice(1);
variables.push(name);
if (typeof textures === "string") break;
const next = (textures as Record<string, string> | undefined)?.[name] ?? model?.textures?.[name];
if (next === undefined) break;
reference = next;
}
return variables;
}
// the block's model and textures for a value with state bits, from the first variant whose conditions match
export function block_variant(block: BlockRegistry, states?: Record<string, number>): Required<BlockVariant> {
const variant: Required<BlockVariant> = {
@@ -168,6 +183,21 @@ export function cube_face_texture(block: BlockRegistry, face: "top" | "bottom" |
return resolve_texture(`#${face}`, block.textures, BUILTIN_MODELS[DEFAULT_MODEL]);
}
// 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]);
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);
}
// 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;
return !block.tint.textures || block.tint.textures.some((name) => variables.includes(name));
}
// baking
export interface BakedQuad {
@@ -176,6 +206,8 @@ export interface BakedQuad {
// u v per corner, in pixels of the texture (0-16)
uvs: number[];
texture: string;
// the texture variables it went through to get there, like ["front", "side"], for "tint"
variables: string[];
// the side it faces most, as the mesher's face index (see FACE_INDEX)
face: number;
// the side whose neighbor can hide it, or -1
@@ -257,6 +289,7 @@ export function bake_model(model: ModelJson, textures: BlockTextures | undefined
positions,
uvs,
texture: resolve_texture(face_json.texture, textures, model),
variables: texture_variables(face_json.texture, textures, model),
face: facing,
cull,
flush,