Fix old rendering hack
This commit is contained in:
+45
-8
@@ -17,6 +17,10 @@ export interface TextureSize {
|
||||
height: number;
|
||||
}
|
||||
|
||||
// pixels around each texture, copies of its edge, so a face whose uvs reach the texture's edge never shows a bit of
|
||||
// the next texture over, however the lookup rounds. see extrude_borders
|
||||
export const ATLAS_BORDER = 1;
|
||||
|
||||
// shelves, tallest first so each row wastes little, and by id when they're the same size so the same textures always
|
||||
// give the same atlas. engine:missing is 16×16 and always there
|
||||
export function atlas_layout(sizes: Map<string, TextureSize>): AtlasLayout {
|
||||
@@ -24,8 +28,11 @@ export function atlas_layout(sizes: Map<string, TextureSize>): AtlasLayout {
|
||||
entries.push(["engine:missing", { width: TEXTURE_SIZE, height: TEXTURE_SIZE }]);
|
||||
entries.sort(([a, sa], [b, sb]) => sb.height - sa.height || sb.width - sa.width || a.localeCompare(b));
|
||||
|
||||
const area = entries.reduce((sum, [, { width, height }]) => sum + width * height, 0);
|
||||
const widest = Math.max(...entries.map(([, { width }]) => width));
|
||||
const area = entries.reduce(
|
||||
(sum, [, { width, height }]) => sum + (width + 2 * ATLAS_BORDER) * (height + 2 * ATLAS_BORDER),
|
||||
0,
|
||||
);
|
||||
const widest = Math.max(...entries.map(([, { width }]) => width + 2 * ATLAS_BORDER));
|
||||
let size = 2 ** Math.ceil(Math.log2(Math.max(Math.sqrt(area), widest)));
|
||||
while (true) {
|
||||
const regions = pack_shelves(entries, size);
|
||||
@@ -34,21 +41,22 @@ export function atlas_layout(sizes: Map<string, TextureSize>): AtlasLayout {
|
||||
}
|
||||
}
|
||||
|
||||
// undefined if they don't fit in a size × size square
|
||||
// undefined if they don't fit in a size × size square. regions are the textures themselves, inside their border
|
||||
function pack_shelves(entries: [string, TextureSize][], size: number) {
|
||||
const regions: Record<string, SpriteRegion> = {};
|
||||
let x = 0;
|
||||
let y = 0;
|
||||
let shelf = 0;
|
||||
for (const [id, { width, height }] of entries) {
|
||||
if (x + width > size) {
|
||||
const [outer_width, outer_height] = [width + 2 * ATLAS_BORDER, height + 2 * ATLAS_BORDER];
|
||||
if (x + outer_width > size) {
|
||||
x = 0;
|
||||
y += shelf;
|
||||
shelf = 0;
|
||||
}
|
||||
regions[id] = { x, y, width, height };
|
||||
x += width;
|
||||
shelf = Math.max(shelf, height);
|
||||
regions[id] = { x: x + ATLAS_BORDER, y: y + ATLAS_BORDER, width, height };
|
||||
x += outer_width;
|
||||
shelf = Math.max(shelf, outer_height);
|
||||
}
|
||||
return y + shelf <= size ? regions : undefined;
|
||||
}
|
||||
@@ -110,9 +118,12 @@ export async function build_atlas(textures: Map<string, Uint8Array>, texture_jso
|
||||
regions[id] ??= missing;
|
||||
}
|
||||
|
||||
// which pixels of each texture can be seen, for the 3d models of dropped items (see item_model.ts)
|
||||
const image_data = ctx.getImageData(0, 0, size, size);
|
||||
const pixels = image_data.data;
|
||||
extrude_borders(pixels, size, regions);
|
||||
ctx.putImageData(image_data, 0, 0);
|
||||
|
||||
// which pixels of each texture can be seen, for the 3d models of dropped items (see item_model.ts)
|
||||
built_atlas = image_data;
|
||||
opaque.clear();
|
||||
for (const [id, region] of Object.entries(regions)) {
|
||||
@@ -129,6 +140,32 @@ export async function build_atlas(textures: Map<string, Uint8Array>, texture_jso
|
||||
return { image: canvas.transferToImageBitmap(), regions, animated };
|
||||
}
|
||||
|
||||
// fills each region's border with copies of its edge pixels, the corners with its corners. pixels is a size × size
|
||||
// rgba atlas. an animated texture's border keeps its first frame's edge, only rounding ever shows it
|
||||
export function extrude_borders(pixels: Uint8ClampedArray, size: number, regions: Record<string, SpriteRegion>) {
|
||||
const copy = (from_x: number, from_y: number, to_x: number, to_y: number) => {
|
||||
if (to_x < 0 || to_y < 0 || to_x >= size || to_y >= size) return;
|
||||
const from = (from_y * size + from_x) * 4;
|
||||
const to = (to_y * size + to_x) * 4;
|
||||
for (let c = 0; c < 4; c++) pixels[to + c] = pixels[from + c];
|
||||
};
|
||||
// the missing texture fills in for unknown ids too, once is enough
|
||||
for (const { x, y, width, height } of new Set(Object.values(regions))) {
|
||||
for (let b = 1; b <= ATLAS_BORDER; b++) {
|
||||
for (let px = -b; px < width + b; px++) {
|
||||
const from_x = x + Math.min(Math.max(px, 0), width - 1);
|
||||
copy(from_x, y, x + px, y - b);
|
||||
copy(from_x, y + height - 1, x + px, y + height - 1 + b);
|
||||
}
|
||||
for (let py = -b; py < height + b; py++) {
|
||||
const from_y = y + Math.min(Math.max(py, 0), height - 1);
|
||||
copy(x, from_y, x - b, y + py);
|
||||
copy(x + width - 1, from_y, x + width - 1 + b, y + py);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// every frame of an animated texture stays in memory, to copy into the atlas when it's that frame's turn
|
||||
function image_pixels(image: ImageBitmap) {
|
||||
const ctx = new OffscreenCanvas(image.width, image.height).getContext("2d")!;
|
||||
|
||||
@@ -26,8 +26,6 @@ import type { Vec3 } from "./player_renderer.ts";
|
||||
// how big items on the ground are drawn, minecraft's ground transform
|
||||
const BLOCK_SCALE = 0.25;
|
||||
const ITEM_SCALE = 0.5;
|
||||
// keeps texture lookups off the sprite's edge
|
||||
const UV_PAD = 0.5;
|
||||
|
||||
// each face's corners in drawing order (counter clockwise from outside) on a unit cube, with its shade.
|
||||
// top, bottom, front, back, left, right, like the chunk mesher
|
||||
@@ -254,22 +252,21 @@ 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
|
||||
// where a model's uv in pixels (0-16) is in the atlas. the atlas's border around each sprite (see ATLAS_BORDER) keeps
|
||||
// lookups that round past its edge on its own pixels
|
||||
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;
|
||||
return (region.x + pixels / TEXTURE_SIZE * region.width) / 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;
|
||||
return (region.y + pixels / TEXTURE_SIZE * region.height) / 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;
|
||||
const u0 = region.x / atlas.width;
|
||||
const v0 = region.y / atlas.height;
|
||||
const u1 = (region.x + region.width) / atlas.width;
|
||||
const v1 = (region.y + region.height) / atlas.height;
|
||||
|
||||
for (const i of [0, 1, 2, 0, 2, 3]) {
|
||||
const [px, py, pz] = corners[i];
|
||||
|
||||
@@ -57,9 +57,6 @@ import { liquid_height, liquid_textures } from "$/common/everything_registry.ts"
|
||||
|
||||
type TexturesInfo = Record<string, SpriteRegion>;
|
||||
|
||||
// keeps texture lookups off the sprite's edge
|
||||
const UV_PAD = 0.5;
|
||||
|
||||
// same order as FACE_NORMALS in translucent_sort.ts, and FACE_CORNERS and CORNER_UVS in block_models.ts
|
||||
// minecraft's shading by direction, so faces stay apart even in flat light
|
||||
const FACE_SHADE = [1.0, 0.5, 0.8, 0.8, 0.6, 0.6];
|
||||
@@ -437,15 +434,14 @@ function push_quad(
|
||||
buffer.count += 1;
|
||||
}
|
||||
|
||||
// model uvs (0 to TEXTURE_SIZE across the sprite, whatever its size) to atlas coordinates, kept off the sprite's edge
|
||||
// model uvs (0 to TEXTURE_SIZE across the sprite, whatever its size) to atlas coordinates. the atlas's border around
|
||||
// each sprite (see ATLAS_BORDER) keeps lookups that round past its edge on its own pixels
|
||||
function atlas_u(sprite: SpriteRegion, u: number) {
|
||||
const px = u * sprite.width / TEXTURE_SIZE;
|
||||
return (sprite.x + Math.min(Math.max(px, UV_PAD), sprite.width - UV_PAD)) / image.width;
|
||||
return (sprite.x + u * sprite.width / TEXTURE_SIZE) / image.width;
|
||||
}
|
||||
|
||||
function atlas_v(sprite: SpriteRegion, v: number) {
|
||||
const px = v * sprite.height / TEXTURE_SIZE;
|
||||
return (sprite.y + Math.min(Math.max(px, UV_PAD), sprite.height - UV_PAD)) / image.height;
|
||||
return (sprite.y + v * sprite.height / TEXTURE_SIZE) / image.height;
|
||||
}
|
||||
|
||||
// the model quads for a block value, baked the first time it's seen
|
||||
|
||||
Reference in New Issue
Block a user