3d models for items

This commit is contained in:
2026-09-26 21:45:18 -03:00
parent 7e5bcaa239
commit aef8836f15
4 changed files with 209 additions and 14 deletions
+24 -14
View File
@@ -5,6 +5,7 @@ import { get_sprite_region } from "$/client/sprites.ts";
import type { ItemEntity } from "$/client/entity/item_entity.ts";
import { push_vertex, Texture } from "$/client/renderer/mod.ts";
import { block_item_texture, cube_face_texture } from "$/common/block_models.ts";
import { item_model } from "./item_model.ts";
// how big items on the ground are drawn, minecraft's ground transform
const BLOCK_SCALE = 0.25;
@@ -87,7 +88,7 @@ function push_block(
}
}
// a flat sprite standing up and turning, drawn from both sides
// a flat item as its 3d model (see item_model.ts), standing up and turning around its middle
function push_sprite(
atlas: Texture,
texture_id: string,
@@ -96,19 +97,28 @@ function push_sprite(
angle: number,
) {
const region = get_sprite_region(texture_id);
const dx = Math.cos(angle) * size / 2;
const dz = Math.sin(angle) * size / 2;
const bottom = base.y;
const top = base.y + size;
const front = [
[base.x - dx, bottom, base.z - dz],
[base.x + dx, bottom, base.z + dz],
[base.x + dx, top, base.z + dz],
[base.x - dx, top, base.z - dz],
];
push_textured_quad(atlas, region, front, 1);
// the back is the same quad the other way round, mirrored so it isn't drawn backwards
push_textured_quad(atlas, region, [front[1], front[0], front[3], front[2]], 1);
const sin = Math.sin(angle);
const cos = Math.cos(angle);
const u = (pixels: number) => (region.x * TEXTURE_SIZE + pixels) / atlas.width;
const v = (pixels: number) => (region.y * TEXTURE_SIZE + pixels) / atlas.height;
for (const quad of item_model(texture_id)) {
for (const i of [0, 1, 2, 0, 2, 3]) {
const [lx, ly, lz] = quad.corners[i];
const [pu, pv] = quad.uvs[i];
push_vertex(
base.x + (lx * cos - lz * sin) * size,
base.y + ly * size,
base.z + (lx * sin + lz * cos) * size,
u(pu),
v(pv),
quad.shade,
quad.shade,
quad.shade,
1,
);
}
}
}
function push_textured_quad(atlas: Texture, region: { x: number; y: number }, corners: number[][], shade: number) {