/** * Chunk-wide fluid atlas fragment shaders (single-pass bake). * Derive localTile + puv from atlas v_texCoord; sample 16×16 pack textures. */ const FBM_COMMON = [ "vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }", "vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }", "vec4 permute(vec4 x) { return mod289(((x * 34.0) + 1.0) * x); }", "vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r; }", "float snoise(vec3 v) {", " vec2 C = vec2(1.0 / 6.0, 1.0 / 3.0);", " vec4 D = vec4(0.0, 0.5, 1.0, 2.0);", " vec3 i = floor(v + dot(v, C.yyy));", " vec3 x0 = v - i + dot(i, C.xxx);", " vec3 g = step(x0.yzx, x0.xyz);", " vec3 l = 1.0 - g;", " vec3 i1 = min(g.xyz, l.zxy);", " vec3 i2 = max(g.xyz, l.zxy);", " vec3 x1 = x0 - i1 + C.xxx;", " vec3 x2 = x0 - i2 + C.yyy;", " vec3 x3 = x0 - D.yyy;", " i = mod289(i);", " vec4 p = permute(permute(permute(", " i.z + vec4(0.0, i1.z, i2.z, 1.0))", " + i.y + vec4(0.0, i1.y, i2.y, 1.0))", " + i.x + vec4(0.0, i1.x, i2.x, 1.0));", " float n_ = 0.142857142857;", " vec3 ns = n_ * D.wyz - D.xzx;", " vec4 j = p - 49.0 * floor(p * ns.z * ns.z);", " vec4 x_ = floor(j * ns.z);", " vec4 y_ = floor(j - 7.0 * x_);", " vec4 x = x_ * ns.x + ns.yyyy;", " vec4 y = y_ * ns.x + ns.yyyy;", " vec4 h = 1.0 - abs(x) - abs(y);", " vec4 b0 = vec4(x.xy, y.xy);", " vec4 b1 = vec4(x.zw, y.zw);", " vec4 s0 = floor(b0) * 2.0 + 1.0;", " vec4 s1 = floor(b1) * 2.0 + 1.0;", " vec4 sh = -step(h, vec4(0.0));", " vec4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;", " vec4 a1 = b1.xzyw + s1.xzyw * sh.zzww;", " vec3 p0 = vec3(a0.xy, h.x);", " vec3 p1 = vec3(a0.zw, h.y);", " vec3 p2 = vec3(a1.xy, h.z);", " vec3 p3 = vec3(a1.zw, h.w);", " vec4 norm = taylorInvSqrt(vec4(dot(p0, p0), dot(p1, p1), dot(p2, p2), dot(p3, p3)));", " p0 *= norm.x; p1 *= norm.y; p2 *= norm.z; p3 *= norm.w;", " vec4 m = max(0.6 - vec4(dot(x0, x0), dot(x1, x1), dot(x2, x2), dot(x3, x3)), 0.0);", " m = m * m;", " return 42.0 * dot(m * m, vec4(dot(p0, x0), dot(p1, x1), dot(p2, x2), dot(p3, x3)));", "}", "float fbm(vec3 p) {", " float sum = 0.0;", " float amp = 0.5;", " sum += amp * snoise(p); p *= 2.0; amp *= 0.5;", " sum += amp * snoise(p); p *= 2.0; amp *= 0.5;", " sum += amp * snoise(p); p *= 2.0; amp *= 0.5;", " sum += amp * snoise(p);", " return sum;", "}", "vec2 wrapUv(vec2 uv) {", " return fract(uv) * 0.998 + 0.001;", "}", "float bit4(float packedByte, float bitIndex) {", " float div = floor(packedByte / pow(2.0, bitIndex));", " return step(0.5, mod(div, 2.0));", "}" ].join("\n"); const ATLAS_TILE_SETUP = [ " vec2 atlasPx = floor(v_texCoord * u_atlasPx);", " vec2 localTile = floor(atlasPx / u_tilePx);", " vec2 puv = (mod(atlasPx, u_tilePx) + 0.5) / u_tilePx;", " vec2 packUV = (localTile + 0.5) / u_tilePx;", " vec4 mask = texture2D(u_tileMask, packUV);", " float kind = floor(mask.r * 255.0 + 0.5);", " if (kind < 0.5 || abs(kind - u_kindFilter) > 0.5) { discard; }", " float edgeBits = floor(mask.g * 255.0 + 0.5);", " float diagBits = floor(mask.b * 255.0 + 0.5);", " float shoreCardBits = floor(mask.a * 255.0 + 0.5);", " float u_edgeN = bit4(edgeBits, 0.0);", " float u_edgeE = bit4(edgeBits, 1.0);", " float u_edgeS = bit4(edgeBits, 2.0);", " float u_edgeW = bit4(edgeBits, 3.0);", " float u_edgeNE = bit4(diagBits, 0.0);", " float u_edgeNW = bit4(diagBits, 1.0);", " float u_edgeSE = bit4(diagBits, 2.0);", " float u_edgeSW = bit4(diagBits, 3.0);", " vec4 shorePack = texture2D(u_tileShore, packUV);", " float shoreDiagBits = floor(shorePack.r * 255.0 + 0.5);", " float u_hasShore = step(0.5, shorePack.g);", " float u_shoreEdgeN = bit4(shoreCardBits, 0.0);", " float u_shoreEdgeE = bit4(shoreCardBits, 1.0);", " float u_shoreEdgeS = bit4(shoreCardBits, 2.0);", " float u_shoreEdgeW = bit4(shoreCardBits, 3.0);", " float u_shoreEdgeNE = bit4(shoreDiagBits, 0.0);", " float u_shoreEdgeNW = bit4(shoreDiagBits, 1.0);", " float u_shoreEdgeSE = bit4(shoreDiagBits, 2.0);", " float u_shoreEdgeSW = bit4(shoreDiagBits, 3.0);", " vec4 litPack = texture2D(u_tileLit, packUV);", " float u_litNW = litPack.r;", " float u_litNE = litPack.g;", " float u_litSW = litPack.b;", " float u_litSE = litPack.a;", " vec4 deepPack = texture2D(u_tileDeep, packUV);", " float u_deepNW = deepPack.r;", " float u_deepNE = deepPack.g;", " float u_deepSW = deepPack.b;", " float u_deepSE = deepPack.a;", " vec4 shoreUv = texture2D(u_tileShoreUv, packUV);", " vec2 world = u_chunkOrigin + localTile + puv;" ].join("\n"); const TUNE_UNIFORMS_WATER = [ "uniform float u_flowX;", "uniform float u_flowY;", "uniform float u_flowZ;", "uniform float u_waveScale;", "uniform float u_fbmAmp;", "uniform float u_texAmp;", "uniform float u_distort;", "uniform float u_normalZ;", "uniform float u_waveGain;", "uniform float u_crestLo;", "uniform float u_crestHi;", "uniform float u_crestPow;", "uniform float u_glitterPow;", "uniform float u_crestGlitter;", "uniform float u_glitterCap;", "uniform float u_glitterMix;", "uniform float u_shadeLo;", "uniform float u_shadeHi;", "uniform float u_bodyLo;", "uniform float u_bodyHi;", "uniform float u_litLo;", "uniform float u_litHi;", "uniform float u_litMix;", "uniform float u_edgeDark;", "uniform float u_lightDirX;", "uniform float u_lightDirY;", "uniform float u_lightDirZ;", "uniform float u_normalEps;", "uniform float u_foamStrength;", "uniform float u_shoreClip;", "uniform float u_deepDark;", "uniform float u_deepAbrupt;" ].join("\n"); const TUNE_UNIFORMS_LAVA = [ "uniform float u_flowX;", "uniform float u_flowY;", "uniform float u_flowZ;", "uniform float u_waveScale;", "uniform float u_fbmAmp;", "uniform float u_texAmp;", "uniform float u_distort;", "uniform float u_normalZ;", "uniform float u_waveGain;", "uniform float u_crestLo;", "uniform float u_crestHi;", "uniform float u_crestPow;", "uniform float u_glitterPow;", "uniform float u_crestGlitter;", "uniform float u_glitterCap;", "uniform float u_glitterMix;", "uniform float u_shadeLo;", "uniform float u_shadeHi;", "uniform float u_bodyLo;", "uniform float u_bodyHi;", "uniform float u_litLo;", "uniform float u_litHi;", "uniform float u_litMix;", "uniform float u_edgeDark;", "uniform float u_lightDirX;", "uniform float u_lightDirY;", "uniform float u_lightDirZ;", "uniform float u_normalEps;", "uniform float u_shoreClip;" ].join("\n"); const PACK_UNIFORMS = [ "uniform sampler2D u_tileMask;", "uniform sampler2D u_tileShore;", "uniform sampler2D u_tileLit;", "uniform sampler2D u_tileDeep;", "uniform sampler2D u_tileShoreUv;", "uniform vec2 u_chunkOrigin;", "uniform float u_atlasPx;", "uniform float u_tilePx;", "uniform float u_kindFilter;", "uniform float u_time;", "uniform float u_ambientDark;" ].join("\n"); export const WATER_ATLAS_CHUNK_FRAG = [ "precision mediump float;", "varying vec2 v_texCoord;", "uniform sampler2D u_texture;", "uniform sampler2D u_secondary;", "uniform float u_alpha;", PACK_UNIFORMS, TUNE_UNIFORMS_WATER, FBM_COMMON, "float sampleShore(", " float enabled, float useShore, float hasShore, vec2 shoreNu, vec4 u_shoreUvRect,", " inout vec3 shoreRgb, inout float shoreW", ") {", " if (enabled < 0.5 || useShore < 0.5 || hasShore < 0.5) { return 0.0; }", " vec2 shoreUv = u_shoreUvRect.xy + shoreNu * (u_shoreUvRect.zw - u_shoreUvRect.xy);", " vec4 shore = texture2D(u_secondary, shoreUv);", " if (shore.a <= 0.1) { return 0.0; }", " shoreRgb = shore.rgb;", " shoreW = 1.0;", " return 1.0;", "}", "void main() {", ATLAS_TILE_SETUP, " float t = u_time;", " vec2 flow = vec2(t * u_flowX, t * u_flowY);", " float eps = max(u_normalEps, 0.001);", " vec3 base = vec3(world * u_waveScale + flow, t * u_flowZ);", " float nC = fbm(base);", " float nL = fbm(base + vec3(-eps, 0.0, 0.0));", " float nR = fbm(base + vec3(eps, 0.0, 0.0));", " float nD = fbm(base + vec3(0.0, -eps, 0.0));", " float nU = fbm(base + vec3(0.0, eps, 0.0));", " vec2 distort = vec2(nL - nR, nD - nU) * u_distort;", " float texN = texture2D(u_texture, wrapUv(world * 0.28 + flow * 0.6 + distort)).r;", " float h = nC * u_fbmAmp + (texN - 0.5) * u_texAmp;", " vec3 normal = normalize(vec3(nL - nR, nD - nU, max(u_normalZ, 0.05)));", " float ndotl = clamp(dot(normal, normalize(vec3(u_lightDirX, u_lightDirY, u_lightDirZ))), 0.0, 1.0);", " float wave = clamp(h * u_waveGain + 0.5, 0.0, 1.0);", " float crest = pow(smoothstep(u_crestLo, max(u_crestHi, u_crestLo + 0.01), wave), max(u_crestPow, 0.01));", " float glitter = pow(max(0.0, ndotl), max(u_glitterPow, 0.01)) * crest;", " glitter = max(glitter, pow(crest, 2.5) * u_crestGlitter);", " float edgeProx = 0.0;", " if (u_edgeN > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.y)); }", " if (u_edgeS > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.y)); }", " if (u_edgeW > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.x)); }", " if (u_edgeE > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.x)); }", " if (u_edgeNE > 0.5) {", " edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, puv.y))));", " }", " if (u_edgeNW > 0.5) {", " edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, puv.y))));", " }", " if (u_edgeSE > 0.5) {", " edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, 1.0 - puv.y))));", " }", " if (u_edgeSW > 0.5) {", " edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, 1.0 - puv.y))));", " }", " float waveBody = wave * (1.0 - edgeProx * 0.72);", " float deepTop = mix(u_deepNW, u_deepNE, puv.x);", " float deepBot = mix(u_deepSW, u_deepSE, puv.x);", " float deepMix = mix(deepTop, deepBot, puv.y);", " float deepAmt = pow(clamp(deepMix, 0.0, 1.0), max(u_deepAbrupt, 0.05));", " float darkW = clamp(u_deepDark, 0.0, 1.0);", " 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);", " 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);", " 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);", " 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);", " vec3 foamCol = vec3(0.94, 0.99, 1.0);", " float shade = mix(u_shadeLo, u_shadeHi, ndotl);", " vec3 rgb = mix(deepCol, midCol, smoothstep(u_bodyLo, max(u_bodyHi, u_bodyLo + 0.01), waveBody));", " rgb = mix(rgb, litCol, smoothstep(u_litLo, max(u_litHi, u_litLo + 0.01), waveBody) * u_litMix);", " rgb *= shade;", " rgb = mix(rgb, sparkCol, min(glitter, u_glitterCap) * u_glitterMix * (1.0 - edgeProx * 0.7));", " rgb = mix(rgb, deepCol, edgeProx * u_edgeDark);", " float shoreW = 0.0;", " vec3 shoreRgb = rgb;", " if (u_edgeN > 0.5) {", " sampleShore(1.0, u_shoreEdgeN, u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);", " }", " if (u_edgeS > 0.5) {", " sampleShore(1.0, u_shoreEdgeS, u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);", " }", " if (u_edgeW > 0.5) {", " sampleShore(1.0, u_shoreEdgeW, u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);", " }", " if (u_edgeE > 0.5) {", " sampleShore(1.0, u_shoreEdgeE, u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);", " }", " if (u_edgeNE > 0.5) {", " sampleShore(1.0, max(u_shoreEdgeNE, u_hasShore), u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);", " }", " if (u_edgeNW > 0.5) {", " sampleShore(1.0, max(u_shoreEdgeNW, u_hasShore), u_hasShore, vec2(puv.x, puv.y), shoreUv, shoreRgb, shoreW);", " }", " if (u_edgeSE > 0.5) {", " sampleShore(1.0, max(u_shoreEdgeSE, u_hasShore), u_hasShore, vec2(1.0 - puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);", " }", " if (u_edgeSW > 0.5) {", " sampleShore(1.0, max(u_shoreEdgeSW, u_hasShore), u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);", " }", " float darkAmt = 1.0 - smoothstep(0.18, 0.52, waveBody);", " float retreat = edgeProx * (0.35 + 0.65 * darkAmt);", " float shoreClip = u_shoreClip;", " float shoreMask = step(shoreClip, retreat) * shoreW;", " float foamAmt = (1.0 - shoreMask) * smoothstep(shoreClip - 0.24, shoreClip - 0.08, retreat)", " * (1.0 - smoothstep(shoreClip - 0.08, shoreClip, retreat));", " rgb = mix(rgb, foamCol, foamAmt * u_foamStrength);", " rgb = mix(rgb, shoreRgb, shoreMask);", " float litTop = mix(u_litNW, u_litNE, puv.x);", " float litBottom = mix(u_litSW, u_litSE, puv.x);", " float lit = mix(litTop, litBottom, puv.y);", " float darkness = u_ambientDark * (1.0 - lit);", " rgb = mix(rgb, vec3(0.0), darkness);", " gl_FragColor = vec4(rgb, u_alpha);", "}" ].join("\n"); export const LAVA_ATLAS_CHUNK_FRAG = [ "precision mediump float;", "varying vec2 v_texCoord;", "uniform sampler2D u_texture;", "uniform sampler2D u_secondary;", "uniform float u_alpha;", PACK_UNIFORMS, TUNE_UNIFORMS_LAVA, FBM_COMMON, "float sampleShore(", " float enabled, float useShore, float hasShore, vec2 shoreNu, vec4 u_shoreUvRect,", " inout vec3 shoreRgb, inout float shoreW", ") {", " if (enabled < 0.5 || useShore < 0.5 || hasShore < 0.5) { return 0.0; }", " vec2 shoreUv = u_shoreUvRect.xy + shoreNu * (u_shoreUvRect.zw - u_shoreUvRect.xy);", " vec4 shore = texture2D(u_secondary, shoreUv);", " if (shore.a <= 0.1) { return 0.0; }", " shoreRgb = shore.rgb;", " shoreW = 1.0;", " return 1.0;", "}", "void main() {", ATLAS_TILE_SETUP, " float t = u_time;", " vec2 flow = vec2(t * u_flowX, t * u_flowY);", " float eps = max(u_normalEps, 0.001);", " vec3 base = vec3(world * u_waveScale + flow, t * u_flowZ);", " float nC = fbm(base);", " float nL = fbm(base + vec3(-eps, 0.0, 0.0));", " float nR = fbm(base + vec3(eps, 0.0, 0.0));", " float nD = fbm(base + vec3(0.0, -eps, 0.0));", " float nU = fbm(base + vec3(0.0, eps, 0.0));", " vec2 distort = vec2(nL - nR, nD - nU) * u_distort;", " float texN = texture2D(u_texture, wrapUv(world * 0.28 + flow * 0.6 + distort)).r;", " float h = nC * u_fbmAmp + (texN - 0.5) * u_texAmp;", " vec3 normal = normalize(vec3(nL - nR, nD - nU, max(u_normalZ, 0.05)));", " float ndotl = clamp(dot(normal, normalize(vec3(u_lightDirX, u_lightDirY, u_lightDirZ))), 0.0, 1.0);", " float wave = clamp(h * u_waveGain + 0.5, 0.0, 1.0);", " float crest = pow(smoothstep(u_crestLo, max(u_crestHi, u_crestLo + 0.01), wave), max(u_crestPow, 0.01));", " float glitter = pow(max(0.0, ndotl), max(u_glitterPow, 0.01)) * crest;", " glitter = max(glitter, pow(crest, 2.5) * u_crestGlitter);", " float edgeProx = 0.0;", " if (u_edgeN > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.y)); }", " if (u_edgeS > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.y)); }", " if (u_edgeW > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.x)); }", " if (u_edgeE > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.x)); }", " if (u_edgeNE > 0.5) {", " edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, puv.y))));", " }", " if (u_edgeNW > 0.5) {", " edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, puv.y))));", " }", " if (u_edgeSE > 0.5) {", " edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, 1.0 - puv.y))));", " }", " if (u_edgeSW > 0.5) {", " edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, 1.0 - puv.y))));", " }", " float waveBody = wave * (1.0 - edgeProx * 0.72);", " float heat = smoothstep(u_bodyLo, max(u_bodyHi, u_bodyLo + 0.01), waveBody);", " heat = mix(heat, 1.0, smoothstep(u_litLo, max(u_litHi, u_litLo + 0.01), waveBody) * u_litMix * 0.35);", " heat = clamp(heat + min(glitter, u_glitterCap) * u_glitterMix * 0.45, 0.0, 1.0);", " vec3 darkRed = vec3(0.16, 0.015, 0.01);", " vec3 brightRed = vec3(0.78, 0.08, 0.02);", " vec3 orange = vec3(0.98, 0.38, 0.04);", " vec3 yellow = vec3(1.0, 0.9, 0.28);", " vec3 rgb = mix(darkRed, brightRed, smoothstep(0.0, 0.38, heat));", " rgb = mix(rgb, orange, smoothstep(0.28, 0.68, heat));", " float yellowAmt = smoothstep(0.55, 0.98, heat);", " rgb = mix(rgb, yellow, yellowAmt);", " float shade = mix(u_shadeLo, u_shadeHi, ndotl);", " rgb *= shade;", " rgb = mix(rgb, darkRed, edgeProx * u_edgeDark * 0.35);", " float shoreW = 0.0;", " vec3 shoreRgb = rgb;", " if (u_edgeN > 0.5) {", " sampleShore(1.0, u_shoreEdgeN, u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);", " }", " if (u_edgeS > 0.5) {", " sampleShore(1.0, u_shoreEdgeS, u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);", " }", " if (u_edgeW > 0.5) {", " sampleShore(1.0, u_shoreEdgeW, u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);", " }", " if (u_edgeE > 0.5) {", " sampleShore(1.0, u_shoreEdgeE, u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);", " }", " if (u_edgeNE > 0.5) {", " sampleShore(1.0, max(u_shoreEdgeNE, u_hasShore), u_hasShore, vec2(1.0 - puv.x, puv.y), shoreUv, shoreRgb, shoreW);", " }", " if (u_edgeNW > 0.5) {", " sampleShore(1.0, max(u_shoreEdgeNW, u_hasShore), u_hasShore, vec2(puv.x, puv.y), shoreUv, shoreRgb, shoreW);", " }", " if (u_edgeSE > 0.5) {", " sampleShore(1.0, max(u_shoreEdgeSE, u_hasShore), u_hasShore, vec2(1.0 - puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);", " }", " if (u_edgeSW > 0.5) {", " sampleShore(1.0, max(u_shoreEdgeSW, u_hasShore), u_hasShore, vec2(puv.x, 1.0 - puv.y), shoreUv, shoreRgb, shoreW);", " }", " float darkAmt = 1.0 - smoothstep(0.12, 0.48, waveBody);", " float retreat = edgeProx * mix(0.72, 1.0, darkAmt);", " float shoreClip = u_shoreClip;", " float shoreMask = step(shoreClip, retreat) * shoreW;", " float crustAmt = (1.0 - shoreMask) * smoothstep(shoreClip - 0.2, shoreClip - 0.06, retreat)", " * (1.0 - smoothstep(shoreClip - 0.06, shoreClip, retreat));", " rgb = mix(rgb, vec3(0.05, 0.01, 0.0), crustAmt);", " rgb = mix(rgb, shoreRgb, shoreMask);", " float litTop = mix(u_litNW, u_litNE, puv.x);", " float litBottom = mix(u_litSW, u_litSE, puv.x);", " float lit = mix(litTop, litBottom, puv.y);", " float darkness = u_ambientDark * (1.0 - lit);", " rgb = mix(rgb, vec3(0.0), darkness);", " gl_FragColor = vec4(rgb, u_alpha);", "}" ].join("\n"); export const LAVA_BLOOM_ATLAS_CHUNK_FRAG = [ "precision mediump float;", "varying vec2 v_texCoord;", "uniform sampler2D u_texture;", "uniform float u_alpha;", "uniform sampler2D u_tileMask;", "uniform vec2 u_chunkOrigin;", "uniform float u_atlasPx;", "uniform float u_tilePx;", "uniform float u_kindFilter;", "uniform float u_time;", TUNE_UNIFORMS_LAVA, "uniform float u_bloomStrength;", FBM_COMMON, "void main() {", " vec2 atlasPx = floor(v_texCoord * u_atlasPx);", " vec2 localTile = floor(atlasPx / u_tilePx);", " vec2 puv = (mod(atlasPx, u_tilePx) + 0.5) / u_tilePx;", " vec2 packUV = (localTile + 0.5) / u_tilePx;", " vec4 mask = texture2D(u_tileMask, packUV);", " float kind = floor(mask.r * 255.0 + 0.5);", " if (kind < 0.5 || abs(kind - u_kindFilter) > 0.5) { discard; }", " float edgeBits = floor(mask.g * 255.0 + 0.5);", " float diagBits = floor(mask.b * 255.0 + 0.5);", " float u_edgeN = bit4(edgeBits, 0.0);", " float u_edgeE = bit4(edgeBits, 1.0);", " float u_edgeS = bit4(edgeBits, 2.0);", " float u_edgeW = bit4(edgeBits, 3.0);", " float u_edgeNE = bit4(diagBits, 0.0);", " float u_edgeNW = bit4(diagBits, 1.0);", " float u_edgeSE = bit4(diagBits, 2.0);", " float u_edgeSW = bit4(diagBits, 3.0);", " vec2 world = u_chunkOrigin + localTile + puv;", " float t = u_time;", " vec2 flow = vec2(t * u_flowX, t * u_flowY);", " float eps = max(u_normalEps, 0.001);", " vec3 base = vec3(world * u_waveScale + flow, t * u_flowZ);", " float nC = fbm(base);", " float nL = fbm(base + vec3(-eps, 0.0, 0.0));", " float nR = fbm(base + vec3(eps, 0.0, 0.0));", " float nD = fbm(base + vec3(0.0, -eps, 0.0));", " float nU = fbm(base + vec3(0.0, eps, 0.0));", " vec2 distort = vec2(nL - nR, nD - nU) * u_distort;", " float texN = texture2D(u_texture, wrapUv(world * 0.28 + flow * 0.6 + distort)).r;", " float h = nC * u_fbmAmp + (texN - 0.5) * u_texAmp;", " vec3 normal = normalize(vec3(nL - nR, nD - nU, max(u_normalZ, 0.05)));", " float ndotl = clamp(dot(normal, normalize(vec3(u_lightDirX, u_lightDirY, u_lightDirZ))), 0.0, 1.0);", " float wave = clamp(h * u_waveGain + 0.5, 0.0, 1.0);", " float crest = pow(smoothstep(u_crestLo, max(u_crestHi, u_crestLo + 0.01), wave), max(u_crestPow, 0.01));", " float glitter = pow(max(0.0, ndotl), max(u_glitterPow, 0.01)) * crest;", " glitter = max(glitter, pow(crest, 2.5) * u_crestGlitter);", " float edgeProx = 0.0;", " if (u_edgeN > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.y)); }", " if (u_edgeS > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.y)); }", " if (u_edgeW > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, puv.x)); }", " if (u_edgeE > 0.5) { edgeProx = max(edgeProx, smoothstep(0.55, 0.0, 1.0 - puv.x)); }", " if (u_edgeNE > 0.5) {", " edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, puv.y))));", " }", " if (u_edgeNW > 0.5) {", " edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, puv.y))));", " }", " if (u_edgeSE > 0.5) {", " edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(1.0 - puv.x, 1.0 - puv.y))));", " }", " if (u_edgeSW > 0.5) {", " edgeProx = max(edgeProx, 1.0 - smoothstep(0.0, 0.78, length(vec2(puv.x, 1.0 - puv.y))));", " }", " float waveBody = wave * (1.0 - edgeProx * 0.72);", " float heat = smoothstep(u_bodyLo, max(u_bodyHi, u_bodyLo + 0.01), waveBody);", " heat = mix(heat, 1.0, smoothstep(u_litLo, max(u_litHi, u_litLo + 0.01), waveBody) * u_litMix * 0.35);", " heat = clamp(heat + min(glitter, u_glitterCap) * u_glitterMix * 0.45, 0.0, 1.0);", " float darkAmt = 1.0 - smoothstep(0.12, 0.48, waveBody);", " float retreat = edgeProx * mix(0.72, 1.0, darkAmt);", " float hasEdge = max(max(u_edgeN, u_edgeE), max(u_edgeS, u_edgeW));", " hasEdge = max(hasEdge, max(max(u_edgeNE, u_edgeNW), max(u_edgeSE, u_edgeSW)));", " float shoreW = step(0.5, hasEdge);", " float shoreMask = step(u_shoreClip, retreat) * shoreW;", " float yellowAmt = smoothstep(0.55, 0.98, heat) * (1.0 - edgeProx * 0.85) * (1.0 - shoreMask);", " float bloom = yellowAmt * max(u_bloomStrength, 0.0);", " vec3 glow = vec3(1.0, 0.82, 0.28) * bloom;", " gl_FragColor = vec4(glow, bloom * u_alpha);", "}" ].join("\n");