Allow for textures that aren't 16x16

This commit is contained in:
2026-09-28 16:20:37 -03:00
parent 52ef4e5ca6
commit fe149bb9ad
18 changed files with 268 additions and 136 deletions
+39 -25
View File
@@ -1,20 +1,19 @@
// the 3d model of a flat item, like minecraft's item/generated: its sprite on the front and back, one pixel apart, with
// a side face along every edge between a pixel that can be seen and one that can't (or the sprite's border)
import { TEXTURE_SIZE } from "$/common/constants.ts";
import { opaque_pixels } from "$/client/atlas.ts";
import { opaque_pixels, type OpaqueMask } from "$/client/atlas.ts";
export interface ItemQuad {
// 4 corners counter clockwise seen from outside. x from -0.5 to 0.5 and y from 0 to 1 across the sprite, z its
// thickness around 0
corners: [number, number, number][];
// u v per corner, in pixels of the sprite (0 to TEXTURE_SIZE, v down)
// u v per corner, in pixels of the sprite (0 to its width or height, v down)
uvs: [number, number][];
shade: number;
}
// one pixel of a 16 pixel sprite
const PIXEL = 1 / TEXTURE_SIZE;
const HALF_THICKNESS = PIXEL / 2;
// as thick as a pixel of a 16 pixel sprite, whatever the sprite's size
const HALF_THICKNESS = 1 / TEXTURE_SIZE / 2;
// keeps lookups off the neighboring sprite, and side faces in the middle of their pixel
const FACE_PAD = 0.02;
const SIDE_PAD = 0.25;
@@ -37,35 +36,50 @@ export function item_model(texture_id: string): ItemQuad[] {
}
// the model for a sprite's opaque pixels (see opaque_pixels in atlas.ts)
export function extrude_item(mask: Uint8Array | undefined): ItemQuad[] {
// without a mask it's a 16×16 sprite
export function extrude_item(opaque: OpaqueMask | undefined): ItemQuad[] {
const width = opaque?.width ?? TEXTURE_SIZE;
const height = opaque?.height ?? TEXTURE_SIZE;
const t = HALF_THICKNESS;
const lo = FACE_PAD;
const hi = TEXTURE_SIZE - FACE_PAD;
const [u_lo, u_hi] = [FACE_PAD, width - FACE_PAD];
const [v_lo, v_hi] = [FACE_PAD, height - FACE_PAD];
const quads: ItemQuad[] = [
// the front, and the back mirrored so it reads the same way round as minecraft's
quad([[-0.5, 0, t], [0.5, 0, t], [0.5, 1, t], [-0.5, 1, t]], [[lo, hi], [hi, hi], [hi, lo], [lo, lo]], [
0,
0,
quad(
[[-0.5, 0, t], [0.5, 0, t], [0.5, 1, t], [-0.5, 1, t]],
[[u_lo, v_hi], [u_hi, v_hi], [u_hi, v_lo], [
u_lo,
v_lo,
]],
[0, 0, 1],
1,
], 1),
quad([[0.5, 0, -t], [-0.5, 0, -t], [-0.5, 1, -t], [0.5, 1, -t]], [[hi, hi], [lo, hi], [lo, lo], [hi, lo]], [
0,
0,
-1,
], 1),
),
quad(
[[0.5, 0, -t], [-0.5, 0, -t], [-0.5, 1, -t], [0.5, 1, -t]],
[[u_hi, v_hi], [u_lo, v_hi], [u_lo, v_lo], [
u_hi,
v_lo,
]],
[0, 0, -1],
1,
),
];
if (!mask) return quads;
if (!opaque) return quads;
const { mask } = opaque;
const seen = (px: number, py: number) =>
px >= 0 && py >= 0 && px < TEXTURE_SIZE && py < TEXTURE_SIZE && mask[py * TEXTURE_SIZE + px] === 1;
px >= 0 && py >= 0 && px < width && py < height && mask[py * width + px] === 1;
for (let py = 0; py < TEXTURE_SIZE; py++) {
for (let px = 0; px < TEXTURE_SIZE; px++) {
// a sprite that isn't square is squeezed into the square
const pixel_w = 1 / width;
const pixel_h = 1 / height;
for (let py = 0; py < height; py++) {
for (let px = 0; px < width; px++) {
if (!seen(px, py)) continue;
const x0 = px * PIXEL - 0.5;
const x1 = x0 + PIXEL;
const y1 = 1 - py * PIXEL;
const y0 = y1 - PIXEL;
const x0 = px * pixel_w - 0.5;
const x1 = x0 + pixel_w;
const y1 = 1 - py * pixel_h;
const y0 = y1 - pixel_h;
// the whole side face is this pixel's color
const u0 = px + SIDE_PAD;
const u1 = px + 1 - SIDE_PAD;
+8 -8
View File
@@ -1,4 +1,4 @@
import { TEXTURE_SIZE } from "$/common/constants.ts";
import type { SpriteRegion } from "$/common/constants.ts";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { AssetManager } from "$/client/assets.ts";
import { get_sprite_region } from "$/client/sprites.ts";
@@ -91,8 +91,8 @@ export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransf
const texture_id = item_texture(item);
const region = get_sprite_region(texture_id);
const u = (pixels: number) => (region.x * TEXTURE_SIZE + pixels) / atlas.width;
const v = (pixels: number) => (region.y * TEXTURE_SIZE + pixels) / atlas.height;
const u = (pixels: number) => (region.x + pixels) / atlas.width;
const v = (pixels: number) => (region.y + pixels) / atlas.height;
for (const quad of item_model(texture_id)) {
for (const i of [0, 1, 2, 0, 2, 3]) {
const [px, py, pz] = transform(...quad.corners[i]);
@@ -102,11 +102,11 @@ export function push_item(atlas: Texture, item: ItemStack, transform: ItemTransf
}
}
function push_textured_quad(atlas: Texture, region: { x: number; y: number }, corners: number[][], color: number[]) {
const u0 = (region.x * TEXTURE_SIZE + UV_PAD) / atlas.width;
const v0 = (region.y * TEXTURE_SIZE + UV_PAD) / atlas.height;
const u1 = ((region.x + 1) * TEXTURE_SIZE - UV_PAD) / atlas.width;
const v1 = ((region.y + 1) * TEXTURE_SIZE - UV_PAD) / 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;
const u1 = (region.x + region.width - UV_PAD) / atlas.width;
const v1 = (region.y + region.height - UV_PAD) / atlas.height;
for (const i of [0, 1, 2, 0, 2, 3]) {
const [px, py, pz] = corners[i];
+4 -5
View File
@@ -1,4 +1,3 @@
import { TEXTURE_SIZE } from "$/common/constants.ts";
import { type Chunk, CHUNK_SIZE, type ChunkMesh, ClientLevel } from "$/client/level/client_level.ts";
import { Camera } from "$/client/camera.ts";
import { AssetManager } from "$/client/assets.ts";
@@ -138,10 +137,10 @@ export class LevelRenderer {
block.x,
block.y,
block.z,
region.x * TEXTURE_SIZE,
region.y * TEXTURE_SIZE,
TEXTURE_SIZE,
TEXTURE_SIZE,
region.x,
region.y,
region.width,
region.height,
);
}
}
+13 -13
View File
@@ -1,4 +1,4 @@
import { SLOT_SIZE, TEXTURE_SIZE } from "$/common/constants.ts";
import { SLOT_SIZE } from "$/common/constants.ts";
import { get_sprite_region } from "$/client/sprites.ts";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { AssetManager } from "$/client/assets.ts";
@@ -132,10 +132,10 @@ function draw_item_block(block_info: BlockRegistry, x: number, y: number) {
draw_texture_region_skewed(
atlas,
top.x * TEXTURE_SIZE,
top.y * TEXTURE_SIZE,
TEXTURE_SIZE,
TEXTURE_SIZE,
top.x,
top.y,
top.width,
top.height,
[
{ x: cx, y: cy - half_h },
{ x: cx + half_w, y: cy },
@@ -146,10 +146,10 @@ function draw_item_block(block_info: BlockRegistry, x: number, y: number) {
);
draw_texture_region_skewed(
atlas,
left.x * TEXTURE_SIZE,
left.y * TEXTURE_SIZE,
TEXTURE_SIZE,
TEXTURE_SIZE,
left.x,
left.y,
left.width,
left.height,
[
{ x: cx - half_w, y: cy },
{ x: cx, y: cy + half_h },
@@ -160,10 +160,10 @@ function draw_item_block(block_info: BlockRegistry, x: number, y: number) {
);
draw_texture_region_skewed(
atlas,
front.x * TEXTURE_SIZE,
front.y * TEXTURE_SIZE,
TEXTURE_SIZE,
TEXTURE_SIZE,
front.x,
front.y,
front.width,
front.height,
[
{ x: cx + half_w, y: cy },
{ x: cx, y: cy + half_h },