/** * Per-chunk fluid (water / lava) atlas: render procedural shaders at true * SPRITE_SIZE_PX, then nearest-upscale the atlas to chunk world size. * * Tile placement is Y-down (localY), content upright (no per-tile mirrorY — * that breaks world-space noise continuity / seams). FBO↔sprite V mismatch * is corrected only by mirrorY on the atlas GameObject. * * Single-pass bake packs per-tile uniforms into 16×16 RGBA textures and draws * the full atlas with one (or two, water+lava) chunk shader pass. */ import { CHUNK_SIZE, SPRITE_SIZE_PX } from "/shared/protocol/constants.js"; import { frameworkImageFromCanvas } from "./canvasSprite.js"; export const FLUID_ATLAS_PX = CHUNK_SIZE * SPRITE_SIZE_PX; export const FLUID_PACK_SIZE = CHUNK_SIZE; /** Kind bytes stored in pack mask.R (0 = empty). */ export const FLUID_KIND_WATER = 1; export const FLUID_KIND_LAVA = 2; /** * @param {import("../../frameworkv3/framework/engine/gl/glContext.js").default | object} glCtx * @returns {{ canvas: HTMLCanvasElement, tex: WebGLTexture, fb: WebGLFramebuffer, image: object, size: number } | null} */ export function createFluidAtlasTarget (glCtx) { const gl = glCtx?.gl; if (!gl) { return null; } const size = FLUID_ATLAS_PX; const canvas = document.createElement("canvas"); canvas.width = size; canvas.height = size; const tex = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, tex); 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); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, size, size, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); // Seed WeakMap so Framework getTexture(canvas) returns this FBO color attachment. glCtx.textures.set(canvas, tex); const fb = gl.createFramebuffer(); gl.bindFramebuffer(gl.FRAMEBUFFER, fb); gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0); const status = gl.checkFramebufferStatus(gl.FRAMEBUFFER); gl.bindFramebuffer(gl.FRAMEBUFFER, null); if (status !== gl.FRAMEBUFFER_COMPLETE) { gl.deleteFramebuffer(fb); gl.deleteTexture(tex); glCtx.textures.delete(canvas); return null; } return { canvas, tex, fb, image: frameworkImageFromCanvas(canvas), size }; } export function destroyFluidAtlasTarget (glCtx, target) { if (!target) { return; } const gl = glCtx?.gl; if (gl) { if (target.fb) { gl.deleteFramebuffer(target.fb); } if (target.tex) { gl.deleteTexture(target.tex); } } if (glCtx?.textures && target.canvas) { glCtx.textures.delete(target.canvas); } target.fb = null; target.tex = null; } /** * Bind atlas FBO, clear transparent, run drawFn with drawSprite matrix sized to atlas. * Restores framebuffer, viewport, and _lastWidth/_lastHeight. */ export function withFluidAtlasPass (glCtx, target, drawFn) { const gl = glCtx?.gl; if (!gl || !target?.fb) { return; } const prevFb = gl.getParameter(gl.FRAMEBUFFER_BINDING); const prevViewport = gl.getParameter(gl.VIEWPORT); const prevW = glCtx._lastWidth; const prevH = glCtx._lastHeight; const prevBlend = gl.isEnabled(gl.BLEND); try { glCtx._lastWidth = target.size; glCtx._lastHeight = target.size; gl.bindFramebuffer(gl.FRAMEBUFFER, target.fb); gl.viewport(0, 0, target.size, target.size); // Opaque replace — blending onto clear left 1px fringes / grid seams. gl.disable(gl.BLEND); gl.clearColor(0, 0, 0, 0); gl.clear(gl.COLOR_BUFFER_BIT); drawFn(); } finally { glCtx._lastWidth = prevW; glCtx._lastHeight = prevH; gl.bindFramebuffer(gl.FRAMEBUFFER, prevFb); gl.viewport(prevViewport[0], prevViewport[1], prevViewport[2], prevViewport[3]); if (!prevBlend) { gl.disable(gl.BLEND); } else { gl.enable(gl.BLEND); } gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); } } /** Pixel-space center of a chunk-local tile inside the FBO (Y-down, same as world). */ export function fluidAtlasTileCenter (localX, localY) { return { x: localX * SPRITE_SIZE_PX + SPRITE_SIZE_PX / 2, y: localY * SPRITE_SIZE_PX + SPRITE_SIZE_PX / 2 }; } /** * Draw one fluid tile into the currently bound atlas pass. * @param {object} glCtx * @param {{ * textureHtml: TexImageSource, * secondaryHtml?: TexImageSource | null, * localX: number, * localY: number, * shader: object, * uniforms: object, * uvRect?: { u0: number, v0: number, u1: number, v1: number } | null * }} opts */ export function drawFluidAtlasTile (glCtx, { textureHtml, secondaryHtml = null, localX, localY, shader, uniforms, uvRect = null }) { if (!textureHtml || !shader) { return; } const center = fluidAtlasTileCenter(localX, localY); const textures = secondaryHtml ? { u_secondary: glCtx.getTexture(secondaryHtml) } : null; glCtx.drawSprite({ texture: glCtx.getTexture(textureHtml), x: center.x, y: center.y, w: SPRITE_SIZE_PX, h: SPRITE_SIZE_PX, rotationDeg: 0, mirrorX: false, // Do not mirrorY per tile — inverts puv vs u_tileY and seams the FBM field. mirrorY: false, alpha: 1, uvRect: uvRect ?? { u0: 0, v0: 0, u1: 1, v1: 1 }, shader, uniforms, textures, lights: null }); } function createPackTexture (gl) { const tex = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, tex); 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); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); gl.texImage2D( gl.TEXTURE_2D, 0, gl.RGBA, FLUID_PACK_SIZE, FLUID_PACK_SIZE, 0, gl.RGBA, gl.UNSIGNED_BYTE, null ); return tex; } /** * CPU+GPU pack set for one chunk (5 × 16×16 RGBA8). * @returns {{ * mask: Uint8Array, shore: Uint8Array, lit: Uint8Array, deep: Uint8Array, shoreUv: Uint8Array, * texMask: WebGLTexture, texShore: WebGLTexture, texLit: WebGLTexture, texDeep: WebGLTexture, texShoreUv: WebGLTexture * } | null} */ export function createFluidPackSet (glCtx) { const gl = glCtx?.gl; if (!gl) { return null; } const n = FLUID_PACK_SIZE * FLUID_PACK_SIZE * 4; return { mask: new Uint8Array(n), shore: new Uint8Array(n), lit: new Uint8Array(n), deep: new Uint8Array(n), shoreUv: new Uint8Array(n), texMask: createPackTexture(gl), texShore: createPackTexture(gl), texLit: createPackTexture(gl), texDeep: createPackTexture(gl), texShoreUv: createPackTexture(gl) }; } export function destroyFluidPackSet (glCtx, packs) { if (!packs) { return; } const gl = glCtx?.gl; if (gl) { for (const key of ["texMask", "texShore", "texLit", "texDeep", "texShoreUv"]) { if (packs[key]) { gl.deleteTexture(packs[key]); } packs[key] = null; } } } export function clearFluidPackPixels (packs) { if (!packs) { return; } packs.mask.fill(0); packs.shore.fill(0); packs.lit.fill(0); packs.deep.fill(0); packs.shoreUv.fill(0); } function packBits4 (a, b, c, d) { return ((a > 0.5 ? 1 : 0) | ((b > 0.5 ? 1 : 0) << 1) | ((c > 0.5 ? 1 : 0) << 2) | ((d > 0.5 ? 1 : 0) << 3)) & 0xff; } function u8 (v) { return Math.max(0, Math.min(255, (v * 255 + 0.5) | 0)); } /** * Write one tile's uniforms into pack CPU buffers. * @param {object} packs * @param {number} localX * @param {number} localY * @param {number} kind FLUID_KIND_WATER | FLUID_KIND_LAVA * @param {object} uniforms tile shaderUniforms */ export function writeFluidPackTile (packs, localX, localY, kind, uniforms) { if (!packs || !uniforms) { return; } const lx = localX | 0; const ly = localY | 0; if (lx < 0 || ly < 0 || lx >= FLUID_PACK_SIZE || ly >= FLUID_PACK_SIZE) { return; } const i = (ly * FLUID_PACK_SIZE + lx) * 4; const u = uniforms; packs.mask[i] = kind | 0; packs.mask[i + 1] = packBits4(u.u_edgeN, u.u_edgeE, u.u_edgeS, u.u_edgeW); packs.mask[i + 2] = packBits4(u.u_edgeNE, u.u_edgeNW, u.u_edgeSE, u.u_edgeSW); packs.mask[i + 3] = packBits4(u.u_shoreEdgeN, u.u_shoreEdgeE, u.u_shoreEdgeS, u.u_shoreEdgeW); packs.shore[i] = packBits4(u.u_shoreEdgeNE, u.u_shoreEdgeNW, u.u_shoreEdgeSE, u.u_shoreEdgeSW); packs.shore[i + 1] = (u.u_hasShore > 0.5) ? 255 : 0; packs.shore[i + 2] = 0; packs.shore[i + 3] = 0; packs.lit[i] = u8(u.u_litNW ?? 1); packs.lit[i + 1] = u8(u.u_litNE ?? 1); packs.lit[i + 2] = u8(u.u_litSW ?? 1); packs.lit[i + 3] = u8(u.u_litSE ?? 1); packs.deep[i] = u8(u.u_deepNW ?? 0); packs.deep[i + 1] = u8(u.u_deepNE ?? 0); packs.deep[i + 2] = u8(u.u_deepSW ?? 0); packs.deep[i + 3] = u8(u.u_deepSE ?? 0); const shoreUv = u.u_shoreUvRect; const u0 = Array.isArray(shoreUv) ? (shoreUv[0] ?? 0) : 0; const v0 = Array.isArray(shoreUv) ? (shoreUv[1] ?? 0) : 0; const u1 = Array.isArray(shoreUv) ? (shoreUv[2] ?? 1) : 1; const v1 = Array.isArray(shoreUv) ? (shoreUv[3] ?? 1) : 1; packs.shoreUv[i] = u8(u0); packs.shoreUv[i + 1] = u8(v0); packs.shoreUv[i + 2] = u8(u1); packs.shoreUv[i + 3] = u8(v1); } function uploadPack (gl, tex, pixels) { gl.bindTexture(gl.TEXTURE_2D, tex); gl.texSubImage2D( gl.TEXTURE_2D, 0, 0, 0, FLUID_PACK_SIZE, FLUID_PACK_SIZE, gl.RGBA, gl.UNSIGNED_BYTE, pixels ); } export function uploadFluidPacks (glCtx, packs) { const gl = glCtx?.gl; if (!gl || !packs) { return; } uploadPack(gl, packs.texMask, packs.mask); uploadPack(gl, packs.texShore, packs.shore); uploadPack(gl, packs.texLit, packs.lit); uploadPack(gl, packs.texDeep, packs.deep); uploadPack(gl, packs.texShoreUv, packs.shoreUv); } /** * One full-atlas draw for a fluid kind (water or lava ground, or lava bloom). * Must run inside withFluidAtlasPass (FBO already bound/cleared as needed). */ export function drawFluidAtlasChunkPass (glCtx, { textureHtml, secondaryHtml = null, shader, uniforms, packs, kindFilter, includeShorePacks = true }) { if (!textureHtml || !shader || !packs) { return; } const textures = { u_tileMask: packs.texMask }; if (includeShorePacks) { textures.u_tileShore = packs.texShore; textures.u_tileLit = packs.texLit; textures.u_tileDeep = packs.texDeep; textures.u_tileShoreUv = packs.texShoreUv; textures.u_secondary = glCtx.getTexture(secondaryHtml || textureHtml); } const half = FLUID_ATLAS_PX / 2; glCtx.drawSprite({ texture: glCtx.getTexture(textureHtml), x: half, y: half, w: FLUID_ATLAS_PX, h: FLUID_ATLAS_PX, rotationDeg: 0, mirrorX: false, mirrorY: false, alpha: 1, uvRect: { u0: 0, v0: 0, u1: 1, v1: 1 }, shader, uniforms: { ...uniforms, u_kindFilter: kindFilter, u_atlasPx: FLUID_ATLAS_PX, u_tilePx: SPRITE_SIZE_PX }, textures, lights: null }); }