| 1 | /** |
| 2 | * Per-chunk fluid (water / lava) atlas: render procedural shaders at true |
| 3 | * SPRITE_SIZE_PX, then nearest-upscale the atlas to chunk world size. |
| 4 | * |
| 5 | * Tile placement is Y-down (localY), content upright (no per-tile mirrorY — |
| 6 | * that breaks world-space noise continuity / seams). FBO↔sprite V mismatch |
| 7 | * is corrected only by mirrorY on the atlas GameObject. |
| 8 | */ |
| 9 | |
| 10 | import { CHUNK_SIZE, SPRITE_SIZE_PX } from "/shared/protocol/constants.js"; |
| 11 | import { frameworkImageFromCanvas } from "./canvasSprite.js"; |
| 12 | |
| 13 | export const FLUID_ATLAS_PX = CHUNK_SIZE * SPRITE_SIZE_PX; |
| 14 | |
| 15 | /** |
| 16 | * @param {import("../../frameworkv3/framework/engine/gl/glContext.js").default | object} glCtx |
| 17 | * @returns {{ canvas: HTMLCanvasElement, tex: WebGLTexture, fb: WebGLFramebuffer, image: object, size: number } | null} |
| 18 | */ |
| 19 | export function createFluidAtlasTarget (glCtx) |
| 20 | { |
| 21 | const gl = glCtx?.gl; |
| 22 | if (!gl) |
| 23 | { return null; } |
| 24 | const size = FLUID_ATLAS_PX; |
| 25 | const canvas = document.createElement("canvas"); |
| 26 | canvas.width = size; |
| 27 | canvas.height = size; |
| 28 | |
| 29 | const tex = gl.createTexture(); |
| 30 | gl.bindTexture(gl.TEXTURE_2D, tex); |
| 31 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); |
| 32 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); |
| 33 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
| 34 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
| 35 | gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, size, size, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); |
| 36 | |
| 37 | // Seed WeakMap so Framework getTexture(canvas) returns this FBO color attachment. |
| 38 | glCtx.textures.set(canvas, tex); |
| 39 | |
| 40 | const fb = gl.createFramebuffer(); |
| 41 | gl.bindFramebuffer(gl.FRAMEBUFFER, fb); |
| 42 | gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0); |
| 43 | const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER); |
| 44 | gl.bindFramebuffer(gl.FRAMEBUFFER, null); |
| 45 | if (status !== gl.FRAMEBUFFER_COMPLETE) |
| 46 | { |
| 47 | gl.deleteFramebuffer(fb); |
| 48 | gl.deleteTexture(tex); |
| 49 | glCtx.textures.delete(canvas); |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | return { |
| 54 | canvas, |
| 55 | tex, |
| 56 | fb, |
| 57 | image: frameworkImageFromCanvas(canvas), |
| 58 | size |
| 59 | }; |
| 60 | } |
| 61 | |
| 62 | export function destroyFluidAtlasTarget (glCtx, target) |
| 63 | { |
| 64 | if (!target) |
| 65 | { return; } |
| 66 | const gl = glCtx?.gl; |
| 67 | if (gl) |
| 68 | { |
| 69 | if (target.fb) |
| 70 | { gl.deleteFramebuffer(target.fb); } |
| 71 | if (target.tex) |
| 72 | { gl.deleteTexture(target.tex); } |
| 73 | } |
| 74 | if (glCtx?.textures && target.canvas) |
| 75 | { glCtx.textures.delete(target.canvas); } |
| 76 | target.fb = null; |
| 77 | target.tex = null; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Bind atlas FBO, clear transparent, run drawFn with drawSprite matrix sized to atlas. |
| 82 | * Restores framebuffer, viewport, and _lastWidth/_lastHeight. |
| 83 | */ |
| 84 | export function withFluidAtlasPass (glCtx, target, drawFn) |
| 85 | { |
| 86 | const gl = glCtx?.gl; |
| 87 | if (!gl || !target?.fb) |
| 88 | { return; } |
| 89 | const prevFb = gl.getParameter(gl.FRAMEBUFFER_BINDING); |
| 90 | const prevViewport = gl.getParameter(gl.VIEWPORT); |
| 91 | const prevW = glCtx._lastWidth; |
| 92 | const prevH = glCtx._lastHeight; |
| 93 | const prevBlend = gl.isEnabled(gl.BLEND); |
| 94 | try |
| 95 | { |
| 96 | glCtx._lastWidth = target.size; |
| 97 | glCtx._lastHeight = target.size; |
| 98 | gl.bindFramebuffer(gl.FRAMEBUFFER, target.fb); |
| 99 | gl.viewport(0, 0, target.size, target.size); |
| 100 | // Opaque replace — blending onto clear left 1px fringes / grid seams. |
| 101 | gl.disable(gl.BLEND); |
| 102 | gl.clearColor(0, 0, 0, 0); |
| 103 | gl.clear(gl.COLOR_BUFFER_BIT); |
| 104 | drawFn(); |
| 105 | } |
| 106 | finally |
| 107 | { |
| 108 | glCtx._lastWidth = prevW; |
| 109 | glCtx._lastHeight = prevH; |
| 110 | gl.bindFramebuffer(gl.FRAMEBUFFER, prevFb); |
| 111 | gl.viewport(prevViewport[0], prevViewport[1], prevViewport[2], prevViewport[3]); |
| 112 | if (!prevBlend) |
| 113 | { gl.disable(gl.BLEND); } |
| 114 | gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** Pixel-space center of a chunk-local tile inside the FBO (Y-down, same as world). */ |
| 119 | export function fluidAtlasTileCenter (localX, localY) |
| 120 | { |
| 121 | return { |
| 122 | x: localX * SPRITE_SIZE_PX + SPRITE_SIZE_PX / 2, |
| 123 | y: localY * SPRITE_SIZE_PX + SPRITE_SIZE_PX / 2 |
| 124 | }; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Draw one fluid tile into the currently bound atlas pass. |
| 129 | * @param {object} glCtx |
| 130 | * @param {{ |
| 131 | * textureHtml: TexImageSource, |
| 132 | * secondaryHtml?: TexImageSource | null, |
| 133 | * localX: number, |
| 134 | * localY: number, |
| 135 | * shader: object, |
| 136 | * uniforms: object, |
| 137 | * uvRect?: { u0: number, v0: number, u1: number, v1: number } | null |
| 138 | * }} opts |
| 139 | */ |
| 140 | export function drawFluidAtlasTile (glCtx, { |
| 141 | textureHtml, |
| 142 | secondaryHtml = null, |
| 143 | localX, |
| 144 | localY, |
| 145 | shader, |
| 146 | uniforms, |
| 147 | uvRect = null |
| 148 | }) |
| 149 | { |
| 150 | if (!textureHtml || !shader) |
| 151 | { return; } |
| 152 | const center = fluidAtlasTileCenter(localX, localY); |
| 153 | const textures = secondaryHtml |
| 154 | ? { u_secondary: glCtx.getTexture(secondaryHtml) } |
| 155 | : null; |
| 156 | glCtx.drawSprite({ |
| 157 | texture: glCtx.getTexture(textureHtml), |
| 158 | x: center.x, |
| 159 | y: center.y, |
| 160 | w: SPRITE_SIZE_PX, |
| 161 | h: SPRITE_SIZE_PX, |
| 162 | rotationDeg: 0, |
| 163 | mirrorX: false, |
| 164 | // Do not mirrorY per tile — inverts puv vs u_tileY and seams the FBM field. |
| 165 | mirrorY: false, |
| 166 | alpha: 1, |
| 167 | uvRect: uvRect ?? { u0: 0, v0: 0, u1: 1, v1: 1 }, |
| 168 | shader, |
| 169 | uniforms, |
| 170 | textures, |
| 171 | lights: null |
| 172 | }); |
| 173 | } |