Render geo models in items
This commit is contained in:
@@ -9,13 +9,14 @@ import { get_sprite_region } from "@/client/sprites.ts";
|
||||
import type { ItemEntity } from "@/client/entity/item_entity.ts";
|
||||
import { contained_fluid, type ItemData, ItemStack } from "@/common/inventory.ts";
|
||||
import type { FluidVariant } from "@/common/mod_api/shared.ts";
|
||||
import type { FluidJson } from "@/common/mod_data.ts";
|
||||
import type { EntityModelJson, FluidJson } from "@/common/mod_data.ts";
|
||||
import { light_color, push_vertex, Texture } from "@/client/renderer/mod.ts";
|
||||
import {
|
||||
bake_model,
|
||||
type BakedQuad,
|
||||
block_item_texture,
|
||||
block_variant,
|
||||
classify_quad,
|
||||
cube_face_texture,
|
||||
cube_face_tint,
|
||||
type DisplayTransform,
|
||||
@@ -30,6 +31,8 @@ import { get_state_value, item_texture } from "@/common/utils.ts";
|
||||
import { ID_MASK, TEXTURE_SIZE } from "@/common/constants.ts";
|
||||
import type { ClientLevel } from "@/client/level/client_level.ts";
|
||||
import { entity_tint } from "./entity_light.ts";
|
||||
import { bone_matrices, IDENTITY } from "./model_math.ts";
|
||||
import { push_model } from "./model_renderer.ts";
|
||||
import type { Vec3 } from "./player_renderer.ts";
|
||||
import { fluid_rules } from "@/client/fluid_rules.ts";
|
||||
import { color_rgb, tint_without_biome, type TintColor } from "@/common/worldgen/biome_colors.ts";
|
||||
@@ -249,11 +252,15 @@ export interface ItemModel {
|
||||
block?: BlockRegistry;
|
||||
}
|
||||
|
||||
// an item's own model, or its block's item_model with the block's textures, baked the first time it's drawn. undefined
|
||||
// for items drawn as a cube or as their sprite
|
||||
// an item's own model, or its block's item_model with the block's textures, baked the first time it's drawn. an item's
|
||||
// own can be an entity model, drawn in the item's texture. undefined for items drawn as a cube or as their sprite
|
||||
export function item_model_of(item: ItemStack): ItemModel | undefined {
|
||||
const info = EverythingRegistry.get<ItemRegistry>("items", item.type_id);
|
||||
if (info?.model !== undefined) return baked_item_model(info, info.model);
|
||||
if (info?.model !== undefined) {
|
||||
const entity_model = EverythingRegistry.get<EntityModelJson>("entity_models", info.model);
|
||||
if (entity_model) return baked_entity_item_model(entity_model, item_texture(item, info));
|
||||
return baked_item_model(info, info.model);
|
||||
}
|
||||
const block = placed_block(item);
|
||||
if (block?.item_model !== undefined) return baked_item_model(block, block.item_model, block);
|
||||
return undefined;
|
||||
@@ -270,6 +277,54 @@ function baked_item_model(key: ItemRegistry | BlockRegistry, id: string, block?:
|
||||
}
|
||||
const baked_items = new WeakMap<ItemRegistry | BlockRegistry, ItemModel>();
|
||||
|
||||
// by texture, since an item's texture can change with its data
|
||||
function baked_entity_item_model(model: EntityModelJson, texture: string): ItemModel {
|
||||
let by_texture = baked_entity_items.get(model);
|
||||
if (!by_texture) {
|
||||
by_texture = new Map();
|
||||
baked_entity_items.set(model, by_texture);
|
||||
}
|
||||
let baked = by_texture.get(texture);
|
||||
if (!baked) {
|
||||
baked = { quads: entity_model_quads(model, texture), display: model.display ?? {} };
|
||||
by_texture.set(texture, baked);
|
||||
}
|
||||
return baked;
|
||||
}
|
||||
const baked_entity_items = new WeakMap<EntityModelJson, Map<string, ItemModel>>();
|
||||
|
||||
// an entity model as a block model's quads, standing on the block's bottom in its middle like it stands on the ground,
|
||||
// 16 of its pixels across the block. its uvs are pixels of the texture, 0 to 16 like a block model's
|
||||
export function entity_model_quads(model: EntityModelJson, texture: string): BakedQuad[] {
|
||||
const vertices: number[][] = [];
|
||||
const pixels = { width: 1, height: 1 } as Texture;
|
||||
const whole_texture = { x: 0, y: 0, width: TEXTURE_SIZE, height: TEXTURE_SIZE };
|
||||
const to_block = (x: number, y: number, z: number): Vec3 => [x / 16 + 0.5, y / 16, z / 16 + 0.5];
|
||||
push_model(model, bone_matrices(model, () => IDENTITY), pixels, whole_texture, to_block, undefined, undefined, (
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
u,
|
||||
v,
|
||||
) => vertices.push([x, y, z, u, v]));
|
||||
const quads: BakedQuad[] = [];
|
||||
// two triangles a box face, its corners 0 1 2 and 0 2 3
|
||||
for (let i = 0; i < vertices.length; i += 6) {
|
||||
const corners = [vertices[i], vertices[i + 1], vertices[i + 2], vertices[i + 5]];
|
||||
const positions = corners.flatMap(([x, y, z]) => [x, y, z]);
|
||||
quads.push({
|
||||
positions,
|
||||
uvs: corners.flatMap(([, , , u, v]) => [u, v]),
|
||||
texture,
|
||||
variables: [],
|
||||
...classify_quad(positions),
|
||||
cull: -1,
|
||||
shade: true,
|
||||
});
|
||||
}
|
||||
return quads;
|
||||
}
|
||||
|
||||
// whether an item is drawn as a block or a model rather than its sprite, which decides how big it's drawn and how it's
|
||||
// held when its model has no display transform for that
|
||||
export function drawn_as_model(item: ItemStack) {
|
||||
|
||||
Reference in New Issue
Block a user