Render geo models in items

This commit is contained in:
2026-11-22 12:24:14 -03:00
parent dee07eaa0c
commit e751d22c6e
10 changed files with 171 additions and 29 deletions
+23 -1
View File
@@ -23,7 +23,7 @@ import {
WATERLOGGED,
type WornProperties,
} from "./everything_registry.ts";
import type { BlockPart, BlockVariant, ModelFileJson } from "./block_models.ts";
import type { BlockPart, BlockVariant, ModelDisplay, ModelFileJson } from "./block_models.ts";
import { expand_items, list_matches } from "./tags.ts";
import { GAME_VERSION } from "./constants.ts";
import type { PlayOptions } from "./mod_api/shared.ts";
@@ -567,6 +567,8 @@ const entity_model_schema = z.object({
// the size its uvs are laid out for, the texture is stretched to it
texture_width: z.int().min(1),
texture_height: z.int().min(1),
// how it's placed where it's shown as an item, from the geometry's item_display_transforms
display: z.partialRecord(z.enum(DISPLAY_CONTEXTS), display_transform).optional(),
// parents come before their children
bones: z.array(entity_bone).superRefine((bones, ctx) => {
const names = new Set<string>();
@@ -1063,10 +1065,12 @@ export function entity_model_from_geo(json: unknown, id: string): { model?: Enti
};
}
const bones = Array.isArray(geometry.bones) ? geometry.bones : [];
const display = geo_display(geometry.item_display_transforms);
const model = {
id,
texture_width: geometry.description.texture_width,
texture_height: geometry.description.texture_height,
...(display === undefined ? {} : { display }),
bones: bones.map((bone) => {
if (!is_object(bone)) return bone;
const { name, parent, pivot, rotation } = bone;
@@ -1094,6 +1098,24 @@ export function entity_model_from_geo(json: unknown, id: string): { model?: Enti
return problems.length === 0 ? { model: model as EntityModelJson, problems } : { problems };
}
// the places a geometry's item_display_transforms has that items are shown in, with what of each the engine uses. their
// rotation_pivot, scale_pivot and fit_to_frame are left out
function geo_display(transforms: unknown): ModelDisplay | undefined {
if (!is_object(transforms)) return undefined;
const display: Record<string, unknown> = {};
for (const context of DISPLAY_CONTEXTS) {
const transform = transforms[context];
if (!is_object(transform)) continue;
const { rotation, translation, scale } = transform;
display[context] = {
...(rotation === undefined ? {} : { rotation }),
...(translation === undefined ? {} : { translation }),
...(scale === undefined ? {} : { scale }),
};
}
return Object.keys(display).length > 0 ? display as ModelDisplay : undefined;
}
export function validate_clip(json: unknown): Problems {
return find_problems(clip_schema, json);
}