BlueGit

timfa questward

Files

questward / client / src / render / chunkBake.js

chunkBake.js

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