BlueGit

timfa questward

Files

questward / client / src / render / chunkBake.js

chunkBake.js

chunkBake.js 5.7 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/grounds are returned for individual 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 animatedGrounds = [];
30 const baseTileX = snapshot.chunkX * CHUNK_SIZE;
31 const baseTileY = snapshot.chunkY * CHUNK_SIZE;
32
33 for (let localY = 0; localY < CHUNK_SIZE; localY++)
34 {
35 for (let localX = 0; localX < CHUNK_SIZE; localX++)
36 {
37 const index = localY * CHUNK_SIZE + localX;
38 const tileX = baseTileX + localX;
39 const tileY = baseTileY + localY;
40 let groundEntry = snapshot.grounds[index];
41 let blockEntry = snapshot.blocks[index];
42 if (preview && preview.tileX === tileX && preview.tileY === tileY)
43 {
44 if (preview.ground !== undefined)
45 { groundEntry = preview.ground; }
46 if (preview.block !== undefined)
47 { blockEntry = preview.block; }
48 }
49
50 const dx = localX * SPRITE_SIZE_PX;
51 const dy = localY * SPRITE_SIZE_PX;
52 const groundName = groundSpriteName(catalog, groundEntry);
53 const groundMeta = groundCatalogMeta(catalog, groundEntry);
54 if (isBakeable(images[groundName], groundMeta))
55 {
56 blit(groundCtx, images[groundName], dx, dy);
57 const groundBloom = groundBloomName(catalog, groundEntry);
58 if (groundBloom)
59 { blit(bloomCtx, images[groundBloom], dx, dy); }
60 }
61 else if (groundName)
62 {
63 animatedGrounds.push({
64 tileX,
65 tileY,
66 sprite: groundName,
67 typeId: normalizeGround(groundEntry).typeId,
68 bloom: groundBloomName(catalog, groundEntry)
69 });
70 }
71
72 const blockName = blockSpriteName(catalog, blockEntry);
73 if (!blockName)
74 { continue; }
75 const blockMeta = blockCatalogMeta(catalog, blockEntry);
76 if (isBakeable(images[blockName], blockMeta))
77 {
78 blit(blockCtxs[localY], images[blockName], dx, 0);
79 const blockBloom = blockBloomName(catalog, blockEntry);
80 if (blockBloom)
81 { blit(bloomCtx, images[blockBloom], dx, dy); }
82 }
83 else
84 {
85 tallBlocks.push({
86 tileX,
87 tileY,
88 sprite: blockName,
89 bloom: blockBloomName(catalog, blockEntry)
90 });
91 }
92 }
93 }
94
95 return { ground, blockRows, bloom, tallBlocks, animatedGrounds };
96}
97
98export function isBakeable (sprite, meta = null)
99{
100 if (!sprite?.g_image)
101 { return false; }
102 if (sprite.width > SPRITE_SIZE_PX || sprite.height > SPRITE_SIZE_PX)
103 { return false; }
104 if ((meta?.frames ?? 1) > 1)
105 { return false; }
106 if ((meta?.animationSpeed ?? 0) > 0)
107 { return false; }
108 return true;
109}
110
111function makeCanvas (width, height)
112{
113 const canvas = document.createElement("canvas");
114 canvas.width = width;
115 canvas.height = height;
116 return canvas;
117}
118
119function blit (ctx, sprite, dx, dy)
120{
121 if (!sprite?.g_image)
122 { return; }
123 const sub = sprite.getSubImage(0);
124 ctx.drawImage(
125 sprite.g_image,
126 sub.sx,
127 sub.sy,
128 sub.sw,
129 sub.sh,
130 dx,
131 dy,
132 SPRITE_SIZE_PX,
133 SPRITE_SIZE_PX
134 );
135}
136
137function groundSpriteName (catalog, ground)
138{
139 const entry = normalizeGround(ground);
140 const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass];
141 const states = catalog.grounds?.[def.key]?.states ?? [];
142 const state = resolveState(states, entry.state);
143 return spriteImageName("ground", def.key, state);
144}
145
146function blockSpriteName (catalog, block)
147{
148 const entry = normalizeBlock(block);
149 if (!entry)
150 { return null; }
151 const def = BLOCK_DEFINITIONS[entry.typeId];
152 if (!def || def.editorHidden)
153 { return null; }
154 const states = catalog.blocks?.[def.key]?.states ?? [];
155 const state = resolveState(states, entry.state);
156 return spriteImageName("block", def.key, state);
157}
158
159function groundBloomName (catalog, ground)
160{
161 const entry = normalizeGround(ground);
162 const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass];
163 const type = catalog.grounds?.[def.key];
164 const state = resolveState(type?.states ?? [], entry.state);
165 if (!state || !type?.blooms?.[state])
166 { return null; }
167 return bloomImageName("ground", def.key, state);
168}
169
170function blockBloomName (catalog, block)
171{
172 const entry = normalizeBlock(block);
173 if (!entry)
174 { return null; }
175 const def = BLOCK_DEFINITIONS[entry.typeId];
176 if (!def)
177 { return null; }
178 const type = catalog.blocks?.[def.key];
179 const state = resolveState(type?.states ?? [], entry.state);
180 if (!state || !type?.blooms?.[state])
181 { return null; }
182 return bloomImageName("block", def.key, state);
183}
184
185function blockCatalogMeta (catalog, block)
186{
187 const entry = normalizeBlock(block);
188 if (!entry)
189 { return null; }
190 const def = BLOCK_DEFINITIONS[entry.typeId];
191 if (!def)
192 { return null; }
193 return catalog.blocks?.[def.key] ?? null;
194}
195
196function groundCatalogMeta (catalog, ground)
197{
198 const entry = normalizeGround(ground);
199 const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass];
200 return catalog.grounds?.[def.key] ?? {
201 frames: def.frames,
202 animationSpeed: def.animationSpeed
203 };
204}