/* ============================================================================ MASSFRONT — DEPTH-AWARE CLOUD POST EFFECT (HIGH / CINEMATIC) ---------------------------------------------------------------------------- mfCloudFxEmit remains the sole cloud recipe owner. This module only caches that frame's emitted descriptors, renders one bounded volume per cloud into a low-resolution target, and composites it over the resolved scene. If the offscreen path, driver, shader, allocation, march, or composite is not proven, the exact cached descriptors are returned to the existing billboard batch in the same frame. No persistent suppress flag exists. World axes used by the 3D renderer are (simulation x, height, simulation y). ============================================================================ */ (function mfCloudPostFxModule(){ 'use strict'; if(typeof window==='undefined')return; const MFCP_MAX_LAYERS=15,MFCP_MAX_CLOUDS=5; const MFCP_ZERO4=new Float32Array([0,0,0,0]); const MFCP_INV_VP=new Float32Array(16); const MFCP_FALLBACK=new Array(MFCP_MAX_LAYERS); const MFCP_PROXY=new Array(MFCP_MAX_CLOUDS); for(let i=0;i=orbitEnter?2:(span>=1450?1:0);} else if(mfcpLod===0&&span>1500)mfcpLod=1; else if(mfcpLod===1&&span<1300)mfcpLod=0; else if(mfcpLod===1&&span>orbitEnter)mfcpLod=2; else if(mfcpLod===2&&span0.0){ float sd=sqrt(disc),t0=(-B-sd)/(2.0*A),t1=(-B+sd)/(2.0*A); float enter=max(0.0,t0),leave=min(t1,sceneT); if(leave>enter){ float count=float(max(uSteps,1)),stepLen=(leave-enter)/count; float jitter=texture(uNoise,uv*3.7+uTime*.0003).a; float t=enter+stepLen*(.18+.64*jitter); for(int i=0;i<24;i++){ if(i>=uSteps)break; vec3 wp=ro+rd*t,qp=wp-uCenter;qp.xz=R*qp.xz;qp/=uRadii; float den=density(qp); float sa=(1.0-exp(-den*stepLen*uExtinct))*uOpacity; float top=clamp(qp.y*.5+.5,0.0,1.0); float powder=1.0-exp(-den*2.2); float sun=max(.16,uSunDir.y*.68+.26); /* Dark belly / bright crown is how a pitched ortho reads volume. A flat pale tint stacked through a tall slab is just milk. */ vec3 shade=mix(vec3(.68,.72,.78),vec3(.96,.97,.99),clamp(top*0.55+powder*0.50,0.0,1.0)); shade*=mix(uTint,vec3(1.0),0.35); shade*=mix(.78,1.10,sun); acc+=(1.0-alpha)*shade*sa;alpha+=(1.0-alpha)*sa; if(alpha>.88)break;t+=stepLen; } } } /* Ground contact is the body shadow billboard on the fallback path. A second noisy disc here painted a purple smudge with a cyan rim across the whole AABB (01 cellular dirt, 03 teal cutout). */ if(alpha>.48){acc*=.48/alpha;alpha=.48;} outColor=vec4(acc,alpha); }`; const MFCP_UP_FS=`#version 300 es precision highp float; precision highp sampler2D; in vec2 vUV;out vec4 outColor; uniform sampler2D uCloud,uDepth; uniform vec2 uSceneSize,uTexSize,uTexScale; vec4 tap(vec2 sceneUV,vec2 off,float centerDepth,out float w){ vec2 fullUV=clamp(sceneUV+off/uSceneSize,vec2(0.0),vec2(1.0)); vec2 cuv=clamp(fullUV*uTexScale,vec2(.5)/uTexSize,uTexScale-vec2(.5)/uTexSize); float d=texture(uDepth,fullUV).r; w=exp(-abs(d-centerDepth)*3500.0); return texture(uCloud,cuv); } void main(){ vec2 sceneUV=gl_FragCoord.xy/uSceneSize; float d=texture(uDepth,sceneUV).r; float w0,w1,w2,w3,w4; vec4 c=tap(sceneUV,vec2(0),d,w0)*w0*2.0; c+=tap(sceneUV,vec2(1,0),d,w1)*w1+tap(sceneUV,vec2(-1,0),d,w2)*w2; c+=tap(sceneUV,vec2(0,1),d,w3)*w3+tap(sceneUV,vec2(0,-1),d,w4)*w4; outColor=c/max(1e-4,w0*2.0+w1+w2+w3+w4); }`; function mfcpDropGL(){ mfcpProg=mfcpProgUp=mfcpVAO=mfcpTex=mfcpFB=mfcpNoise=null; mfcpU=mfcpUup=null;mfcpTexW=mfcpTexH=0;mfcpProgChecked=false; } function mfCloudPostGLReset(){mfcpDropGL();mfcpEpoch=-1;mfcpClearPending();} function mfcpUniforms(p,names){const o={};for(const n of names)o[n]=gl.getUniformLocation(p,n);return o;} function mfcpEnsurePrograms(){ const ep=typeof glEpoch==='number'?glEpoch:0; if(ep!==mfcpEpoch){if(mfcpEpoch>=0)MFCP_TEL.contextResets++;mfcpDropGL();mfcpEpoch=ep;} if(!mfcpProg){ try{ mfcpProg=mkProg(MFCP_VS,MFCP_FS,'cloudpost-volume'); mfcpProgUp=mkProg(MFCP_VS,MFCP_UP_FS,'cloudpost-upsample'); mfcpVAO=gl.createVertexArray(); mfcpU=mfcpUniforms(mfcpProg,['uDepth','uNoise','uInvVP','uCenter','uRadii','uSunDir','uTint','uLobeA','uLobeB','uActiveSize','uRotation','uTime','uOpacity','uShadowAlpha','uClose','uExtinct','uShadowCenter','uShadowRadius','uSteps']); mfcpUup=mfcpUniforms(mfcpProgUp,['uCloud','uDepth','uTexSize','uSceneSize','uTexScale']); }catch(e){MFCP_TEL.shaderFailures++;MFCP_TEL.lastError='init: '+String(e&&e.message||e);return false;} } if(!mfcpProgChecked){ try{mfcpProgChecked=(typeof mfProgOk!=='function'||(mfProgOk(mfcpProg)&&mfProgOk(mfcpProgUp)));} catch(e){mfcpProgChecked=false;} if(!mfcpProgChecked){MFCP_TEL.shaderFailures++;MFCP_TEL.lastError='shader validation failed';return false;} } if(!mfcpNoise){ try{mfcpNoise=typeof mfNoiseUpload==='function'?mfNoiseUpload('smoke',{size:256,seed:0x434c4f55}):null;}catch(e){mfcpNoise=null;} if(!mfcpNoise){MFCP_TEL.driverFailures++;MFCP_TEL.lastError='smoke driver unavailable';return false;} } return !!mfcpVAO; } function mfcpEnsureTarget(w,h){ if(mfcpFailMode==='alloc')return false; if(mfcpTex&&mfcpFB&&mfcpTexW===w&&mfcpTexH===h)return true; if(mfcpFB){gl.bindFramebuffer(gl.FRAMEBUFFER,mfcpFB);gl.framebufferTexture2D(gl.FRAMEBUFFER,gl.COLOR_ATTACHMENT0,gl.TEXTURE_2D,null,0);} if(mfcpTex)gl.deleteTexture(mfcpTex); mfcpTex=gl.createTexture();gl.activeTexture(gl.TEXTURE13);gl.bindTexture(gl.TEXTURE_2D,mfcpTex); gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA8,w,h,0,gl.RGBA,gl.UNSIGNED_BYTE,null); gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.CLAMP_TO_EDGE); if(!mfcpFB)mfcpFB=gl.createFramebuffer(); gl.bindFramebuffer(gl.FRAMEBUFFER,mfcpFB); gl.framebufferTexture2D(gl.FRAMEBUFFER,gl.COLOR_ATTACHMENT0,gl.TEXTURE_2D,mfcpTex,0); gl.drawBuffers([gl.COLOR_ATTACHMENT0]); const ok=gl.checkFramebufferStatus(gl.FRAMEBUFFER)===gl.FRAMEBUFFER_COMPLETE; if(ok){mfcpTexW=w;mfcpTexH=h;return true;} MFCP_TEL.allocFailures++;MFCP_TEL.lastError='cloud target incomplete';return false; } function mfcpProject(x,y,z){ const w=matVP[3]*x+matVP[7]*y+matVP[11]*z+matVP[15];if(Math.abs(w)<1e-6)return null; return [((matVP[0]*x+matVP[4]*y+matVP[8]*z+matVP[12])/w*.5+.5)*aoW, ((matVP[1]*x+matVP[5]*y+matVP[9]*z+matVP[13])/w*.5+.5)*aoH]; } function mfcpSpan(){ return (typeof orthoSpan==='number'&&Number.isFinite(orthoSpan)&&orthoSpan>0)?orthoSpan:1500; } function mfcpCloseK(span){ return Math.max(0,Math.min(1,(1100-span)/680)); } function mfcpYRadius(size,span){ /* Thickness is a puff stack, not a 3000-unit camera column. Parallel ortho rays through a 300–520 slab just integrated into milk. */ const close=mfcpCloseK(span); return Math.max(72,Math.min(168,size*(0.16+0.08*close))); } function mfcpBodyY(B,ry){ /* Keep the ellipsoid centre in the air so half the volume is not dirt. */ return Math.max(B.z,ry*0.48+36); } function mfcpBounds(P){ const span=mfcpSpan(),B=P.body,rx=B.size*.52,ry=mfcpYRadius(B.size,span),rz=B.size*.40,cy=mfcpBodyY(B,ry); let x0=aoW,y0=aoH,x1=0,y1=0,hit=0; for(let ix=-1;ix<=1;ix+=2)for(let iy=-1;iy<=1;iy+=2)for(let iz=-1;iz<=1;iz+=2){ const p=mfcpProject(B.x+ix*rx,cy+iy*ry,B.y+iz*rz);if(!p)continue; x0=Math.min(x0,p[0]);x1=Math.max(x1,p[0]);y0=Math.min(y0,p[1]);y1=Math.max(y1,p[1]);hit++; } if(P.hasShadow){const S=P.shadow,r=S.size*.55;for(const q of [[-r,0],[r,0],[0,-r],[0,r]]){ const p=mfcpProject(S.x+q[0],S.z,S.y+q[1]);if(!p)continue; x0=Math.min(x0,p[0]);x1=Math.max(x1,p[0]);y0=Math.min(y0,p[1]);y1=Math.max(y1,p[1]);hit++; }} if(!hit)return null;return [Math.max(0,Math.floor(x0)-4),Math.max(0,Math.floor(y0)-4),Math.min(aoW,Math.ceil(x1)+4),Math.min(aoH,Math.ceil(y1)+4)]; } function mfCloudPostDrawPending(Sun){ if(!mfcpPending)return false; if(typeof gl==='undefined'||!gl||typeof aoW!=='number'||typeof aoH!=='number'||!aoFB2||!aoDepth||!matVP||typeof m4invert!=='function')return false; /* Quarter-res mid-span (div=4) smeared two huge proxies into one veil. Keep half-res through command zoom; only orbital drops further. */ const lod=mfcpLod,div=lod===2?4:2,maxN=lod===0?3:(lod===1?4:5); const steps=mfcpQuality==='cinematic'?(lod===0?24:(lod===1?20:14)):(lod===0?20:(lod===1?14:10)); const activeW=Math.max(1,Math.ceil(aoW/div)),activeH=Math.max(1,Math.ceil(aoH/div)); const texW=Math.max(1,Math.ceil(aoW/2)),texH=Math.max(1,Math.ceil(aoH/2)); MFCP_TEL.lastLod=lod===0?'tactical':(lod===1?'high':'orbital');MFCP_TEL.lastSteps=steps; MFCP_TEL.lastScale=1/div;MFCP_TEL.lastProxies=Math.min(mfcpProxyN,maxN); if(lod===0)MFCP_TEL.tacticalFrames++;else if(lod===1)MFCP_TEL.highFrames++;else MFCP_TEL.orbitalFrames++; const wasProg=gl.getParameter(gl.CURRENT_PROGRAM),wasVAO=gl.getParameter(gl.VERTEX_ARRAY_BINDING),wasArr=gl.getParameter(gl.ARRAY_BUFFER_BINDING); const wasRead=gl.getParameter(gl.READ_FRAMEBUFFER_BINDING),wasDraw=gl.getParameter(gl.DRAW_FRAMEBUFFER_BINDING); const wasVP=gl.getParameter(gl.VIEWPORT),wasScBox=gl.getParameter(gl.SCISSOR_BOX),wasScissor=gl.isEnabled(gl.SCISSOR_TEST); const wasActive=gl.getParameter(gl.ACTIVE_TEXTURE),wasBlend=gl.isEnabled(gl.BLEND),wasCull=gl.isEnabled(gl.CULL_FACE),wasDepth=gl.isEnabled(gl.DEPTH_TEST); const wasMask=gl.getParameter(gl.DEPTH_WRITEMASK),wasDepthFn=gl.getParameter(gl.DEPTH_FUNC),wasCullMode=gl.getParameter(gl.CULL_FACE_MODE),wasFront=gl.getParameter(gl.FRONT_FACE); const wasSrcRGB=gl.getParameter(gl.BLEND_SRC_RGB),wasDstRGB=gl.getParameter(gl.BLEND_DST_RGB),wasSrcA=gl.getParameter(gl.BLEND_SRC_ALPHA),wasDstA=gl.getParameter(gl.BLEND_DST_ALPHA); const wasEqRGB=gl.getParameter(gl.BLEND_EQUATION_RGB),wasEqA=gl.getParameter(gl.BLEND_EQUATION_ALPHA),wasColor=gl.getParameter(gl.COLOR_WRITEMASK); const savedTex=[];for(const u of [4,12,13]){gl.activeTexture(gl.TEXTURE0+u);savedTex.push(gl.getParameter(gl.TEXTURE_BINDING_2D));}gl.activeTexture(wasActive); gl.getError();let ok=false,depthDetached=false,draws=0,compX0=aoW,compY0=aoH,compX1=0,compY1=0; try{ if(!mfcpEnsurePrograms()||!mfcpEnsureTarget(texW,texH)||mfcpFailMode==='march')return false; if(!m4invert(MFCP_INV_VP,matVP))throw new Error('inverse VP failed'); gl.bindFramebuffer(gl.FRAMEBUFFER,mfcpFB);gl.viewport(0,0,texW,texH);gl.disable(gl.SCISSOR_TEST);gl.clearBufferfv(gl.COLOR,0,MFCP_ZERO4); gl.viewport(0,0,activeW,activeH);gl.disable(gl.DEPTH_TEST);gl.depthMask(false);gl.disable(gl.CULL_FACE);gl.enable(gl.BLEND); gl.blendEquation(gl.FUNC_ADD);gl.blendFunc(gl.ONE,gl.ONE_MINUS_SRC_ALPHA);gl.colorMask(true,true,true,true); gl.useProgram(mfcpProg);gl.bindVertexArray(mfcpVAO); gl.activeTexture(gl.TEXTURE4);gl.bindTexture(gl.TEXTURE_2D,aoDepth);gl.uniform1i(mfcpU.uDepth,4); gl.activeTexture(gl.TEXTURE12);gl.bindTexture(gl.TEXTURE_2D,mfcpNoise);gl.uniform1i(mfcpU.uNoise,12); const span=mfcpSpan(),closeK=mfcpCloseK(span); gl.uniformMatrix4fv(mfcpU.uInvVP,false,MFCP_INV_VP);gl.uniform2f(mfcpU.uActiveSize,activeW,activeH);gl.uniform1f(mfcpU.uTime,mfcpTime);gl.uniform1i(mfcpU.uSteps,steps); gl.uniform1f(mfcpU.uClose,closeK); const sd=Sun&&Sun.dir?Sun.dir:[.42,.78,.30];gl.uniform3f(mfcpU.uSunDir,sd[0],sd[1],sd[2]); for(let i=0;i