Item rendering
This commit is contained in:
@@ -132,7 +132,6 @@ export function convert_blockstate(location: string, json: unknown, read: ModelS
|
||||
if (Array.isArray(value) && value.length > 1) {
|
||||
warnings.add("random models were left out, the first is always used");
|
||||
}
|
||||
if (first.uvlock) warnings.add("uvlock was left out, so turned textures turn with their model");
|
||||
return first;
|
||||
};
|
||||
|
||||
@@ -184,7 +183,7 @@ export function convert_blockstate(location: string, json: unknown, read: ModelS
|
||||
// java takes -90 for 270
|
||||
const x = ((apply.x ?? 0) % 360 + 360) % 360;
|
||||
const y = ((apply.y ?? 0) % 360 + 360) % 360;
|
||||
return { model, ...(x ? { x } : {}), ...(y ? { y } : {}) };
|
||||
return { model, ...(x ? { x } : {}), ...(y ? { y } : {}), ...(apply.uvlock ? { uvlock: true } : {}) };
|
||||
};
|
||||
|
||||
const block: Record<string, unknown> = { id, textures: {} };
|
||||
@@ -249,9 +248,9 @@ export function convert_blockstate(location: string, json: unknown, read: ModelS
|
||||
return { kind: "block", id, json: { format_version: FORMAT_VERSION, block }, warnings: [...warnings] };
|
||||
}
|
||||
|
||||
// why the model kind won't have written a model for a block to be drawn with, if it won't: one the remaps skip, one
|
||||
// made by a loader (obj files, composites), one that doesn't exist, or minecraft's that no remap gives bworld's for
|
||||
function model_missing(from: string, id: string, read: ModelSource, remaps: Remaps): string | undefined {
|
||||
// why the model kind won't have written a model for a block or item to be drawn with, if it won't: one the remaps skip,
|
||||
// one made by a loader (obj files, composites), one that doesn't exist, or minecraft's that no remap gives bworld's for
|
||||
export function model_missing(from: string, id: string, read: ModelSource, remaps: Remaps): string | undefined {
|
||||
if (remaps.skip?.some((pattern) => new RegExp(pattern).test(from))) return "is skipped by the remaps";
|
||||
if (parse_location(from).namespace === "minecraft") {
|
||||
return id.startsWith("minecraft:") ? "is minecraft's, and no --remap names a bworld model for it" : undefined;
|
||||
|
||||
+11
-5
@@ -3,12 +3,12 @@
|
||||
|
||||
// java item models (assets/<namespace>/models/item/*.json) to bworld's items/, for the items that aren't a block's: a
|
||||
// block's item comes with the block (see blocks.ts). java says an item is a flat sprite with item/generated and its
|
||||
// layer0 texture, which is what a bworld item is. one java draws in 3D (a wrench, goggles) is drawn flat here, with a
|
||||
// texture of its model, until bworld items can have models
|
||||
// layer0 texture, which is what a bworld item is. one java draws in 3D (a wrench) is drawn with its model, which the
|
||||
// model kind writes, and gets its model's particle texture as its texture
|
||||
import { join } from "@std/path";
|
||||
import { FORMAT_VERSION } from "$/common/mod_data.ts";
|
||||
import { made_by, model_source } from "./models.ts";
|
||||
import { merged_textures, models_near } from "./blocks.ts";
|
||||
import { merged_textures, model_missing, models_near } from "./blocks.ts";
|
||||
import {
|
||||
DEFAULT_REMAPS,
|
||||
empty_report,
|
||||
@@ -60,6 +60,7 @@ export function import_items(input: string, options: ImportOptions = {}): Import
|
||||
return reference?.startsWith("#") ? undefined : reference;
|
||||
};
|
||||
let texture: string | undefined;
|
||||
let model: string | undefined;
|
||||
if (made_by(location, read)?.startsWith("an item sprite")) {
|
||||
texture = resolve(textures.layer0);
|
||||
if (textures.layer1 !== undefined) {
|
||||
@@ -67,7 +68,12 @@ export function import_items(input: string, options: ImportOptions = {}): Import
|
||||
}
|
||||
} else {
|
||||
texture = resolve(textures.particle) ?? Object.values(textures).map(resolve).find((t) => t !== undefined);
|
||||
report.warnings.push({ location, warning: "drawn in 3D in java, it's a flat texture of its model here" });
|
||||
model = to_id(location, remaps.models);
|
||||
const missing = model_missing(location, model, read, remaps);
|
||||
if (missing) {
|
||||
report.warnings.push({ location, warning: `its model ${missing}, so it's a flat texture of it here` });
|
||||
model = undefined;
|
||||
}
|
||||
}
|
||||
if (texture === undefined) {
|
||||
// like item/pane, a template other item models use as their parent
|
||||
@@ -80,7 +86,7 @@ export function import_items(input: string, options: ImportOptions = {}): Import
|
||||
continue;
|
||||
}
|
||||
if (!claim(id, location)) continue;
|
||||
const json = { format_version: FORMAT_VERSION, item: { id, texture: texture_id } };
|
||||
const json = { format_version: FORMAT_VERSION, item: { id, texture: texture_id, ...(model ? { model } : {}) } };
|
||||
report.written.push({
|
||||
location,
|
||||
id,
|
||||
|
||||
@@ -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