BlueGit

timfa questward

Files

questward / client / src / render / fluidAtlasChunkShaders.js

fluidAtlasChunkShaders.js

fluidAtlasChunkShaders.js 22.6 KiB Download Raw
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
6const 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
71const 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
115const 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
150const 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
182const 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
196export 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
314export 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
430export 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");