Geo renderer improvements

This commit is contained in:
2026-11-21 19:24:09 -03:00
parent 717d2cb766
commit 4bdf5203d4
6 changed files with 141 additions and 18 deletions
+7 -1
View File
@@ -31,6 +31,7 @@ import {
push_vertex,
render_debug,
set_current_texture,
set_face_culling,
Texture,
use_texture,
view_projection,
@@ -180,10 +181,15 @@ export class LevelRenderer {
for (const entity of level.entities.values()) {
if (entity instanceof ItemEntity && this.#entity_in_view(entity, partial_tick)) {
render_item_entity(entity, partial_tick);
} else if (entity instanceof RemoteEntity && this.#entity_in_view(entity, partial_tick)) {
}
}
set_face_culling(false);
for (const entity of level.entities.values()) {
if (entity instanceof RemoteEntity && this.#entity_in_view(entity, partial_tick)) {
render_mod_entity(entity, camera, partial_tick);
}
}
set_face_culling(true);
render_block_items(level);
flush_batch();
}
+39 -8
View File
@@ -3,6 +3,7 @@
import type { Player } from "@/client/entity/player.ts";
import { push_vertex, type Texture } from "@/client/renderer/mod.ts";
import type { FaceUvs } from "@/common/mod_data.ts";
import type { Vec3 } from "./model_math.ts";
import {
type ModelPart,
@@ -24,40 +25,52 @@ const UV_INSET = 0.01;
// a box's faces, like minecraft's ModelPart.Cube. `rect` is where the face is in the box's texture, which is laid
// out as in a skin: the top and bottom above the sides (right, front, left, back). corners go counter clockwise from
// outside as [x, y, z, u, v], 0 for the low end and 1 for the high one
const FACES: { rect: (w: number, h: number, d: number) => number[]; corners: number[][]; shade: number }[] = [
// outside as [x, y, z, u, v], 0 for the low end and 1 for the high one. `name` is the face's in a bedrock model's per
// face uvs, which go where its box uv would put it except the bottom, which is upside down
const FACES: {
name: keyof FaceUvs;
rect: (w: number, h: number, d: number) => number[];
corners: number[][];
shade: number;
}[] = [
// top
{
name: "up",
rect: (w, _h, d) => [d, 0, w, d],
corners: [[1, 1, 0, 0, 1], [0, 1, 0, 1, 1], [0, 1, 1, 1, 0], [1, 1, 1, 0, 0]],
shade: 1.0,
},
// bottom, the same way around as the top so it's mirrored from below like in minecraft
{
name: "down",
rect: (w, _h, d) => [d + w, 0, w, d],
corners: [[1, 0, 0, 0, 1], [1, 0, 1, 0, 0], [0, 0, 1, 1, 0], [0, 0, 0, 1, 1]],
shade: 0.5,
},
// right
{
name: "east",
rect: (_w, h, d) => [0, d, d, h],
corners: [[1, 0, 1, 0, 1], [1, 0, 0, 1, 1], [1, 1, 0, 1, 0], [1, 1, 1, 0, 0]],
shade: 0.6,
},
// front
{
name: "north",
rect: (w, h, d) => [d, d, w, h],
corners: [[1, 0, 0, 0, 1], [0, 0, 0, 1, 1], [0, 1, 0, 1, 0], [1, 1, 0, 0, 0]],
shade: 0.8,
},
// left
{
name: "west",
rect: (w, h, d) => [d + w, d, d, h],
corners: [[0, 0, 0, 0, 1], [0, 0, 1, 1, 1], [0, 1, 1, 1, 0], [0, 1, 0, 0, 0]],
shade: 0.6,
},
// back
{
name: "south",
rect: (w, h, d) => [2 * d + w, d, w, h],
corners: [[0, 0, 1, 0, 1], [1, 0, 1, 1, 1], [1, 1, 1, 1, 0], [0, 1, 1, 0, 0]],
shade: 0.8,
@@ -87,6 +100,18 @@ export function render_player(skin: Texture, pose: PlayerPose, tint?: Vec3) {
}
}
// where a face is in the texture as [u, v, width, height] in pixels, undefined when per face uvs leave it out
function face_rect(face: typeof FACES[number], [w, h, d]: Vec3, uv: [number, number] | FaceUvs) {
if (Array.isArray(uv)) {
const [rx, ry, rw, rh] = face.rect(w, h, d);
return [uv[0] + rx, uv[1] + ry, rw, rh];
}
const own = uv[face.name];
if (!own) return undefined;
const [[u, v], [rw, rh]] = [own.uv, own.uv_size];
return face.name === "down" ? [u, v + rh, rw, -rh] : [u, v, rw, rh];
}
// what a box's vertices go to: the batch, or something collecting them, like the player in the inventory
export type VertexSink = typeof push_vertex;
@@ -105,13 +130,14 @@ export function push_cube(
push_box(part.size, low, high, uv, (u, v) => [u / skin.width, v / skin.height], transform, tint, emit);
}
// a box of a model, from low to high, textured like minecraft's box uv from [u, v] for a box of size (in texture
// pixels). to_uv turns texture pixels into the current texture's coordinates, tint multiplies its colors
// a box of a model, from low to high, textured from uv: minecraft's box uv from [u, v] for a box of size (in texture
// pixels), or a bedrock model's per face uvs. to_uv turns texture pixels into the current texture's coordinates, tint
// multiplies its colors
export function push_box(
size: Vec3,
low: Vec3,
high: Vec3,
[u, v]: [number, number],
uv: [number, number] | FaceUvs,
to_uv: (u: number, v: number) => [number, number],
transform: (x: number, y: number, z: number) => Vec3,
tint: Vec3 = [1, 1, 1],
@@ -125,9 +151,14 @@ export function push_box(
points.push(transform(i & 4 ? high[0] : low[0], i & 2 ? high[1] : low[1], i & 1 ? high[2] : low[2]));
}
for (const face of FACES) {
const [rx, ry, rw, rh] = face.rect(w, h, d);
const [u0, v0] = to_uv(u + rx + UV_INSET, v + ry + UV_INSET);
const [u1, v1] = to_uv(u + rx + rw - UV_INSET, v + ry + rh - UV_INSET);
const rect = face_rect(face, size, uv);
if (!rect) continue;
const [rx, ry, rw, rh] = rect;
// inward, which is backwards on a flipped face
const inset_u = Math.sign(rw) * UV_INSET;
const inset_v = Math.sign(rh) * UV_INSET;
const [u0, v0] = to_uv(rx + inset_u, ry + inset_v);
const [u1, v1] = to_uv(rx + rw - inset_u, ry + rh - inset_v);
const r = tint[0] * face.shade;
const g = tint[1] * face.shade;
const b = tint[2] * face.shade;