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