Item rendering
This commit is contained in:
@@ -3,8 +3,16 @@
|
||||
|
||||
// java block models (assets/<namespace>/models/**.json) to bworld's models/. the formats are close: faces are top and
|
||||
// bottom instead of up and down, ids lose their folders, a parent keeps being a parent (bworld merges them when it
|
||||
// loads, see resolve_models), and what bworld has no use for (blockbench's names, display transforms) is left out
|
||||
import type { ModelElementJson, ModelFace, ModelFaceJson, ModelFileJson } from "$/common/block_models.ts";
|
||||
// loads, see resolve_models), and what bworld has no use for (blockbench's names, display transforms for places bworld
|
||||
// doesn't show items, like third person) is left out
|
||||
import {
|
||||
DISPLAY_CONTEXTS,
|
||||
type ModelDisplay,
|
||||
type ModelElementJson,
|
||||
type ModelFace,
|
||||
type ModelFaceJson,
|
||||
type ModelFileJson,
|
||||
} from "$/common/block_models.ts";
|
||||
import { FORMAT_VERSION } from "$/common/mod_data.ts";
|
||||
import { join } from "@std/path";
|
||||
import {
|
||||
@@ -53,16 +61,18 @@ const NOT_BOXES: Record<string, string> = {
|
||||
"minecraft:builtin/generated": "an item sprite",
|
||||
"minecraft:builtin/entity": "drawn by code",
|
||||
};
|
||||
// minecraft's roots, which hold nothing a bworld model can use: block/block is only display transforms
|
||||
// minecraft's roots, which hold nothing a bworld model needs: block/block is only the display transforms of blocks'
|
||||
// items, which bworld has its own of
|
||||
const EMPTY_PARENTS = ["minecraft:block/block", "minecraft:block/air"];
|
||||
|
||||
// whether a model holds nothing a bworld model can use, all the way up its parents, like block/block or a mod's own
|
||||
// root of display transforms. a model with one as its parent leaves it out, since it isn't written
|
||||
// root of third person display transforms. a model with one as its parent leaves it out, since it isn't written
|
||||
export function is_empty(location: string, read: ModelSource, seen = new Set<string>()): boolean {
|
||||
if (EMPTY_PARENTS.includes(location)) return true;
|
||||
const json = read(location);
|
||||
if (!json || seen.has(location) || json.elements !== undefined) return false;
|
||||
if (Object.keys((json.textures ?? {}) as Record<string, string>).length > 0) return false;
|
||||
if (convert_display(json.display) !== undefined) return false;
|
||||
seen.add(location);
|
||||
return typeof json.parent !== "string" || is_empty(location_string(parse_location(json.parent)), read, seen);
|
||||
}
|
||||
@@ -161,8 +171,9 @@ export function convert_model(location: string, read: ModelSource, remaps: Remap
|
||||
const elements = Array.isArray(json.elements)
|
||||
? json.elements.map((element, i) => convert_element(element, `elements[${i}]`, texture, problems, warnings))
|
||||
: undefined;
|
||||
if (parent === undefined && elements === undefined && Object.keys(textures).length === 0) {
|
||||
return { kind: "skipped", reason: "it has nothing a bworld model holds, like only display transforms" };
|
||||
const display = convert_display(json.display);
|
||||
if (parent === undefined && elements === undefined && Object.keys(textures).length === 0 && !display) {
|
||||
return { kind: "skipped", reason: "it has nothing a bworld model holds, like only third person transforms" };
|
||||
}
|
||||
|
||||
const id = to_id(location, remaps.models);
|
||||
@@ -173,10 +184,26 @@ export function convert_model(location: string, read: ModelSource, remaps: Remap
|
||||
...(parent !== undefined ? { parent } : {}),
|
||||
...(Object.keys(textures).length > 0 ? { textures } : {}),
|
||||
...(elements !== undefined ? { elements } : {}),
|
||||
...(display ? { display } : {}),
|
||||
};
|
||||
return { kind: "model", id, json: { format_version: FORMAT_VERSION, model }, warnings: [...warnings] };
|
||||
}
|
||||
|
||||
// the display transforms of the places bworld shows items, undefined when there are none
|
||||
function convert_display(json: unknown): ModelDisplay | undefined {
|
||||
const display: ModelDisplay = {};
|
||||
for (const context of DISPLAY_CONTEXTS) {
|
||||
const transform = (json as Record<string, Record<string, unknown>> | undefined)?.[context];
|
||||
if (typeof transform !== "object" || transform === null) continue;
|
||||
display[context] = Object.fromEntries(
|
||||
["rotation", "translation", "scale"]
|
||||
.filter((key) => Array.isArray(transform[key]))
|
||||
.map((key) => [key, transform[key]]),
|
||||
);
|
||||
}
|
||||
return Object.keys(display).length > 0 ? display : undefined;
|
||||
}
|
||||
|
||||
// why a model isn't boxes bworld can draw, going up its parents: an item sprite, or one a loader makes (obj files,
|
||||
// composites). undefined for boxes, or when its parents can't be followed, which check-mods then reports
|
||||
export function made_by(location: string, read: ModelSource): string | undefined {
|
||||
|
||||
Reference in New Issue
Block a user