Geo renderer improvements
This commit is contained in:
+20
-1
@@ -42,6 +42,8 @@ let canvas_format: GPUTextureFormat;
|
||||
|
||||
let pipeline_2d: 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 instance_pipelines: { cutout: GPURenderPipeline; translucent: GPURenderPipeline };
|
||||
// 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;
|
||||
|
||||
let mode3d = false;
|
||||
let cull_faces = true;
|
||||
|
||||
let camera: Camera | undefined;
|
||||
|
||||
@@ -496,6 +499,14 @@ export function set_render_time(ticks: number) {
|
||||
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() {
|
||||
if (!mode3d) {
|
||||
return;
|
||||
@@ -832,7 +843,7 @@ function draw(buffer: GPUBuffer, offset: number, vertex_count: number) {
|
||||
|
||||
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) {
|
||||
render_pass.setPipeline(pipeline);
|
||||
current_pipeline = pipeline;
|
||||
@@ -1035,6 +1046,14 @@ function create_pipelines() {
|
||||
primitive: primitive_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_layout = device.createPipelineLayout({
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
+20
-2
@@ -506,11 +506,29 @@ const fluid_schema = z.object({
|
||||
|
||||
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({
|
||||
origin: 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
|
||||
inflate: z.number().optional(),
|
||||
// what rotation turns the box around, inside its bone
|
||||
|
||||
@@ -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 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
|
||||
a block with its feet at 0, and shows the way Blockbench shows it.
|
||||
Cubes can use box UV (`"uv": [u, v]`) or per face UV (`"uv": { "north": { "uv": [u, v], "uv_size": [w, h] }, ... }`),
|
||||
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
|
||||
on their own:
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
entity_model_from_geo,
|
||||
type EntityBoneJson,
|
||||
type EntityJson,
|
||||
type EntityModelJson,
|
||||
validate_entity,
|
||||
} from "@/common/mod_data.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
|
||||
const per_face = { origin: [0, 0, 0], size: [1, 1, 1], uv: { north: { uv: [0, 0], uv_size: [1, 1] } } };
|
||||
// parents that come later and uvs that are neither box nor per face aren't supported
|
||||
const bad_uv = { origin: [0, 0, 0], size: [1, 1, 1], uv: { north: { uv: [0, 0] } } };
|
||||
const geo_problems = (bones: unknown[]) => {
|
||||
const geometry = { description: { texture_width: 16, texture_height: 16 }, bones };
|
||||
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" }]);
|
||||
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] }]);
|
||||
assert(per_face_problems.some((p) => p.includes("per face uvs aren't supported")), String(per_face_problems));
|
||||
const uv_problems = geo_problems([{ name: "leg", cubes: [bad_uv] }]);
|
||||
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);
|
||||
});
|
||||
|
||||
// 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", () => {
|
||||
const pivot = [1, 2, 3];
|
||||
const rotation = [30, -45, 60];
|
||||
|
||||
Reference in New Issue
Block a user