| 1 | import { CHUNK_SIZE, SPRITE_SIZE_PX } from "/shared/protocol/constants.js"; |
| 2 | import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, normalizeGround, normalizeBlock } from "/shared/definitions/tiles.js"; |
| 3 | import { bloomImageName, spriteImageName } from "/shared/definitions/tileSprites.js"; |
| 4 | import { resolveSeasonalVisualState, SeasonId } from "/shared/world/season.js"; |
| 5 | import { |
| 6 | SOFT_TERRAIN_NEIGHBOR_DIRS, |
| 7 | softTerrainBlendWeight, |
| 8 | softTerrainNeighborSample, |
| 9 | mixSoftTerrainPixel |
| 10 | } from "/shared/render/softTerrain.js"; |
| 11 | |
| 12 | /** |
| 13 | * Bake immobile ≤16×16 ground/block/bloom tiles into chunk atlases. |
| 14 | * Oversized or animated blocks/grounds are returned for individual sprites. |
| 15 | * @param {{ |
| 16 | * snapshot: object, |
| 17 | * catalog: object, |
| 18 | * images: object, |
| 19 | * preview?: object | null, |
| 20 | * season?: string, |
| 21 | * softTerrain?: boolean, |
| 22 | * resolveGround?: (tileX: number, tileY: number) => object | number | null |
| 23 | * }} options |
| 24 | */ |
| 25 | export function bakeChunkAtlases ({ |
| 26 | snapshot, |
| 27 | catalog, |
| 28 | images, |
| 29 | preview = null, |
| 30 | season = SeasonId.summer, |
| 31 | softTerrain = false, |
| 32 | resolveGround = null |
| 33 | }) |
| 34 | { |
| 35 | const width = CHUNK_SIZE * SPRITE_SIZE_PX; |
| 36 | const height = CHUNK_SIZE * SPRITE_SIZE_PX; |
| 37 | const ground = makeCanvas(width, height); |
| 38 | const bloom = makeCanvas(width, height); |
| 39 | const blockRows = []; |
| 40 | for (let row = 0; row < CHUNK_SIZE; row++) |
| 41 | { blockRows.push(makeCanvas(width, SPRITE_SIZE_PX)); } |
| 42 | |
| 43 | const groundCtx = ground.getContext("2d", { willReadFrequently: softTerrain }); |
| 44 | const bloomCtx = bloom.getContext("2d"); |
| 45 | const blockCtxs = blockRows.map((canvas) => canvas.getContext("2d")); |
| 46 | for (const ctx of [groundCtx, bloomCtx, ...blockCtxs]) |
| 47 | { |
| 48 | ctx.imageSmoothingEnabled = false; |
| 49 | ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
| 50 | } |
| 51 | |
| 52 | const tallBlocks = []; |
| 53 | const animatedGrounds = []; |
| 54 | const baseTileX = snapshot.chunkX * CHUNK_SIZE; |
| 55 | const baseTileY = snapshot.chunkY * CHUNK_SIZE; |
| 56 | const pixelCache = softTerrain ? new Map() : null; |
| 57 | |
| 58 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 59 | { |
| 60 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 61 | { |
| 62 | const index = localY * CHUNK_SIZE + localX; |
| 63 | const tileX = baseTileX + localX; |
| 64 | const tileY = baseTileY + localY; |
| 65 | let groundEntry = snapshot.grounds[index]; |
| 66 | let blockEntry = snapshot.blocks[index]; |
| 67 | if (preview && preview.tileX === tileX && preview.tileY === tileY) |
| 68 | { |
| 69 | if (preview.ground !== undefined) |
| 70 | { groundEntry = preview.ground; } |
| 71 | if (preview.block !== undefined) |
| 72 | { blockEntry = preview.block; } |
| 73 | } |
| 74 | |
| 75 | const dx = localX * SPRITE_SIZE_PX; |
| 76 | const dy = localY * SPRITE_SIZE_PX; |
| 77 | const groundName = groundSpriteName(catalog, groundEntry, season); |
| 78 | const groundMeta = groundCatalogMeta(catalog, groundEntry); |
| 79 | if (isBakeable(images[groundName], groundMeta)) |
| 80 | { |
| 81 | const blendable = softTerrain && isGroundBlendable(groundEntry); |
| 82 | if (blendable) |
| 83 | { |
| 84 | blitSoftGround( |
| 85 | groundCtx, |
| 86 | images, |
| 87 | catalog, |
| 88 | groundEntry, |
| 89 | groundName, |
| 90 | dx, |
| 91 | dy, |
| 92 | tileX, |
| 93 | tileY, |
| 94 | season, |
| 95 | resolveGround, |
| 96 | pixelCache |
| 97 | ); |
| 98 | } |
| 99 | else |
| 100 | { blit(groundCtx, images[groundName], dx, dy); } |
| 101 | const groundBloom = groundBloomName(catalog, groundEntry, season); |
| 102 | if (groundBloom) |
| 103 | { blit(bloomCtx, images[groundBloom], dx, dy); } |
| 104 | } |
| 105 | else if (groundName) |
| 106 | { |
| 107 | animatedGrounds.push({ |
| 108 | tileX, |
| 109 | tileY, |
| 110 | sprite: groundName, |
| 111 | typeId: normalizeGround(groundEntry).typeId, |
| 112 | bloom: groundBloomName(catalog, groundEntry, season) |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | const blockName = blockSpriteName(catalog, blockEntry, season); |
| 117 | if (!blockName) |
| 118 | { continue; } |
| 119 | const blockMeta = blockCatalogMeta(catalog, blockEntry); |
| 120 | if (isBakeable(images[blockName], blockMeta)) |
| 121 | { |
| 122 | blit(blockCtxs[localY], images[blockName], dx, 0); |
| 123 | const blockBloom = blockBloomName(catalog, blockEntry, season); |
| 124 | if (blockBloom) |
| 125 | { blit(bloomCtx, images[blockBloom], dx, dy); } |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | tallBlocks.push({ |
| 130 | tileX, |
| 131 | tileY, |
| 132 | sprite: blockName, |
| 133 | bloom: blockBloomName(catalog, blockEntry, season) |
| 134 | }); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return { ground, blockRows, bloom, tallBlocks, animatedGrounds }; |
| 140 | } |
| 141 | |
| 142 | export function isBakeable (sprite, meta = null) |
| 143 | { |
| 144 | if (!sprite?.g_image) |
| 145 | { return false; } |
| 146 | if (sprite.width > SPRITE_SIZE_PX || sprite.height > SPRITE_SIZE_PX) |
| 147 | { return false; } |
| 148 | const frames = Number(sprite.subImages) || Number(sprite.subImgTotal) || (meta?.frames ?? 1); |
| 149 | const speed = sprite._animationSpeed ?? meta?.animationSpeed ?? 0; |
| 150 | if (frames > 1) |
| 151 | { return false; } |
| 152 | if (speed > 0) |
| 153 | { return false; } |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | function makeCanvas (width, height) |
| 158 | { |
| 159 | const canvas = document.createElement("canvas"); |
| 160 | canvas.width = width; |
| 161 | canvas.height = height; |
| 162 | return canvas; |
| 163 | } |
| 164 | |
| 165 | function blit (ctx, sprite, dx, dy) |
| 166 | { |
| 167 | if (!sprite?.g_image) |
| 168 | { return; } |
| 169 | const sub = sprite.getSubImage(0); |
| 170 | ctx.drawImage( |
| 171 | sprite.g_image, |
| 172 | sub.sx, |
| 173 | sub.sy, |
| 174 | sub.sw, |
| 175 | sub.sh, |
| 176 | dx, |
| 177 | dy, |
| 178 | SPRITE_SIZE_PX, |
| 179 | SPRITE_SIZE_PX |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | function isGroundBlendable (ground) |
| 184 | { |
| 185 | const entry = normalizeGround(ground); |
| 186 | return !!GROUND_DEFINITIONS[entry.typeId]?.blendable; |
| 187 | } |
| 188 | |
| 189 | function groundVisualKey (catalog, ground, season) |
| 190 | { |
| 191 | const entry = normalizeGround(ground); |
| 192 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 193 | const states = catalog.grounds?.[def.key]?.states ?? []; |
| 194 | const state = resolveSeasonalVisualState(states, entry.state, season); |
| 195 | return `${entry.typeId}:${state ?? ""}`; |
| 196 | } |
| 197 | |
| 198 | function blitSoftGround ( |
| 199 | ctx, |
| 200 | images, |
| 201 | catalog, |
| 202 | groundEntry, |
| 203 | groundName, |
| 204 | dx, |
| 205 | dy, |
| 206 | tileX, |
| 207 | tileY, |
| 208 | season, |
| 209 | resolveGround, |
| 210 | pixelCache |
| 211 | ) |
| 212 | { |
| 213 | const selfPixels = spritePixels(images[groundName], pixelCache); |
| 214 | if (!selfPixels) |
| 215 | { |
| 216 | blit(ctx, images[groundName], dx, dy); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | const selfKey = groundVisualKey(catalog, groundEntry, season); |
| 221 | const neighborSprites = []; |
| 222 | for (const { ndx, ndy } of SOFT_TERRAIN_NEIGHBOR_DIRS) |
| 223 | { |
| 224 | const neighborGround = resolveNeighborGround( |
| 225 | tileX + ndx, |
| 226 | tileY + ndy, |
| 227 | resolveGround |
| 228 | ); |
| 229 | if (neighborGround == null) |
| 230 | { continue; } |
| 231 | if (groundVisualKey(catalog, neighborGround, season) === selfKey) |
| 232 | { continue; } |
| 233 | const neighborName = groundSpriteName(catalog, neighborGround, season); |
| 234 | const pixels = spritePixels(images[neighborName], pixelCache); |
| 235 | if (!pixels) |
| 236 | { continue; } |
| 237 | neighborSprites.push({ |
| 238 | ndx, |
| 239 | ndy, |
| 240 | pixels, |
| 241 | neighborBlendable: isGroundBlendable(neighborGround) |
| 242 | }); |
| 243 | } |
| 244 | |
| 245 | if (!neighborSprites.length) |
| 246 | { |
| 247 | blit(ctx, images[groundName], dx, dy); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | const out = ctx.createImageData(SPRITE_SIZE_PX, SPRITE_SIZE_PX); |
| 252 | const dst = out.data; |
| 253 | for (let py = 0; py < SPRITE_SIZE_PX; py++) |
| 254 | { |
| 255 | for (let px = 0; px < SPRITE_SIZE_PX; px++) |
| 256 | { |
| 257 | const self = readPixel(selfPixels, px, py); |
| 258 | const contribs = []; |
| 259 | for (const neighbor of neighborSprites) |
| 260 | { |
| 261 | const weight = softTerrainBlendWeight(px, py, neighbor.ndx, neighbor.ndy, SPRITE_SIZE_PX, { |
| 262 | neighborBlendable: neighbor.neighborBlendable |
| 263 | }); |
| 264 | if (weight <= 0) |
| 265 | { continue; } |
| 266 | const sample = softTerrainNeighborSample(px, py, neighbor.ndx, neighbor.ndy, SPRITE_SIZE_PX); |
| 267 | contribs.push({ |
| 268 | weight, |
| 269 | color: readPixel(neighbor.pixels, sample.sx, sample.sy) |
| 270 | }); |
| 271 | } |
| 272 | const mixed = mixSoftTerrainPixel(self, contribs); |
| 273 | const i = (py * SPRITE_SIZE_PX + px) * 4; |
| 274 | dst[i] = mixed.r; |
| 275 | dst[i + 1] = mixed.g; |
| 276 | dst[i + 2] = mixed.b; |
| 277 | dst[i + 3] = mixed.a; |
| 278 | } |
| 279 | } |
| 280 | ctx.putImageData(out, dx, dy); |
| 281 | } |
| 282 | |
| 283 | function resolveNeighborGround (tileX, tileY, resolveGround) |
| 284 | { |
| 285 | if (typeof resolveGround === "function") |
| 286 | { return resolveGround(tileX, tileY); } |
| 287 | return null; |
| 288 | } |
| 289 | |
| 290 | function spritePixels (sprite, cache) |
| 291 | { |
| 292 | if (!sprite?.g_image) |
| 293 | { return null; } |
| 294 | const key = sprite.g_image?.src ?? sprite; |
| 295 | if (cache?.has(key)) |
| 296 | { return cache.get(key); } |
| 297 | const sub = sprite.getSubImage(0); |
| 298 | const canvas = makeCanvas(SPRITE_SIZE_PX, SPRITE_SIZE_PX); |
| 299 | const ctx = canvas.getContext("2d", { willReadFrequently: true }); |
| 300 | ctx.imageSmoothingEnabled = false; |
| 301 | ctx.drawImage( |
| 302 | sprite.g_image, |
| 303 | sub.sx, |
| 304 | sub.sy, |
| 305 | sub.sw, |
| 306 | sub.sh, |
| 307 | 0, |
| 308 | 0, |
| 309 | SPRITE_SIZE_PX, |
| 310 | SPRITE_SIZE_PX |
| 311 | ); |
| 312 | const data = ctx.getImageData(0, 0, SPRITE_SIZE_PX, SPRITE_SIZE_PX); |
| 313 | cache?.set(key, data); |
| 314 | return data; |
| 315 | } |
| 316 | |
| 317 | function readPixel (imageData, x, y) |
| 318 | { |
| 319 | const i = (y * imageData.width + x) * 4; |
| 320 | const d = imageData.data; |
| 321 | return { r: d[i], g: d[i + 1], b: d[i + 2], a: d[i + 3] }; |
| 322 | } |
| 323 | |
| 324 | function groundSpriteName (catalog, ground, season = SeasonId.summer) |
| 325 | { |
| 326 | const entry = normalizeGround(ground); |
| 327 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 328 | const states = catalog.grounds?.[def.key]?.states ?? []; |
| 329 | const state = resolveSeasonalVisualState(states, entry.state, season); |
| 330 | return spriteImageName("ground", def.key, state); |
| 331 | } |
| 332 | |
| 333 | function blockSpriteName (catalog, block, season = SeasonId.summer) |
| 334 | { |
| 335 | const entry = normalizeBlock(block); |
| 336 | if (!entry) |
| 337 | { return null; } |
| 338 | const def = BLOCK_DEFINITIONS[entry.typeId]; |
| 339 | if (!def || def.editorHidden) |
| 340 | { return null; } |
| 341 | const states = catalog.blocks?.[def.key]?.states ?? []; |
| 342 | const state = resolveSeasonalVisualState(states, entry.state, season); |
| 343 | return spriteImageName("block", def.key, state); |
| 344 | } |
| 345 | |
| 346 | function groundBloomName (catalog, ground, season = SeasonId.summer) |
| 347 | { |
| 348 | const entry = normalizeGround(ground); |
| 349 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 350 | const type = catalog.grounds?.[def.key]; |
| 351 | const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season); |
| 352 | if (!state || !type?.blooms?.[state]) |
| 353 | { return null; } |
| 354 | return bloomImageName("ground", def.key, state); |
| 355 | } |
| 356 | |
| 357 | function blockBloomName (catalog, block, season = SeasonId.summer) |
| 358 | { |
| 359 | const entry = normalizeBlock(block); |
| 360 | if (!entry) |
| 361 | { return null; } |
| 362 | const def = BLOCK_DEFINITIONS[entry.typeId]; |
| 363 | if (!def) |
| 364 | { return null; } |
| 365 | const type = catalog.blocks?.[def.key]; |
| 366 | const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season); |
| 367 | if (!state || !type?.blooms?.[state]) |
| 368 | { return null; } |
| 369 | return bloomImageName("block", def.key, state); |
| 370 | } |
| 371 | |
| 372 | function blockCatalogMeta (catalog, block) |
| 373 | { |
| 374 | const entry = normalizeBlock(block); |
| 375 | if (!entry) |
| 376 | { return null; } |
| 377 | const def = BLOCK_DEFINITIONS[entry.typeId]; |
| 378 | if (!def) |
| 379 | { return null; } |
| 380 | return catalog.blocks?.[def.key] ?? null; |
| 381 | } |
| 382 | |
| 383 | function groundCatalogMeta (catalog, ground) |
| 384 | { |
| 385 | const entry = normalizeGround(ground); |
| 386 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 387 | return catalog.grounds?.[def.key] ?? { |
| 388 | frames: def.frames, |
| 389 | animationSpeed: def.animationSpeed |
| 390 | }; |
| 391 | } |