BlueGit

timfa questward

Files

questward / client / src / render / chunkBake.js

chunkBake.js

chunkBake.js 5.1 KiB Download Raw
1import { CHUNK_SIZE, SPRITE_SIZE_PX } from "/shared/protocol/constants.js";
2import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, normalizeGround, normalizeBlock } from "/shared/definitions/tiles.js";
3import { 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 */
9export 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
84export 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
97function makeCanvas (width, height)
98{
99 const canvas = document.createElement("canvas");
100 canvas.width = width;
101 canvas.height = height;
102 return canvas;
103}
104
105function blit (ctx, sprite, dx, dy)
106{
107 if (!sprite?.g_image)
108 { return; }
109 const sub = sprite.getSubImage(0);
110 ctx.drawImage(
111 sprite.g_image,
112 sub.sx,
113 sub.sy,
114 sub.sw,
115 sub.sh,
116 dx,
117 dy,
118 SPRITE_SIZE_PX,
119 SPRITE_SIZE_PX
120 );
121}
122
123function groundSpriteName (catalog, ground)
124{
125 const entry = normalizeGround(ground);
126 const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass];
127 const states = catalog.grounds?.[def.key]?.states ?? [];
128 const state = resolveState(states, entry.state);
129 return spriteImageName("ground", def.key, state);
130}
131
132function blockSpriteName (catalog, block)
133{
134 const entry = normalizeBlock(block);
135 if (!entry)
136 { return null; }
137 const def = BLOCK_DEFINITIONS[entry.typeId];
138 if (!def)
139 { return null; }
140 const states = catalog.blocks?.[def.key]?.states ?? [];
141 const state = resolveState(states, entry.state);
142 return spriteImageName("block", def.key, state);
143}
144
145function groundBloomName (catalog, ground)
146{
147 const entry = normalizeGround(ground);
148 const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass];
149 const type = catalog.grounds?.[def.key];
150 const state = resolveState(type?.states ?? [], entry.state);
151 if (!state || !type?.blooms?.[state])
152 { return null; }
153 return bloomImageName("ground", def.key, state);
154}
155
156function blockBloomName (catalog, block)
157{
158 const entry = normalizeBlock(block);
159 if (!entry)
160 { return null; }
161 const def = BLOCK_DEFINITIONS[entry.typeId];
162 if (!def)
163 { return null; }
164 const type = catalog.blocks?.[def.key];
165 const state = resolveState(type?.states ?? [], entry.state);
166 if (!state || !type?.blooms?.[state])
167 { return null; }
168 return bloomImageName("block", def.key, state);
169}
170
171function blockCatalogMeta (catalog, block)
172{
173 const entry = normalizeBlock(block);
174 if (!entry)
175 { return null; }
176 const def = BLOCK_DEFINITIONS[entry.typeId];
177 if (!def)
178 { return null; }
179 return catalog.blocks?.[def.key] ?? null;
180}