| 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, resolveState, spriteImageName } from "/shared/definitions/tileSprites.js"; |
| 4 | |
| 5 | /** |
| 6 | * Bake immobile ≤16×16 ground/block/bloom tiles into chunk atlases. |
| 7 | * Oversized or animated blocks are returned in `tallBlocks` for individual Y-sorted sprites. |
| 8 | */ |
| 9 | export function bakeChunkAtlases ({ snapshot, catalog, images, preview = null }) |
| 10 | { |
| 11 | const width = CHUNK_SIZE * SPRITE_SIZE_PX; |
| 12 | const height = CHUNK_SIZE * SPRITE_SIZE_PX; |
| 13 | const ground = makeCanvas(width, height); |
| 14 | const bloom = makeCanvas(width, height); |
| 15 | const blockRows = []; |
| 16 | for (let row = 0; row < CHUNK_SIZE; row++) |
| 17 | { blockRows.push(makeCanvas(width, SPRITE_SIZE_PX)); } |
| 18 | |
| 19 | const groundCtx = ground.getContext("2d"); |
| 20 | const bloomCtx = bloom.getContext("2d"); |
| 21 | const blockCtxs = blockRows.map((canvas) => canvas.getContext("2d")); |
| 22 | for (const ctx of [groundCtx, bloomCtx, ...blockCtxs]) |
| 23 | { |
| 24 | ctx.imageSmoothingEnabled = false; |
| 25 | ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
| 26 | } |
| 27 | |
| 28 | const tallBlocks = []; |
| 29 | const baseTileX = snapshot.chunkX * CHUNK_SIZE; |
| 30 | const baseTileY = snapshot.chunkY * CHUNK_SIZE; |
| 31 | |
| 32 | for (let localY = 0; localY < CHUNK_SIZE; localY++) |
| 33 | { |
| 34 | for (let localX = 0; localX < CHUNK_SIZE; localX++) |
| 35 | { |
| 36 | const index = localY * CHUNK_SIZE + localX; |
| 37 | const tileX = baseTileX + localX; |
| 38 | const tileY = baseTileY + localY; |
| 39 | let groundEntry = snapshot.grounds[index]; |
| 40 | let blockEntry = snapshot.blocks[index]; |
| 41 | if (preview && preview.tileX === tileX && preview.tileY === tileY) |
| 42 | { |
| 43 | if (preview.ground !== undefined) |
| 44 | { groundEntry = preview.ground; } |
| 45 | if (preview.block !== undefined) |
| 46 | { blockEntry = preview.block; } |
| 47 | } |
| 48 | |
| 49 | const dx = localX * SPRITE_SIZE_PX; |
| 50 | const dy = localY * SPRITE_SIZE_PX; |
| 51 | const groundName = groundSpriteName(catalog, groundEntry); |
| 52 | blit(groundCtx, images[groundName], dx, dy); |
| 53 | |
| 54 | const groundBloom = groundBloomName(catalog, groundEntry); |
| 55 | if (groundBloom) |
| 56 | { blit(bloomCtx, images[groundBloom], dx, dy); } |
| 57 | |
| 58 | const blockName = blockSpriteName(catalog, blockEntry); |
| 59 | if (!blockName) |
| 60 | { continue; } |
| 61 | const blockMeta = blockCatalogMeta(catalog, blockEntry); |
| 62 | if (isBakeable(images[blockName], blockMeta)) |
| 63 | { |
| 64 | blit(blockCtxs[localY], images[blockName], dx, 0); |
| 65 | const blockBloom = blockBloomName(catalog, blockEntry); |
| 66 | if (blockBloom) |
| 67 | { blit(bloomCtx, images[blockBloom], dx, dy); } |
| 68 | } |
| 69 | else |
| 70 | { |
| 71 | tallBlocks.push({ |
| 72 | tileX, |
| 73 | tileY, |
| 74 | sprite: blockName, |
| 75 | bloom: blockBloomName(catalog, blockEntry) |
| 76 | }); |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return { ground, blockRows, bloom, tallBlocks }; |
| 82 | } |
| 83 | |
| 84 | export function isBakeable (sprite, meta = null) |
| 85 | { |
| 86 | if (!sprite?.g_image) |
| 87 | { return false; } |
| 88 | if (sprite.width > SPRITE_SIZE_PX || sprite.height > SPRITE_SIZE_PX) |
| 89 | { return false; } |
| 90 | if ((meta?.frames ?? 1) > 1) |
| 91 | { return false; } |
| 92 | if ((meta?.animationSpeed ?? 0) > 0) |
| 93 | { return false; } |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | export function frameworkImageFromCanvas (canvas) |
| 98 | { |
| 99 | const fw = new image(canvas, 1, 1, false); |
| 100 | fw.cacheWidth = -1; |
| 101 | fw.cacheHeight = -1; |
| 102 | return fw; |
| 103 | } |
| 104 | |
| 105 | export function invalidateCanvasTexture (scene, canvas) |
| 106 | { |
| 107 | const glContext = scene?.glContext; |
| 108 | if (!glContext?.textures || !canvas) |
| 109 | { return; } |
| 110 | const texture = glContext.textures.get(canvas); |
| 111 | if (!texture) |
| 112 | { return; } |
| 113 | glContext.gl.deleteTexture(texture); |
| 114 | glContext.textures.delete(canvas); |
| 115 | } |
| 116 | |
| 117 | function makeCanvas (width, height) |
| 118 | { |
| 119 | const canvas = document.createElement("canvas"); |
| 120 | canvas.width = width; |
| 121 | canvas.height = height; |
| 122 | return canvas; |
| 123 | } |
| 124 | |
| 125 | function blit (ctx, sprite, dx, dy) |
| 126 | { |
| 127 | if (!sprite?.g_image) |
| 128 | { return; } |
| 129 | const sub = sprite.getSubImage(0); |
| 130 | ctx.drawImage( |
| 131 | sprite.g_image, |
| 132 | sub.sx, |
| 133 | sub.sy, |
| 134 | sub.sw, |
| 135 | sub.sh, |
| 136 | dx, |
| 137 | dy, |
| 138 | SPRITE_SIZE_PX, |
| 139 | SPRITE_SIZE_PX |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | function groundSpriteName (catalog, ground) |
| 144 | { |
| 145 | const entry = normalizeGround(ground); |
| 146 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 147 | const states = catalog.grounds?.[def.key]?.states ?? []; |
| 148 | const state = resolveState(states, entry.state); |
| 149 | return spriteImageName("ground", def.key, state); |
| 150 | } |
| 151 | |
| 152 | function blockSpriteName (catalog, block) |
| 153 | { |
| 154 | const entry = normalizeBlock(block); |
| 155 | if (!entry) |
| 156 | { return null; } |
| 157 | const def = BLOCK_DEFINITIONS[entry.typeId]; |
| 158 | if (!def) |
| 159 | { return null; } |
| 160 | const states = catalog.blocks?.[def.key]?.states ?? []; |
| 161 | const state = resolveState(states, entry.state); |
| 162 | return spriteImageName("block", def.key, state); |
| 163 | } |
| 164 | |
| 165 | function groundBloomName (catalog, ground) |
| 166 | { |
| 167 | const entry = normalizeGround(ground); |
| 168 | const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass]; |
| 169 | const type = catalog.grounds?.[def.key]; |
| 170 | const state = resolveState(type?.states ?? [], entry.state); |
| 171 | if (!state || !type?.blooms?.[state]) |
| 172 | { return null; } |
| 173 | return bloomImageName("ground", def.key, state); |
| 174 | } |
| 175 | |
| 176 | function blockBloomName (catalog, block) |
| 177 | { |
| 178 | const entry = normalizeBlock(block); |
| 179 | if (!entry) |
| 180 | { return null; } |
| 181 | const def = BLOCK_DEFINITIONS[entry.typeId]; |
| 182 | if (!def) |
| 183 | { return null; } |
| 184 | const type = catalog.blocks?.[def.key]; |
| 185 | const state = resolveState(type?.states ?? [], entry.state); |
| 186 | if (!state || !type?.blooms?.[state]) |
| 187 | { return null; } |
| 188 | return bloomImageName("block", def.key, state); |
| 189 | } |
| 190 | |
| 191 | function blockCatalogMeta (catalog, block) |
| 192 | { |
| 193 | const entry = normalizeBlock(block); |
| 194 | if (!entry) |
| 195 | { return null; } |
| 196 | const def = BLOCK_DEFINITIONS[entry.typeId]; |
| 197 | if (!def) |
| 198 | { return null; } |
| 199 | return catalog.blocks?.[def.key] ?? null; |
| 200 | } |