BlueGit

timfa questward

Files

questward / shared / render / chunkAtlasKeys.js

chunkAtlasKeys.js

chunkAtlasKeys.js 7.3 KiB Download Raw
1import { CHUNK_SIZE } from "../protocol/constants.js";
2import { GROUND_DEFINITIONS, BLOCK_DEFINITIONS, GroundType, normalizeGround, normalizeBlock, isChunkAtlasBakedBlock } 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 if (!isChunkAtlasBakedBlock(entry))
141 { return "_"; }
142 const def = BLOCK_DEFINITIONS[entry.typeId];
143 if (!def || def.editorHidden)
144 { return "_"; }
145 const states = catalog.blocks?.[def.key]?.states ?? [];
146 const state = resolveSeasonalVisualState(states, entry.state, season);
147 return `${entry.typeId}:${state ?? ""}`;
148}
149
150function bloomFingerprint (catalog, groundEntry, blockEntry, season)
151{
152 const parts = [];
153 const groundBloom = groundBloomName(catalog, groundEntry, season);
154 if (groundBloom)
155 { parts.push(`g:${groundBloom}`); }
156 if (isChunkAtlasBakedBlock(normalizeBlock(blockEntry)))
157 {
158 const blockBloom = blockBloomName(catalog, blockEntry, season);
159 if (blockBloom)
160 { parts.push(`b:${blockBloom}`); }
161 }
162 return parts.length ? parts.join("+") : "_";
163}
164
165function softBorderGroundFingerprint ({
166 snapshot,
167 catalog,
168 season,
169 preview,
170 resolveGround
171})
172{
173 if (typeof resolveGround !== "function")
174 { return "none"; }
175 const baseTileX = snapshot.chunkX * CHUNK_SIZE;
176 const baseTileY = snapshot.chunkY * CHUNK_SIZE;
177 const parts = [];
178 for (let localY = 0; localY < CHUNK_SIZE; localY++)
179 {
180 for (let localX = 0; localX < CHUNK_SIZE; localX++)
181 {
182 const onBorder = localX === 0 || localY === 0 || localX === CHUNK_SIZE - 1 || localY === CHUNK_SIZE - 1;
183 if (!onBorder)
184 { continue; }
185 const tileX = baseTileX + localX;
186 const tileY = baseTileY + localY;
187 const groundEntry = tileGroundEntry(snapshot, preview, localX, localY);
188 if (!isGroundBlendable(groundEntry))
189 { continue; }
190 const selfKey = groundVisualKey(catalog, groundEntry, season);
191 for (const { ndx, ndy } of SOFT_TERRAIN_NEIGHBOR_DIRS)
192 {
193 const nx = tileX + ndx;
194 const ny = tileY + ndy;
195 if (nx >= baseTileX && nx < baseTileX + CHUNK_SIZE
196 && ny >= baseTileY && ny < baseTileY + CHUNK_SIZE)
197 { continue; }
198 const neighbor = resolveGround(nx, ny);
199 if (neighbor == null)
200 { continue; }
201 const neighborKey = groundVisualKey(catalog, neighbor, season);
202 if (neighborKey === selfKey)
203 { continue; }
204 parts.push(`${tileX},${tileY}:${ndx},${ndy}:${neighborKey}`);
205 }
206 }
207 }
208 return parts.length ? parts.join(";") : "flat";
209}
210
211function isGroundBlendable (ground)
212{
213 const entry = normalizeGround(ground);
214 return !!GROUND_DEFINITIONS[entry.typeId]?.blendable;
215}
216
217function groundVisualKey (catalog, ground, season)
218{
219 const entry = normalizeGround(ground);
220 const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass];
221 const states = catalog.grounds?.[def.key]?.states ?? [];
222 const state = resolveSeasonalVisualState(states, entry.state, season);
223 return `${entry.typeId}:${state ?? ""}`;
224}
225
226function groundBloomName (catalog, ground, season)
227{
228 const entry = normalizeGround(ground);
229 const def = GROUND_DEFINITIONS[entry.typeId] ?? GROUND_DEFINITIONS[GroundType.grass];
230 const type = catalog.grounds?.[def.key];
231 const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season);
232 if (!state || !type?.blooms?.[state])
233 { return null; }
234 return bloomImageName("ground", def.key, state);
235}
236
237function blockBloomName (catalog, block, season)
238{
239 const entry = normalizeBlock(block);
240 if (!entry)
241 { return null; }
242 const def = BLOCK_DEFINITIONS[entry.typeId];
243 if (!def)
244 { return null; }
245 const type = catalog.blocks?.[def.key];
246 const state = resolveSeasonalVisualState(type?.states ?? [], entry.state, season);
247 if (!state || !type?.blooms?.[state])
248 { return null; }
249 return bloomImageName("block", def.key, state);
250}