/* ============================================================================ PROCEDURAL NOISE + 4K TEXTURE GENERATION SYSTEM ---------------------------------------------------------------------------- MASSFRONT's mobile WebGL2 target cannot ship 4K authored atlases in the APK, but it CAN generate them on-device from compact math. This module produces deterministic, tileable, high-resolution textures for noise, shockwaves, energy fields, terrain detail and explosion falloffs. Every generator returns either a Float32Array/RGBA buffer OR uploads directly to a WebGL texture. All functions are GPU-friendly (no per-pixel allocation in the hot path) and quality-aware: HIGH/CINEMATIC gets 2K/4K, MEDIUM gets 1K, LOW falls back to existing authored sheets. INTEGRATION CONTRACT mfNoiseInit() once after gl is available mfNoiseUpload(name, opts) -> WebGLTexture (cached) mfNoiseCanvas(name, opts) -> HTMLCanvasElement (for canvas paths) mfNoiseQuality() -> 'low'|'medium'|'high'|'cinematic' mfNoiseSize() -> pixel dimension for current quality mfNoiseGLReset() call on context rebuild Globals intentionally avoided: do not declare `const NOISE_*` at top level; the module prefix `MF_NG_` is used for internal constants only. ============================================================================ */ (function(){ 'use strict'; const MF_NG_CACHE = new Map(); const MF_NG_TEX_CACHE = new Map(); let mfNgEpoch = -1; function mfNoiseQuality(){ try{ if(typeof qualityKey==='function') return qualityKey(); if(typeof META!=='undefined'&&META.settings&&META.settings.quality) return META.settings.quality; }catch(_){} return 'high'; } /* Cap generated size by quality. 4K procedural generation hitches too hard on a mobile CPU, so CINEMATIC caps at 2K — still 4x the legacy authored sheets and visibly crisp at zoom. HIGH shares the 2K sheet; MEDIUM gets 1K. */ function mfNoiseSize(){ const q=mfNoiseQuality(); return q==='low'?256:q==='medium'?1024:2048; } function mfNoiseGLReset(){ /* This boundary is context recovery, not live cache eviction. The cached wrappers belong to the lost generation; deleting them through the restored wrapper itself raises INVALID_OPERATION. Discard references and let the deterministic generators repopulate the current context. */ MF_NG_TEX_CACHE.clear(); MF_NG_CACHE.clear(); mfNgEpoch=-1; } function mfNoiseCtxCheck(){ const ep=(typeof glEpoch!=='undefined')?glEpoch:0; if(mfNgEpoch!==ep) mfNoiseGLReset(); mfNgEpoch=ep; } /* Deterministic seeded random for reproducible worlds. */ function mfNgHash(seed){ let h=seed|0; h=((h>>>16)^h)*0x45d9f3b; h=((h>>>16)^h)*0x45d9f3b; h=(h>>>16)^h; return (h>>>0)/4294967296; } /* Permutations are immutable for a seed. The procedural texture builders call FBM once per texel, so rebuilding this table there turned a 2K material into millions of short-lived allocations and a minute-long boot on some systems. */ const MF_NG_PERM_CACHE=new Map(); function mfNgPerm(seed){ seed=seed|0; const cached=MF_NG_PERM_CACHE.get(seed); if(cached) return cached; const p=new Uint8Array(512); for(let i=0;i<256;i++) p[i]=i; let s=seed|0; for(let i=255;i>0;i--){ s=((s*1664525+1013904223)|0); const j=(s>>>0)%(i+1); const t=p[i];p[i]=p[j];p[j]=t; } for(let i=0;i<256;i++) p[i+256]=p[i]; MF_NG_PERM_CACHE.set(seed,p); return p; } /* Gradient table for value/Perlin noise. */ const MF_NG_GRAD3=[ [1,1,0],[-1,1,0],[1,-1,0],[-1,-1,0], [1,0,1],[-1,0,1],[1,0,-1],[-1,0,-1], [0,1,1],[0,-1,1],[0,1,-1],[0,-1,-1] ]; function mfNgFade(t){ return t*t*t*(t*(t*6-15)+10); } function mfNgSmoothstep(edge0,edge1,x){ const t=Math.max(0,Math.min(1,(x-edge0)/(edge1-edge0))); return t*t*(3-2*t); } function mfNgLerp(a,b,t){ return a+(b-a)*t; } function mfNgDot(g,x,y){ return g[0]*x+g[1]*y; } /* 2D Perlin-like noise with a fixed permutation. Tileable when sampled at integer boundaries; for seamless tiles we wrap the lattice with the perm. */ function mfNgNoise2D(x,y,perm){ const X=(Math.floor(x)&255), Y=(Math.floor(y)&255); const xf=x-Math.floor(x), yf=y-Math.floor(y); const u=mfNgFade(xf), v=mfNgFade(yf); const A=perm[X]+Y, B=perm[X+1]+Y; const g1=MF_NG_GRAD3[perm[A]%12], g2=MF_NG_GRAD3[perm[B]%12]; const g3=MF_NG_GRAD3[perm[A+1]%12], g4=MF_NG_GRAD3[perm[B+1]%12]; const n1=mfNgDot(g1,xf,yf); const n2=mfNgDot(g2,xf-1,yf); const n3=mfNgDot(g3,xf,yf-1); const n4=mfNgDot(g4,xf-1,yf-1); return mfNgLerp(mfNgLerp(n1,n2,u),mfNgLerp(n3,n4,u),v); } /* Fractal Brownian Motion: layered octaves with lacunarity and gain. */ function mfNgFBM(x,y,octaves,seed,opts){ const o=opts||{}; const lac=o.lac||2.0, gain=o.gain||0.5; const perm=mfNgPerm(seed||123); let amp=0.5, freq=o.freq||1.0, sum=0, norm=0; for(let i=0;i{ x=(x+size)%size; y=(y+size)%size; return buf[(y*size+x)*4]/255; }; for(let y=0;y