This commit is contained in:
2026-11-22 13:21:27 -03:00
parent e751d22c6e
commit ac791b886e
23 changed files with 494 additions and 91 deletions
+30 -14
View File
@@ -5,7 +5,7 @@
// turns to the entity's yaw, and its bones are posed by its type's animator from a mod (see entity_animators.ts). without
// one, a bone named "head" turns to where it looks, and bones named like minecraft's humanoid ones (right_arm, left_arm,
// right_leg, left_leg) swing as it walks, the right arm when it attacks too. entities without a model are their texture
// facing the camera, like minecraft's fishing hook
// facing the camera, like minecraft's fishing hook, and objects without one are the item they hold
import { AssetManager } from "@/client/assets.ts";
import type { Camera } from "@/client/camera.ts";
import type { RemoteEntity } from "@/client/entity/remote_entity.ts";
@@ -18,6 +18,8 @@ import { HURT_TINT } from "./player_renderer.ts";
import { bone_matrices, IDENTITY, model_pivot, multiply, rotation_around, type Vec3 } from "./model_math.ts";
import { push_model } from "./model_renderer.ts";
import { entity_tint } from "./entity_light.ts";
import { push_item, synced_stack } from "./item_renderer.ts";
import type { ItemData } from "@/common/inventory.ts";
// models are in texture pixels, 16 to a block
const PIXEL = 1 / 16;
@@ -27,29 +29,42 @@ export function render_mod_entity(entity: RemoteEntity, camera: Camera, partial_
const json = EverythingRegistry.get<EntityJson>("entities", entity.type);
if (!json) return;
const atlas = AssetManager.instance.get<Texture>("cubi:textures");
const region = get_sprite_region(json.texture);
const hurt = entity.hurt_time > 0 || entity.dead ? HURT_TINT : undefined;
const tint = entity_tint(entity, partial_tick, hurt, entity.data.on_fire === true);
const scale = json.scale ?? 1;
if (json.model === undefined) {
render_sprite(entity, camera, atlas, region, partial_tick, tint);
// an object is the item it holds, the size the item's model is made
const item = entity.data.item as ItemData | undefined;
if (json.behavior === "object") {
if (item) push_item(atlas, synced_stack(item), turned_with(entity, partial_tick, scale), tint);
} else if (json.texture !== undefined) {
render_sprite(entity, camera, atlas, get_sprite_region(json.texture), scale, partial_tick, tint);
}
return;
}
const model = EverythingRegistry.get<EntityModelJson>("entity_models", json.model);
if (!model) return;
const { x, y, z } = entity.render_position(partial_tick);
const yaw_sin = Math.sin(entity.yaw);
const yaw_cos = Math.cos(entity.yaw);
const to_world = (px: number, py: number, pz: number): Vec3 => {
const [mx, my, mz] = [px * PIXEL, py * PIXEL, pz * PIXEL];
return [x + mx * yaw_cos + mz * yaw_sin, y + my, z - mx * yaw_sin + mz * yaw_cos];
};
if (!model || json.texture === undefined) return;
const placed = turned_with(entity, partial_tick, scale);
const to_world = (px: number, py: number, pz: number) => placed(px * PIXEL, py * PIXEL, pz * PIXEL);
const offsets = entity.animation?.pose(entity.age + partial_tick);
const pose = offsets ? () => IDENTITY : default_pose(entity, partial_tick);
const region = get_sprite_region(json.texture);
push_model(model, bone_matrices(model, pose, offsets), atlas, region, to_world, tint);
}
// from blocks around the entity's feet, its own way round, to the world: sized by scale and turned to its yaw
function turned_with(entity: RemoteEntity, partial_tick: number, scale: number) {
const { x, y, z } = entity.render_position(partial_tick);
const yaw_sin = Math.sin(entity.yaw) * scale;
const yaw_cos = Math.cos(entity.yaw) * scale;
return (bx: number, by: number, bz: number): Vec3 => [
x + bx * yaw_cos + bz * yaw_sin,
y + by * scale,
z - bx * yaw_sin + bz * yaw_cos,
];
}
// the head looking and the humanoid swing, for an entity without an animator
function default_pose(entity: RemoteEntity, partial_tick: number) {
const limbs = limb_angles(entity, partial_tick);
@@ -86,14 +101,15 @@ function render_sprite(
camera: Camera,
atlas: Texture,
region: SpriteRegion,
scale: number,
partial_tick: number,
[r, g, b]: Vec3,
) {
const { x, y, z } = entity.render_position(partial_tick);
const cy = y + entity.height / 2;
// the camera's right and up, halved to the sprite's size
const half_width = region.width * PIXEL / 2;
const half_height = region.height * PIXEL / 2;
const half_width = region.width * PIXEL * scale / 2;
const half_height = region.height * PIXEL * scale / 2;
const [yaw_sin, yaw_cos] = [Math.sin(camera.yaw), Math.cos(camera.yaw)];
const [pitch_sin, pitch_cos] = [Math.sin(camera.pitch), Math.cos(camera.pitch)];
const right: Vec3 = [yaw_cos * half_width, 0, -yaw_sin * half_width];
+12 -7
View File
@@ -127,8 +127,17 @@ export function render_item_entity(entity: ItemEntity, partial_tick: number) {
}
}
// the stacks shown items are drawn as, by their "item" client data, which is replaced whenever it changes
const shown_stacks = new WeakMap<ItemData, ItemStack>();
// the stack an item synced to clients is drawn as, like a shown item's "item" client data or an object's. made once
// for each, since the data is replaced whenever it changes
export function synced_stack(data: ItemData): ItemStack {
let stack = synced_stacks.get(data);
if (!stack) {
stack = ItemStack.from_data(data);
synced_stacks.set(data, stack);
}
return stack;
}
const synced_stacks = new WeakMap<ItemData, ItemStack>();
// every item blocks with item_display show on top of them, their "item" client data, see BlockRegistry.item_display.
// the atlas has to be the current texture
@@ -143,11 +152,7 @@ export function render_block_items(level: ClientLevel) {
const value = level.get_block_value(x, y, z);
const block = blocks[value & ID_MASK];
if (!block?.item_display) continue;
let item = shown_stacks.get(shown);
if (!item) {
item = ItemStack.from_data(shown);
shown_stacks.set(shown, item);
}
const item = synced_stack(shown);
// the block's own light, or the one above for blocks light doesn't get into, since the items sit on top
const here = level.get_light(x, y, z);
const above = level.get_light(x, y + 1, z);