Perf: single-pass fluid atlas bake via per-tile pack textures.
Replace per-tile drawFluidAtlasTile loops with chunk-wide water/lava (and bloom) draws that sample 16×16 RGBA packs for edges, lit/deep corners, and shore UV. Keep the per-tile path as a fallback (__QW_FLUID_ATLAS_SINGLE_PASS=false).
@@ -0,0 +1,513 @@
1
/**
2
* Chunk-wide fluid atlas fragment shaders (single-pass bake).
3
* Derive localTile + puv from atlas v_texCoord; sample 16×16 pack textures.
4
*/
5
6
const FBM_COMMON = [
7
"vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }",
8
"vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }",
9
"vec4 permute(vec4 x) { return mod289(((x * 34.0) + 1.0) * x); }",
10
"vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; }",
11
"float snoise(vec3 v) {",
12
" vec2 C = vec2(1.0 / 6.0, 1.0 / 3.0);",
13
" vec4 D = vec4(0.0, 0.5, 1.0, 2.0);",
14
" vec3 i = floor(v + dot(v, C.yyy));",
15
" vec3 x0 = v - i + dot(i, C.xxx);",
16
" vec3 g = step(x0.yzx, x0.xyz);",
17
" vec3 l = 1.0 - g;",
18
" vec3 i1 = min(g.xyz, l.zxy);",
19
" vec3 i2 = max(g.xyz, l.zxy);",
20
" vec3 x1 = x0 - i1 + C.xxx;",
21
" vec3 x2 = x0 - i2 + C.yyy;",
22
" vec3 x3 = x0 - D.yyy;",
23
" i = mod289(i);",
24
" vec4 p = permute(permute(permute(",
25
" i.z + vec4(0.0, i1.z, i2.z, 1.0))",
26
" + i.y + vec4(0.0, i1.y, i2.y, 1.0))",
27
" + i.x + vec4(0.0, i1.x, i2.x, 1.0));",
28
" float n_ = 0.142857142857;",
29
" vec3 ns = n_ * D.wyz - D.xzx;",
30
" vec4 j = p - 49.0 * floor(p * ns.z * ns.z);",
31
" vec4 x_ = floor(j * ns.z);",
32
" vec4 y_ = floor(j - 7.0 * x_);",
33
" vec4 x = x_ * ns.x + ns.yyyy;",
34
" vec4 y = y_ * ns.x + ns.yyyy;",
35
" vec4 h = 1.0 - abs(x) - abs(y);",
36
" vec4 b0 = vec4(x.xy, y.xy);",
37
" vec4 b1 = vec4(x.zw, y.zw);",
38
" vec4 s0 = floor(b0) * 2.0 + 1.0;",
39
" vec4 s1 = floor(b1) * 2.0 + 1.0;",
40
" vec4 sh = -step(h, vec4(0.0));",
41
" vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;",
42
" vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;",
43
" vec3 p0 = vec3(a0.xy, h.x);",
44
" vec3 p1 = vec3(a0.zw, h.y);",
45
" vec3 p2 = vec3(a1.xy, h.z);",
46
" vec3 p3 = vec3(a1.zw, h.w);",
47
" vec4 norm = taylorInvSqrt(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));",
48
" p0 *= norm.x; p1 *= norm.y; p2 *= norm.z; p3 *= norm.w;",
49
" vec4 m = max(0.6 - vec4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);",
50
" m = m * m;",
51
" return 42.0 * dot(m * m, vec4(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3)));",
52
"}",
53
"float fbm(vec3 p) {",
54
" float sum = 0.0;",
55
" float amp = 0.5;",
56
" sum += amp * snoise(p); p *= 2.0; amp *= 0.5;",
57
" sum += amp * snoise(p); p *= 2.0; amp *= 0.5;",
58
" sum += amp * snoise(p); p *= 2.0; amp *= 0.5;",
59
" sum += amp * snoise(p);",
60
" return sum;",
61
"}",
62
"vec2 wrapUv(vec2 uv) {",
63
" return fract(uv) * 0.998 + 0.001;",
64
"}",
65
"float bit4(float packedByte, float bitIndex) {",
66
" float div = floor(packedByte / pow(2.0, bitIndex));",
67
" return step(0.5, mod(div, 2.0));",
68
"}"
69
].join("\n");
70
71
const ATLAS_TILE_SETUP = [
72
" vec2 atlasPx = floor(v_texCoord * u_atlasPx);",
73
" vec2 localTile = floor(atlasPx / u_tilePx);",
74
" vec2 puv = (mod(atlasPx, u_tilePx) + 0.5) / u_tilePx;",
75
" vec2 packUV = (localTile + 0.5) / u_tilePx;",
76
" vec4 mask = texture2D(u_tileMask, packUV);",
77
" float kind = floor(mask.r * 255.0 + 0.5);",
78
" if (kind < 0.5 || abs(kind - u_kindFilter) > 0.5) { discard; }",
79
" float edgeBits = floor(mask.g * 255.0 + 0.5);",
80
" float diagBits = floor(mask.b * 255.0 + 0.5);",
81
" float shoreCardBits = floor(mask.a * 255.0 + 0.5);",
82
" float u_edgeN = bit4(edgeBits, 0.0);",
83
" float u_edgeE = bit4(edgeBits, 1.0);",
84
" float u_edgeS = bit4(edgeBits, 2.0);",
85
" float u_edgeW = bit4(edgeBits, 3.0);",
86
" float u_edgeNE = bit4(diagBits, 0.0);",
87
" float u_edgeNW = bit4(diagBits, 1.0);",
88
" float u_edgeSE = bit4(diagBits, 2.0);",
89
" float u_edgeSW = bit4(diagBits, 3.0);",
90
" vec4 shorePack = texture2D(u_tileShore, packUV);",
91
" float shoreDiagBits = floor(shorePack.r * 255.0 + 0.5);",
92
" float u_hasShore = step(0.5, shorePack.g);",
93
" float u_shoreEdgeN = bit4(shoreCardBits, 0.0);",
94
" float u_shoreEdgeE = bit4(shoreCardBits, 1.0);",
95
" float u_shoreEdgeS = bit4(shoreCardBits, 2.0);",
96
" float u_shoreEdgeW = bit4(shoreCardBits, 3.0);",
97
" float u_shoreEdgeNE = bit4(shoreDiagBits, 0.0);",
98
" float u_shoreEdgeNW = bit4(shoreDiagBits, 1.0);",
99
" float u_shoreEdgeSE = bit4(shoreDiagBits, 2.0);",
100
" float u_shoreEdgeSW = bit4(shoreDiagBits, 3.0);",
101
" vec4 litPack = texture2D(u_tileLit, packUV);",
102
" float u_litNW = litPack.r;",
103
" float u_litNE = litPack.g;",
104
" float u_litSW = litPack.b;",
105
" float u_litSE = litPack.a;",
106
" vec4 deepPack = texture2D(u_tileDeep, packUV);",
107
" float u_deepNW = deepPack.r;",
108
" float u_deepNE = deepPack.g;",
109
" float u_deepSW = deepPack.b;",
110
" float u_deepSE = deepPack.a;",
111
" vec4 shoreUv = texture2D(u_tileShoreUv, packUV);",
112
" vec2 world = u_chunkOrigin + localTile + puv;"
113
].join("\n");
114
115
const TUNE_UNIFORMS_WATER = [
116
"uniform float u_flowX;",
117
"uniform float u_flowY;",
118
"uniform float u_flowZ;",
119
"uniform float u_waveScale;",
120
"uniform float u_fbmAmp;",
121
"uniform float u_texAmp;",
122
"uniform float u_distort;",
123
"uniform float u_normalZ;",
124
"uniform float u_waveGain;",
125
"uniform float u_crestLo;",
126
"uniform float u_crestHi;",
127
"uniform float u_crestPow;",
128
"uniform float u_glitterPow;",
129
"uniform float u_crestGlitter;",
130
"uniform float u_glitterCap;",
131
"uniform float u_glitterMix;",
132
"uniform float u_shadeLo;",
133
"uniform float u_shadeHi;",
134
"uniform float u_bodyLo;",
135
"uniform float u_bodyHi;",
136
"uniform float u_litLo;",
137
"uniform float u_litHi;",
138
"uniform float u_litMix;",
139
"uniform float u_edgeDark;",
140
"uniform float u_lightDirX;",
141
"uniform float u_lightDirY;",
142
"uniform float u_lightDirZ;",
143
"uniform float u_normalEps;",
144
"uniform float u_foamStrength;",
145
"uniform float u_shoreClip;",
146
"uniform float u_deepDark;",
147
"uniform float u_deepAbrupt;"
148
].join("\n");
149
150
const TUNE_UNIFORMS_LAVA = [
151
"uniform float u_flowX;",
152
"uniform float u_flowY;",
153
"uniform float u_flowZ;",
154
"uniform float u_waveScale;",
155
"uniform float u_fbmAmp;",
156
"uniform float u_texAmp;",
157
"uniform float u_distort;",
158
"uniform float u_normalZ;",
159
"uniform float u_waveGain;",
160
"uniform float u_crestLo;",
161
"uniform float u_crestHi;",
162
"uniform float u_crestPow;",
163
"uniform float u_glitterPow;",
164
"uniform float u_crestGlitter;",
165
"uniform float u_glitterCap;",
166
"uniform float u_glitterMix;",
167
"uniform float u_shadeLo;",
168
"uniform float u_shadeHi;",
169
"uniform float u_bodyLo;",
170
"uniform float u_bodyHi;",
171
"uniform float u_litLo;",
172
"uniform float u_litHi;",
173
"uniform float u_litMix;",
174
"uniform float u_edgeDark;",
175
"uniform float u_lightDirX;",
176
"uniform float u_lightDirY;",
177
"uniform float u_lightDirZ;",
178
"uniform float u_normalEps;",
179
"uniform float u_shoreClip;"
180
].join("\n");
181
182
const PACK_UNIFORMS = [
183
"uniform sampler2D u_tileMask;",
184
"uniform sampler2D u_tileShore;",
185
"uniform sampler2D u_tileLit;",
186
"uniform sampler2D u_tileDeep;",
187
"uniform sampler2D u_tileShoreUv;",
188
"uniform vec2 u_chunkOrigin;",
189
"uniform float u_atlasPx;",
190
"uniform float u_tilePx;",
191
"uniform float u_kindFilter;",
192
"uniform float u_time;",
193
"uniform float u_ambientDark;"
194
].join("\n");
195
196
export const WATER_ATLAS_CHUNK_FRAG = [
197
"precision mediump float;",
198
"varying vec2 v_texCoord;",
199
"uniform sampler2D u_texture;",
200
"uniform sampler2D u_secondary;",
201
"uniform float u_alpha;",
202
PACK_UNIFORMS,
203
TUNE_UNIFORMS_WATER,
204
FBM_COMMON,
205
"float sampleShore(",
206
" float enabled, float useShore, float hasShore, vec2 shoreNu, vec4 u_shoreUvRect,",
207
" inout vec3 shoreRgb, inout float shoreW",
208
") {",
209
" if (enabled < 0.5 || useShore < 0.5 || hasShore < 0.5) { return 0.0; }",
210
" vec2 shoreUv = u_shoreUvRect.xy + shoreNu * (u_shoreUvRect.zw - u_shoreUvRect.xy);",
211
" vec4 shore = texture2D(u_secondary, shoreUv);",
212
" if (shore.a <= 0.1) { return 0.0; }",
213
" shoreRgb = shore.rgb;",
214
" shoreW = 1.0;",
215
" return 1.0;",
216
"}",
217
"void main() {",
218
ATLAS_TILE_SETUP,
219
" float t = u_time;",
220
" vec2 flow = vec2(t * u_flowX, t * u_flowY);",
221
" float eps = max(u_normalEps, 0.001);",
222
" vec3 base = vec3(world * u_waveScale + flow, t * u_flowZ);",
223
" float nC = fbm(base);",
224
" float nL = fbm(base + vec3(-eps, 0.0, 0.0));",
225
" float nR = fbm(base + vec3(eps, 0.0, 0.0));",
226
" float nD = fbm(base + vec3(0.0, -eps, 0.0));",
227
" float nU = fbm(base + vec3(0.0, eps, 0.0));",
228
" vec2 distort = vec2(nL - nR, nD - nU) * u_distort;",
229
" float texN = texture2D(u_texture, wrapUv(world * 0.28 + flow * 0.6 + distort)).r;",
230
" float h = nC * u_fbmAmp + (texN - 0.5) * u_texAmp;",
231
" vec3 normal = normalize(vec3(nL - nR, nD - nU, max(u_normalZ, 0.05)));",
232
" float ndotl = clamp(dot(normal, normalize(vec3(u_lightDirX, u_lightDirY, u_lightDirZ))), 0.0, 1.0);",
233
" float wave = clamp(h * u_waveGain + 0.5, 0.0, 1.0);",
234
" float crest = pow(smoothstep(u_crestLo, max(u_crestHi, u_crestLo + 0.01), wave), max(u_crestPow, 0.01));",
235
" float glitter = pow(max(0.0, ndotl), max(u_glitterPow, 0.01)) * crest;",
236
" glitter = max(glitter, pow(crest, 2.5) * u_crestGlitter);",
237
" float edgeProx = 0.0;",
238
" if (u_edgeN > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.y)); }",
239
" if (u_edgeS > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.y)); }",
240
" if (u_edgeW > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.x)); }",
241
" if (u_edgeE > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.x)); }",
242
" if (u_edgeNE > 0.5) {",
243
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, puv.y))));",
244
" }",
245
" if (u_edgeNW > 0.5) {",
246
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, puv.y))));",
247
" }",
248
" if (u_edgeSE > 0.5) {",
249
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, 1.0 - puv.y))));",
250
" }",
251
" if (u_edgeSW > 0.5) {",
252
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, 1.0 - puv.y))));",
253
" }",
254
" float waveBody = wave * (1.0 - edgeProx * 0.72);",
255
" float deepTop = mix(u_deepNW, u_deepNE, puv.x);",
256
" float deepBot = mix(u_deepSW, u_deepSE, puv.x);",
257
" float deepMix = mix(deepTop, deepBot, puv.y);",
258
" float deepAmt = pow(clamp(deepMix, 0.0, 1.0), max(u_deepAbrupt, 0.05));",
259
" float darkW = clamp(u_deepDark, 0.0, 1.0);",
260
" vec3 deepCol = mix(vec3(0.12, 0.34, 0.54), mix(vec3(0.05, 0.16, 0.34), vec3(0.015, 0.055, 0.14), darkW), deepAmt);",
261
" vec3 midCol = mix(vec3(0.24, 0.52, 0.70), mix(vec3(0.10, 0.30, 0.50), vec3(0.04, 0.14, 0.28), darkW), deepAmt);",
262
" vec3 litCol = mix(vec3(0.52, 0.78, 0.90), mix(vec3(0.26, 0.46, 0.66), vec3(0.12, 0.28, 0.46), darkW), deepAmt);",
263
" vec3 sparkCol = mix(vec3(0.98, 1.0, 1.0), mix(vec3(0.72, 0.88, 0.98), vec3(0.48, 0.68, 0.86), darkW), deepAmt);",
264
" vec3 foamCol = vec3(0.94, 0.99, 1.0);",
265
" float shade = mix(u_shadeLo, u_shadeHi, ndotl);",
266
" vec3 rgb = mix(deepCol, midCol, smoothstep(u_bodyLo, max(u_bodyHi, u_bodyLo + 0.01), waveBody));",
267
" rgb = mix(rgb, litCol, smoothstep(u_litLo, max(u_litHi, u_litLo + 0.01), waveBody) * u_litMix);",
268
" rgb *= shade;",
269
" rgb = mix(rgb, sparkCol, min(glitter, u_glitterCap) * u_glitterMix * (1.0 - edgeProx * 0.7));",
270
" rgb = mix(rgb, deepCol, edgeProx * u_edgeDark);",
271
" float shoreW = 0.0;",
272
" vec3 shoreRgb = rgb;",
273
" if (u_edgeN > 0.5) {",
274
" sampleShore(1.0, u_shoreEdgeN, u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",
275
" }",
276
" if (u_edgeS > 0.5) {",
277
" sampleShore(1.0, u_shoreEdgeS, u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",
278
" }",
279
" if (u_edgeW > 0.5) {",
280
" sampleShore(1.0, u_shoreEdgeW, u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);",
281
" }",
282
" if (u_edgeE > 0.5) {",
283
" sampleShore(1.0, u_shoreEdgeE, u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);",
284
" }",
285
" if (u_edgeNE > 0.5) {",
286
" sampleShore(1.0, max(u_shoreEdgeNE, u_hasShore), u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);",
287
" }",
288
" if (u_edgeNW > 0.5) {",
289
" sampleShore(1.0, max(u_shoreEdgeNW, u_hasShore), u_hasShore, vec2(puv.x, puv.y), shoreUv, shoreRgb, shoreW);",
290
" }",
291
" if (u_edgeSE > 0.5) {",
292
" sampleShore(1.0, max(u_shoreEdgeSE, u_hasShore), u_hasShore, vec2(1.0 - puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",
293
" }",
294
" if (u_edgeSW > 0.5) {",
295
" sampleShore(1.0, max(u_shoreEdgeSW, u_hasShore), u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",
296
" }",
297
" float darkAmt = 1.0 - smoothstep(0.18, 0.52, waveBody);",
298
" float retreat = edgeProx * (0.35 + 0.65 * darkAmt);",
299
" float shoreClip = u_shoreClip;",
300
" float shoreMask = step(shoreClip, retreat) * shoreW;",
301
" float foamAmt = (1.0 - shoreMask) * smoothstep(shoreClip - 0.24, shoreClip - 0.08, retreat)",
302
" * (1.0 - smoothstep(shoreClip - 0.08, shoreClip, retreat));",
303
" rgb = mix(rgb, foamCol, foamAmt * u_foamStrength);",
304
" rgb = mix(rgb, shoreRgb, shoreMask);",
305
" float litTop = mix(u_litNW, u_litNE, puv.x);",
306
" float litBottom = mix(u_litSW, u_litSE, puv.x);",
307
" float lit = mix(litTop, litBottom, puv.y);",
308
" float darkness = u_ambientDark * (1.0 - lit);",
309
" rgb = mix(rgb, vec3(0.0), darkness);",
310
" gl_FragColor = vec4(rgb, u_alpha);",
311
"}"
312
].join("\n");
313
314
export const LAVA_ATLAS_CHUNK_FRAG = [
315
"precision mediump float;",
316
"varying vec2 v_texCoord;",
317
"uniform sampler2D u_texture;",
318
"uniform sampler2D u_secondary;",
319
"uniform float u_alpha;",
320
PACK_UNIFORMS,
321
TUNE_UNIFORMS_LAVA,
322
FBM_COMMON,
323
"float sampleShore(",
324
" float enabled, float useShore, float hasShore, vec2 shoreNu, vec4 u_shoreUvRect,",
325
" inout vec3 shoreRgb, inout float shoreW",
326
") {",
327
" if (enabled < 0.5 || useShore < 0.5 || hasShore < 0.5) { return 0.0; }",
328
" vec2 shoreUv = u_shoreUvRect.xy + shoreNu * (u_shoreUvRect.zw - u_shoreUvRect.xy);",
329
" vec4 shore = texture2D(u_secondary, shoreUv);",
330
" if (shore.a <= 0.1) { return 0.0; }",
331
" shoreRgb = shore.rgb;",
332
" shoreW = 1.0;",
333
" return 1.0;",
334
"}",
335
"void main() {",
336
ATLAS_TILE_SETUP,
337
" float t = u_time;",
338
" vec2 flow = vec2(t * u_flowX, t * u_flowY);",
339
" float eps = max(u_normalEps, 0.001);",
340
" vec3 base = vec3(world * u_waveScale + flow, t * u_flowZ);",
341
" float nC = fbm(base);",
342
" float nL = fbm(base + vec3(-eps, 0.0, 0.0));",
343
" float nR = fbm(base + vec3(eps, 0.0, 0.0));",
344
" float nD = fbm(base + vec3(0.0, -eps, 0.0));",
345
" float nU = fbm(base + vec3(0.0, eps, 0.0));",
346
" vec2 distort = vec2(nL - nR, nD - nU) * u_distort;",
347
" float texN = texture2D(u_texture, wrapUv(world * 0.28 + flow * 0.6 + distort)).r;",
348
" float h = nC * u_fbmAmp + (texN - 0.5) * u_texAmp;",
349
" vec3 normal = normalize(vec3(nL - nR, nD - nU, max(u_normalZ, 0.05)));",
350
" float ndotl = clamp(dot(normal, normalize(vec3(u_lightDirX, u_lightDirY, u_lightDirZ))), 0.0, 1.0);",
351
" float wave = clamp(h * u_waveGain + 0.5, 0.0, 1.0);",
352
" float crest = pow(smoothstep(u_crestLo, max(u_crestHi, u_crestLo + 0.01), wave), max(u_crestPow, 0.01));",
353
" float glitter = pow(max(0.0, ndotl), max(u_glitterPow, 0.01)) * crest;",
354
" glitter = max(glitter, pow(crest, 2.5) * u_crestGlitter);",
355
" float edgeProx = 0.0;",
356
" if (u_edgeN > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.y)); }",
357
" if (u_edgeS > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.y)); }",
358
" if (u_edgeW > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.x)); }",
359
" if (u_edgeE > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.x)); }",
360
" if (u_edgeNE > 0.5) {",
361
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, puv.y))));",
362
" }",
363
" if (u_edgeNW > 0.5) {",
364
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, puv.y))));",
365
" }",
366
" if (u_edgeSE > 0.5) {",
367
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, 1.0 - puv.y))));",
368
" }",
369
" if (u_edgeSW > 0.5) {",
370
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, 1.0 - puv.y))));",
371
" }",
372
" float waveBody = wave * (1.0 - edgeProx * 0.72);",
373
" float heat = smoothstep(u_bodyLo, max(u_bodyHi, u_bodyLo + 0.01), waveBody);",
374
" heat = mix(heat, 1.0, smoothstep(u_litLo, max(u_litHi, u_litLo + 0.01), waveBody) * u_litMix * 0.35);",
375
" heat = clamp(heat + min(glitter, u_glitterCap) * u_glitterMix * 0.45, 0.0, 1.0);",
376
" vec3 darkRed = vec3(0.16, 0.015, 0.01);",
377
" vec3 brightRed = vec3(0.78, 0.08, 0.02);",
378
" vec3 orange = vec3(0.98, 0.38, 0.04);",
379
" vec3 yellow = vec3(1.0, 0.9, 0.28);",
380
" vec3 rgb = mix(darkRed, brightRed, smoothstep(0.0, 0.38, heat));",
381
" rgb = mix(rgb, orange, smoothstep(0.28, 0.68, heat));",
382
" float yellowAmt = smoothstep(0.55, 0.98, heat);",
383
" rgb = mix(rgb, yellow, yellowAmt);",
384
" float shade = mix(u_shadeLo, u_shadeHi, ndotl);",
385
" rgb *= shade;",
386
" rgb = mix(rgb, darkRed, edgeProx * u_edgeDark * 0.35);",
387
" float shoreW = 0.0;",
388
" vec3 shoreRgb = rgb;",
389
" if (u_edgeN > 0.5) {",
390
" sampleShore(1.0, u_shoreEdgeN, u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",
391
" }",
392
" if (u_edgeS > 0.5) {",
393
" sampleShore(1.0, u_shoreEdgeS, u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",
394
" }",
395
" if (u_edgeW > 0.5) {",
396
" sampleShore(1.0, u_shoreEdgeW, u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);",
397
" }",
398
" if (u_edgeE > 0.5) {",
399
" sampleShore(1.0, u_shoreEdgeE, u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);",
400
" }",
401
" if (u_edgeNE > 0.5) {",
402
" sampleShore(1.0, max(u_shoreEdgeNE, u_hasShore), u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);",
403
" }",
404
" if (u_edgeNW > 0.5) {",
405
" sampleShore(1.0, max(u_shoreEdgeNW, u_hasShore), u_hasShore, vec2(puv.x, puv.y), shoreUv, shoreRgb, shoreW);",
406
" }",
407
" if (u_edgeSE > 0.5) {",
408
" sampleShore(1.0, max(u_shoreEdgeSE, u_hasShore), u_hasShore, vec2(1.0 - puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",
409
" }",
410
" if (u_edgeSW > 0.5) {",
411
" sampleShore(1.0, max(u_shoreEdgeSW, u_hasShore), u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",
412
" }",
413
" float darkAmt = 1.0 - smoothstep(0.12, 0.48, waveBody);",
414
" float retreat = edgeProx * mix(0.72, 1.0, darkAmt);",
415
" float shoreClip = u_shoreClip;",
416
" float shoreMask = step(shoreClip, retreat) * shoreW;",
417
" float crustAmt = (1.0 - shoreMask) * smoothstep(shoreClip - 0.2, shoreClip - 0.06, retreat)",
418
" * (1.0 - smoothstep(shoreClip - 0.06, shoreClip, retreat));",
419
" rgb = mix(rgb, vec3(0.05, 0.01, 0.0), crustAmt);",
420
" rgb = mix(rgb, shoreRgb, shoreMask);",
421
" float litTop = mix(u_litNW, u_litNE, puv.x);",
422
" float litBottom = mix(u_litSW, u_litSE, puv.x);",
423
" float lit = mix(litTop, litBottom, puv.y);",
424
" float darkness = u_ambientDark * (1.0 - lit);",
425
" rgb = mix(rgb, vec3(0.0), darkness);",
426
" gl_FragColor = vec4(rgb, u_alpha);",
427
"}"
428
].join("\n");
429
430
export const LAVA_BLOOM_ATLAS_CHUNK_FRAG = [
431
"precision mediump float;",
432
"varying vec2 v_texCoord;",
433
"uniform sampler2D u_texture;",
434
"uniform float u_alpha;",
435
"uniform sampler2D u_tileMask;",
436
"uniform vec2 u_chunkOrigin;",
437
"uniform float u_atlasPx;",
438
"uniform float u_tilePx;",
439
"uniform float u_kindFilter;",
440
"uniform float u_time;",
441
TUNE_UNIFORMS_LAVA,
442
"uniform float u_bloomStrength;",
443
FBM_COMMON,
444
"void main() {",
445
" vec2 atlasPx = floor(v_texCoord * u_atlasPx);",
446
" vec2 localTile = floor(atlasPx / u_tilePx);",
447
" vec2 puv = (mod(atlasPx, u_tilePx) + 0.5) / u_tilePx;",
448
" vec2 packUV = (localTile + 0.5) / u_tilePx;",
449
" vec4 mask = texture2D(u_tileMask, packUV);",
450
" float kind = floor(mask.r * 255.0 + 0.5);",
451
" if (kind < 0.5 || abs(kind - u_kindFilter) > 0.5) { discard; }",
452
" float edgeBits = floor(mask.g * 255.0 + 0.5);",
453
" float diagBits = floor(mask.b * 255.0 + 0.5);",
454
" float u_edgeN = bit4(edgeBits, 0.0);",
455
" float u_edgeE = bit4(edgeBits, 1.0);",
456
" float u_edgeS = bit4(edgeBits, 2.0);",
457
" float u_edgeW = bit4(edgeBits, 3.0);",
458
" float u_edgeNE = bit4(diagBits, 0.0);",
459
" float u_edgeNW = bit4(diagBits, 1.0);",
460
" float u_edgeSE = bit4(diagBits, 2.0);",
461
" float u_edgeSW = bit4(diagBits, 3.0);",
462
" vec2 world = u_chunkOrigin + localTile + puv;",
463
" float t = u_time;",
464
" vec2 flow = vec2(t * u_flowX, t * u_flowY);",
465
" float eps = max(u_normalEps, 0.001);",
466
" vec3 base = vec3(world * u_waveScale + flow, t * u_flowZ);",
467
" float nC = fbm(base);",
468
" float nL = fbm(base + vec3(-eps, 0.0, 0.0));",
469
" float nR = fbm(base + vec3(eps, 0.0, 0.0));",
470
" float nD = fbm(base + vec3(0.0, -eps, 0.0));",
471
" float nU = fbm(base + vec3(0.0, eps, 0.0));",
472
" vec2 distort = vec2(nL - nR, nD - nU) * u_distort;",
473
" float texN = texture2D(u_texture, wrapUv(world * 0.28 + flow * 0.6 + distort)).r;",
474
" float h = nC * u_fbmAmp + (texN - 0.5) * u_texAmp;",
475
" vec3 normal = normalize(vec3(nL - nR, nD - nU, max(u_normalZ, 0.05)));",
476
" float ndotl = clamp(dot(normal, normalize(vec3(u_lightDirX, u_lightDirY, u_lightDirZ))), 0.0, 1.0);",
477
" float wave = clamp(h * u_waveGain + 0.5, 0.0, 1.0);",
478
" float crest = pow(smoothstep(u_crestLo, max(u_crestHi, u_crestLo + 0.01), wave), max(u_crestPow, 0.01));",
479
" float glitter = pow(max(0.0, ndotl), max(u_glitterPow, 0.01)) * crest;",
480
" glitter = max(glitter, pow(crest, 2.5) * u_crestGlitter);",
481
" float edgeProx = 0.0;",
482
" if (u_edgeN > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.y)); }",
483
" if (u_edgeS > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.y)); }",
484
" if (u_edgeW > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.x)); }",
485
" if (u_edgeE > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.x)); }",
486
" if (u_edgeNE > 0.5) {",
487
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, puv.y))));",
488
" }",
489
" if (u_edgeNW > 0.5) {",
490
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, puv.y))));",
491
" }",
492
" if (u_edgeSE > 0.5) {",
493
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, 1.0 - puv.y))));",
494
" }",
495
" if (u_edgeSW > 0.5) {",
496
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, 1.0 - puv.y))));",
497
" }",
498
" float waveBody = wave * (1.0 - edgeProx * 0.72);",
499
" float heat = smoothstep(u_bodyLo, max(u_bodyHi, u_bodyLo + 0.01), waveBody);",
500
" heat = mix(heat, 1.0, smoothstep(u_litLo, max(u_litHi, u_litLo + 0.01), waveBody) * u_litMix * 0.35);",
501
" heat = clamp(heat + min(glitter, u_glitterCap) * u_glitterMix * 0.45, 0.0, 1.0);",
502
" float darkAmt = 1.0 - smoothstep(0.12, 0.48, waveBody);",
503
" float retreat = edgeProx * mix(0.72, 1.0, darkAmt);",
504
" float hasEdge = max(max(u_edgeN, u_edgeE), max(u_edgeS, u_edgeW));",
505
" hasEdge = max(hasEdge, max(max(u_edgeNE, u_edgeNW), max(u_edgeSE, u_edgeSW)));",
506
" float shoreW = step(0.5, hasEdge);",
507
" float shoreMask = step(u_shoreClip, retreat) * shoreW;",
508
" float yellowAmt = smoothstep(0.55, 0.98, heat) * (1.0 - edgeProx * 0.85) * (1.0 - shoreMask);",
509
" float bloom = yellowAmt * max(u_bloomStrength, 0.0);",
510
" vec3 glow = vec3(1.0, 0.82, 0.28) * bloom;",
511
" gl_FragColor = vec4(glow, bloom * u_alpha);",
512
"}"
513
].join("\n");
@@ -0,0 +1,513 @@1
/**2
* Chunk-wide fluid atlas fragment shaders (single-pass bake).3
* Derive localTile + puv from atlas v_texCoord; sample 16×16 pack textures.4
*/5
6
const FBM_COMMON = [7
"vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }",8
"vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }",9
"vec4 permute(vec4 x) { return mod289(((x * 34.0) + 1.0) * x); }",10
"vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; }",11
"float snoise(vec3 v) {",12
" vec2 C = vec2(1.0 / 6.0, 1.0 / 3.0);",13
" vec4 D = vec4(0.0, 0.5, 1.0, 2.0);",14
" vec3 i = floor(v + dot(v, C.yyy));",15
" vec3 x0 = v - i + dot(i, C.xxx);",16
" vec3 g = step(x0.yzx, x0.xyz);",17
" vec3 l = 1.0 - g;",18
" vec3 i1 = min(g.xyz, l.zxy);",19
" vec3 i2 = max(g.xyz, l.zxy);",20
" vec3 x1 = x0 - i1 + C.xxx;",21
" vec3 x2 = x0 - i2 + C.yyy;",22
" vec3 x3 = x0 - D.yyy;",23
" i = mod289(i);",24
" vec4 p = permute(permute(permute(",25
" i.z + vec4(0.0, i1.z, i2.z, 1.0))",26
" + i.y + vec4(0.0, i1.y, i2.y, 1.0))",27
" + i.x + vec4(0.0, i1.x, i2.x, 1.0));",28
" float n_ = 0.142857142857;",29
" vec3 ns = n_ * D.wyz - D.xzx;",30
" vec4 j = p - 49.0 * floor(p * ns.z * ns.z);",31
" vec4 x_ = floor(j * ns.z);",32
" vec4 y_ = floor(j - 7.0 * x_);",33
" vec4 x = x_ * ns.x + ns.yyyy;",34
" vec4 y = y_ * ns.x + ns.yyyy;",35
" vec4 h = 1.0 - abs(x) - abs(y);",36
" vec4 b0 = vec4(x.xy, y.xy);",37
" vec4 b1 = vec4(x.zw, y.zw);",38
" vec4 s0 = floor(b0) * 2.0 + 1.0;",39
" vec4 s1 = floor(b1) * 2.0 + 1.0;",40
" vec4 sh = -step(h, vec4(0.0));",41
" vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;",42
" vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;",43
" vec3 p0 = vec3(a0.xy, h.x);",44
" vec3 p1 = vec3(a0.zw, h.y);",45
" vec3 p2 = vec3(a1.xy, h.z);",46
" vec3 p3 = vec3(a1.zw, h.w);",47
" vec4 norm = taylorInvSqrt(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));",48
" p0 *= norm.x; p1 *= norm.y; p2 *= norm.z; p3 *= norm.w;",49
" vec4 m = max(0.6 - vec4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);",50
" m = m * m;",51
" return 42.0 * dot(m * m, vec4(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3)));",52
"}",53
"float fbm(vec3 p) {",54
" float sum = 0.0;",55
" float amp = 0.5;",56
" sum += amp * snoise(p); p *= 2.0; amp *= 0.5;",57
" sum += amp * snoise(p); p *= 2.0; amp *= 0.5;",58
" sum += amp * snoise(p); p *= 2.0; amp *= 0.5;",59
" sum += amp * snoise(p);",60
" return sum;",61
"}",62
"vec2 wrapUv(vec2 uv) {",63
" return fract(uv) * 0.998 + 0.001;",64
"}",65
"float bit4(float packedByte, float bitIndex) {",66
" float div = floor(packedByte / pow(2.0, bitIndex));",67
" return step(0.5, mod(div, 2.0));",68
"}"69
].join("\n");70
71
const ATLAS_TILE_SETUP = [72
" vec2 atlasPx = floor(v_texCoord * u_atlasPx);",73
" vec2 localTile = floor(atlasPx / u_tilePx);",74
" vec2 puv = (mod(atlasPx, u_tilePx) + 0.5) / u_tilePx;",75
" vec2 packUV = (localTile + 0.5) / u_tilePx;",76
" vec4 mask = texture2D(u_tileMask, packUV);",77
" float kind = floor(mask.r * 255.0 + 0.5);",78
" if (kind < 0.5 || abs(kind - u_kindFilter) > 0.5) { discard; }",79
" float edgeBits = floor(mask.g * 255.0 + 0.5);",80
" float diagBits = floor(mask.b * 255.0 + 0.5);",81
" float shoreCardBits = floor(mask.a * 255.0 + 0.5);",82
" float u_edgeN = bit4(edgeBits, 0.0);",83
" float u_edgeE = bit4(edgeBits, 1.0);",84
" float u_edgeS = bit4(edgeBits, 2.0);",85
" float u_edgeW = bit4(edgeBits, 3.0);",86
" float u_edgeNE = bit4(diagBits, 0.0);",87
" float u_edgeNW = bit4(diagBits, 1.0);",88
" float u_edgeSE = bit4(diagBits, 2.0);",89
" float u_edgeSW = bit4(diagBits, 3.0);",90
" vec4 shorePack = texture2D(u_tileShore, packUV);",91
" float shoreDiagBits = floor(shorePack.r * 255.0 + 0.5);",92
" float u_hasShore = step(0.5, shorePack.g);",93
" float u_shoreEdgeN = bit4(shoreCardBits, 0.0);",94
" float u_shoreEdgeE = bit4(shoreCardBits, 1.0);",95
" float u_shoreEdgeS = bit4(shoreCardBits, 2.0);",96
" float u_shoreEdgeW = bit4(shoreCardBits, 3.0);",97
" float u_shoreEdgeNE = bit4(shoreDiagBits, 0.0);",98
" float u_shoreEdgeNW = bit4(shoreDiagBits, 1.0);",99
" float u_shoreEdgeSE = bit4(shoreDiagBits, 2.0);",100
" float u_shoreEdgeSW = bit4(shoreDiagBits, 3.0);",101
" vec4 litPack = texture2D(u_tileLit, packUV);",102
" float u_litNW = litPack.r;",103
" float u_litNE = litPack.g;",104
" float u_litSW = litPack.b;",105
" float u_litSE = litPack.a;",106
" vec4 deepPack = texture2D(u_tileDeep, packUV);",107
" float u_deepNW = deepPack.r;",108
" float u_deepNE = deepPack.g;",109
" float u_deepSW = deepPack.b;",110
" float u_deepSE = deepPack.a;",111
" vec4 shoreUv = texture2D(u_tileShoreUv, packUV);",112
" vec2 world = u_chunkOrigin + localTile + puv;"113
].join("\n");114
115
const TUNE_UNIFORMS_WATER = [116
"uniform float u_flowX;",117
"uniform float u_flowY;",118
"uniform float u_flowZ;",119
"uniform float u_waveScale;",120
"uniform float u_fbmAmp;",121
"uniform float u_texAmp;",122
"uniform float u_distort;",123
"uniform float u_normalZ;",124
"uniform float u_waveGain;",125
"uniform float u_crestLo;",126
"uniform float u_crestHi;",127
"uniform float u_crestPow;",128
"uniform float u_glitterPow;",129
"uniform float u_crestGlitter;",130
"uniform float u_glitterCap;",131
"uniform float u_glitterMix;",132
"uniform float u_shadeLo;",133
"uniform float u_shadeHi;",134
"uniform float u_bodyLo;",135
"uniform float u_bodyHi;",136
"uniform float u_litLo;",137
"uniform float u_litHi;",138
"uniform float u_litMix;",139
"uniform float u_edgeDark;",140
"uniform float u_lightDirX;",141
"uniform float u_lightDirY;",142
"uniform float u_lightDirZ;",143
"uniform float u_normalEps;",144
"uniform float u_foamStrength;",145
"uniform float u_shoreClip;",146
"uniform float u_deepDark;",147
"uniform float u_deepAbrupt;"148
].join("\n");149
150
const TUNE_UNIFORMS_LAVA = [151
"uniform float u_flowX;",152
"uniform float u_flowY;",153
"uniform float u_flowZ;",154
"uniform float u_waveScale;",155
"uniform float u_fbmAmp;",156
"uniform float u_texAmp;",157
"uniform float u_distort;",158
"uniform float u_normalZ;",159
"uniform float u_waveGain;",160
"uniform float u_crestLo;",161
"uniform float u_crestHi;",162
"uniform float u_crestPow;",163
"uniform float u_glitterPow;",164
"uniform float u_crestGlitter;",165
"uniform float u_glitterCap;",166
"uniform float u_glitterMix;",167
"uniform float u_shadeLo;",168
"uniform float u_shadeHi;",169
"uniform float u_bodyLo;",170
"uniform float u_bodyHi;",171
"uniform float u_litLo;",172
"uniform float u_litHi;",173
"uniform float u_litMix;",174
"uniform float u_edgeDark;",175
"uniform float u_lightDirX;",176
"uniform float u_lightDirY;",177
"uniform float u_lightDirZ;",178
"uniform float u_normalEps;",179
"uniform float u_shoreClip;"180
].join("\n");181
182
const PACK_UNIFORMS = [183
"uniform sampler2D u_tileMask;",184
"uniform sampler2D u_tileShore;",185
"uniform sampler2D u_tileLit;",186
"uniform sampler2D u_tileDeep;",187
"uniform sampler2D u_tileShoreUv;",188
"uniform vec2 u_chunkOrigin;",189
"uniform float u_atlasPx;",190
"uniform float u_tilePx;",191
"uniform float u_kindFilter;",192
"uniform float u_time;",193
"uniform float u_ambientDark;"194
].join("\n");195
196
export const WATER_ATLAS_CHUNK_FRAG = [197
"precision mediump float;",198
"varying vec2 v_texCoord;",199
"uniform sampler2D u_texture;",200
"uniform sampler2D u_secondary;",201
"uniform float u_alpha;",202
PACK_UNIFORMS,203
TUNE_UNIFORMS_WATER,204
FBM_COMMON,205
"float sampleShore(",206
" float enabled, float useShore, float hasShore, vec2 shoreNu, vec4 u_shoreUvRect,",207
" inout vec3 shoreRgb, inout float shoreW",208
") {",209
" if (enabled < 0.5 || useShore < 0.5 || hasShore < 0.5) { return 0.0; }",210
" vec2 shoreUv = u_shoreUvRect.xy + shoreNu * (u_shoreUvRect.zw - u_shoreUvRect.xy);",211
" vec4 shore = texture2D(u_secondary, shoreUv);",212
" if (shore.a <= 0.1) { return 0.0; }",213
" shoreRgb = shore.rgb;",214
" shoreW = 1.0;",215
" return 1.0;",216
"}",217
"void main() {",218
ATLAS_TILE_SETUP,219
" float t = u_time;",220
" vec2 flow = vec2(t * u_flowX, t * u_flowY);",221
" float eps = max(u_normalEps, 0.001);",222
" vec3 base = vec3(world * u_waveScale + flow, t * u_flowZ);",223
" float nC = fbm(base);",224
" float nL = fbm(base + vec3(-eps, 0.0, 0.0));",225
" float nR = fbm(base + vec3(eps, 0.0, 0.0));",226
" float nD = fbm(base + vec3(0.0, -eps, 0.0));",227
" float nU = fbm(base + vec3(0.0, eps, 0.0));",228
" vec2 distort = vec2(nL - nR, nD - nU) * u_distort;",229
" float texN = texture2D(u_texture, wrapUv(world * 0.28 + flow * 0.6 + distort)).r;",230
" float h = nC * u_fbmAmp + (texN - 0.5) * u_texAmp;",231
" vec3 normal = normalize(vec3(nL - nR, nD - nU, max(u_normalZ, 0.05)));",232
" float ndotl = clamp(dot(normal, normalize(vec3(u_lightDirX, u_lightDirY, u_lightDirZ))), 0.0, 1.0);",233
" float wave = clamp(h * u_waveGain + 0.5, 0.0, 1.0);",234
" float crest = pow(smoothstep(u_crestLo, max(u_crestHi, u_crestLo + 0.01), wave), max(u_crestPow, 0.01));",235
" float glitter = pow(max(0.0, ndotl), max(u_glitterPow, 0.01)) * crest;",236
" glitter = max(glitter, pow(crest, 2.5) * u_crestGlitter);",237
" float edgeProx = 0.0;",238
" if (u_edgeN > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.y)); }",239
" if (u_edgeS > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.y)); }",240
" if (u_edgeW > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.x)); }",241
" if (u_edgeE > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.x)); }",242
" if (u_edgeNE > 0.5) {",243
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, puv.y))));",244
" }",245
" if (u_edgeNW > 0.5) {",246
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, puv.y))));",247
" }",248
" if (u_edgeSE > 0.5) {",249
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, 1.0 - puv.y))));",250
" }",251
" if (u_edgeSW > 0.5) {",252
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, 1.0 - puv.y))));",253
" }",254
" float waveBody = wave * (1.0 - edgeProx * 0.72);",255
" float deepTop = mix(u_deepNW, u_deepNE, puv.x);",256
" float deepBot = mix(u_deepSW, u_deepSE, puv.x);",257
" float deepMix = mix(deepTop, deepBot, puv.y);",258
" float deepAmt = pow(clamp(deepMix, 0.0, 1.0), max(u_deepAbrupt, 0.05));",259
" float darkW = clamp(u_deepDark, 0.0, 1.0);",260
" vec3 deepCol = mix(vec3(0.12, 0.34, 0.54), mix(vec3(0.05, 0.16, 0.34), vec3(0.015, 0.055, 0.14), darkW), deepAmt);",261
" vec3 midCol = mix(vec3(0.24, 0.52, 0.70), mix(vec3(0.10, 0.30, 0.50), vec3(0.04, 0.14, 0.28), darkW), deepAmt);",262
" vec3 litCol = mix(vec3(0.52, 0.78, 0.90), mix(vec3(0.26, 0.46, 0.66), vec3(0.12, 0.28, 0.46), darkW), deepAmt);",263
" vec3 sparkCol = mix(vec3(0.98, 1.0, 1.0), mix(vec3(0.72, 0.88, 0.98), vec3(0.48, 0.68, 0.86), darkW), deepAmt);",264
" vec3 foamCol = vec3(0.94, 0.99, 1.0);",265
" float shade = mix(u_shadeLo, u_shadeHi, ndotl);",266
" vec3 rgb = mix(deepCol, midCol, smoothstep(u_bodyLo, max(u_bodyHi, u_bodyLo + 0.01), waveBody));",267
" rgb = mix(rgb, litCol, smoothstep(u_litLo, max(u_litHi, u_litLo + 0.01), waveBody) * u_litMix);",268
" rgb *= shade;",269
" rgb = mix(rgb, sparkCol, min(glitter, u_glitterCap) * u_glitterMix * (1.0 - edgeProx * 0.7));",270
" rgb = mix(rgb, deepCol, edgeProx * u_edgeDark);",271
" float shoreW = 0.0;",272
" vec3 shoreRgb = rgb;",273
" if (u_edgeN > 0.5) {",274
" sampleShore(1.0, u_shoreEdgeN, u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",275
" }",276
" if (u_edgeS > 0.5) {",277
" sampleShore(1.0, u_shoreEdgeS, u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",278
" }",279
" if (u_edgeW > 0.5) {",280
" sampleShore(1.0, u_shoreEdgeW, u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);",281
" }",282
" if (u_edgeE > 0.5) {",283
" sampleShore(1.0, u_shoreEdgeE, u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);",284
" }",285
" if (u_edgeNE > 0.5) {",286
" sampleShore(1.0, max(u_shoreEdgeNE, u_hasShore), u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);",287
" }",288
" if (u_edgeNW > 0.5) {",289
" sampleShore(1.0, max(u_shoreEdgeNW, u_hasShore), u_hasShore, vec2(puv.x, puv.y), shoreUv, shoreRgb, shoreW);",290
" }",291
" if (u_edgeSE > 0.5) {",292
" sampleShore(1.0, max(u_shoreEdgeSE, u_hasShore), u_hasShore, vec2(1.0 - puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",293
" }",294
" if (u_edgeSW > 0.5) {",295
" sampleShore(1.0, max(u_shoreEdgeSW, u_hasShore), u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",296
" }",297
" float darkAmt = 1.0 - smoothstep(0.18, 0.52, waveBody);",298
" float retreat = edgeProx * (0.35 + 0.65 * darkAmt);",299
" float shoreClip = u_shoreClip;",300
" float shoreMask = step(shoreClip, retreat) * shoreW;",301
" float foamAmt = (1.0 - shoreMask) * smoothstep(shoreClip - 0.24, shoreClip - 0.08, retreat)",302
" * (1.0 - smoothstep(shoreClip - 0.08, shoreClip, retreat));",303
" rgb = mix(rgb, foamCol, foamAmt * u_foamStrength);",304
" rgb = mix(rgb, shoreRgb, shoreMask);",305
" float litTop = mix(u_litNW, u_litNE, puv.x);",306
" float litBottom = mix(u_litSW, u_litSE, puv.x);",307
" float lit = mix(litTop, litBottom, puv.y);",308
" float darkness = u_ambientDark * (1.0 - lit);",309
" rgb = mix(rgb, vec3(0.0), darkness);",310
" gl_FragColor = vec4(rgb, u_alpha);",311
"}"312
].join("\n");313
314
export const LAVA_ATLAS_CHUNK_FRAG = [315
"precision mediump float;",316
"varying vec2 v_texCoord;",317
"uniform sampler2D u_texture;",318
"uniform sampler2D u_secondary;",319
"uniform float u_alpha;",320
PACK_UNIFORMS,321
TUNE_UNIFORMS_LAVA,322
FBM_COMMON,323
"float sampleShore(",324
" float enabled, float useShore, float hasShore, vec2 shoreNu, vec4 u_shoreUvRect,",325
" inout vec3 shoreRgb, inout float shoreW",326
") {",327
" if (enabled < 0.5 || useShore < 0.5 || hasShore < 0.5) { return 0.0; }",328
" vec2 shoreUv = u_shoreUvRect.xy + shoreNu * (u_shoreUvRect.zw - u_shoreUvRect.xy);",329
" vec4 shore = texture2D(u_secondary, shoreUv);",330
" if (shore.a <= 0.1) { return 0.0; }",331
" shoreRgb = shore.rgb;",332
" shoreW = 1.0;",333
" return 1.0;",334
"}",335
"void main() {",336
ATLAS_TILE_SETUP,337
" float t = u_time;",338
" vec2 flow = vec2(t * u_flowX, t * u_flowY);",339
" float eps = max(u_normalEps, 0.001);",340
" vec3 base = vec3(world * u_waveScale + flow, t * u_flowZ);",341
" float nC = fbm(base);",342
" float nL = fbm(base + vec3(-eps, 0.0, 0.0));",343
" float nR = fbm(base + vec3(eps, 0.0, 0.0));",344
" float nD = fbm(base + vec3(0.0, -eps, 0.0));",345
" float nU = fbm(base + vec3(0.0, eps, 0.0));",346
" vec2 distort = vec2(nL - nR, nD - nU) * u_distort;",347
" float texN = texture2D(u_texture, wrapUv(world * 0.28 + flow * 0.6 + distort)).r;",348
" float h = nC * u_fbmAmp + (texN - 0.5) * u_texAmp;",349
" vec3 normal = normalize(vec3(nL - nR, nD - nU, max(u_normalZ, 0.05)));",350
" float ndotl = clamp(dot(normal, normalize(vec3(u_lightDirX, u_lightDirY, u_lightDirZ))), 0.0, 1.0);",351
" float wave = clamp(h * u_waveGain + 0.5, 0.0, 1.0);",352
" float crest = pow(smoothstep(u_crestLo, max(u_crestHi, u_crestLo + 0.01), wave), max(u_crestPow, 0.01));",353
" float glitter = pow(max(0.0, ndotl), max(u_glitterPow, 0.01)) * crest;",354
" glitter = max(glitter, pow(crest, 2.5) * u_crestGlitter);",355
" float edgeProx = 0.0;",356
" if (u_edgeN > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.y)); }",357
" if (u_edgeS > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.y)); }",358
" if (u_edgeW > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.x)); }",359
" if (u_edgeE > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.x)); }",360
" if (u_edgeNE > 0.5) {",361
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, puv.y))));",362
" }",363
" if (u_edgeNW > 0.5) {",364
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, puv.y))));",365
" }",366
" if (u_edgeSE > 0.5) {",367
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, 1.0 - puv.y))));",368
" }",369
" if (u_edgeSW > 0.5) {",370
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, 1.0 - puv.y))));",371
" }",372
" float waveBody = wave * (1.0 - edgeProx * 0.72);",373
" float heat = smoothstep(u_bodyLo, max(u_bodyHi, u_bodyLo + 0.01), waveBody);",374
" heat = mix(heat, 1.0, smoothstep(u_litLo, max(u_litHi, u_litLo + 0.01), waveBody) * u_litMix * 0.35);",375
" heat = clamp(heat + min(glitter, u_glitterCap) * u_glitterMix * 0.45, 0.0, 1.0);",376
" vec3 darkRed = vec3(0.16, 0.015, 0.01);",377
" vec3 brightRed = vec3(0.78, 0.08, 0.02);",378
" vec3 orange = vec3(0.98, 0.38, 0.04);",379
" vec3 yellow = vec3(1.0, 0.9, 0.28);",380
" vec3 rgb = mix(darkRed, brightRed, smoothstep(0.0, 0.38, heat));",381
" rgb = mix(rgb, orange, smoothstep(0.28, 0.68, heat));",382
" float yellowAmt = smoothstep(0.55, 0.98, heat);",383
" rgb = mix(rgb, yellow, yellowAmt);",384
" float shade = mix(u_shadeLo, u_shadeHi, ndotl);",385
" rgb *= shade;",386
" rgb = mix(rgb, darkRed, edgeProx * u_edgeDark * 0.35);",387
" float shoreW = 0.0;",388
" vec3 shoreRgb = rgb;",389
" if (u_edgeN > 0.5) {",390
" sampleShore(1.0, u_shoreEdgeN, u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",391
" }",392
" if (u_edgeS > 0.5) {",393
" sampleShore(1.0, u_shoreEdgeS, u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",394
" }",395
" if (u_edgeW > 0.5) {",396
" sampleShore(1.0, u_shoreEdgeW, u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);",397
" }",398
" if (u_edgeE > 0.5) {",399
" sampleShore(1.0, u_shoreEdgeE, u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);",400
" }",401
" if (u_edgeNE > 0.5) {",402
" sampleShore(1.0, max(u_shoreEdgeNE, u_hasShore), u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);",403
" }",404
" if (u_edgeNW > 0.5) {",405
" sampleShore(1.0, max(u_shoreEdgeNW, u_hasShore), u_hasShore, vec2(puv.x, puv.y), shoreUv, shoreRgb, shoreW);",406
" }",407
" if (u_edgeSE > 0.5) {",408
" sampleShore(1.0, max(u_shoreEdgeSE, u_hasShore), u_hasShore, vec2(1.0 - puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",409
" }",410
" if (u_edgeSW > 0.5) {",411
" sampleShore(1.0, max(u_shoreEdgeSW, u_hasShore), u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);",412
" }",413
" float darkAmt = 1.0 - smoothstep(0.12, 0.48, waveBody);",414
" float retreat = edgeProx * mix(0.72, 1.0, darkAmt);",415
" float shoreClip = u_shoreClip;",416
" float shoreMask = step(shoreClip, retreat) * shoreW;",417
" float crustAmt = (1.0 - shoreMask) * smoothstep(shoreClip - 0.2, shoreClip - 0.06, retreat)",418
" * (1.0 - smoothstep(shoreClip - 0.06, shoreClip, retreat));",419
" rgb = mix(rgb, vec3(0.05, 0.01, 0.0), crustAmt);",420
" rgb = mix(rgb, shoreRgb, shoreMask);",421
" float litTop = mix(u_litNW, u_litNE, puv.x);",422
" float litBottom = mix(u_litSW, u_litSE, puv.x);",423
" float lit = mix(litTop, litBottom, puv.y);",424
" float darkness = u_ambientDark * (1.0 - lit);",425
" rgb = mix(rgb, vec3(0.0), darkness);",426
" gl_FragColor = vec4(rgb, u_alpha);",427
"}"428
].join("\n");429
430
export const LAVA_BLOOM_ATLAS_CHUNK_FRAG = [431
"precision mediump float;",432
"varying vec2 v_texCoord;",433
"uniform sampler2D u_texture;",434
"uniform float u_alpha;",435
"uniform sampler2D u_tileMask;",436
"uniform vec2 u_chunkOrigin;",437
"uniform float u_atlasPx;",438
"uniform float u_tilePx;",439
"uniform float u_kindFilter;",440
"uniform float u_time;",441
TUNE_UNIFORMS_LAVA,442
"uniform float u_bloomStrength;",443
FBM_COMMON,444
"void main() {",445
" vec2 atlasPx = floor(v_texCoord * u_atlasPx);",446
" vec2 localTile = floor(atlasPx / u_tilePx);",447
" vec2 puv = (mod(atlasPx, u_tilePx) + 0.5) / u_tilePx;",448
" vec2 packUV = (localTile + 0.5) / u_tilePx;",449
" vec4 mask = texture2D(u_tileMask, packUV);",450
" float kind = floor(mask.r * 255.0 + 0.5);",451
" if (kind < 0.5 || abs(kind - u_kindFilter) > 0.5) { discard; }",452
" float edgeBits = floor(mask.g * 255.0 + 0.5);",453
" float diagBits = floor(mask.b * 255.0 + 0.5);",454
" float u_edgeN = bit4(edgeBits, 0.0);",455
" float u_edgeE = bit4(edgeBits, 1.0);",456
" float u_edgeS = bit4(edgeBits, 2.0);",457
" float u_edgeW = bit4(edgeBits, 3.0);",458
" float u_edgeNE = bit4(diagBits, 0.0);",459
" float u_edgeNW = bit4(diagBits, 1.0);",460
" float u_edgeSE = bit4(diagBits, 2.0);",461
" float u_edgeSW = bit4(diagBits, 3.0);",462
" vec2 world = u_chunkOrigin + localTile + puv;",463
" float t = u_time;",464
" vec2 flow = vec2(t * u_flowX, t * u_flowY);",465
" float eps = max(u_normalEps, 0.001);",466
" vec3 base = vec3(world * u_waveScale + flow, t * u_flowZ);",467
" float nC = fbm(base);",468
" float nL = fbm(base + vec3(-eps, 0.0, 0.0));",469
" float nR = fbm(base + vec3(eps, 0.0, 0.0));",470
" float nD = fbm(base + vec3(0.0, -eps, 0.0));",471
" float nU = fbm(base + vec3(0.0, eps, 0.0));",472
" vec2 distort = vec2(nL - nR, nD - nU) * u_distort;",473
" float texN = texture2D(u_texture, wrapUv(world * 0.28 + flow * 0.6 + distort)).r;",474
" float h = nC * u_fbmAmp + (texN - 0.5) * u_texAmp;",475
" vec3 normal = normalize(vec3(nL - nR, nD - nU, max(u_normalZ, 0.05)));",476
" float ndotl = clamp(dot(normal, normalize(vec3(u_lightDirX, u_lightDirY, u_lightDirZ))), 0.0, 1.0);",477
" float wave = clamp(h * u_waveGain + 0.5, 0.0, 1.0);",478
" float crest = pow(smoothstep(u_crestLo, max(u_crestHi, u_crestLo + 0.01), wave), max(u_crestPow, 0.01));",479
" float glitter = pow(max(0.0, ndotl), max(u_glitterPow, 0.01)) * crest;",480
" glitter = max(glitter, pow(crest, 2.5) * u_crestGlitter);",481
" float edgeProx = 0.0;",482
" if (u_edgeN > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.y)); }",483
" if (u_edgeS > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.y)); }",484
" if (u_edgeW > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.x)); }",485
" if (u_edgeE > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.x)); }",486
" if (u_edgeNE > 0.5) {",487
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, puv.y))));",488
" }",489
" if (u_edgeNW > 0.5) {",490
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, puv.y))));",491
" }",492
" if (u_edgeSE > 0.5) {",493
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, 1.0 - puv.y))));",494
" }",495
" if (u_edgeSW > 0.5) {",496
" edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, 1.0 - puv.y))));",497
" }",498
" float waveBody = wave * (1.0 - edgeProx * 0.72);",499
" float heat = smoothstep(u_bodyLo, max(u_bodyHi, u_bodyLo + 0.01), waveBody);",500
" heat = mix(heat, 1.0, smoothstep(u_litLo, max(u_litHi, u_litLo + 0.01), waveBody) * u_litMix * 0.35);",501
" heat = clamp(heat + min(glitter, u_glitterCap) * u_glitterMix * 0.45, 0.0, 1.0);",502
" float darkAmt = 1.0 - smoothstep(0.12, 0.48, waveBody);",503
" float retreat = edgeProx * mix(0.72, 1.0, darkAmt);",504
" float hasEdge = max(max(u_edgeN, u_edgeE), max(u_edgeS, u_edgeW));",505
" hasEdge = max(hasEdge, max(max(u_edgeNE, u_edgeNW), max(u_edgeSE, u_edgeSW)));",506
" float shoreW = step(0.5, hasEdge);",507
" float shoreMask = step(u_shoreClip, retreat) * shoreW;",508
" float yellowAmt = smoothstep(0.55, 0.98, heat) * (1.0 - edgeProx * 0.85) * (1.0 - shoreMask);",509
" float bloom = yellowAmt * max(u_bloomStrength, 0.0);",510
" vec3 glow = vec3(1.0, 0.82, 0.28) * bloom;",511
" gl_FragColor = vec4(glow, bloom * u_alpha);",512
"}"513
].join("\n");@@ -5,12 +5,20 @@
5
5
* Tile placement is Y-down (localY), content upright (no per-tile mirrorY —
6
6
* that breaks world-space noise continuity / seams). FBO↔sprite V mismatch
7
7
* is corrected only by mirrorY on the atlas GameObject.
8
*
9
* Single-pass bake packs per-tile uniforms into 16×16 RGBA textures and draws
10
* the full atlas with one (or two, water+lava) chunk shader pass.
8
11
*/
9
12
10
13
import { CHUNK_SIZE, SPRITE_SIZE_PX } from "/shared/protocol/constants.js";
11
14
import { frameworkImageFromCanvas } from "./canvasSprite.js";
12
15
13
16
export const FLUID_ATLAS_PX = CHUNK_SIZE * SPRITE_SIZE_PX;
17
export const FLUID_PACK_SIZE = CHUNK_SIZE;
18
19
/** Kind bytes stored in pack mask.R (0 = empty). */
20
export const FLUID_KIND_WATER = 1;
21
export const FLUID_KIND_LAVA = 2;
14
22
15
23
/**
16
24
* @param {import("../../frameworkv3/framework/engine/gl/glContext.js").default | object} glCtx
@@ -111,6 +119,8 @@ export function withFluidAtlasPass (glCtx, target, drawFn)
111
119
gl.viewport(prevViewport[0], prevViewport[1], prevViewport[2], prevViewport[3]);
112
120
if (!prevBlend)
113
121
{ gl.disable(gl.BLEND); }
122
else
123
{ gl.enable(gl.BLEND); }
114
124
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
115
125
}
116
126
}
@@ -171,3 +181,209 @@ export function drawFluidAtlasTile (glCtx, {
171
181
lights: null
172
182
});
173
183
}
184
185
function createPackTexture (gl)
186
{
187
const tex = gl.createTexture();
188
gl.bindTexture(gl.TEXTURE_2D, tex);
189
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
190
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
191
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
192
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
193
gl.texImage2D(
194
gl.TEXTURE_2D, 0, gl.RGBA,
195
FLUID_PACK_SIZE, FLUID_PACK_SIZE, 0,
196
gl.RGBA, gl.UNSIGNED_BYTE, null
197
);
198
return tex;
199
}
200
201
/**
202
* CPU+GPU pack set for one chunk (5 × 16×16 RGBA8).
203
* @returns {{
204
* mask: Uint8Array, shore: Uint8Array, lit: Uint8Array, deep: Uint8Array, shoreUv: Uint8Array,
205
* texMask: WebGLTexture, texShore: WebGLTexture, texLit: WebGLTexture, texDeep: WebGLTexture, texShoreUv: WebGLTexture
206
* } | null}
207
*/
208
export function createFluidPackSet (glCtx)
209
{
210
const gl = glCtx?.gl;
211
if (!gl)
212
{ return null; }
213
const n = FLUID_PACK_SIZE * FLUID_PACK_SIZE * 4;
214
return {
215
mask: new Uint8Array(n),
216
shore: new Uint8Array(n),
217
lit: new Uint8Array(n),
218
deep: new Uint8Array(n),
219
shoreUv: new Uint8Array(n),
220
texMask: createPackTexture(gl),
221
texShore: createPackTexture(gl),
222
texLit: createPackTexture(gl),
223
texDeep: createPackTexture(gl),
224
texShoreUv: createPackTexture(gl)
225
};
226
}
227
228
export function destroyFluidPackSet (glCtx, packs)
229
{
230
if (!packs)
231
{ return; }
232
const gl = glCtx?.gl;
233
if (gl)
234
{
235
for (const key of ["texMask", "texShore", "texLit", "texDeep", "texShoreUv"])
236
{
237
if (packs[key])
238
{ gl.deleteTexture(packs[key]); }
239
packs[key] = null;
240
}
241
}
242
}
243
244
export function clearFluidPackPixels (packs)
245
{
246
if (!packs)
247
{ return; }
248
packs.mask.fill(0);
249
packs.shore.fill(0);
250
packs.lit.fill(0);
251
packs.deep.fill(0);
252
packs.shoreUv.fill(0);
253
}
254
255
function packBits4 (a, b, c, d)
256
{
257
return ((a > 0.5 ? 1 : 0)
258
| ((b > 0.5 ? 1 : 0) << 1)
259
| ((c > 0.5 ? 1 : 0) << 2)
260
| ((d > 0.5 ? 1 : 0) << 3)) & 0xff;
261
}
262
263
function u8 (v)
264
{
265
return Math.max(0, Math.min(255, (v * 255 + 0.5) | 0));
266
}
267
268
/**
269
* Write one tile's uniforms into pack CPU buffers.
270
* @param {object} packs
271
* @param {number} localX
272
* @param {number} localY
273
* @param {number} kind FLUID_KIND_WATER | FLUID_KIND_LAVA
274
* @param {object} uniforms tile shaderUniforms
275
*/
276
export function writeFluidPackTile (packs, localX, localY, kind, uniforms)
277
{
278
if (!packs || !uniforms)
279
{ return; }
280
const lx = localX | 0;
281
const ly = localY | 0;
282
if (lx < 0 || ly < 0 || lx >= FLUID_PACK_SIZE || ly >= FLUID_PACK_SIZE)
283
{ return; }
284
const i = (ly * FLUID_PACK_SIZE + lx) * 4;
285
const u = uniforms;
286
287
packs.mask[i] = kind | 0;
288
packs.mask[i + 1] = packBits4(u.u_edgeN, u.u_edgeE, u.u_edgeS, u.u_edgeW);
289
packs.mask[i + 2] = packBits4(u.u_edgeNE, u.u_edgeNW, u.u_edgeSE, u.u_edgeSW);
290
packs.mask[i + 3] = packBits4(u.u_shoreEdgeN, u.u_shoreEdgeE, u.u_shoreEdgeS, u.u_shoreEdgeW);
291
292
packs.shore[i] = packBits4(u.u_shoreEdgeNE, u.u_shoreEdgeNW, u.u_shoreEdgeSE, u.u_shoreEdgeSW);
293
packs.shore[i + 1] = (u.u_hasShore > 0.5) ? 255 : 0;
294
packs.shore[i + 2] = 0;
295
packs.shore[i + 3] = 0;
296
297
packs.lit[i] = u8(u.u_litNW ?? 1);
298
packs.lit[i + 1] = u8(u.u_litNE ?? 1);
299
packs.lit[i + 2] = u8(u.u_litSW ?? 1);
300
packs.lit[i + 3] = u8(u.u_litSE ?? 1);
301
302
packs.deep[i] = u8(u.u_deepNW ?? 0);
303
packs.deep[i + 1] = u8(u.u_deepNE ?? 0);
304
packs.deep[i + 2] = u8(u.u_deepSW ?? 0);
305
packs.deep[i + 3] = u8(u.u_deepSE ?? 0);
306
307
const shoreUv = u.u_shoreUvRect;
308
const u0 = Array.isArray(shoreUv) ? (shoreUv[0] ?? 0) : 0;
309
const v0 = Array.isArray(shoreUv) ? (shoreUv[1] ?? 0) : 0;
310
const u1 = Array.isArray(shoreUv) ? (shoreUv[2] ?? 1) : 1;
311
const v1 = Array.isArray(shoreUv) ? (shoreUv[3] ?? 1) : 1;
312
packs.shoreUv[i] = u8(u0);
313
packs.shoreUv[i + 1] = u8(v0);
314
packs.shoreUv[i + 2] = u8(u1);
315
packs.shoreUv[i + 3] = u8(v1);
316
}
317
318
function uploadPack (gl, tex, pixels)
319
{
320
gl.bindTexture(gl.TEXTURE_2D, tex);
321
gl.texSubImage2D(
322
gl.TEXTURE_2D, 0, 0, 0,
323
FLUID_PACK_SIZE, FLUID_PACK_SIZE,
324
gl.RGBA, gl.UNSIGNED_BYTE, pixels
325
);
326
}
327
328
export function uploadFluidPacks (glCtx, packs)
329
{
330
const gl = glCtx?.gl;
331
if (!gl || !packs)
332
{ return; }
333
uploadPack(gl, packs.texMask, packs.mask);
334
uploadPack(gl, packs.texShore, packs.shore);
335
uploadPack(gl, packs.texLit, packs.lit);
336
uploadPack(gl, packs.texDeep, packs.deep);
337
uploadPack(gl, packs.texShoreUv, packs.shoreUv);
338
}
339
340
/**
341
* One full-atlas draw for a fluid kind (water or lava ground, or lava bloom).
342
* Must run inside withFluidAtlasPass (FBO already bound/cleared as needed).
343
*/
344
export function drawFluidAtlasChunkPass (glCtx, {
345
textureHtml,
346
secondaryHtml = null,
347
shader,
348
uniforms,
349
packs,
350
kindFilter,
351
includeShorePacks = true
352
})
353
{
354
if (!textureHtml || !shader || !packs)
355
{ return; }
356
const textures = {
357
u_tileMask: packs.texMask
358
};
359
if (includeShorePacks)
360
{
361
textures.u_tileShore = packs.texShore;
362
textures.u_tileLit = packs.texLit;
363
textures.u_tileDeep = packs.texDeep;
364
textures.u_tileShoreUv = packs.texShoreUv;
365
textures.u_secondary = glCtx.getTexture(secondaryHtml || textureHtml);
366
}
367
const half = FLUID_ATLAS_PX / 2;
368
glCtx.drawSprite({
369
texture: glCtx.getTexture(textureHtml),
370
x: half,
371
y: half,
372
w: FLUID_ATLAS_PX,
373
h: FLUID_ATLAS_PX,
374
rotationDeg: 0,
375
mirrorX: false,
376
mirrorY: false,
377
alpha: 1,
378
uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 },
379
shader,
380
uniforms: {
381
...uniforms,
382
u_kindFilter: kindFilter,
383
u_atlasPx: FLUID_ATLAS_PX,
384
u_tilePx: SPRITE_SIZE_PX
385
},
386
textures,
387
lights: null
388
});
389
}
@@ -5,12 +5,20 @@5
* Tile placement is Y-down (localY), content upright (no per-tile mirrorY —5
* Tile placement is Y-down (localY), content upright (no per-tile mirrorY —6
* that breaks world-space noise continuity / seams). FBO↔sprite V mismatch6
* that breaks world-space noise continuity / seams). FBO↔sprite V mismatch7
* is corrected only by mirrorY on the atlas GameObject.7
* is corrected only by mirrorY on the atlas GameObject.8
*9
* Single-pass bake packs per-tile uniforms into 16×16 RGBA textures and draws10
* the full atlas with one (or two, water+lava) chunk shader pass.8
*/11
*/9
12
10
import { CHUNK_SIZE, SPRITE_SIZE_PX } from "/shared/protocol/constants.js";13
import { CHUNK_SIZE, SPRITE_SIZE_PX } from "/shared/protocol/constants.js";11
import { frameworkImageFromCanvas } from "./canvasSprite.js";14
import { frameworkImageFromCanvas } from "./canvasSprite.js";12
15
13
export const FLUID_ATLAS_PX = CHUNK_SIZE * SPRITE_SIZE_PX;16
export const FLUID_ATLAS_PX = CHUNK_SIZE * SPRITE_SIZE_PX;17
export const FLUID_PACK_SIZE = CHUNK_SIZE;18
19
/** Kind bytes stored in pack mask.R (0 = empty). */20
export const FLUID_KIND_WATER = 1;21
export const FLUID_KIND_LAVA = 2;14
22
15
/**23
/**16
* @param {import("../../frameworkv3/framework/engine/gl/glContext.js").default | object} glCtx24
* @param {import("../../frameworkv3/framework/engine/gl/glContext.js").default | object} glCtx@@ -111,6 +119,8 @@ export function withFluidAtlasPass (glCtx, target, drawFn)111
gl.viewport(prevViewport[0], prevViewport[1], prevViewport[2], prevViewport[3]);119
gl.viewport(prevViewport[0], prevViewport[1], prevViewport[2], prevViewport[3]);112
if (!prevBlend)120
if (!prevBlend)113
{ gl.disable(gl.BLEND); }121
{ gl.disable(gl.BLEND); }122
else123
{ gl.enable(gl.BLEND); }114
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);124
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);115
}125
}116
}126
}@@ -171,3 +181,209 @@ export function drawFluidAtlasTile (glCtx, {171
lights: null181
lights: null172
});182
});173
}183
}184
185
function createPackTexture (gl)186
{187
const tex = gl.createTexture();188
gl.bindTexture(gl.TEXTURE_2D, tex);189
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);190
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);191
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);192
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);193
gl.texImage2D(194
gl.TEXTURE_2D, 0, gl.RGBA,195
FLUID_PACK_SIZE, FLUID_PACK_SIZE, 0,196
gl.RGBA, gl.UNSIGNED_BYTE, null197
);198
return tex;199
}200
201
/**202
* CPU+GPU pack set for one chunk (5 × 16×16 RGBA8).203
* @returns {{204
* mask: Uint8Array, shore: Uint8Array, lit: Uint8Array, deep: Uint8Array, shoreUv: Uint8Array,205
* texMask: WebGLTexture, texShore: WebGLTexture, texLit: WebGLTexture, texDeep: WebGLTexture, texShoreUv: WebGLTexture206
* } | null}207
*/208
export function createFluidPackSet (glCtx)209
{210
const gl = glCtx?.gl;211
if (!gl)212
{ return null; }213
const n = FLUID_PACK_SIZE * FLUID_PACK_SIZE * 4;214
return {215
mask: new Uint8Array(n),216
shore: new Uint8Array(n),217
lit: new Uint8Array(n),218
deep: new Uint8Array(n),219
shoreUv: new Uint8Array(n),220
texMask: createPackTexture(gl),221
texShore: createPackTexture(gl),222
texLit: createPackTexture(gl),223
texDeep: createPackTexture(gl),224
texShoreUv: createPackTexture(gl)225
};226
}227
228
export function destroyFluidPackSet (glCtx, packs)229
{230
if (!packs)231
{ return; }232
const gl = glCtx?.gl;233
if (gl)234
{235
for (const key of ["texMask", "texShore", "texLit", "texDeep", "texShoreUv"])236
{237
if (packs[key])238
{ gl.deleteTexture(packs[key]); }239
packs[key] = null;240
}241
}242
}243
244
export function clearFluidPackPixels (packs)245
{246
if (!packs)247
{ return; }248
packs.mask.fill(0);249
packs.shore.fill(0);250
packs.lit.fill(0);251
packs.deep.fill(0);252
packs.shoreUv.fill(0);253
}254
255
function packBits4 (a, b, c, d)256
{257
return ((a > 0.5 ? 1 : 0)258
| ((b > 0.5 ? 1 : 0) << 1)259
| ((c > 0.5 ? 1 : 0) << 2)260
| ((d > 0.5 ? 1 : 0) << 3)) & 0xff;261
}262
263
function u8 (v)264
{265
return Math.max(0, Math.min(255, (v * 255 + 0.5) | 0));266
}267
268
/**269
* Write one tile's uniforms into pack CPU buffers.270
* @param {object} packs271
* @param {number} localX272
* @param {number} localY273
* @param {number} kind FLUID_KIND_WATER | FLUID_KIND_LAVA274
* @param {object} uniforms tile shaderUniforms275
*/276
export function writeFluidPackTile (packs, localX, localY, kind, uniforms)277
{278
if (!packs || !uniforms)279
{ return; }280
const lx = localX | 0;281
const ly = localY | 0;282
if (lx < 0 || ly < 0 || lx >= FLUID_PACK_SIZE || ly >= FLUID_PACK_SIZE)283
{ return; }284
const i = (ly * FLUID_PACK_SIZE + lx) * 4;285
const u = uniforms;286
287
packs.mask[i] = kind | 0;288
packs.mask[i + 1] = packBits4(u.u_edgeN, u.u_edgeE, u.u_edgeS, u.u_edgeW);289
packs.mask[i + 2] = packBits4(u.u_edgeNE, u.u_edgeNW, u.u_edgeSE, u.u_edgeSW);290
packs.mask[i + 3] = packBits4(u.u_shoreEdgeN, u.u_shoreEdgeE, u.u_shoreEdgeS, u.u_shoreEdgeW);291
292
packs.shore[i] = packBits4(u.u_shoreEdgeNE, u.u_shoreEdgeNW, u.u_shoreEdgeSE, u.u_shoreEdgeSW);293
packs.shore[i + 1] = (u.u_hasShore > 0.5) ? 255 : 0;294
packs.shore[i + 2] = 0;295
packs.shore[i + 3] = 0;296
297
packs.lit[i] = u8(u.u_litNW ?? 1);298
packs.lit[i + 1] = u8(u.u_litNE ?? 1);299
packs.lit[i + 2] = u8(u.u_litSW ?? 1);300
packs.lit[i + 3] = u8(u.u_litSE ?? 1);301
302
packs.deep[i] = u8(u.u_deepNW ?? 0);303
packs.deep[i + 1] = u8(u.u_deepNE ?? 0);304
packs.deep[i + 2] = u8(u.u_deepSW ?? 0);305
packs.deep[i + 3] = u8(u.u_deepSE ?? 0);306
307
const shoreUv = u.u_shoreUvRect;308
const u0 = Array.isArray(shoreUv) ? (shoreUv[0] ?? 0) : 0;309
const v0 = Array.isArray(shoreUv) ? (shoreUv[1] ?? 0) : 0;310
const u1 = Array.isArray(shoreUv) ? (shoreUv[2] ?? 1) : 1;311
const v1 = Array.isArray(shoreUv) ? (shoreUv[3] ?? 1) : 1;312
packs.shoreUv[i] = u8(u0);313
packs.shoreUv[i + 1] = u8(v0);314
packs.shoreUv[i + 2] = u8(u1);315
packs.shoreUv[i + 3] = u8(v1);316
}317
318
function uploadPack (gl, tex, pixels)319
{320
gl.bindTexture(gl.TEXTURE_2D, tex);321
gl.texSubImage2D(322
gl.TEXTURE_2D, 0, 0, 0,323
FLUID_PACK_SIZE, FLUID_PACK_SIZE,324
gl.RGBA, gl.UNSIGNED_BYTE, pixels325
);326
}327
328
export function uploadFluidPacks (glCtx, packs)329
{330
const gl = glCtx?.gl;331
if (!gl || !packs)332
{ return; }333
uploadPack(gl, packs.texMask, packs.mask);334
uploadPack(gl, packs.texShore, packs.shore);335
uploadPack(gl, packs.texLit, packs.lit);336
uploadPack(gl, packs.texDeep, packs.deep);337
uploadPack(gl, packs.texShoreUv, packs.shoreUv);338
}339
340
/**341
* One full-atlas draw for a fluid kind (water or lava ground, or lava bloom).342
* Must run inside withFluidAtlasPass (FBO already bound/cleared as needed).343
*/344
export function drawFluidAtlasChunkPass (glCtx, {345
textureHtml,346
secondaryHtml = null,347
shader,348
uniforms,349
packs,350
kindFilter,351
includeShorePacks = true352
})353
{354
if (!textureHtml || !shader || !packs)355
{ return; }356
const textures = {357
u_tileMask: packs.texMask358
};359
if (includeShorePacks)360
{361
textures.u_tileShore = packs.texShore;362
textures.u_tileLit = packs.texLit;363
textures.u_tileDeep = packs.texDeep;364
textures.u_tileShoreUv = packs.texShoreUv;365
textures.u_secondary = glCtx.getTexture(secondaryHtml || textureHtml);366
}367
const half = FLUID_ATLAS_PX / 2;368
glCtx.drawSprite({369
texture: glCtx.getTexture(textureHtml),370
x: half,371
y: half,372
w: FLUID_ATLAS_PX,373
h: FLUID_ATLAS_PX,374
rotationDeg: 0,375
mirrorX: false,376
mirrorY: false,377
alpha: 1,378
uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 },379
shader,380
uniforms: {381
...uniforms,382
u_kindFilter: kindFilter,383
u_atlasPx: FLUID_ATLAS_PX,384
u_tilePx: SPRITE_SIZE_PX385
},386
textures,387
lights: null388
});389
}@@ -36,8 +36,22 @@ import
36
36
createFluidAtlasTarget,
37
37
destroyFluidAtlasTarget,
38
38
withFluidAtlasPass,
39
drawFluidAtlasTile
39
drawFluidAtlasTile,
40
createFluidPackSet,
41
destroyFluidPackSet,
42
clearFluidPackPixels,
43
writeFluidPackTile,
44
uploadFluidPacks,
45
drawFluidAtlasChunkPass,
46
FLUID_KIND_WATER,
47
FLUID_KIND_LAVA
40
48
} from "./fluidChunkAtlas.js";
49
import
50
{
51
WATER_ATLAS_CHUNK_FRAG,
52
LAVA_ATLAS_CHUNK_FRAG,
53
LAVA_BLOOM_ATLAS_CHUNK_FRAG
54
} from "./fluidAtlasChunkShaders.js";
41
55
import { waterDebugUniforms, lavaDebugUniforms } from "../play/waterDebug.js";
42
56
import { natureNoise01, natureTintUniforms, NATURE_TINT, NATURE_NOISE_SIZE, NATURE_NOISE_PAD } from "/shared/render/natureNoise.js";
43
57
import { SeasonId, seasonIdAt, resolveSeasonalVisualState } from "/shared/world/season.js";
@@ -970,6 +984,9 @@ let cliffGroundTopShader = null;
970
984
let waterGroundShader = null;
971
985
let lavaGroundShader = null;
972
986
let lavaBloomShader = null;
987
let waterAtlasChunkShader = null;
988
let lavaAtlasChunkShader = null;
989
let lavaBloomAtlasChunkShader = null;
973
990
let tintLitSpriteShader = null;
974
991
let dualTintLitSpriteShader = null;
975
992
let tightFitTintLitSpriteShader = null;
@@ -1017,6 +1034,27 @@ function getLavaBloomShader ()
1017
1034
return lavaBloomShader;
1018
1035
}
1019
1036
1037
function getWaterAtlasChunkShader ()
1038
{
1039
if (!waterAtlasChunkShader)
1040
{ waterAtlasChunkShader = Shader.fragment(WATER_ATLAS_CHUNK_FRAG); }
1041
return waterAtlasChunkShader;
1042
}
1043
1044
function getLavaAtlasChunkShader ()
1045
{
1046
if (!lavaAtlasChunkShader)
1047
{ lavaAtlasChunkShader = Shader.fragment(LAVA_ATLAS_CHUNK_FRAG); }
1048
return lavaAtlasChunkShader;
1049
}
1050
1051
function getLavaBloomAtlasChunkShader ()
1052
{
1053
if (!lavaBloomAtlasChunkShader)
1054
{ lavaBloomAtlasChunkShader = Shader.fragment(LAVA_BLOOM_ATLAS_CHUNK_FRAG); }
1055
return lavaBloomAtlasChunkShader;
1056
}
1057
1020
1058
function getTintLitSpriteShader ()
1021
1059
{
1022
1060
if (!tintLitSpriteShader)
@@ -1067,6 +1105,8 @@ export class WorldView
1067
1105
#placeSizeScratch = new vector(0, 0);
1068
1106
/** When true, water/lava shade into per-chunk 16×16 atlases (set false if FBO init fails). */
1069
1107
#useFluidAtlas = true;
1108
/** Chunk-wide single-pass fluid atlas bake (pack textures). Set false or window.__QW_FLUID_ATLAS_SINGLE_PASS=false for per-tile fallback. */
1109
#useFluidAtlasSinglePass = true;
1070
1110
/** Bumped on chunk bake / season change — invalidates cached fluid coast topology. */
1071
1111
#fluidCoastEpoch = 0;
1072
1112
@@ -4540,8 +4580,34 @@ export class WorldView
4540
4580
baked.fluidBloomObject = null;
4541
4581
destroyFluidAtlasTarget(glCtx, baked._fluidGroundTarget);
4542
4582
destroyFluidAtlasTarget(glCtx, baked._fluidBloomTarget);
4583
destroyFluidPackSet(glCtx, baked._fluidPacks);
4543
4584
baked._fluidGroundTarget = null;
4544
4585
baked._fluidBloomTarget = null;
4586
baked._fluidPacks = null;
4587
}
4588
4589
#ensureFluidPacks (baked)
4590
{
4591
const glCtx = this.scene?.glContext;
4592
if (!glCtx?.gl || !baked)
4593
{ return null; }
4594
if (!baked._fluidPacks)
4595
{
4596
const packs = createFluidPackSet(glCtx);
4597
if (!packs)
4598
{ return null; }
4599
baked._fluidPacks = packs;
4600
}
4601
return baked._fluidPacks;
4602
}
4603
4604
#fluidAtlasSinglePassEnabled ()
4605
{
4606
if (!this.#useFluidAtlasSinglePass)
4607
{ return false; }
4608
if (globalThis.__QW_FLUID_ATLAS_SINGLE_PASS === false)
4609
{ return false; }
4610
return true;
4545
4611
}
4546
4612
4547
4613
/**
@@ -4623,6 +4689,15 @@ export class WorldView
4623
4689
return;
4624
4690
}
4625
4691
4692
const usedSingle = this.#fluidAtlasSinglePassEnabled()
4693
&& this.#renderFluidAtlasChunkSinglePass(baked, tileObjects, groundSlot);
4694
if (!usedSingle)
4695
{ this.#renderFluidAtlasChunkPerTile(baked, tileObjects, groundSlot); }
4696
}
4697
4698
#renderFluidAtlasChunkPerTile (baked, tileObjects, groundSlot)
4699
{
4700
const glCtx = this.scene?.glContext;
4626
4701
const bloomTiles = [];
4627
4702
withFluidAtlasPass(glCtx, groundSlot.target, () =>
4628
4703
{
@@ -4685,6 +4760,180 @@ export class WorldView
4685
4760
this.#setRendererVisible(bloomSlot.object, true);
4686
4761
}
4687
4762
4763
/**
4764
* Pack per-tile uniforms into 16×16 textures and bake with 1–2 chunk draws.
4765
* @returns {boolean} true when single-pass succeeded
4766
*/
4767
#renderFluidAtlasChunkSinglePass (baked, tileObjects, groundSlot)
4768
{
4769
const glCtx = this.scene?.glContext;
4770
const packs = this.#ensureFluidPacks(baked);
4771
if (!packs || !glCtx?.gl)
4772
{ return false; }
4773
4774
const waterShader = getWaterAtlasChunkShader();
4775
const lavaShader = getLavaAtlasChunkShader();
4776
const bloomShader = getLavaBloomAtlasChunkShader();
4777
if (!waterShader?.getProgramInfo(glCtx.gl)
4778
|| !lavaShader?.getProgramInfo(glCtx.gl)
4779
|| !bloomShader?.getProgramInfo(glCtx.gl))
4780
{ return false; }
4781
4782
clearFluidPackPixels(packs);
4783
const waterTiles = [];
4784
const lavaTiles = [];
4785
const bloomTiles = [];
4786
let waterNoise = null;
4787
let lavaNoise = null;
4788
let waterSecondary = null;
4789
let lavaSecondary = null;
4790
let waterUniforms = null;
4791
let lavaUniforms = null;
4792
let bloomUniforms = null;
4793
let ambientDark = 0;
4794
let timeSec = 0;
4795
4796
for (const objects of tileObjects)
4797
{
4798
const renderer = objects.ground?.renderer;
4799
if (!renderer?.shaderUniforms || !renderer.sprite?.g_image)
4800
{ continue; }
4801
const kindName = renderer._shaderUniformKind;
4802
const kind = kindName === "lava" ? FLUID_KIND_LAVA
4803
: kindName === "water" ? FLUID_KIND_WATER
4804
: null;
4805
if (!kind)
4806
{ continue; }
4807
const localX = objects.tileX - baked.chunkX * CHUNK_SIZE;
4808
const localY = objects.tileY - baked.chunkY * CHUNK_SIZE;
4809
writeFluidPackTile(packs, localX, localY, kind, renderer.shaderUniforms);
4810
const noise = renderer.sprite.g_image;
4811
const secondary = renderer.secondaryMap ?? null;
4812
const u = renderer.shaderUniforms;
4813
ambientDark = u.u_ambientDark ?? ambientDark;
4814
timeSec = u.u_time ?? timeSec;
4815
if (kind === FLUID_KIND_WATER)
4816
{
4817
waterTiles.push(objects);
4818
if (!waterNoise)
4819
{ waterNoise = noise; }
4820
else if (waterNoise !== noise)
4821
{ return false; }
4822
if (secondary)
4823
{
4824
if (!waterSecondary)
4825
{ waterSecondary = secondary; }
4826
else if (waterSecondary !== secondary)
4827
{ return false; }
4828
}
4829
if (!waterUniforms)
4830
{ waterUniforms = u; }
4831
}
4832
else
4833
{
4834
lavaTiles.push(objects);
4835
if (!lavaNoise)
4836
{ lavaNoise = noise; }
4837
else if (lavaNoise !== noise)
4838
{ return false; }
4839
if (secondary)
4840
{
4841
if (!lavaSecondary)
4842
{ lavaSecondary = secondary; }
4843
else if (lavaSecondary !== secondary)
4844
{ return false; }
4845
}
4846
if (!lavaUniforms)
4847
{ lavaUniforms = u; }
4848
if (objects.lavaBloom?.renderer?.shaderUniforms)
4849
{
4850
bloomTiles.push(objects);
4851
if (!bloomUniforms)
4852
{ bloomUniforms = objects.lavaBloom.renderer.shaderUniforms; }
4853
}
4854
}
4855
}
4856
4857
if (!waterTiles.length && !lavaTiles.length)
4858
{ return false; }
4859
4860
uploadFluidPacks(glCtx, packs);
4861
const chunkOriginX = baked.chunkX * CHUNK_SIZE;
4862
const chunkOriginY = baked.chunkY * CHUNK_SIZE;
4863
4864
withFluidAtlasPass(glCtx, groundSlot.target, () =>
4865
{
4866
if (waterTiles.length && waterNoise && waterUniforms)
4867
{
4868
drawFluidAtlasChunkPass(glCtx, {
4869
textureHtml: waterNoise,
4870
secondaryHtml: waterSecondary,
4871
shader: waterShader,
4872
uniforms: {
4873
...waterDebugUniforms(),
4874
u_chunkOrigin: [chunkOriginX, chunkOriginY],
4875
u_time: timeSec,
4876
u_ambientDark: ambientDark,
4877
u_alpha: 1
4878
},
4879
packs,
4880
kindFilter: FLUID_KIND_WATER,
4881
includeShorePacks: true
4882
});
4883
}
4884
if (lavaTiles.length && lavaNoise && lavaUniforms)
4885
{
4886
drawFluidAtlasChunkPass(glCtx, {
4887
textureHtml: lavaNoise,
4888
secondaryHtml: lavaSecondary,
4889
shader: lavaShader,
4890
uniforms: {
4891
...lavaDebugUniforms(),
4892
u_chunkOrigin: [chunkOriginX, chunkOriginY],
4893
u_time: timeSec,
4894
u_ambientDark: ambientDark,
4895
u_alpha: 1
4896
},
4897
packs,
4898
kindFilter: FLUID_KIND_LAVA,
4899
includeShorePacks: true
4900
});
4901
}
4902
});
4903
this.#layoutFluidAtlasObject(baked, "ground");
4904
this.#setRendererVisible(groundSlot.object, true);
4905
4906
if (!bloomTiles.length || !this.bloomEnabled || !bloomUniforms || !lavaNoise)
4907
{
4908
this.#setRendererVisible(baked.fluidBloomObject, false);
4909
return true;
4910
}
4911
4912
const bloomSlot = this.#ensureFluidAtlasSlot(baked, "bloom");
4913
if (!bloomSlot || !this.#useFluidAtlas)
4914
{ return true; }
4915
withFluidAtlasPass(glCtx, bloomSlot.target, () =>
4916
{
4917
drawFluidAtlasChunkPass(glCtx, {
4918
textureHtml: lavaNoise,
4919
secondaryHtml: null,
4920
shader: bloomShader,
4921
uniforms: {
4922
...lavaDebugUniforms(),
4923
u_chunkOrigin: [chunkOriginX, chunkOriginY],
4924
u_time: bloomUniforms.u_time ?? timeSec,
4925
u_alpha: 1
4926
},
4927
packs,
4928
kindFilter: FLUID_KIND_LAVA,
4929
includeShorePacks: false
4930
});
4931
});
4932
this.#layoutFluidAtlasObject(baked, "bloom");
4933
this.#setRendererVisible(bloomSlot.object, true);
4934
return true;
4935
}
4936
4688
4937
#ambientDarkness ()
4689
4938
{
4690
4939
const hour = this.nightPreview ?? this.worldHour;
@@ -36,8 +36,22 @@ import36
createFluidAtlasTarget,36
createFluidAtlasTarget,37
destroyFluidAtlasTarget,37
destroyFluidAtlasTarget,38
withFluidAtlasPass,38
withFluidAtlasPass,39
drawFluidAtlasTile39
drawFluidAtlasTile,40
createFluidPackSet,41
destroyFluidPackSet,42
clearFluidPackPixels,43
writeFluidPackTile,44
uploadFluidPacks,45
drawFluidAtlasChunkPass,46
FLUID_KIND_WATER,47
FLUID_KIND_LAVA40
} from "./fluidChunkAtlas.js";48
} from "./fluidChunkAtlas.js";49
import50
{51
WATER_ATLAS_CHUNK_FRAG,52
LAVA_ATLAS_CHUNK_FRAG,53
LAVA_BLOOM_ATLAS_CHUNK_FRAG54
} from "./fluidAtlasChunkShaders.js";41
import { waterDebugUniforms, lavaDebugUniforms } from "../play/waterDebug.js";55
import { waterDebugUniforms, lavaDebugUniforms } from "../play/waterDebug.js";42
import { natureNoise01, natureTintUniforms, NATURE_TINT, NATURE_NOISE_SIZE, NATURE_NOISE_PAD } from "/shared/render/natureNoise.js";56
import { natureNoise01, natureTintUniforms, NATURE_TINT, NATURE_NOISE_SIZE, NATURE_NOISE_PAD } from "/shared/render/natureNoise.js";43
import { SeasonId, seasonIdAt, resolveSeasonalVisualState } from "/shared/world/season.js";57
import { SeasonId, seasonIdAt, resolveSeasonalVisualState } from "/shared/world/season.js";@@ -970,6 +984,9 @@ let cliffGroundTopShader = null;970
let waterGroundShader = null;984
let waterGroundShader = null;971
let lavaGroundShader = null;985
let lavaGroundShader = null;972
let lavaBloomShader = null;986
let lavaBloomShader = null;987
let waterAtlasChunkShader = null;988
let lavaAtlasChunkShader = null;989
let lavaBloomAtlasChunkShader = null;973
let tintLitSpriteShader = null;990
let tintLitSpriteShader = null;974
let dualTintLitSpriteShader = null;991
let dualTintLitSpriteShader = null;975
let tightFitTintLitSpriteShader = null;992
let tightFitTintLitSpriteShader = null;@@ -1017,6 +1034,27 @@ function getLavaBloomShader ()1017
return lavaBloomShader;1034
return lavaBloomShader;1018
}1035
}1019
1036
1037
function getWaterAtlasChunkShader ()1038
{1039
if (!waterAtlasChunkShader)1040
{ waterAtlasChunkShader = Shader.fragment(WATER_ATLAS_CHUNK_FRAG); }1041
return waterAtlasChunkShader;1042
}1043
1044
function getLavaAtlasChunkShader ()1045
{1046
if (!lavaAtlasChunkShader)1047
{ lavaAtlasChunkShader = Shader.fragment(LAVA_ATLAS_CHUNK_FRAG); }1048
return lavaAtlasChunkShader;1049
}1050
1051
function getLavaBloomAtlasChunkShader ()1052
{1053
if (!lavaBloomAtlasChunkShader)1054
{ lavaBloomAtlasChunkShader = Shader.fragment(LAVA_BLOOM_ATLAS_CHUNK_FRAG); }1055
return lavaBloomAtlasChunkShader;1056
}1057
1020
function getTintLitSpriteShader ()1058
function getTintLitSpriteShader ()1021
{1059
{1022
if (!tintLitSpriteShader)1060
if (!tintLitSpriteShader)@@ -1067,6 +1105,8 @@ export class WorldView1067
#placeSizeScratch = new vector(0, 0);1105
#placeSizeScratch = new vector(0, 0);1068
/** When true, water/lava shade into per-chunk 16×16 atlases (set false if FBO init fails). */1106
/** When true, water/lava shade into per-chunk 16×16 atlases (set false if FBO init fails). */1069
#useFluidAtlas = true;1107
#useFluidAtlas = true;1108
/** Chunk-wide single-pass fluid atlas bake (pack textures). Set false or window.__QW_FLUID_ATLAS_SINGLE_PASS=false for per-tile fallback. */1109
#useFluidAtlasSinglePass = true;1070
/** Bumped on chunk bake / season change — invalidates cached fluid coast topology. */1110
/** Bumped on chunk bake / season change — invalidates cached fluid coast topology. */1071
#fluidCoastEpoch = 0;1111
#fluidCoastEpoch = 0;1072
1112
@@ -4540,8 +4580,34 @@ export class WorldView4540
baked.fluidBloomObject = null;4580
baked.fluidBloomObject = null;4541
destroyFluidAtlasTarget(glCtx, baked._fluidGroundTarget);4581
destroyFluidAtlasTarget(glCtx, baked._fluidGroundTarget);4542
destroyFluidAtlasTarget(glCtx, baked._fluidBloomTarget);4582
destroyFluidAtlasTarget(glCtx, baked._fluidBloomTarget);4583
destroyFluidPackSet(glCtx, baked._fluidPacks);4543
baked._fluidGroundTarget = null;4584
baked._fluidGroundTarget = null;4544
baked._fluidBloomTarget = null;4585
baked._fluidBloomTarget = null;4586
baked._fluidPacks = null;4587
}4588
4589
#ensureFluidPacks (baked)4590
{4591
const glCtx = this.scene?.glContext;4592
if (!glCtx?.gl || !baked)4593
{ return null; }4594
if (!baked._fluidPacks)4595
{4596
const packs = createFluidPackSet(glCtx);4597
if (!packs)4598
{ return null; }4599
baked._fluidPacks = packs;4600
}4601
return baked._fluidPacks;4602
}4603
4604
#fluidAtlasSinglePassEnabled ()4605
{4606
if (!this.#useFluidAtlasSinglePass)4607
{ return false; }4608
if (globalThis.__QW_FLUID_ATLAS_SINGLE_PASS === false)4609
{ return false; }4610
return true;4545
}4611
}4546
4612
4547
/**4613
/**@@ -4623,6 +4689,15 @@ export class WorldView4623
return;4689
return;4624
}4690
}4625
4691
4692
const usedSingle = this.#fluidAtlasSinglePassEnabled()4693
&& this.#renderFluidAtlasChunkSinglePass(baked, tileObjects, groundSlot);4694
if (!usedSingle)4695
{ this.#renderFluidAtlasChunkPerTile(baked, tileObjects, groundSlot); }4696
}4697
4698
#renderFluidAtlasChunkPerTile (baked, tileObjects, groundSlot)4699
{4700
const glCtx = this.scene?.glContext;4626
const bloomTiles = [];4701
const bloomTiles = [];4627
withFluidAtlasPass(glCtx, groundSlot.target, () =>4702
withFluidAtlasPass(glCtx, groundSlot.target, () =>4628
{4703
{@@ -4685,6 +4760,180 @@ export class WorldView4685
this.#setRendererVisible(bloomSlot.object, true);4760
this.#setRendererVisible(bloomSlot.object, true);4686
}4761
}4687
4762
4763
/**4764
* Pack per-tile uniforms into 16×16 textures and bake with 1–2 chunk draws.4765
* @returns {boolean} true when single-pass succeeded4766
*/4767
#renderFluidAtlasChunkSinglePass (baked, tileObjects, groundSlot)4768
{4769
const glCtx = this.scene?.glContext;4770
const packs = this.#ensureFluidPacks(baked);4771
if (!packs || !glCtx?.gl)4772
{ return false; }4773
4774
const waterShader = getWaterAtlasChunkShader();4775
const lavaShader = getLavaAtlasChunkShader();4776
const bloomShader = getLavaBloomAtlasChunkShader();4777
if (!waterShader?.getProgramInfo(glCtx.gl)4778
|| !lavaShader?.getProgramInfo(glCtx.gl)4779
|| !bloomShader?.getProgramInfo(glCtx.gl))4780
{ return false; }4781
4782
clearFluidPackPixels(packs);4783
const waterTiles = [];4784
const lavaTiles = [];4785
const bloomTiles = [];4786
let waterNoise = null;4787
let lavaNoise = null;4788
let waterSecondary = null;4789
let lavaSecondary = null;4790
let waterUniforms = null;4791
let lavaUniforms = null;4792
let bloomUniforms = null;4793
let ambientDark = 0;4794
let timeSec = 0;4795
4796
for (const objects of tileObjects)4797
{4798
const renderer = objects.ground?.renderer;4799
if (!renderer?.shaderUniforms || !renderer.sprite?.g_image)4800
{ continue; }4801
const kindName = renderer._shaderUniformKind;4802
const kind = kindName === "lava" ? FLUID_KIND_LAVA4803
: kindName === "water" ? FLUID_KIND_WATER4804
: null;4805
if (!kind)4806
{ continue; }4807
const localX = objects.tileX - baked.chunkX * CHUNK_SIZE;4808
const localY = objects.tileY - baked.chunkY * CHUNK_SIZE;4809
writeFluidPackTile(packs, localX, localY, kind, renderer.shaderUniforms);4810
const noise = renderer.sprite.g_image;4811
const secondary = renderer.secondaryMap ?? null;4812
const u = renderer.shaderUniforms;4813
ambientDark = u.u_ambientDark ?? ambientDark;4814
timeSec = u.u_time ?? timeSec;4815
if (kind === FLUID_KIND_WATER)4816
{4817
waterTiles.push(objects);4818
if (!waterNoise)4819
{ waterNoise = noise; }4820
else if (waterNoise !== noise)4821
{ return false; }4822
if (secondary)4823
{4824
if (!waterSecondary)4825
{ waterSecondary = secondary; }4826
else if (waterSecondary !== secondary)4827
{ return false; }4828
}4829
if (!waterUniforms)4830
{ waterUniforms = u; }4831
}4832
else4833
{4834
lavaTiles.push(objects);4835
if (!lavaNoise)4836
{ lavaNoise = noise; }4837
else if (lavaNoise !== noise)4838
{ return false; }4839
if (secondary)4840
{4841
if (!lavaSecondary)4842
{ lavaSecondary = secondary; }4843
else if (lavaSecondary !== secondary)4844
{ return false; }4845
}4846
if (!lavaUniforms)4847
{ lavaUniforms = u; }4848
if (objects.lavaBloom?.renderer?.shaderUniforms)4849
{4850
bloomTiles.push(objects);4851
if (!bloomUniforms)4852
{ bloomUniforms = objects.lavaBloom.renderer.shaderUniforms; }4853
}4854
}4855
}4856
4857
if (!waterTiles.length && !lavaTiles.length)4858
{ return false; }4859
4860
uploadFluidPacks(glCtx, packs);4861
const chunkOriginX = baked.chunkX * CHUNK_SIZE;4862
const chunkOriginY = baked.chunkY * CHUNK_SIZE;4863
4864
withFluidAtlasPass(glCtx, groundSlot.target, () =>4865
{4866
if (waterTiles.length && waterNoise && waterUniforms)4867
{4868
drawFluidAtlasChunkPass(glCtx, {4869
textureHtml: waterNoise,4870
secondaryHtml: waterSecondary,4871
shader: waterShader,4872
uniforms: {4873
...waterDebugUniforms(),4874
u_chunkOrigin: [chunkOriginX, chunkOriginY],4875
u_time: timeSec,4876
u_ambientDark: ambientDark,4877
u_alpha: 14878
},4879
packs,4880
kindFilter: FLUID_KIND_WATER,4881
includeShorePacks: true4882
});4883
}4884
if (lavaTiles.length && lavaNoise && lavaUniforms)4885
{4886
drawFluidAtlasChunkPass(glCtx, {4887
textureHtml: lavaNoise,4888
secondaryHtml: lavaSecondary,4889
shader: lavaShader,4890
uniforms: {4891
...lavaDebugUniforms(),4892
u_chunkOrigin: [chunkOriginX, chunkOriginY],4893
u_time: timeSec,4894
u_ambientDark: ambientDark,4895
u_alpha: 14896
},4897
packs,4898
kindFilter: FLUID_KIND_LAVA,4899
includeShorePacks: true4900
});4901
}4902
});4903
this.#layoutFluidAtlasObject(baked, "ground");4904
this.#setRendererVisible(groundSlot.object, true);4905
4906
if (!bloomTiles.length || !this.bloomEnabled || !bloomUniforms || !lavaNoise)4907
{4908
this.#setRendererVisible(baked.fluidBloomObject, false);4909
return true;4910
}4911
4912
const bloomSlot = this.#ensureFluidAtlasSlot(baked, "bloom");4913
if (!bloomSlot || !this.#useFluidAtlas)4914
{ return true; }4915
withFluidAtlasPass(glCtx, bloomSlot.target, () =>4916
{4917
drawFluidAtlasChunkPass(glCtx, {4918
textureHtml: lavaNoise,4919
secondaryHtml: null,4920
shader: bloomShader,4921
uniforms: {4922
...lavaDebugUniforms(),4923
u_chunkOrigin: [chunkOriginX, chunkOriginY],4924
u_time: bloomUniforms.u_time ?? timeSec,4925
u_alpha: 14926
},4927
packs,4928
kindFilter: FLUID_KIND_LAVA,4929
includeShorePacks: false4930
});4931
});4932
this.#layoutFluidAtlasObject(baked, "bloom");4933
this.#setRendererVisible(bloomSlot.object, true);4934
return true;4935
}4936
4688
#ambientDarkness ()4937
#ambientDarkness ()4689
{4938
{4690
const hour = this.nightPreview ?? this.worldHour;4939
const hour = this.nightPreview ?? this.worldHour;