import { CHUNK_SIZE, SPRITE_SIZE_PX } from "/shared/protocol/constants.js"; import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, normalizeGround, normalizeBlock } from "/shared/definitions/tiles.js"; import { bloomImageName, spriteImageName } from "/shared/definitions/tileSprites.js"; import { resolveSeasonalVisualState, SeasonId } from "/shared/world/season.js"; import { SOFT_TERRAIN_NEIGHBOR_DIRS, softTerrainBlendWeight, softTerrainNeighborSample, mixSoftTerrainPixel } from "/shared/render/softTerrain.js"; /** * Bake immobile ≤16×16 ground/block/bloom tiles into chunk atlases. * Oversized or animated blocks/grounds are returned for individual sprites. * @param {{ * snapshot: object, * catalog: object, * images: object, * preview?: object | null, * season?: string, * softTerrain?: boolean, * resolveGround?: (tileX: number, tileY: number) => object | number | null * }} options */ export function bakeChunkAtlases ({ snapshot, catalog, images, preview = null, season = SeasonId.summer, softTerrain = false, resolveGround = null }) { const width = CHUNK_SIZE * SPRITE_SIZE_PX; const height = CHUNK_SIZE * SPRITE_SIZE_PX; const ground = makeCanvas(width, height); const bloom = makeCanvas(width, height); const blockRows = []; for (let row = 0; row < CHUNK_SIZE; row++) { blockRows.push(makeCanvas(width, SPRITE_SIZE_PX)); } const groundCtx = ground.getContext("2d", { willReadFrequently: softTerrain }); const bloomCtx = bloom.getContext("2d"); const blockCtxs = blockRows.map((canvas) => canvas.getContext("2d")); for (const ctx of [groundCtx, bloomCtx, ...blockCtxs]) { ctx.imageSmoothingEnabled = false; ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); } const tallBlocks = []; const animatedGrounds = []; const baseTileX = snapshot.chunkX * CHUNK_SIZE; const baseTileY = snapshot.chunkY * CHUNK_SIZE; const pixelCache = softTerrain ? new Map() : null; for (let localY = 0; localY < CHUNK_SIZE; localY++) { for (let localX = 0; localX < CHUNK_SIZE; localX++) { const index = localY * CHUNK_SIZE + localX; const tileX = baseTileX + localX; const tileY = baseTileY + localY; let groundEntry = snapshot.grounds[index]; let blockEntry = snapshot.blocks[index]; if (preview && preview.tileX === tileX && preview.tileY === tileY) { if (preview.ground !== undefined) { groundEntry = preview.ground; } if (preview.block !== undefined) { blockEntry = preview.block; } } const dx = localX * SPRITE_SIZE_PX; const dy = localY * SPRITE_SIZE_PX; const groundName = groundSpriteName(catalog, groundEntry, season); const groundMeta = groundCatalogMeta(catalog, groundEntry); if (isBakeable(images[groundName], groundMeta)) { const blendable = softTerrain && isGroundBlendable(groundEntry); if (blendable) { blitSoftGround( groundCtx, images, catalog, groundEntry, groundName, dx, dy, tileX, tileY, season, resolveGround, pixelCache ); } else { blit(groundCtx, images[groundName], dx, dy); } const groundBloom = groundBloomName(catalog, groundEntry, season); if (groundBloom) { blit(bloomCtx, images[groundBloom], dx, dy); } } else if (groundName) { animatedGrounds.push({ tileX, tileY, sprite: groundName, typeId: normalizeGround(groundEntry).typeId, bloom: groundBloomName(catalog, groundEntry, season) }); } const blockName = blockSpriteName(catalog, blockEntry, season); if (!blockName) { continue; } const blockMeta = blockCatalogMeta(catalog, blockEntry); if (isBakeable(images[blockName], blockMeta)) { blit(blockCtxs[localY], images[blockName], dx, 0); const blockBloom = blockBloomName(catalog, blockEntry, season); if (blockBloom) { blit(bloomCtx, images[blockBloom], dx, dy); } } else { tallBlocks.push({ tileX, tileY, sprite: blockName, bloom: blockBloomName(catalog, blockEntry, season) }); } } } return { ground, blockRows, bloom, tallBlocks, animatedGrounds }; } export function isBakeable (sprite, meta = null) { if (!sprite?.g_image) { return false; } if (sprite.width > SPRITE_SIZE_PX || sprite.height > SPRITE_SIZE_PX) { return false; } const frames = Number(sprite.subImages) || Number(sprite.subImgTotal) || (meta?.frames ?? 1); const speed = sprite._animationSpeed ?? meta?.animationSpeed ?? 0; if (frames > 1) { return false; } if (speed > 0) { return false; } return true; } function makeCanvas (width, height) { const canvas = document.createElement("canvas"); canvas.width = width; canvas.height = height; return canvas; } function blit (ctx, sprite, dx, dy) { if (!sprite?.g_image) { return; } const sub = sprite.getSubImage(0); ctx.drawImage( sprite.g_image, sub.sx, sub.sy, sub.sw, sub.sh, dx, dy, SPRITE_SIZE_PX, SPRITE_SIZE_PX ); } function isGroundBlendable (ground) { const entry = normalizeGround(ground); return !!GROUND_DEFINITIONS[entry.typeId]?.blendable; } function groundVisualKey (catalog, ground, season) { const entry = normalizeGround(ground); const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; const states = catalog.grounds?.[def.key]?.states ?? []; const state = resolveSeasonalVisualState(states, entry.state, season); return `${entry.typeId}:${state ?? ""}`; } function blitSoftGround ( ctx, images, catalog, groundEntry, groundName, dx, dy, tileX, tileY, season, resolveGround, pixelCache ) { const selfPixels = spritePixels(images[groundName], pixelCache); if (!selfPixels) { blit(ctx, images[groundName], dx, dy); return; } const selfKey = groundVisualKey(catalog, groundEntry, season); const neighborSprites = []; for (const { ndx, ndy } of SOFT_TERRAIN_NEIGHBOR_DIRS) { const neighborGround = resolveNeighborGround( tileX + ndx, tileY + ndy, resolveGround ); if (neighborGround == null) { continue; } if (groundVisualKey(catalog, neighborGround, season) === selfKey) { continue; } const neighborName = groundSpriteName(catalog, neighborGround, season); const pixels = spritePixels(images[neighborName], pixelCache); if (!pixels) { continue; } neighborSprites.push({ ndx, ndy, pixels, neighborBlendable: isGroundBlendable(neighborGround) }); } if (!neighborSprites.length) { blit(ctx, images[groundName], dx, dy); return; } const out = ctx.createImageData(SPRITE_SIZE_PX, SPRITE_SIZE_PX); const dst = out.data; for (let py = 0; py < SPRITE_SIZE_PX; py++) { for (let px = 0; px < SPRITE_SIZE_PX; px++) { const self = readPixel(selfPixels, px, py); const contribs = []; for (const neighbor of neighborSprites) { const weight = softTerrainBlendWeight(px, py, neighbor.ndx, neighbor.ndy, SPRITE_SIZE_PX, { neighborBlendable: neighbor.neighborBlendable }); if (weight <= 0) { continue; } const sample = softTerrainNeighborSample(px, py, neighbor.ndx, neighbor.ndy, SPRITE_SIZE_PX); contribs.push({ weight, color: readPixel(neighbor.pixels, sample.sx, sample.sy) }); } const mixed = mixSoftTerrainPixel(self, contribs); const i = (py * SPRITE_SIZE_PX + px) * 4; dst[i] = mixed.r; dst[i + 1] = mixed.g; dst[i + 2] = mixed.b; dst[i + 3] = mixed.a; } } ctx.putImageData(out, dx, dy); } function resolveNeighborGround (tileX, tileY, resolveGround) { if (typeof resolveGround === "function") { return resolveGround(tileX, tileY); } return null; } function spritePixels (sprite, cache) { if (!sprite?.g_image) { return null; } const key = sprite.g_image?.src ?? sprite; if (cache?.has(key)) { return cache.get(key); } const sub = sprite.getSubImage(0); const canvas = makeCanvas(SPRITE_SIZE_PX, SPRITE_SIZE_PX); const ctx = canvas.getContext("2d", { willReadFrequently: true }); ctx.imageSmoothingEnabled = false; ctx.drawImage( sprite.g_image, sub.sx, sub.sy, sub.sw, sub.sh, 0, 0, SPRITE_SIZE_PX, SPRITE_SIZE_PX ); const data = ctx.getImageData(0, 0, SPRITE_SIZE_PX, SPRITE_SIZE_PX); cache?.set(key, data); return data; } function readPixel (imageData, x, y) { const i = (y * imageData.width + x) * 4; const d = imageData.data; return { r: d[i], g: d[i + 1], b: d[i + 2], a: d[i + 3] }; } function groundSpriteName (catalog, ground, season = SeasonId.summer) { const entry = normalizeGround(ground); const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; const states = catalog.grounds?.[def.key]?.states ?? []; const state = resolveSeasonalVisualState(states, entry.state, season); return spriteImageName("ground", def.key, state); } function blockSpriteName (catalog, block, season = SeasonId.summer) { const entry = normalizeBlock(block); if (!entry) { return null; } const def = BLOCK_DEFINITIONS[entry.typeId]; if (!def || def.editorHidden) { return null; } const states = catalog.blocks?.[def.key]?.states ?? []; const state = resolveSeasonalVisualState(states, entry.state, season); return spriteImageName("block", def.key, state); } function groundBloomName (catalog, ground, season = SeasonId.summer) { const entry = normalizeGround(ground); const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; const type = catalog.grounds?.[def.key]; const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season); if (!state || !type?.blooms?.[state]) { return null; } return bloomImageName("ground", def.key, state); } function blockBloomName (catalog, block, season = SeasonId.summer) { const entry = normalizeBlock(block); if (!entry) { return null; } const def = BLOCK_DEFINITIONS[entry.typeId]; if (!def) { return null; } const type = catalog.blocks?.[def.key]; const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season); if (!state || !type?.blooms?.[state]) { return null; } return bloomImageName("block", def.key, state); } function blockCatalogMeta (catalog, block) { const entry = normalizeBlock(block); if (!entry) { return null; } const def = BLOCK_DEFINITIONS[entry.typeId]; if (!def) { return null; } return catalog.blocks?.[def.key] ?? null; } function groundCatalogMeta (catalog, ground) { const entry = normalizeGround(ground); const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; return catalog.grounds?.[def.key] ?? { frames: def.frames, animationSpeed: def.animationSpeed }; }