Fences
This commit is contained in:
@@ -5,7 +5,17 @@ import { get_sprite_region } from "$/client/sprites.ts";
|
||||
import type { ItemEntity } from "$/client/entity/item_entity.ts";
|
||||
import type { ItemStack } from "$/common/inventory.ts";
|
||||
import { light_color, push_vertex, Texture } from "$/client/renderer/mod.ts";
|
||||
import { block_item_texture, block_variant, cube_face_texture, cube_face_tint } from "$/common/block_models.ts";
|
||||
import {
|
||||
type BakedQuad,
|
||||
block_item_quads,
|
||||
block_item_texture,
|
||||
block_variant,
|
||||
cube_face_texture,
|
||||
cube_face_tint,
|
||||
ITEM_FACE_SHADE,
|
||||
item_tint,
|
||||
type ModelJson,
|
||||
} from "$/common/block_models.ts";
|
||||
import { item_model } from "./item_model.ts";
|
||||
import { get_state_value, item_texture } from "$/common/utils.ts";
|
||||
import { ID_MASK, TEXTURE_SIZE } from "$/common/constants.ts";
|
||||
@@ -174,10 +184,46 @@ export function item_block(item: ItemStack): BlockRegistry | undefined {
|
||||
return block && block_item_texture(block) === undefined ? block : undefined;
|
||||
}
|
||||
|
||||
// an item's model: a cube for blocks, its sprite made 3d for everything else (see item_model.ts). tint multiplies its
|
||||
// colors, like the light it's in
|
||||
// a block's item_model quads, baked the first time they're drawn
|
||||
export function item_model_quads(block: BlockRegistry): BakedQuad[] | undefined {
|
||||
if (block.item_model === undefined) return undefined;
|
||||
let quads = baked_items.get(block);
|
||||
if (!quads) {
|
||||
quads = block_item_quads(block, Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"))) ?? [];
|
||||
baked_items.set(block, quads);
|
||||
}
|
||||
return quads;
|
||||
}
|
||||
const baked_items = new WeakMap<BlockRegistry, BakedQuad[]>();
|
||||
|
||||
// an item's model: its block's item_model or a cube for blocks, its sprite made 3d for everything else (see
|
||||
// item_model.ts). tint multiplies its colors, like the light it's in
|
||||
export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransform, [tr, tg, tb]: Vec3 = [1, 1, 1]) {
|
||||
const block = item_block(item);
|
||||
const model = block && item_model_quads(block);
|
||||
if (block && model) {
|
||||
for (const quad of model) {
|
||||
const region = get_sprite_region(quad.texture);
|
||||
const [r, g, b] = item_tint(block, quad.variables);
|
||||
const shade = quad.shade ? ITEM_FACE_SHADE[quad.face] : 1;
|
||||
for (const i of [0, 1, 2, 0, 2, 3]) {
|
||||
const p = quad.positions;
|
||||
const [px, py, pz] = transform(p[i * 3] - 0.5, p[i * 3 + 1], p[i * 3 + 2] - 0.5);
|
||||
push_vertex(
|
||||
px,
|
||||
py,
|
||||
pz,
|
||||
sprite_u(atlas, region, quad.uvs[i * 2]),
|
||||
sprite_v(atlas, region, quad.uvs[i * 2 + 1]),
|
||||
shade * r * tr,
|
||||
shade * g * tg,
|
||||
shade * b * tb,
|
||||
1,
|
||||
);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (block) {
|
||||
for (const face of CUBE_FACES) {
|
||||
const region = get_sprite_region(cube_face_texture(block, face.texture));
|
||||
@@ -201,6 +247,17 @@ export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransf
|
||||
}
|
||||
}
|
||||
|
||||
// where a model's uv in pixels (0-16) is in the atlas, kept off the sprite's edge
|
||||
export function sprite_u(atlas: Texture, region: SpriteRegion, pixels: number) {
|
||||
const t = Math.min(Math.max(pixels / TEXTURE_SIZE * region.width, UV_PAD), region.width - UV_PAD);
|
||||
return (region.x + t) / atlas.width;
|
||||
}
|
||||
|
||||
export function sprite_v(atlas: Texture, region: SpriteRegion, pixels: number) {
|
||||
const t = Math.min(Math.max(pixels / TEXTURE_SIZE * region.height, UV_PAD), region.height - UV_PAD);
|
||||
return (region.y + t) / atlas.height;
|
||||
}
|
||||
|
||||
function push_textured_quad(atlas: Texture, region: SpriteRegion, corners: number[][], color: number[]) {
|
||||
const u0 = (region.x + UV_PAD) / atlas.width;
|
||||
const v0 = (region.y + UV_PAD) / atlas.height;
|
||||
|
||||
@@ -26,20 +26,10 @@ import {
|
||||
white_tex,
|
||||
} from "$/client/renderer/mod.ts";
|
||||
import { type BlockRegistry, EverythingRegistry, type RenderLayer } from "$/common/everything_registry.ts";
|
||||
import {
|
||||
bake_model,
|
||||
type BakedQuad,
|
||||
block_variant,
|
||||
BUILTIN_MODELS,
|
||||
DEFAULT_MODEL,
|
||||
FACE_INDEX,
|
||||
find_model,
|
||||
type ModelJson,
|
||||
} from "$/common/block_models.ts";
|
||||
import { bake_block, type BakedQuad, FACE_INDEX, looks_by_state, type ModelJson } from "$/common/block_models.ts";
|
||||
import { ID_MASK } from "$/common/constants.ts";
|
||||
import { type Shape, shape_at } from "$/common/block_shapes.ts";
|
||||
import type { BlockHitResult } from "$/client/level/client_level.ts";
|
||||
import { get_state_value } from "$/common/utils.ts";
|
||||
import { FACE_GROUPS, UNALIGNED_GROUP } from "$/client/workers/chunk_messages.ts";
|
||||
import { FACE_AXIS, FACE_NORMALS } from "$/client/workers/translucent_sort.ts";
|
||||
import { Frustum } from "./frustum.ts";
|
||||
@@ -211,15 +201,10 @@ export class LevelRenderer {
|
||||
|
||||
// the block's model at a value, baked once
|
||||
#destroy_quads(block: BlockRegistry, value: number): BakedQuad[] {
|
||||
const key = block.variants ? value : value & ID_MASK;
|
||||
const key = looks_by_state(block) ? value : value & ID_MASK;
|
||||
let quads = this.#baked.get(key);
|
||||
if (!quads) {
|
||||
const states: Record<string, number> = {};
|
||||
for (const state of block.states ?? []) states[state.name] = get_state_value(value, block, state.name)!;
|
||||
const variant = block_variant(block, states);
|
||||
const models = Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"));
|
||||
const model = find_model(variant.model, models) ?? BUILTIN_MODELS[DEFAULT_MODEL];
|
||||
quads = bake_model(model, variant.textures, variant.y);
|
||||
quads = bake_block(block, value, Object.fromEntries(EverythingRegistry.entries<ModelJson>("models")));
|
||||
this.#baked.set(key, quads);
|
||||
}
|
||||
return quads;
|
||||
|
||||
@@ -4,12 +4,24 @@ import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everyt
|
||||
import { AssetManager } from "$/client/assets.ts";
|
||||
import { ItemStack } from "$/common/inventory.ts";
|
||||
import { item_texture } from "$/common/utils.ts";
|
||||
import { block_item_texture, cube_face_texture, cube_face_tint } from "$/common/block_models.ts";
|
||||
import {
|
||||
type BakedQuad,
|
||||
block_item_texture,
|
||||
cube_face_texture,
|
||||
cube_face_tint,
|
||||
ITEM_FACE_SHADE,
|
||||
item_tint,
|
||||
} from "$/common/block_models.ts";
|
||||
import { item_model_quads, sprite_u, sprite_v } from "./item_renderer.ts";
|
||||
import {
|
||||
draw_rect,
|
||||
draw_text,
|
||||
draw_texture_region,
|
||||
draw_texture_region_skewed,
|
||||
flush_batch,
|
||||
get_current_texture,
|
||||
push_vertex,
|
||||
set_current_texture,
|
||||
Texture,
|
||||
} from "$/client/renderer/mod.ts";
|
||||
|
||||
@@ -75,7 +87,10 @@ export function draw_item(item: ItemStack, x: number, y: number) {
|
||||
? EverythingRegistry.get<BlockRegistry>("blocks", item_info.block_id)
|
||||
: undefined;
|
||||
// blocks that aren't cubes, like plants, show a flat sprite
|
||||
if (block_info && block_item_texture(block_info) === undefined) {
|
||||
const model = block_info && item_model_quads(block_info);
|
||||
if (block_info && model) {
|
||||
draw_item_model(block_info, model, x, y);
|
||||
} else if (block_info && block_item_texture(block_info) === undefined) {
|
||||
draw_item_block(block_info, x, y);
|
||||
} else {
|
||||
draw_item_item(item, item_info, x, y);
|
||||
@@ -129,6 +144,71 @@ function draw_item_item(item: ItemStack, item_info: ItemRegistry, x: number, y:
|
||||
);
|
||||
}
|
||||
|
||||
// where the corners of the cube icon are, so models are drawn at the same size and angle: seen from above the
|
||||
// north west corner, west on the left and north on the right
|
||||
function item_projection(x: number, y: number) {
|
||||
const size = (SLOT_SIZE - 4 * 2) / 2;
|
||||
const cx = x + SLOT_SIZE / 2;
|
||||
const cy = y + SLOT_SIZE / 2 - 10;
|
||||
return (px: number, py: number, pz: number) => [
|
||||
cx + (px - pz) * size,
|
||||
cy + size / 2 - (px + pz) * size / 2 + (1 - py) * size,
|
||||
];
|
||||
}
|
||||
|
||||
// toward whoever looks at an icon, see item_projection
|
||||
const ICON_VIEW = [-1, 1, -1];
|
||||
|
||||
// a block's item_model in a slot: its faces that face the viewer, back to front
|
||||
function draw_item_model(block_info: BlockRegistry, quads: BakedQuad[], x: number, y: number) {
|
||||
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
|
||||
if (get_current_texture() !== atlas.tex) {
|
||||
flush_batch();
|
||||
set_current_texture(atlas.tex);
|
||||
}
|
||||
const project = item_projection(x, y);
|
||||
const toward_viewer = (p: number[]) => p[0] * ICON_VIEW[0] + p[1] * ICON_VIEW[1] + p[2] * ICON_VIEW[2];
|
||||
const visible = quads
|
||||
.filter((quad) => {
|
||||
const p = quad.positions;
|
||||
const e1 = [p[3] - p[0], p[4] - p[1], p[5] - p[2]];
|
||||
const e2 = [p[9] - p[0], p[10] - p[1], p[11] - p[2]];
|
||||
const normal = [
|
||||
e1[1] * e2[2] - e1[2] * e2[1],
|
||||
e1[2] * e2[0] - e1[0] * e2[2],
|
||||
e1[0] * e2[1] - e1[1] * e2[0],
|
||||
];
|
||||
return toward_viewer(normal) > 1e-6;
|
||||
})
|
||||
.map((quad) => {
|
||||
const p = quad.positions;
|
||||
const center = [0, 1, 2].map((axis) => (p[axis] + p[3 + axis] + p[6 + axis] + p[9 + axis]) / 4);
|
||||
return { quad, depth: toward_viewer(center) };
|
||||
})
|
||||
.sort((a, b) => a.depth - b.depth);
|
||||
|
||||
for (const { quad } of visible) {
|
||||
const region = get_sprite_region(quad.texture);
|
||||
const [r, g, b] = item_tint(block_info, quad.variables);
|
||||
const shade = quad.shade ? ITEM_FACE_SHADE[quad.face] : 1;
|
||||
for (const i of [0, 1, 2, 0, 2, 3]) {
|
||||
const p = quad.positions;
|
||||
const [sx, sy] = project(p[i * 3], p[i * 3 + 1], p[i * 3 + 2]);
|
||||
push_vertex(
|
||||
sx,
|
||||
sy,
|
||||
0,
|
||||
sprite_u(atlas, region, quad.uvs[i * 2]),
|
||||
sprite_v(atlas, region, quad.uvs[i * 2 + 1]),
|
||||
shade * r,
|
||||
shade * g,
|
||||
shade * b,
|
||||
1,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function draw_item_block(block_info: BlockRegistry, x: number, y: number) {
|
||||
const top_texture = cube_face_texture(block_info, "top");
|
||||
const front_texture = cube_face_texture(block_info, "front");
|
||||
|
||||
Reference in New Issue
Block a user