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, resolveState, spriteImageName } from "/shared/definitions/tileSprites.js"; /** * Bake immobile ≤16×16 ground/block/bloom tiles into chunk atlases. * Oversized or animated blocks are returned in `tallBlocks` for individual Y-sorted sprites. */ export function bakeChunkAtlases ({ snapshot, catalog, images, preview = 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"); 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 baseTileX = snapshot.chunkX * CHUNK_SIZE; const baseTileY = snapshot.chunkY * CHUNK_SIZE; 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); blit(groundCtx, images[groundName], dx, dy); const groundBloom = groundBloomName(catalog, groundEntry); if (groundBloom) { blit(bloomCtx, images[groundBloom], dx, dy); } const blockName = blockSpriteName(catalog, blockEntry); 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); if (blockBloom) { blit(bloomCtx, images[blockBloom], dx, dy); } } else { tallBlocks.push({ tileX, tileY, sprite: blockName, bloom: blockBloomName(catalog, blockEntry) }); } } } return { ground, blockRows, bloom, tallBlocks }; } 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; } if ((meta?.frames ?? 1) > 1) { return false; } if ((meta?.animationSpeed ?? 0) > 0) { return false; } return true; } export function frameworkImageFromCanvas (canvas) { const fw = new image(canvas, 1, 1, false); fw.cacheWidth = -1; fw.cacheHeight = -1; return fw; } export function invalidateCanvasTexture (scene, canvas) { const glContext = scene?.glContext; if (!glContext?.textures || !canvas) { return; } const texture = glContext.textures.get(canvas); if (!texture) { return; } glContext.gl.deleteTexture(texture); glContext.textures.delete(canvas); } 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 groundSpriteName (catalog, ground) { const entry = normalizeGround(ground); const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; const states = catalog.grounds?.[def.key]?.states ?? []; const state = resolveState(states, entry.state); return spriteImageName("ground", def.key, state); } function blockSpriteName (catalog, block) { const entry = normalizeBlock(block); if (!entry) { return null; } const def = BLOCK_DEFINITIONS[entry.typeId]; if (!def) { return null; } const states = catalog.blocks?.[def.key]?.states ?? []; const state = resolveState(states, entry.state); return spriteImageName("block", def.key, state); } function groundBloomName (catalog, ground) { const entry = normalizeGround(ground); const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; const type = catalog.grounds?.[def.key]; const state = resolveState(type?.states ?? [], entry.state); if (!state || !type?.blooms?.[state]) { return null; } return bloomImageName("ground", def.key, state); } function blockBloomName (catalog, block) { 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 = resolveState(type?.states ?? [], entry.state); 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; }