Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 256x 256x 256x 256x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 5x 4x 4x 4x 4x 4x 4x 4x 4x 4x 256x 256x 256x 256x 256x 256x 4x 4x 4x 5x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 1x | import {
DMG_PALETTE,
TILE_COLOR_PALETTE,
TILE_COLOR_PROP_FLIP_HORIZONTAL,
TILE_COLOR_PROP_FLIP_VERTICAL,
TILE_SIZE,
} from "consts";
import { hex2GBCrgb } from "shared/lib/helpers/color";
import {
buildSceneTilesetReferenceLookup,
decodeSceneTileReference,
} from "shared/lib/tiles/sceneTilemapReferences";
import type {
ColorCorrectionSetting,
MonoBGPPalette,
TilesetSnapshot,
} from "shared/lib/resources/types";
// eslint-disable-next-line no-restricted-globals
const workerCtx: Worker = self as unknown as Worker;
export interface TilemapLayersWorkerTileset {
id: string;
width: number;
src: string;
}
export interface TilemapLayersCanvasData {
canvasId: string;
sequence: number;
width: number;
height: number;
tiles: Uint32Array;
tileColors: Uint8Array;
tilesetSnapshots: TilesetSnapshot[];
tilesets: Array<TilemapLayersWorkerTileset | undefined>;
palettes: string[][];
previewAsMono?: boolean;
monoBGP: MonoBGPPalette;
colorCorrection: ColorCorrectionSetting;
}
export interface TilemapLayersCanvasResult {
canvasId: string;
sequence: number;
width: number;
height: number;
canvasImage: ImageBitmap;
}
const imageCache = new Map<string, Promise<ImageBitmap | undefined>>();
const colorizedTileCache = new Map<string, OffscreenCanvas>();
const loadImage = (src: string): Promise<ImageBitmap | undefined> => {
const cached = imageCache.get(src);
Iif (cached) return cached;
const image = fetch(src)
.then((response) => {
Iif (!response.ok) throw new Error(`Unable to load tileset: ${src}`);
return response.blob();
})
.then(createImageBitmap)
.catch(() => undefined);
imageCache.set(src, image);
return image;
};
const indexColour = (green: number) => {
Iif (green < 65) return 3;
Iif (green < 130) return 2;
Iif (green < 205) return 1;
return 0;
};
export const renderTilemapLayers = async (
data: TilemapLayersCanvasData,
): Promise<TilemapLayersCanvasResult | undefined> => {
const {
canvasId,
sequence,
width,
height,
tiles,
tileColors,
tilesetSnapshots,
tilesets,
palettes,
previewAsMono,
monoBGP,
colorCorrection,
} = data;
const canvas = new OffscreenCanvas(width * TILE_SIZE, height * TILE_SIZE);
const ctx = canvas.getContext("2d");
Iif (!ctx) return;
const loadedImages = await Promise.all(
tilesets.map((tileset) =>
tileset ? loadImage(tileset.src) : Promise.resolve(undefined),
),
);
const correctColor = hex2GBCrgb(colorCorrection);
const palettesRGB = palettes.map((palette) => palette.map(correctColor));
const dmgPalette = DMG_PALETTE.colors.map(correctColor);
const colorizationKey = JSON.stringify([
colorCorrection,
previewAsMono,
monoBGP,
palettes,
]);
ctx.imageSmoothingEnabled = false;
const emptyColor = dmgPalette[0];
ctx.fillStyle = `rgb(${emptyColor.r}, ${emptyColor.g}, ${emptyColor.b})`;
ctx.fillRect(0, 0, canvas.width, canvas.height);
const tilesetLookup = buildSceneTilesetReferenceLookup({
tilesets: tilesetSnapshots,
});
const tilesetsLookup = Object.fromEntries(
tilesets
.filter((tileset): tileset is TilemapLayersWorkerTileset => !!tileset)
.map((tileset) => [tileset.id, tileset]),
);
const colorizedTile = (
image: ImageBitmap,
sourceX: number,
sourceY: number,
cacheKey: string,
attr: number,
) => {
const cached = colorizedTileCache.get(cacheKey);
Iif (cached) return cached;
const tileCanvas = new OffscreenCanvas(TILE_SIZE, TILE_SIZE);
const tileCtx = tileCanvas.getContext("2d", { willReadFrequently: true });
Iif (!tileCtx) return undefined;
tileCtx.drawImage(
image,
sourceX,
sourceY,
TILE_SIZE,
TILE_SIZE,
0,
0,
TILE_SIZE,
TILE_SIZE,
);
const imageData = tileCtx.getImageData(0, 0, TILE_SIZE, TILE_SIZE);
const palette = palettesRGB[attr & TILE_COLOR_PALETTE] ?? palettesRGB[0];
for (let index = 0; index < imageData.data.length; index += 4) {
const colorIndex = indexColour(imageData.data[index + 1]);
const color = previewAsMono
? dmgPalette[monoBGP[colorIndex]]
: (palette?.[colorIndex] ?? dmgPalette[colorIndex]);
imageData.data[index] = color.r;
imageData.data[index + 1] = color.g;
imageData.data[index + 2] = color.b;
imageData.data[index + 3] = 255;
}
tileCtx.putImageData(imageData, 0, 0);
colorizedTileCache.set(cacheKey, tileCanvas);
return tileCanvas;
};
const fillEmptyTile = (cellIndex: number, attr: number) => {
const palette = palettesRGB[attr & TILE_COLOR_PALETTE] ?? palettesRGB[0];
const color = previewAsMono
? dmgPalette[monoBGP[0]]
: (palette?.[0] ?? dmgPalette[0]);
ctx.fillStyle = `rgb(${color.r}, ${color.g}, ${color.b})`;
ctx.fillRect(
(cellIndex % width) * TILE_SIZE,
Math.floor(cellIndex / width) * TILE_SIZE,
TILE_SIZE,
TILE_SIZE,
);
};
tiles.forEach((value, cellIndex) => {
const ref = decodeSceneTileReference(value, tilesetLookup);
const tileset = ref && tilesetsLookup[ref.tilesetId];
const image = ref && loadedImages[ref.tilesetIndex];
const attr = tileColors[cellIndex] ?? 0;
if (!ref || !tileset || !image) {
fillEmptyTile(cellIndex, attr);
return;
}
const sourceX = (ref.tileIndex % Math.max(1, tileset.width)) * TILE_SIZE;
const sourceY =
Math.floor(ref.tileIndex / Math.max(1, tileset.width)) * TILE_SIZE;
const destX = (cellIndex % width) * TILE_SIZE;
const destY = Math.floor(cellIndex / width) * TILE_SIZE;
const tileCanvas = colorizedTile(
image,
sourceX,
sourceY,
`${colorizationKey}:${tileset.src}:${tileset.width}:${ref.tileIndex}:${
attr & TILE_COLOR_PALETTE
}`,
attr,
);
Iif (!tileCanvas) return;
const flipX = !!(attr & TILE_COLOR_PROP_FLIP_HORIZONTAL);
const flipY = !!(attr & TILE_COLOR_PROP_FLIP_VERTICAL);
ctx.save();
ctx.translate(
destX + (flipX ? TILE_SIZE : 0),
destY + (flipY ? TILE_SIZE : 0),
);
ctx.scale(flipX ? -1 : 1, flipY ? -1 : 1);
ctx.drawImage(tileCanvas, 0, 0);
ctx.restore();
});
const canvasImage = canvas.transferToImageBitmap();
return {
canvasId,
sequence,
width: canvas.width,
height: canvas.height,
canvasImage,
};
};
workerCtx.onmessage = async (evt: MessageEvent<TilemapLayersCanvasData>) => {
const result = await renderTilemapLayers(evt.data);
if (result) {
workerCtx.postMessage(result, [result.canvasImage]);
}
};
|