BlueGit

timfa questward

Files

questward / shared / render / chunkAtlasKeys.js

chunkAtlasKeys.js

chunkAtlasKeys.js 7.1 KiB Download Raw
1import { CHUNK_SIZE } from "../protocol/constants.js";
2import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, normalizeGround, normalizeBlock } from "../definitions/tiles.js";
3import { bloomImageName } from "../definitions/tileSprites.js";
4import { resolveSeasonalVisualState } from "../world/season.js";
5import { SOFT_TERRAIN_NEIGHBOR_DIRS } from "./softTerrain.js";
6
7export const EMPTY_BLOCK_ROW_KEY = "blockRow|empty";
8export const EMPTY_BLOOM_KEY = "bloom|empty";
9
10/**
11 * Fingerprints for chunk ground / block-row / bloom atlases.
12 * Identical keys mean identical baked pixels (ignoring chunk coordinates).
13 */
14export function computeChunkAtlasKeys ({
15 snapshot,
16 catalog,
17 preview = null,
18 season,
19 softTerrain = false,
20 resolveGround = null
21})
22{
23 const baseTileX = snapshot.chunkX * CHUNK_SIZE;
24 const baseTileY = snapshot.chunkY * CHUNK_SIZE;
25 const groundFps = [];
26 for (let localY = 0; localY < CHUNK_SIZE; localY++)
27 {
28 for (let localX = 0; localX < CHUNK_SIZE; localX++)
29 {
30 groundFps.push(groundFingerprint(
31 catalog,
32 tileGroundEntry(snapshot, preview, localX, localY),
33 season
34 ));
35 }
36 }
37 let groundBody = compactFingerprint("ground", groundFps);
38 if (softTerrain)
39 {
40 groundBody += `|soft:${softBorderGroundFingerprint({
41 snapshot,
42 catalog,
43 season,
44 preview,
45 resolveGround
46 })}`;
47 }
48 const groundKey = `ground|${season}|${groundBody}`;
49
50 const blockRowKeys = [];
51 for (let row = 0; row < CHUNK_SIZE; row++)
52 {
53 const rowFps = [];
54 for (let localX = 0; localX < CHUNK_SIZE; localX++)
55 {
56 const index = row * CHUNK_SIZE + localX;
57 const tileX = baseTileX + localX;
58 const tileY = baseTileY + row;
59 rowFps.push(blockFingerprint(
60 catalog,
61 tileBlockEntry(snapshot, preview, tileX, tileY, index),
62 season
63 ));
64 }
65 blockRowKeys.push(compactRowFingerprint("blockRow", rowFps));
66 }
67
68 const bloomFps = [];
69 for (let localY = 0; localY < CHUNK_SIZE; localY++)
70 {
71 for (let localX = 0; localX < CHUNK_SIZE; localX++)
72 {
73 const index = localY * CHUNK_SIZE + localX;
74 const tileX = baseTileX + localX;
75 const tileY = baseTileY + localY;
76 bloomFps.push(bloomFingerprint(
77 catalog,
78 tileGroundEntry(snapshot, preview, localX, localY),
79 tileBlockEntry(snapshot, preview, tileX, tileY, index),
80 season
81 ));
82 }
83 }
84 const bloomKey = compactRowFingerprint("bloom", bloomFps);
85
86 return { groundKey, blockRowKeys, bloomKey };
87}
88
89function tileGroundEntry (snapshot, preview, localX, localY)
90{
91 const index = localY * CHUNK_SIZE + localX;
92 const baseTileX = snapshot.chunkX * CHUNK_SIZE;
93 const baseTileY = snapshot.chunkY * CHUNK_SIZE;
94 const tileX = baseTileX + localX;
95 const tileY = baseTileY + localY;
96 let groundEntry = snapshot.grounds[index];
97 if (preview && preview.tileX === tileX && preview.tileY === tileY && preview.ground !== undefined)
98 { groundEntry = preview.ground; }
99 return groundEntry;
100}
101
102function tileBlockEntry (snapshot, preview, tileX, tileY, index)
103{
104 let blockEntry = snapshot.blocks[index];
105 if (preview && preview.tileX === tileX && preview.tileY === tileY && preview.block !== undefined)
106 { blockEntry = preview.block; }
107 return blockEntry;
108}
109
110function compactFingerprint (prefix, fps)
111{
112 if (!fps.length)
113 { return `${prefix}|empty`; }
114 const first = fps[0];
115 if (fps.every((fp) => fp === first))
116 { return `${prefix}|u:${first}`; }
117 return `${prefix}|m:${fps.join(",")}`;
118}
119
120function compactRowFingerprint (prefix, fps)
121{
122 if (!fps.length || fps.every((fp) => fp === "_"))
123 { return prefix === "blockRow" ? EMPTY_BLOCK_ROW_KEY : EMPTY_BLOOM_KEY; }
124 const first = fps[0];
125 if (fps.every((fp) => fp === first))
126 { return `${prefix}|u:${first}`; }
127 return `${prefix}|m:${fps.join(",")}`;
128}
129
130function groundFingerprint (catalog, ground, season)
131{
132 return groundVisualKey(catalog, ground, season);
133}
134
135function blockFingerprint (catalog, block, season)
136{
137 const entry = normalizeBlock(block);
138 if (!entry)
139 { return "_"; }
140 const def = BLOCK_DEFINITIONS[entry.typeId];
141 if (!def || def.editorHidden)
142 { return "_"; }
143 const states = catalog.blocks?.[def.key]?.states ?? [];
144 const state = resolveSeasonalVisualState(states, entry.state, season);
145 return `${entry.typeId}:${state ?? ""}`;
146}
147
148function bloomFingerprint (catalog, groundEntry, blockEntry, season)
149{
150 const parts = [];
151 const groundBloom = groundBloomName(catalog, groundEntry, season);
152 const blockBloom = blockBloomName(catalog, blockEntry, season);
153 if (groundBloom)
154 { parts.push(`g:${groundBloom}`); }
155 if (blockBloom)
156 { parts.push(`b:${blockBloom}`); }
157 return parts.length ? parts.join("+") : "_";
158}
159
160function softBorderGroundFingerprint ({
161 snapshot,
162 catalog,
163 season,
164 preview,
165 resolveGround
166})
167{
168 if (typeof resolveGround !== "function")
169 { return "none"; }
170 const baseTileX = snapshot.chunkX * CHUNK_SIZE;
171 const baseTileY = snapshot.chunkY * CHUNK_SIZE;
172 const parts = [];
173 for (let localY = 0; localY < CHUNK_SIZE; localY++)
174 {
175 for (let localX = 0; localX < CHUNK_SIZE; localX++)
176 {
177 const onBorder = localX === 0 || localY === 0 || localX === CHUNK_SIZE - 1 || localY === CHUNK_SIZE - 1;
178 if (!onBorder)
179 { continue; }
180 const tileX = baseTileX + localX;
181 const tileY = baseTileY + localY;
182 const groundEntry = tileGroundEntry(snapshot, preview, localX, localY);
183 if (!isGroundBlendable(groundEntry))
184 { continue; }
185 const selfKey = groundVisualKey(catalog, groundEntry, season);
186 for (const { ndx, ndy } of SOFT_TERRAIN_NEIGHBOR_DIRS)
187 {
188 const nx = tileX + ndx;
189 const ny = tileY + ndy;
190 if (nx >= baseTileX && nx < baseTileX + CHUNK_SIZE
191 && ny >= baseTileY && ny < baseTileY + CHUNK_SIZE)
192 { continue; }
193 const neighbor = resolveGround(nx, ny);
194 if (neighbor == null)
195 { continue; }
196 const neighborKey = groundVisualKey(catalog, neighbor, season);
197 if (neighborKey === selfKey)
198 { continue; }
199 parts.push(`${tileX},${tileY}:${ndx},${ndy}:${neighborKey}`);
200 }
201 }
202 }
203 return parts.length ? parts.join(";") : "flat";
204}
205
206function isGroundBlendable (ground)
207{
208 const entry = normalizeGround(ground);
209 return !!GROUND_DEFINITIONS[entry.typeId]?.blendable;
210}
211
212function groundVisualKey (catalog, ground, season)
213{
214 const entry = normalizeGround(ground);
215 const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass];
216 const states = catalog.grounds?.[def.key]?.states ?? [];
217 const state = resolveSeasonalVisualState(states, entry.state, season);
218 return `${entry.typeId}:${state ?? ""}`;
219}
220
221function groundBloomName (catalog, ground, season)
222{
223 const entry = normalizeGround(ground);
224 const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass];
225 const type = catalog.grounds?.[def.key];
226 const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season);
227 if (!state || !type?.blooms?.[state])
228 { return null; }
229 return bloomImageName("ground", def.key, state);
230}
231
232function blockBloomName (catalog, block, season)
233{
234 const entry = normalizeBlock(block);
235 if (!entry)
236 { return null; }
237 const def = BLOCK_DEFINITIONS[entry.typeId];
238 if (!def)
239 { return null; }
240 const type = catalog.blocks?.[def.key];
241 const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season);
242 if (!state || !type?.blooms?.[state])
243 { return null; }
244 return bloomImageName("block", def.key, state);
245}