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