Render geo models in items
This commit is contained in:
@@ -500,7 +500,7 @@ export function bake_model(
|
||||
let cull = face_json.cullface ? FACE_INDEX[face_json.cullface] : -1;
|
||||
for (let t = 0; t < x_turns && cull >= 0; t++) cull = TURN_X[cull];
|
||||
for (let t = 0; t < turns && cull >= 0; t++) cull = TURN_Y[cull];
|
||||
const { face: facing, flush, aligned } = classify(positions);
|
||||
const { face: facing, flush, aligned } = classify_quad(positions);
|
||||
quads.push({
|
||||
positions,
|
||||
uvs,
|
||||
@@ -560,7 +560,7 @@ export function element_rotation(element: ModelElementJson): (point: number[]) =
|
||||
}
|
||||
|
||||
// which way a quad faces most, whether it's flat against the block's edge, and whether it faces straight along an axis
|
||||
function classify(p: number[]): { face: number; flush: boolean; aligned: boolean } {
|
||||
export function classify_quad(p: number[]): { face: number; flush: boolean; aligned: boolean } {
|
||||
const e1 = [p[3] - p[0], p[4] - p[1], p[5] - p[2]];
|
||||
const e2 = [p[9] - p[0], p[10] - p[1], p[11] - p[2]];
|
||||
const normal = [e1[1] * e2[2] - e1[2] * e2[1], e1[2] * e2[0] - e1[0] * e2[2], e1[0] * e2[1] - e1[1] * e2[0]];
|
||||
|
||||
@@ -284,7 +284,8 @@ export interface ItemRegistry<T = unknown | undefined> {
|
||||
texture_id: string | ((item: ItemStack<T>) => string);
|
||||
// shown in place of texture_id while the stack's data has that key set, like a cast fishing rod. the first wins
|
||||
data_textures?: Record<string, string>;
|
||||
// a model it's drawn with instead of its texture, with the model's own textures, like a 3d wrench
|
||||
// a model it's drawn with instead of its texture, with the model's own textures, like a 3d wrench. or an entity
|
||||
// model (see EntityModelJson), drawn in its texture
|
||||
model?: string;
|
||||
// what players see, like "Iron Ingot". made from the id when not set, see display_name in common/utils.ts
|
||||
name?: string;
|
||||
|
||||
+2
-1
@@ -361,7 +361,8 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
errors.push(`${file}: item ${json.id} has no texture, and no block of this mod gives it an item`);
|
||||
}
|
||||
for (const texture of Object.values(json.data_textures ?? {})) uses(file, texture, "texture");
|
||||
if (json.model) uses(file, json.model, "model");
|
||||
// an entity model is drawn in the item's texture
|
||||
if (json.model && !entity_model_owners.has(json.model)) uses(file, json.model, "model");
|
||||
if (json.places) uses(file, json.places, "block");
|
||||
if (json.food?.returns) uses(file, json.food.returns, "item");
|
||||
for (const layer of json.layers ?? []) uses(file, layer.texture, "texture");
|
||||
|
||||
+23
-1
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user