Messy but working chicken
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
// mobs mods define (entities/<name>.json), drawn with their entity model and a texture from the atlas. there are no
|
||||
// animations yet: the model turns to the entity's yaw, and a bone named "head" to where it looks
|
||||
import { AssetManager } from "$/client/assets.ts";
|
||||
import type { RemoteEntity } from "$/client/entity/remote_entity.ts";
|
||||
import type { Texture } from "$/client/renderer/mod.ts";
|
||||
import { get_sprite_region } from "$/client/sprites.ts";
|
||||
import { EverythingRegistry } from "$/common/everything_registry.ts";
|
||||
import type { EntityBoneJson, EntityJson, EntityModelJson } from "$/common/mod_data.ts";
|
||||
import { push_box, type Vec3 } from "./player_renderer.ts";
|
||||
|
||||
// models are in texture pixels, 16 to a block
|
||||
const PIXEL = 1 / 16;
|
||||
// how much of each color is left while it flashes red after being hurt, like minecraft's
|
||||
const HURT_TINT: Vec3 = [1, 0.6, 0.6];
|
||||
|
||||
// the atlas has to be the current texture
|
||||
export function render_mod_entity(entity: RemoteEntity, partial_tick: number) {
|
||||
const json = EverythingRegistry.get<EntityJson>("entities", entity.type);
|
||||
const model = json && EverythingRegistry.get<EntityModelJson>("entity_models", json.model);
|
||||
if (!model) return;
|
||||
|
||||
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
|
||||
const region = get_sprite_region(json.texture);
|
||||
const to_uv = (u: number, v: number): [number, number] => [
|
||||
(region.x + u * region.width / model.texture_width) / atlas.width,
|
||||
(region.y + v * region.height / model.texture_height) / atlas.height,
|
||||
];
|
||||
|
||||
const { x, y, z } = entity.render_position(partial_tick);
|
||||
const yaw_sin = Math.sin(entity.yaw);
|
||||
const yaw_cos = Math.cos(entity.yaw);
|
||||
const tint = entity.hurt_time > 0 || entity.dead ? HURT_TINT : undefined;
|
||||
|
||||
const bones = new Map<string, Matrix>();
|
||||
for (const bone of model.bones) {
|
||||
let matrix = bone_matrix(bone);
|
||||
if (bone.name === "head") {
|
||||
const look = rotation_around(model_pivot(bone), entity.pitch, entity.head_yaw - entity.yaw, 0);
|
||||
matrix = multiply(look, matrix);
|
||||
}
|
||||
if (bone.parent) matrix = multiply(bones.get(bone.parent) ?? IDENTITY, matrix);
|
||||
bones.set(bone.name, matrix);
|
||||
|
||||
const transform = (px: number, py: number, pz: number): Vec3 => {
|
||||
const [mx, my, mz] = apply(matrix, px, py, pz).map((n) => n * PIXEL);
|
||||
return [x + mx * yaw_cos + mz * yaw_sin, y + my, z - mx * yaw_sin + mz * yaw_cos];
|
||||
};
|
||||
for (const cube of bone.cubes) {
|
||||
const inflate = cube.inflate ?? 0;
|
||||
const [ox, oy, oz] = cube.origin;
|
||||
const [w, h, d] = cube.size;
|
||||
// bedrock models are mirrored on x from the engine's, like blockbench shows them: +x is the model's right
|
||||
const low: Vec3 = [-(ox + w) - inflate, oy - inflate, oz - inflate];
|
||||
const high: Vec3 = [-ox + inflate, oy + h + inflate, oz + d + inflate];
|
||||
push_box(cube.size, low, high, cube.uv, to_uv, transform, tint);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3 rows of 4, the last column moving: a turn and a move, from a bone's pixels to the model's
|
||||
type Matrix = number[];
|
||||
|
||||
const IDENTITY: Matrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0];
|
||||
|
||||
function model_pivot(bone: EntityBoneJson): Vec3 {
|
||||
return [-bone.pivot[0], bone.pivot[1], bone.pivot[2]];
|
||||
}
|
||||
|
||||
// its own rotation around its pivot. mirroring x turns x and y rotations the other way
|
||||
function bone_matrix(bone: EntityBoneJson): Matrix {
|
||||
if (!bone.rotation) return IDENTITY;
|
||||
const [rx, ry, rz] = bone.rotation.map((degrees) => degrees * Math.PI / 180);
|
||||
return rotation_around(model_pivot(bone), -rx, -ry, rz);
|
||||
}
|
||||
|
||||
// turns around pivot by x, then y, then z (radians). y turns like the whole model does (see render_mod_entity) and x
|
||||
// like the player's head pitch
|
||||
function rotation_around(pivot: Vec3, rx: number, ry: number, rz: number): Matrix {
|
||||
const [sx, cx] = [Math.sin(rx), Math.cos(rx)];
|
||||
const [sy, cy] = [Math.sin(ry), Math.cos(ry)];
|
||||
const [sz, cz] = [Math.sin(rz), Math.cos(rz)];
|
||||
const rot_x = [1, 0, 0, 0, 0, cx, -sx, 0, 0, sx, cx, 0];
|
||||
const rot_y = [cy, 0, sy, 0, 0, 1, 0, 0, -sy, 0, cy, 0];
|
||||
const rot_z = [cz, -sz, 0, 0, sz, cz, 0, 0, 0, 0, 1, 0];
|
||||
const [px, py, pz] = pivot;
|
||||
const rotation = multiply(rot_z, multiply(rot_y, rot_x));
|
||||
return multiply(
|
||||
[1, 0, 0, px, 0, 1, 0, py, 0, 0, 1, pz],
|
||||
multiply(rotation, [1, 0, 0, -px, 0, 1, 0, -py, 0, 0, 1, -pz]),
|
||||
);
|
||||
}
|
||||
|
||||
// a after b
|
||||
function multiply(a: Matrix, b: Matrix): Matrix {
|
||||
const out: Matrix = [];
|
||||
for (let row = 0; row < 3; row++) {
|
||||
for (let col = 0; col < 4; col++) {
|
||||
let sum = col === 3 ? a[row * 4 + 3] : 0;
|
||||
for (let k = 0; k < 3; k++) sum += a[row * 4 + k] * b[k * 4 + col];
|
||||
out.push(sum);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function apply(m: Matrix, x: number, y: number, z: number): Vec3 {
|
||||
return [
|
||||
m[0] * x + m[1] * y + m[2] * z + m[3],
|
||||
m[4] * x + m[5] * y + m[6] * z + m[7],
|
||||
m[8] * x + m[9] * y + m[10] * z + m[11],
|
||||
];
|
||||
}
|
||||
@@ -8,6 +8,8 @@ import { RemotePlayer } from "$/client/entity/remote_player.ts";
|
||||
import { ItemEntity } from "$/client/entity/item_entity.ts";
|
||||
import { render_item_entity } from "./item_renderer.ts";
|
||||
import { render_player } from "./player_renderer.ts";
|
||||
import { render_mod_entity } from "./entity_renderer.ts";
|
||||
import { RemoteEntity } from "$/client/entity/remote_entity.ts";
|
||||
import {
|
||||
draw_terrain,
|
||||
flush_batch,
|
||||
@@ -111,6 +113,8 @@ export class LevelRenderer {
|
||||
for (const entity of level.entities.values()) {
|
||||
if (entity instanceof ItemEntity) {
|
||||
render_item_entity(entity, partial_tick);
|
||||
} else if (entity instanceof RemoteEntity) {
|
||||
render_mod_entity(entity, partial_tick);
|
||||
}
|
||||
}
|
||||
flush_batch();
|
||||
|
||||
@@ -2,7 +2,7 @@ import { PLAYER_HEIGHT } from "$/common/constants.ts";
|
||||
import type { Player } from "$/client/entity/player.ts";
|
||||
import { push_vertex, type Texture } from "$/client/renderer/mod.ts";
|
||||
|
||||
type Vec3 = [number, number, number];
|
||||
export type Vec3 = [number, number, number];
|
||||
|
||||
// minecraft's player model is 32 skin pixels tall, it's scaled to the player's height
|
||||
const PIXEL = PLAYER_HEIGHT / 32;
|
||||
@@ -181,20 +181,32 @@ export function render_player(player: Player, skin: Texture, partial_tick: numbe
|
||||
function push_cube(
|
||||
skin: Texture,
|
||||
part: ModelPart,
|
||||
[u, v]: [number, number],
|
||||
uv: [number, number],
|
||||
inflate: number,
|
||||
transform: (x: number, y: number, z: number) => Vec3,
|
||||
) {
|
||||
const [w, h, d] = part.size;
|
||||
const low = part.from.map((n) => n - inflate);
|
||||
const high = part.from.map((n, i) => n + part.size[i] + inflate);
|
||||
const low = part.from.map((n) => n - inflate) as Vec3;
|
||||
const high = part.from.map((n, i) => n + part.size[i] + inflate) as Vec3;
|
||||
push_box(part.size, low, high, uv, (u, v) => [u / skin.width, v / skin.height], transform);
|
||||
}
|
||||
|
||||
// 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
|
||||
export function push_box(
|
||||
size: Vec3,
|
||||
low: Vec3,
|
||||
high: Vec3,
|
||||
[u, v]: [number, number],
|
||||
to_uv: (u: number, v: number) => [number, number],
|
||||
transform: (x: number, y: number, z: number) => Vec3,
|
||||
tint: Vec3 = [1, 1, 1],
|
||||
) {
|
||||
const [w, h, d] = size;
|
||||
for (const face of FACES) {
|
||||
const [rx, ry, rw, rh] = face.rect(w, h, d);
|
||||
const u0 = (u + rx + UV_INSET) / skin.width;
|
||||
const v0 = (v + ry + UV_INSET) / skin.height;
|
||||
const u1 = (u + rx + rw - UV_INSET) / skin.width;
|
||||
const v1 = (v + ry + rh - UV_INSET) / skin.height;
|
||||
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 [r, g, b] = tint.map((c) => c * face.shade);
|
||||
|
||||
for (const i of [0, 1, 2, 0, 2, 3]) {
|
||||
const [cx, cy, cz, cu, cv] = face.corners[i];
|
||||
@@ -203,7 +215,7 @@ function push_cube(
|
||||
cy ? high[1] : low[1],
|
||||
cz ? high[2] : low[2],
|
||||
);
|
||||
push_vertex(px, py, pz, cu ? u1 : u0, cv ? v1 : v0, face.shade, face.shade, face.shade, 1);
|
||||
push_vertex(px, py, pz, cu ? u1 : u0, cv ? v1 : v0, r, g, b, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user