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")!;
|
||||
|
||||
Reference in New Issue
Block a user