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
+20 -1
View File
@@ -42,6 +42,8 @@ let canvas_format: GPUTextureFormat;
let pipeline_2d: GPURenderPipeline; let pipeline_2d: GPURenderPipeline;
let pipeline_3d: GPURenderPipeline; let pipeline_3d: GPURenderPipeline;
// the same, drawing both sides of each face, see set_face_culling
let pipeline_3d_no_cull: GPURenderPipeline;
let terrain_pipelines: Record<RenderLayer, GPURenderPipeline>; let terrain_pipelines: Record<RenderLayer, GPURenderPipeline>;
let instance_pipelines: { cutout: GPURenderPipeline; translucent: GPURenderPipeline }; let instance_pipelines: { cutout: GPURenderPipeline; translucent: GPURenderPipeline };
// 0 1 2 0 2 3 for every quad, shared by all solid and cutout chunk meshes // 0 1 2 0 2 3 for every quad, shared by all solid and cutout chunk meshes
@@ -76,6 +78,7 @@ let current_texture: GPUTexture | null = null;
export let white_tex: GPUTexture | null = null; export let white_tex: GPUTexture | null = null;
let mode3d = false; let mode3d = false;
let cull_faces = true;
let camera: Camera | undefined; let camera: Camera | undefined;
@@ -496,6 +499,14 @@ export function set_render_time(ticks: number) {
render_time = ticks % RENDER_TIME_PERIOD; render_time = ticks % RENDER_TIME_PERIOD;
} }
// whether 3d drawing leaves out faces seen from behind, true unless turned off. off draws both sides, like entity
// models do, where a flat cube's one face is seen from either side and an open box shows its inside
export function set_face_culling(cull: boolean) {
if (cull_faces === cull) return;
flush_batch();
cull_faces = cull;
}
export function end_mode_3d() { export function end_mode_3d() {
if (!mode3d) { if (!mode3d) {
return; return;
@@ -832,7 +843,7 @@ function draw(buffer: GPUBuffer, offset: number, vertex_count: number) {
const render_pass = ensure_pass(); const render_pass = ensure_pass();
const pipeline = mode3d ? pipeline_3d : pipeline_2d; const pipeline = mode3d ? (cull_faces ? pipeline_3d : pipeline_3d_no_cull) : pipeline_2d;
if (current_pipeline !== pipeline) { if (current_pipeline !== pipeline) {
render_pass.setPipeline(pipeline); render_pass.setPipeline(pipeline);
current_pipeline = pipeline; current_pipeline = pipeline;
@@ -1035,6 +1046,14 @@ function create_pipelines() {
primitive: primitive_3d, primitive: primitive_3d,
depthStencil: depth_3d, depthStencil: depth_3d,
}); });
pipeline_3d_no_cull = device.createRenderPipeline({
layout,
vertex,
fragment,
multisample,
primitive: { ...primitive_3d, cullMode: "none" },
depthStencil: depth_3d,
});
const terrain_module = device.createShaderModule({ code: terrain_shader_src }); const terrain_module = device.createShaderModule({ code: terrain_shader_src });
const terrain_layout = device.createPipelineLayout({ const terrain_layout = device.createPipelineLayout({
+7 -1
View File
@@ -31,6 +31,7 @@ import {
push_vertex, push_vertex,
render_debug, render_debug,
set_current_texture, set_current_texture,
set_face_culling,
Texture, Texture,
use_texture, use_texture,
view_projection, view_projection,
@@ -180,10 +181,15 @@ export class LevelRenderer {
for (const entity of level.entities.values()) { for (const entity of level.entities.values()) {
if (entity instanceof ItemEntity && this.#entity_in_view(entity, partial_tick)) { if (entity instanceof ItemEntity && this.#entity_in_view(entity, partial_tick)) {
render_item_entity(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); render_mod_entity(entity, camera, partial_tick);
} }
} }
set_face_culling(true);
render_block_items(level); render_block_items(level);
flush_batch(); flush_batch();
} }
+39 -8
View File
@@ -3,6 +3,7 @@
import type { Player } from "@/client/entity/player.ts"; import type { Player } from "@/client/entity/player.ts";
import { push_vertex, type Texture } from "@/client/renderer/mod.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 { Vec3 } from "./model_math.ts";
import { import {
type ModelPart, 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 // 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 // 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 // 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
const FACES: { rect: (w: number, h: number, d: number) => number[]; corners: number[][]; shade: number }[] = [ // 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 // top
{ {
name: "up",
rect: (w, _h, d) => [d, 0, w, d], 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]], corners: [[1, 1, 0, 0, 1], [0, 1, 0, 1, 1], [0, 1, 1, 1, 0], [1, 1, 1, 0, 0]],
shade: 1.0, shade: 1.0,
}, },
// bottom, the same way around as the top so it's mirrored from below like in minecraft // 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], 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]], corners: [[1, 0, 0, 0, 1], [1, 0, 1, 0, 0], [0, 0, 1, 1, 0], [0, 0, 0, 1, 1]],
shade: 0.5, shade: 0.5,
}, },
// right // right
{ {
name: "east",
rect: (_w, h, d) => [0, d, d, h], 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]], corners: [[1, 0, 1, 0, 1], [1, 0, 0, 1, 1], [1, 1, 0, 1, 0], [1, 1, 1, 0, 0]],
shade: 0.6, shade: 0.6,
}, },
// front // front
{ {
name: "north",
rect: (w, h, d) => [d, d, w, h], 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]], corners: [[1, 0, 0, 0, 1], [0, 0, 0, 1, 1], [0, 1, 0, 1, 0], [1, 1, 0, 0, 0]],
shade: 0.8, shade: 0.8,
}, },
// left // left
{ {
name: "west",
rect: (w, h, d) => [d + w, d, d, h], 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]], corners: [[0, 0, 0, 0, 1], [0, 0, 1, 1, 1], [0, 1, 1, 1, 0], [0, 1, 0, 0, 0]],
shade: 0.6, shade: 0.6,
}, },
// back // back
{ {
name: "south",
rect: (w, h, d) => [2 * d + w, d, w, h], 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]], corners: [[0, 0, 1, 0, 1], [1, 0, 1, 1, 1], [1, 1, 1, 1, 0], [0, 1, 1, 0, 0]],
shade: 0.8, 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 // 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; 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); 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 // 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). to_uv turns texture pixels into the current texture's coordinates, tint multiplies its colors // 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( export function push_box(
size: Vec3, size: Vec3,
low: Vec3, low: Vec3,
high: Vec3, high: Vec3,
[u, v]: [number, number], uv: [number, number] | FaceUvs,
to_uv: (u: number, v: number) => [number, number], to_uv: (u: number, v: number) => [number, number],
transform: (x: number, y: number, z: number) => Vec3, transform: (x: number, y: number, z: number) => Vec3,
tint: Vec3 = [1, 1, 1], 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])); 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) { for (const face of FACES) {
const [rx, ry, rw, rh] = face.rect(w, h, d); const rect = face_rect(face, size, uv);
const [u0, v0] = to_uv(u + rx + UV_INSET, v + ry + UV_INSET); if (!rect) continue;
const [u1, v1] = to_uv(u + rx + rw - UV_INSET, v + ry + rh - UV_INSET); 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 r = tint[0] * face.shade;
const g = tint[1] * face.shade; const g = tint[1] * face.shade;
const b = tint[2] * face.shade; const b = tint[2] * face.shade;
+20 -2
View File
@@ -506,11 +506,29 @@ const fluid_schema = z.object({
export type FluidJson = z.infer<typeof fluid_schema>; export type FluidJson = z.infer<typeof fluid_schema>;
// a box from origin to origin + size, textured like minecraft's box uv: see the renderer's FACES const uv_pair = z.tuple([z.number(), z.number()]);
// where one face of a cube is in the texture, from uv to uv + uv_size. a negative size flips it
const face_uv = z.object({ uv: uv_pair, uv_size: uv_pair });
// a cube's faces, by the way they face. one that isn't here isn't drawn
const face_uvs = z.object({
north: face_uv.optional(),
east: face_uv.optional(),
south: face_uv.optional(),
west: face_uv.optional(),
up: face_uv.optional(),
down: face_uv.optional(),
});
export type FaceUvs = z.infer<typeof face_uvs>;
// a box from origin to origin + size. its uv is either [u, v], where a box's texture starts laid out like minecraft's
// box uv (see the renderer's FACES), or where each face is
const entity_cube = z.object({ const entity_cube = z.object({
origin: vec3, origin: vec3,
size: vec3, size: vec3,
uv: z.tuple([z.number(), z.number()], "must be [u, v], per face uvs aren't supported"), uv: z.union([uv_pair, face_uvs]),
// grows the box this many pixels each way without moving its texture // grows the box this many pixels each way without moving its texture
inflate: z.number().optional(), inflate: z.number().optional(),
// what rotation turns the box around, inside its bone // what rotation turns the box around, inside its bone
+4 -2
View File
@@ -322,8 +322,10 @@ get the id `<mod id>:<name>`. From the file's first geometry, bworld uses:
- each bone's `name`, `parent`, `pivot` and `rotation`, - each bone's `name`, `parent`, `pivot` and `rotation`,
- each cube's `origin`, `size`, `uv`, `inflate`, `pivot` and `rotation`. - each cube's `origin`, `size`, `uv`, `inflate`, `pivot` and `rotation`.
Cubes need box UV: per face UV isn't supported. A bone's parent has to come before it. The model faces -z, 16 pixels to Cubes can use box UV (`"uv": [u, v]`) or per face UV (`"uv": { "north": { "uv": [u, v], "uv_size": [w, h] }, ... }`),
a block with its feet at 0, and shows the way Blockbench shows it. where a face that's left out isn't drawn and a negative size flips it. Faces are drawn from both sides, like Bedrock's
`entity_alphatest` material, so a flat cube needs only one textured face. A bone's parent has to come before it. The
model faces -z, 16 pixels to a block with its feet at 0, and shows the way Blockbench shows it.
There are no animation files yet. The whole model turns with the entity, and bones with Minecraft's humanoid names move There are no animation files yet. The whole model turns with the entity, and bones with Minecraft's humanoid names move
on their own: on their own:
+51 -4
View File
@@ -8,6 +8,7 @@ import {
entity_model_from_geo, entity_model_from_geo,
type EntityBoneJson, type EntityBoneJson,
type EntityJson, type EntityJson,
type EntityModelJson,
validate_entity, validate_entity,
} from "@/common/mod_data.ts"; } from "@/common/mod_data.ts";
import type { Texture } from "@/client/renderer/mod.ts"; import type { Texture } from "@/client/renderer/mod.ts";
@@ -33,8 +34,8 @@ Deno.test("a bedrock geo.json becomes an entity model with only what the engine
], ],
}); });
// per face uvs and parents that come later aren't supported // parents that come later and uvs that are neither box nor per face aren't supported
const per_face = { origin: [0, 0, 0], size: [1, 1, 1], uv: { north: { uv: [0, 0], uv_size: [1, 1] } } }; const bad_uv = { origin: [0, 0, 0], size: [1, 1, 1], uv: { north: { uv: [0, 0] } } };
const geo_problems = (bones: unknown[]) => { const geo_problems = (bones: unknown[]) => {
const geometry = { description: { texture_width: 16, texture_height: 16 }, bones }; const geometry = { description: { texture_width: 16, texture_height: 16 }, bones };
const { model, problems } = entity_model_from_geo({ "minecraft:geometry": [geometry] }, "a:b"); const { model, problems } = entity_model_from_geo({ "minecraft:geometry": [geometry] }, "a:b");
@@ -43,11 +44,57 @@ Deno.test("a bedrock geo.json becomes an entity model with only what the engine
}; };
const late_parent = geo_problems([{ name: "leg", parent: "body" }, { name: "body" }]); const late_parent = geo_problems([{ name: "leg", parent: "body" }, { name: "body" }]);
assert(late_parent.some((p) => p.includes("parent body must be a bone before it")), String(late_parent)); assert(late_parent.some((p) => p.includes("parent body must be a bone before it")), String(late_parent));
const per_face_problems = geo_problems([{ name: "leg", cubes: [per_face] }]); const uv_problems = geo_problems([{ name: "leg", cubes: [bad_uv] }]);
assert(per_face_problems.some((p) => p.includes("per face uvs aren't supported")), String(per_face_problems)); assert(uv_problems.some((p) => p.includes("uv.north.uv_size is required")), String(uv_problems));
assertEquals(entity_model_from_geo({ format_version: "1.12.0" }, "a:b").model, undefined); assertEquals(entity_model_from_geo({ format_version: "1.12.0" }, "a:b").model, undefined);
}); });
// every vertex of the model's bones as [x, y, z, u, v], in texture pixels
function model_vertices(model: EntityModelJson, only?: (bone: EntityBoneJson) => boolean) {
const out: number[][] = [];
const atlas = { width: model.texture_width, height: model.texture_height } as Texture;
const region = { x: 0, y: 0, width: model.texture_width, height: model.texture_height };
push_model(model, () => IDENTITY, atlas, region, (x, y, z) => [x, y, z], undefined, only, (x, y, z, u, v) => {
out.push([x, y, z, u * model.texture_width, v * model.texture_height]);
});
return out;
}
Deno.test("per face uvs texture a cube like box uv does when they're where box uv puts each face", () => {
const [u, v] = [4, 2];
const [w, h, d] = [3, 5, 2];
const face = (fu: number, fv: number, fw: number, fh: number) => ({ uv: [u + fu, v + fv], uv_size: [fw, fh] });
// the way blockbench writes a box uv cube out per face: the bottom upside down
const per_face = {
north: face(d, d, w, h),
east: face(0, d, d, h),
south: face(2 * d + w, d, w, h),
west: face(d + w, d, d, h),
up: face(d, 0, w, d),
down: face(d + w, d, w, -d),
};
const geometry = {
description: { texture_width: 32, texture_height: 32 },
bones: [
{ name: "box", pivot: [0, 0, 0], cubes: [{ origin: [1, 2, 3], size: [w, h, d], uv: [u, v] }] },
{ name: "per_face", pivot: [0, 0, 0], cubes: [{ origin: [1, 2, 3], size: [w, h, d], uv: per_face }] },
{
name: "top_only",
pivot: [0, 0, 0],
cubes: [{ origin: [1, 2, 3], size: [w, h, d], uv: { up: per_face.up } }],
},
],
};
const { model, problems } = entity_model_from_geo({ "minecraft:geometry": [geometry] }, "a:b");
assertEquals(problems, []);
const box = model_vertices(model!, (bone) => bone.name === "box");
const per_face_vertices = model_vertices(model!, (bone) => bone.name === "per_face");
assertEquals(per_face_vertices.length, box.length);
per_face_vertices.forEach((vertex, i) => vertex.forEach((n, j) => assertAlmostEquals(n, box[i][j], 1e-9)));
// faces it leaves out aren't drawn
assertEquals(model_vertices(model!, (bone) => bone.name === "top_only"), box.slice(0, 6));
});
Deno.test("a cube's rotation turns it around its pivot like the same rotation on its bone", () => { Deno.test("a cube's rotation turns it around its pivot like the same rotation on its bone", () => {
const pivot = [1, 2, 3]; const pivot = [1, 2, 3];
const rotation = [30, -45, 60]; const rotation = [30, -45, 60];