From 824cae072fd5444f17739a28e8decf080c978a4f Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 00:10:04 +0000 Subject: [PATCH 01/11] Tests: the MaskEffect drawing checks run on a WebGPU device On a machine with a WebGPU adapter, 14 tests of mask-effect-image.spec.js failed: the pixel helper's WebGPU branch called a method the renderer does not have, and the WebGPU application made in the same file took the place of the Canvas one the later tests use. - helpers/pixels.js: grabFrame() reads a whole frame on any backend (a texture-to-buffer copy on WebGPU), and drawOffscreen() has a WebGPU renderer draw into a texture of its own, which reads back the same on a headless software adapter, where a canvas texture cannot be presented - the drawing checks move to helpers/mask-effect-checks.js, and take each frame once, as it is drawn: an application's loop draws over it while a test awaits - the WebGPU checks get a spec file of their own, with a projection set Verified with a software WebGPU adapter: the 12 WebGPU checks pass, and skip as before where there is no adapter. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01JnzwA4a7ZyjLBpbeEpFLu6 --- .../tests/helpers/mask-effect-checks.js | 271 ++++++++++++++++ packages/melonjs/tests/helpers/pixels.js | 121 +++++++- .../tests/mask-effect-image-webgpu.spec.js | 67 ++++ .../melonjs/tests/mask-effect-image.spec.js | 288 +----------------- 4 files changed, 467 insertions(+), 280 deletions(-) create mode 100644 packages/melonjs/tests/helpers/mask-effect-checks.js create mode 100644 packages/melonjs/tests/mask-effect-image-webgpu.spec.js diff --git a/packages/melonjs/tests/helpers/mask-effect-checks.js b/packages/melonjs/tests/helpers/mask-effect-checks.js new file mode 100644 index 0000000000..335e83fbcb --- /dev/null +++ b/packages/melonjs/tests/helpers/mask-effect-checks.js @@ -0,0 +1,271 @@ +/** + * What the `MaskEffect` drawing tests share, so that each renderer can run + * them in a spec file of its own: an application with a GPU device takes the + * place of the one before it, and the checks below only need a renderer. + */ +import { expect, it, vi } from "vitest"; +import { Camera2d, Ellipse, MaskEffect, Polygon } from "../../src/index.js"; +import { grabFrame } from "./pixels.js"; + +export const SIZE = 64; + +/** a 32 px disc, radius 14, with a hole of radius 4 at its centre */ +export const discWithHole = () => { + const canvas = document.createElement("canvas"); + canvas.width = canvas.height = 32; + const ctx = canvas.getContext("2d"); + ctx.fillStyle = "#3a7"; + ctx.beginPath(); + ctx.arc(16, 16, 14, 0, Math.PI * 2); + ctx.fill(); + ctx.globalCompositeOperation = "destination-out"; + ctx.beginPath(); + ctx.arc(16, 16, 4, 0, Math.PI * 2); + ctx.fill(); + return canvas; +}; + +/** an image whose left half only is opaque */ +export const leftHalf = (w = 20, h = 30) => { + const canvas = document.createElement("canvas"); + canvas.width = w; + canvas.height = h; + const ctx = canvas.getContext("2d"); + ctx.fillStyle = "#f00"; + ctx.fillRect(0, 0, w / 2, h); + return canvas; +}; + +/** a 32 px U: its middle is open to the outside, so not part of it */ +export const uShape = () => { + const canvas = document.createElement("canvas"); + canvas.width = canvas.height = 32; + const ctx = canvas.getContext("2d"); + ctx.fillStyle = "#000"; + ctx.fillRect(2, 2, 8, 28); + ctx.fillRect(22, 2, 8, 28); + ctx.fillRect(2, 22, 28, 8); + return canvas; +}; + +/** an image whose top left quarter only is opaque */ +export const topLeftQuarter = (w = 20, h = 30) => { + const canvas = document.createElement("canvas"); + canvas.width = w; + canvas.height = h; + const ctx = canvas.getContext("2d"); + ctx.fillStyle = "#0f0"; + ctx.fillRect(0, 0, w / 2, h / 2); + return canvas; +}; + +/** a unit square that does not surround its origin */ +export const offOrigin = () => { + return new Polygon(0, 0, [ + { x: 0.2, y: 0.2 }, + { x: 1, y: 0.2 }, + { x: 1, y: 1 }, + { x: 0.2, y: 1 }, + ]); +}; + +/** an effect at a given progress (1 = all visible, 0 = all covered) */ +export const at = (camera, progress, options) => { + const effect = new MaskEffect(camera, { + color: "#000", + duration: 1000, + direction: "hide", + ...options, + }); + effect.tween.stop(); + effect.progress.value = progress; + return effect; +}; + +/** + * Draw an effect over a white screen, and return the frame. The frame is + * taken at once, in one piece: an application's own loop draws its next + * frame while a test awaits, over the one a pixel would be read from. + */ +export const drawOverWhite = (renderer, effect) => { + // a frame of its own: a GPU renderer draws nothing outside one + renderer.clear(); + renderer.save(); + renderer.resetTransform(); + renderer.setColor("#ffffff"); + renderer.fillRect(0, 0, SIZE, SIZE); + renderer.restore(); + effect.draw(renderer, SIZE, SIZE); + return grabFrame(renderer); +}; + +/** 1 where a pixel of a frame is covered (dark), 0 where it is visible (light) */ +export const covered = (frame, x, y) => { + return frame.data[(y * frame.width + x) * 4] < 128 ? 1 : 0; +}; + +export const CORNERS = [ + [1, 1], + [SIZE - 2, 1], + [1, SIZE - 2], + [SIZE - 2, SIZE - 2], +]; + +/** the same drawing checks, on one renderer */ +export const drawingChecks = (getRenderer) => { + const camera = new Camera2d(0, 0, SIZE, SIZE); + + it("an image: the scene shows through its silhouette, hole filled in", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const effect = at(camera, 0.3, { shape: discWithHole() }); + frame = await drawOverWhite(renderer, effect); + // the centre is the image's hole: part of the silhouette, so visible + expect(covered(frame, 32, 32)).toBe(0); + expect(covered(frame, 32, 26)).toBe(0); + for (const [x, y] of CORNERS) { + expect(covered(frame, x, y)).toBe(1); + } + effect.destroy(); + }); + + it("an image: covers everything once closed", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const effect = at(camera, 0, { shape: discWithHole() }); + frame = await drawOverWhite(renderer, effect); + expect(covered(frame, 32, 32)).toBe(1); + expect(covered(frame, 1, 1)).toBe(1); + effect.destroy(); + }); + + it("an image: just before the end, the whole screen is already uncovered", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const effect = at(camera, 0.995, { shape: discWithHole() }); + frame = await drawOverWhite(renderer, effect); + for (const [x, y] of CORNERS) { + expect(covered(frame, x, y)).toBe(0); + } + effect.destroy(); + }); + + it("a polygon: just before the end, the whole screen is already uncovered", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const diamond = new Polygon(0, 0, [ + { x: 0, y: -1 }, + { x: 1, y: 0 }, + { x: 0, y: 1 }, + { x: -1, y: 0 }, + ]); + const effect = at(camera, 0.995, { shape: diamond }); + frame = await drawOverWhite(renderer, effect); + for (const [x, y] of CORNERS) { + expect(covered(frame, x, y)).toBe(0); + } + effect.destroy(); + }); + + it("inverted: the silhouette is the cover, the scene shows round it", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const effect = at(camera, 0.7, { shape: discWithHole(), invert: true }); + frame = await drawOverWhite(renderer, effect); + expect(covered(frame, 32, 32)).toBe(1); + for (const [x, y] of CORNERS) { + expect(covered(frame, x, y)).toBe(0); + } + effect.destroy(); + }); + + it("inverted: covers everything once closed, nothing at the start", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const closed = at(camera, 0, { shape: discWithHole(), invert: true }); + frame = await drawOverWhite(renderer, closed); + expect(covered(frame, 1, 1)).toBe(1); + expect(covered(frame, 32, 32)).toBe(1); + closed.destroy(); + const open = at(camera, 1, { shape: discWithHole(), invert: true }); + frame = await drawOverWhite(renderer, open); + expect(covered(frame, 32, 32)).toBe(0); + open.destroy(); + }); + + it("inverted geometry: an iris filled, growing out of the centre", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const effect = at(camera, 0.7, { + shape: new Ellipse(0, 0, 1, 1), + invert: true, + }); + frame = await drawOverWhite(renderer, effect); + expect(covered(frame, 32, 32)).toBe(1); + expect(covered(frame, 1, 1)).toBe(0); + effect.destroy(); + }); + + it("an image open in its middle (a U): grows about its thickest part, and still uncovers everything", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + // just after the hide starts, the middle of the screen is still seen + const start = at(camera, 0.99, { shape: uShape() }); + frame = await drawOverWhite(renderer, start); + expect(covered(frame, 32, 32)).toBe(0); + start.destroy(); + // and just before a reveal ends, nothing is covered + const end = at(camera, 0.995, { shape: uShape() }); + frame = await drawOverWhite(renderer, end); + for (const [x, y] of [...CORNERS, [32, 32], [32, 3]]) { + expect(covered(frame, x, y)).toBe(0); + } + end.destroy(); + }); + + it("a polygon off its origin: still uncovers everything, and inverted still covers it", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const open = at(camera, 0.995, { shape: offOrigin() }); + frame = await drawOverWhite(renderer, open); + for (const [x, y] of CORNERS) { + expect(covered(frame, x, y)).toBe(0); + } + open.destroy(); + // the cover nearly grown: the screen is covered by the polygon itself + const cover = at(camera, 0.005, { shape: offOrigin(), invert: true }); + frame = await drawOverWhite(renderer, cover); + for (const [x, y] of [...CORNERS, [32, 32]]) { + expect(covered(frame, x, y)).toBe(1); + } + cover.destroy(); + }); + + it("nearly closed, the image is still drawn, a sliver of the scene in the middle", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const effect = at(camera, 0.05, { shape: discWithHole() }); + const drawImage = vi.spyOn(renderer, "drawImage"); + frame = await drawOverWhite(renderer, effect); + expect(drawImage).toHaveBeenCalled(); + drawImage.mockRestore(); + expect(covered(frame, 32, 32)).toBe(0); + for (const [x, y] of [...CORNERS, [32, 20], [44, 32]]) { + expect(covered(frame, x, y)).toBe(1); + } + effect.destroy(); + }); + + it("center: closes on that point, not on the centre of the screen", async (ctx) => { + const renderer = getRenderer(ctx); + let frame; + const effect = at(camera, 0.2, { + shape: discWithHole(), + center: { x: 12, y: 12 }, + }); + frame = await drawOverWhite(renderer, effect); + expect(covered(frame, 12, 12)).toBe(0); + expect(covered(frame, 52, 52)).toBe(1); + effect.destroy(); + }); +}; diff --git a/packages/melonjs/tests/helpers/pixels.js b/packages/melonjs/tests/helpers/pixels.js index 31aa566198..5a27f80540 100644 --- a/packages/melonjs/tests/helpers/pixels.js +++ b/packages/melonjs/tests/helpers/pixels.js @@ -43,10 +43,12 @@ export async function readPixel(renderer, x, y) { return Array.from(out); } - // WebGPU: async readback off the presented texture - if (typeof renderer.readCanvasPixels === "function") { - const data = await renderer.readCanvasPixels(px, py, 1, 1); - return Array.from(data.data ?? data).slice(0, 4); + // WebGPU: its canvas has no synchronous readback, but still holds the + // frame just submitted until this task ends + if (renderer.type === "WebGPU") { + const frame = await grabFrame(renderer); + const o = (py * frame.width + px) * 4; + return Array.from(frame.data.subarray(o, o + 4)); } // Canvas 2D: top-down rows already @@ -55,6 +57,117 @@ export async function readPixel(renderer, x, y) { return Array.from(ctx.getImageData(px, py, 1, 1).data); } +/** + * Have a WebGPU renderer draw its frames into a texture of its own, in place + * of its canvas's, and return that renderer. + * + * A test reads a frame back from the texture it was drawn into (see + * {@link grabFrame}), and a canvas's texture is only there until the browser + * presents it. Presenting is also what a headless browser on a software + * adapter cannot do: the device is lost the moment a canvas texture is + * asked for. Drawn into a plain texture, the renderer's own work (its + * pipelines, shaders and passes) runs and reads back the same everywhere. + * + * Call it right after `app.init()`, before the first frame. A renderer that + * is not WebGPU is returned as it is. + * @param {object} renderer - any melonJS renderer + * @returns {object} the renderer + */ +export function drawOffscreen(renderer) { + if (renderer.type !== "WebGPU" || renderer.context.__offscreen === true) { + return renderer; + } + const canvas = renderer.getCanvas(); + const context = renderer.context; + const format = + context.getConfiguration?.()?.format ?? + navigator.gpu.getPreferredCanvasFormat(); + let texture; + context.getCurrentTexture = () => { + if ( + texture === undefined || + texture.width !== canvas.width || + texture.height !== canvas.height + ) { + texture?.destroy(); + texture = renderer.device.createTexture({ + label: "test frame", + size: [canvas.width, canvas.height], + format, + usage: + GPUTextureUsage.RENDER_ATTACHMENT | + GPUTextureUsage.COPY_SRC | + GPUTextureUsage.TEXTURE_BINDING, + }); + } + return texture; + }; + context.__offscreen = true; + return renderer; +} + +/** + * The whole frame a renderer has just drawn, as straight-alpha `ImageData`, + * rows top-down, on any backend. + * + * To be called in the task that drew the frame, before anything is awaited: + * a GPU canvas only holds its frame until the browser presents it. On WebGPU + * the frame must have been begun with `renderer.clear()`; it is submitted + * here, and its texture copied into a buffer before this returns its promise + * (see {@link drawOffscreen}, which a test on WebGPU wants first). + * @param {object} renderer - any melonJS renderer + * @returns {Promise} the frame + */ +export async function grabFrame(renderer) { + renderer.flush?.(); + const source = renderer.getCanvas(); + const width = source.width; + const height = source.height; + + if (renderer.type === "WebGPU") { + // the canvas texture of this frame, still current until presented + const device = renderer.device; + const texture = renderer.context.getCurrentTexture(); + const bytesPerRow = Math.ceil((width * 4) / 256) * 256; + const buffer = device.createBuffer({ + size: bytesPerRow * height, + usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ, + }); + const encoder = device.createCommandEncoder(); + encoder.copyTextureToBuffer( + { texture }, + { buffer, bytesPerRow }, + { width, height }, + ); + device.queue.submit([encoder.finish()]); + await buffer.mapAsync(GPUMapMode.READ); + const raw = new Uint8Array(buffer.getMappedRange()); + const bgra = texture.format.startsWith("bgra"); + const out = new Uint8ClampedArray(width * height * 4); + for (let y = 0; y < height; y++) { + for (let x = 0; x < width; x++) { + const i = y * bytesPerRow + x * 4; + const o = (y * width + x) * 4; + out[o] = raw[i + (bgra ? 2 : 0)]; + out[o + 1] = raw[i + 1]; + out[o + 2] = raw[i + (bgra ? 0 : 2)]; + out[o + 3] = raw[i + 3]; + } + } + buffer.unmap(); + buffer.destroy(); + return new ImageData(out, width, height); + } + + // WebGL and Canvas: copied through a 2D canvas, while the frame is there + const canvas = document.createElement("canvas"); + canvas.width = width; + canvas.height = height; + const context = canvas.getContext("2d", { willReadFrequently: true }); + context.drawImage(source, 0, 0); + return context.getImageData(0, 0, width, height); +} + /** * Read the same pixel from several renderers at once, in canvas coordinates. * @param {object[]} renderers - the renderers to sample diff --git a/packages/melonjs/tests/mask-effect-image-webgpu.spec.js b/packages/melonjs/tests/mask-effect-image-webgpu.spec.js new file mode 100644 index 0000000000..68aa4eb03e --- /dev/null +++ b/packages/melonjs/tests/mask-effect-image-webgpu.spec.js @@ -0,0 +1,67 @@ +/** + * The `MaskEffect` drawing checks (#1399) on WebGPU, in a spec file of its + * own: an application with a GPU device takes the place of the one before + * it, which the Canvas and WebGL checks of `mask-effect-image.spec.js` use. + * + * Skipped, visibly, where the browser has no WebGPU adapter. + */ +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { Application, boot, Camera2d, video } from "../src/index.js"; +import { + at, + discWithHole, + drawingChecks, + drawOverWhite, + SIZE, +} from "./helpers/mask-effect-checks.js"; +import { drawOffscreen } from "./helpers/pixels.js"; + +describe("MaskEffect: image and sprite shapes, on WebGPU", () => { + let app; + let available = false; + beforeAll(async () => { + boot(); + try { + app = new Application(SIZE, SIZE, { + parent: "screen", + renderer: video.WEBGPU, + consoleHeader: false, + }); + await app.init(); + available = app.renderer.type === "WebGPU"; + if (available) { + const renderer = drawOffscreen(app.renderer); + // the checks draw without a camera, which is what sets the + // projection in a game + renderer.projectionMatrix.ortho(0, SIZE, SIZE, 0, -1000, 1000); + renderer.setProjection(renderer.projectionMatrix); + } + } catch { + available = false; + } + }); + afterAll(() => { + app?.destroy(); + }); + + const getRenderer = (ctx) => { + if (!available) { + ctx.skip("WebGPU not available in this environment"); + } + return app.renderer; + }; + + drawingChecks(getRenderer); + + it("destroy frees the copy of the image uploaded to the GPU", async (ctx) => { + const renderer = getRenderer(ctx); + const effect = at(new Camera2d(0, 0, SIZE, SIZE), 0.5, { + shape: discWithHole(), + }); + await drawOverWhite(renderer, effect); + const canvas = effect._cover.canvas; + expect(renderer.cache.has(canvas)).toBe(true); + effect.destroy(); + expect(renderer.cache.has(canvas)).toBe(false); + }); +}); diff --git a/packages/melonjs/tests/mask-effect-image.spec.js b/packages/melonjs/tests/mask-effect-image.spec.js index e1f8cafd6e..be070555de 100644 --- a/packages/melonjs/tests/mask-effect-image.spec.js +++ b/packages/melonjs/tests/mask-effect-image.spec.js @@ -32,258 +32,21 @@ import { video, } from "../src/index.js"; import GLTFScene from "../src/level/gltf/GLTFScene.js"; -import { readPixel } from "./helpers/pixels.js"; +import { + at, + discWithHole, + drawingChecks, + drawOverWhite, + leftHalf, + offOrigin, + SIZE, + topLeftQuarter, +} from "./helpers/mask-effect-checks.js"; import { getWebGLRenderer, releaseWebGLRenderer, } from "./helpers/webgl-context.js"; -const SIZE = 64; - -/** a 32 px disc, radius 14, with a hole of radius 4 at its centre */ -const discWithHole = () => { - const canvas = document.createElement("canvas"); - canvas.width = canvas.height = 32; - const ctx = canvas.getContext("2d"); - ctx.fillStyle = "#3a7"; - ctx.beginPath(); - ctx.arc(16, 16, 14, 0, Math.PI * 2); - ctx.fill(); - ctx.globalCompositeOperation = "destination-out"; - ctx.beginPath(); - ctx.arc(16, 16, 4, 0, Math.PI * 2); - ctx.fill(); - return canvas; -}; - -/** an image whose left half only is opaque */ -const leftHalf = (w = 20, h = 30) => { - const canvas = document.createElement("canvas"); - canvas.width = w; - canvas.height = h; - const ctx = canvas.getContext("2d"); - ctx.fillStyle = "#f00"; - ctx.fillRect(0, 0, w / 2, h); - return canvas; -}; - -/** a 32 px U: its middle is open to the outside, so not part of it */ -const uShape = () => { - const canvas = document.createElement("canvas"); - canvas.width = canvas.height = 32; - const ctx = canvas.getContext("2d"); - ctx.fillStyle = "#000"; - ctx.fillRect(2, 2, 8, 28); - ctx.fillRect(22, 2, 8, 28); - ctx.fillRect(2, 22, 28, 8); - return canvas; -}; - -/** an image whose top left quarter only is opaque */ -const topLeftQuarter = (w = 20, h = 30) => { - const canvas = document.createElement("canvas"); - canvas.width = w; - canvas.height = h; - const ctx = canvas.getContext("2d"); - ctx.fillStyle = "#0f0"; - ctx.fillRect(0, 0, w / 2, h / 2); - return canvas; -}; - -/** a unit square that does not surround its origin */ -const offOrigin = () => { - return new Polygon(0, 0, [ - { x: 0.2, y: 0.2 }, - { x: 1, y: 0.2 }, - { x: 1, y: 1 }, - { x: 0.2, y: 1 }, - ]); -}; - -/** an effect at a given progress (1 = all visible, 0 = all covered) */ -const at = (camera, progress, options) => { - const effect = new MaskEffect(camera, { - color: "#000", - duration: 1000, - direction: "hide", - ...options, - }); - effect.tween.stop(); - effect.progress.value = progress; - return effect; -}; - -/** draw an effect over a white screen */ -const drawOverWhite = (renderer, effect) => { - renderer.save(); - renderer.resetTransform(); - renderer.setColor("#ffffff"); - renderer.fillRect(0, 0, SIZE, SIZE); - renderer.restore(); - effect.draw(renderer, SIZE, SIZE); - renderer.flush(); -}; - -/** 1 where a pixel is covered (dark), 0 where it is visible (light) */ -const covered = async (renderer, x, y) => { - return (await readPixel(renderer, x, y))[0] < 128 ? 1 : 0; -}; - -const CORNERS = [ - [1, 1], - [SIZE - 2, 1], - [1, SIZE - 2], - [SIZE - 2, SIZE - 2], -]; - -/** the same drawing checks, on one renderer */ -const drawingChecks = (getRenderer) => { - const camera = new Camera2d(0, 0, SIZE, SIZE); - - it("an image: the scene shows through its silhouette, hole filled in", async (ctx) => { - const renderer = getRenderer(ctx); - const effect = at(camera, 0.3, { shape: discWithHole() }); - drawOverWhite(renderer, effect); - // the centre is the image's hole: part of the silhouette, so visible - expect(await covered(renderer, 32, 32)).toBe(0); - expect(await covered(renderer, 32, 26)).toBe(0); - for (const [x, y] of CORNERS) { - expect(await covered(renderer, x, y)).toBe(1); - } - effect.destroy(); - }); - - it("an image: covers everything once closed", async (ctx) => { - const renderer = getRenderer(ctx); - const effect = at(camera, 0, { shape: discWithHole() }); - drawOverWhite(renderer, effect); - expect(await covered(renderer, 32, 32)).toBe(1); - expect(await covered(renderer, 1, 1)).toBe(1); - effect.destroy(); - }); - - it("an image: just before the end, the whole screen is already uncovered", async (ctx) => { - const renderer = getRenderer(ctx); - const effect = at(camera, 0.995, { shape: discWithHole() }); - drawOverWhite(renderer, effect); - for (const [x, y] of CORNERS) { - expect(await covered(renderer, x, y)).toBe(0); - } - effect.destroy(); - }); - - it("a polygon: just before the end, the whole screen is already uncovered", async (ctx) => { - const renderer = getRenderer(ctx); - const diamond = new Polygon(0, 0, [ - { x: 0, y: -1 }, - { x: 1, y: 0 }, - { x: 0, y: 1 }, - { x: -1, y: 0 }, - ]); - const effect = at(camera, 0.995, { shape: diamond }); - drawOverWhite(renderer, effect); - for (const [x, y] of CORNERS) { - expect(await covered(renderer, x, y)).toBe(0); - } - effect.destroy(); - }); - - it("inverted: the silhouette is the cover, the scene shows round it", async (ctx) => { - const renderer = getRenderer(ctx); - const effect = at(camera, 0.7, { shape: discWithHole(), invert: true }); - drawOverWhite(renderer, effect); - expect(await covered(renderer, 32, 32)).toBe(1); - for (const [x, y] of CORNERS) { - expect(await covered(renderer, x, y)).toBe(0); - } - effect.destroy(); - }); - - it("inverted: covers everything once closed, nothing at the start", async (ctx) => { - const renderer = getRenderer(ctx); - const closed = at(camera, 0, { shape: discWithHole(), invert: true }); - drawOverWhite(renderer, closed); - expect(await covered(renderer, 1, 1)).toBe(1); - expect(await covered(renderer, 32, 32)).toBe(1); - closed.destroy(); - const open = at(camera, 1, { shape: discWithHole(), invert: true }); - drawOverWhite(renderer, open); - expect(await covered(renderer, 32, 32)).toBe(0); - open.destroy(); - }); - - it("inverted geometry: an iris filled, growing out of the centre", async (ctx) => { - const renderer = getRenderer(ctx); - const effect = at(camera, 0.7, { - shape: new Ellipse(0, 0, 1, 1), - invert: true, - }); - drawOverWhite(renderer, effect); - expect(await covered(renderer, 32, 32)).toBe(1); - expect(await covered(renderer, 1, 1)).toBe(0); - effect.destroy(); - }); - - it("an image open in its middle (a U): grows about its thickest part, and still uncovers everything", async (ctx) => { - const renderer = getRenderer(ctx); - // just after the hide starts, the middle of the screen is still seen - const start = at(camera, 0.99, { shape: uShape() }); - drawOverWhite(renderer, start); - expect(await covered(renderer, 32, 32)).toBe(0); - start.destroy(); - // and just before a reveal ends, nothing is covered - const end = at(camera, 0.995, { shape: uShape() }); - drawOverWhite(renderer, end); - for (const [x, y] of [...CORNERS, [32, 32], [32, 3]]) { - expect(await covered(renderer, x, y)).toBe(0); - } - end.destroy(); - }); - - it("a polygon off its origin: still uncovers everything, and inverted still covers it", async (ctx) => { - const renderer = getRenderer(ctx); - const open = at(camera, 0.995, { shape: offOrigin() }); - drawOverWhite(renderer, open); - for (const [x, y] of CORNERS) { - expect(await covered(renderer, x, y)).toBe(0); - } - open.destroy(); - // the cover nearly grown: the screen is covered by the polygon itself - const cover = at(camera, 0.005, { shape: offOrigin(), invert: true }); - drawOverWhite(renderer, cover); - for (const [x, y] of [...CORNERS, [32, 32]]) { - expect(await covered(renderer, x, y)).toBe(1); - } - cover.destroy(); - }); - - it("nearly closed, the image is still drawn, a sliver of the scene in the middle", async (ctx) => { - const renderer = getRenderer(ctx); - const effect = at(camera, 0.05, { shape: discWithHole() }); - const drawImage = vi.spyOn(renderer, "drawImage"); - drawOverWhite(renderer, effect); - expect(drawImage).toHaveBeenCalled(); - drawImage.mockRestore(); - expect(await covered(renderer, 32, 32)).toBe(0); - for (const [x, y] of [...CORNERS, [32, 20], [44, 32]]) { - expect(await covered(renderer, x, y)).toBe(1); - } - effect.destroy(); - }); - - it("center: closes on that point, not on the centre of the screen", async (ctx) => { - const renderer = getRenderer(ctx); - const effect = at(camera, 0.2, { - shape: discWithHole(), - center: { x: 12, y: 12 }, - }); - drawOverWhite(renderer, effect); - expect(await covered(renderer, 12, 12)).toBe(0); - expect(await covered(renderer, 52, 52)).toBe(1); - effect.destroy(); - }); -}; - // One Canvas application for the whole file, made first: the engine gives a // viewport only to the application live at the first `init()` let app; @@ -333,7 +96,7 @@ describe("MaskEffect: image and sprite shapes", () => { return renderer; }); - it("destroy frees the copy of the image uploaded to the GPU", (ctx) => { + it("destroy frees the copy of the image uploaded to the GPU", async (ctx) => { if (renderer === undefined) { ctx.skip("WebGL renderer not available in this environment"); return; @@ -341,40 +104,13 @@ describe("MaskEffect: image and sprite shapes", () => { const effect = at(new Camera2d(0, 0, SIZE, SIZE), 0.5, { shape: discWithHole(), }); - drawOverWhite(renderer, effect); + await drawOverWhite(renderer, effect); const canvas = effect._cover.canvas; expect(renderer.cache.has(canvas)).toBe(true); effect.destroy(); expect(renderer.cache.has(canvas)).toBe(false); }); }); - - describe("WebGPU", () => { - let gpuApp; - let available = false; - beforeAll(async () => { - try { - gpuApp = new Application(SIZE, SIZE, { - parent: "screen", - renderer: video.WEBGPU, - consoleHeader: false, - }); - await gpuApp.init(); - available = gpuApp.renderer.type === "WebGPU"; - } catch { - available = false; - } - }); - afterAll(() => { - gpuApp?.destroy(); - }); - drawingChecks((ctx) => { - if (!available) { - ctx.skip("WebGPU not available in this environment"); - } - return gpuApp.renderer; - }); - }); }); describe("MaskEffect: what a shape and a center resolve to", () => { From 5a3557e7a9a1bffa5d8f1141dd2ac758a67da269 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 00:18:36 +0000 Subject: [PATCH 02/11] GPU tile layers for isometric maps: the shader render mode (#1897) An isometric tile layer can take the "shader" render mode, on WebGL 2 and on WebGPU: one quad for the view, the fragment shader finding each pixel's tile. For a pixel it walks the cells whose tile image could cover it, front to back (the reverse of the order TMXIsometricRenderer draws in), and keeps the first texel that is not transparent, so tall tiles overlap the rows behind them as on the CPU path. - shaders: the part every orientation shares (index and animation lookups, flips, atlas sampling) is split from the orthogonal part, in GLSL and in WGSL; the isometric part adds only its cell maths - renderers: a TMXLayerGPURenderer base per backend, with the orthogonal and isometric ones on top; the part of a layer a view shows is computed in one module both backends read (gpu/tmxlayer_region.js) - TMXLayer: an isometric layer is eligible when its tiles come from one tileset (they overlap, and a pass draws one tileset), an image one, with tiles within the shader's bounds; a tileoffset is supported. A tile of another tileset set at run time moves the layer back to the CPU path - tests: helpers/tile-layer-compare.js draws a layer through the CPU and the GPU path, with the whole engine in between, and compares the frames Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01JnzwA4a7ZyjLBpbeEpFLu6 --- packages/melonjs/src/level/tiled/TMXLayer.js | 111 +++- .../melonjs/src/video/gpu/tmxlayer_region.js | 143 +++++ .../webgl/renderers/tmxlayer/isometric.js | 54 ++ .../webgl/renderers/tmxlayer/orthogonal.js | 438 +-------------- .../webgl/renderers/tmxlayer/tmxlayer.js | 488 +++++++++++++++++ .../webgl/shaders/isometric-tmxlayer.frag | 67 +++ .../webgl/shaders/orthogonal-tmxlayer.frag | 166 +----- .../video/webgl/shaders/tmxlayer-common.frag | 168 ++++++ ...orthogonal-tmxlayer.vert => tmxlayer.vert} | 0 .../melonjs/src/video/webgl/webgl_renderer.js | 62 ++- .../webgpu/renderers/tmxlayer/isometric.js | 54 ++ .../webgpu/renderers/tmxlayer/orthogonal.js | 449 +-------------- .../webgpu/renderers/tmxlayer/tmxlayer.js | 515 ++++++++++++++++++ .../{tmxlayer.wgsl => tmxlayer-common.wgsl} | 85 +-- .../webgpu/shaders/tmxlayer-isometric.wgsl | 67 +++ .../webgpu/shaders/tmxlayer-orthogonal.wgsl | 69 +++ .../src/video/webgpu/webgpu_renderer.js | 40 +- .../tests/helpers/tile-layer-compare.js | 333 +++++++++++ 18 files changed, 2172 insertions(+), 1137 deletions(-) create mode 100644 packages/melonjs/src/video/gpu/tmxlayer_region.js create mode 100644 packages/melonjs/src/video/webgl/renderers/tmxlayer/isometric.js create mode 100644 packages/melonjs/src/video/webgl/renderers/tmxlayer/tmxlayer.js create mode 100644 packages/melonjs/src/video/webgl/shaders/isometric-tmxlayer.frag create mode 100644 packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag rename packages/melonjs/src/video/webgl/shaders/{orthogonal-tmxlayer.vert => tmxlayer.vert} (100%) create mode 100644 packages/melonjs/src/video/webgpu/renderers/tmxlayer/isometric.js create mode 100644 packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js rename packages/melonjs/src/video/webgpu/shaders/{tmxlayer.wgsl => tmxlayer-common.wgsl} (73%) create mode 100644 packages/melonjs/src/video/webgpu/shaders/tmxlayer-isometric.wgsl create mode 100644 packages/melonjs/src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl create mode 100644 packages/melonjs/tests/helpers/tile-layer-compare.js diff --git a/packages/melonjs/src/level/tiled/TMXLayer.js b/packages/melonjs/src/level/tiled/TMXLayer.js index e5bd7d2d72..54c7fb89d9 100644 --- a/packages/melonjs/src/level/tiled/TMXLayer.js +++ b/packages/melonjs/src/level/tiled/TMXLayer.js @@ -26,6 +26,13 @@ const FLIP_H_BIT = 1 << 0; const FLIP_V_BIT = 1 << 1; const FLIP_AD_BIT = 1 << 2; +// The largest tiles the isometric shader path draws, in grid cells: the +// bounds of the loops in `isometric-tmxlayer.frag` and its WGSL twin. +const ISOMETRIC_MAX_TILE_WIDTHS = 4; +const ISOMETRIC_MAX_TILE_HEIGHTS = 8; +const ISOMETRIC_ONE_TILESET = + "tiles of more than one tileset in an isometric layer"; + // the area an offset camera shows, handed to the tile renderers in its place // (made on first use) let viewRect; @@ -680,7 +687,7 @@ export default class TMXLayer extends Renderable { if (!renderer || renderer.supportsShaderTileLayers !== true) { return { ok: false, reason: "no-gpu-renderer" }; } - if (this.orientation !== "orthogonal") { + if (this.orientation !== "orthogonal" && this.orientation !== "isometric") { return { ok: false, reason: `no gpu renderer supported yet for "${this.orientation}" orientation`, @@ -689,6 +696,9 @@ export default class TMXLayer extends Renderable { if (!this.tilesets || this.tilesets.tilesets.length === 0) { return { ok: false, reason: "no tilesets" }; } + if (this.orientation === "isometric") { + return this._checkIsometricShaderEligibility(); + } // the shader iterates a fixed-size loop over candidate cells to // support oversized tiles (tile dim > cell dim) drawn bottom-aligned. // Loop bound is MAX_OVERFLOW + 1; anything beyond would silently clip, @@ -719,6 +729,102 @@ export default class TMXLayer extends Renderable { return { ok: true }; } + /** + * Check whether this isometric layer can take the shader path, and note + * the tileset it would draw with. + * + * Isometric tiles overlap (each is drawn as its whole image, over the + * rows behind it), and the shader path draws one tileset at a time: it + * can keep the tile drawn last among those of one tileset, not across + * two. So the layer's tiles must all come from the same tileset. Which + * one is found by looking at the tiles, not at the map's list: a map + * commonly has a tileset per layer. + * @returns {{ok: boolean, reason?: string}} + * @ignore + * @internal + */ + _checkIsometricShaderEligibility() { + /** + * the one tileset an isometric layer on the shader path draws with + * @ignore + */ + this._shaderTileset = undefined; + const data = this.layerData; + let tileset = null; + for (let i = 0; i < data.length; i += 2) { + const gid = data[i]; + if (gid !== 0 && (tileset === null || !tileset.contains(gid))) { + if (tileset !== null) { + return { ok: false, reason: ISOMETRIC_ONE_TILESET }; + } + tileset = this.tilesets.getTilesetByGid(gid); + } + } + // an empty layer: its tileset is the first one a tile is set from + if (tileset !== null) { + const reason = this._isometricTilesetReason(tileset); + if (reason !== undefined) { + return { ok: false, reason }; + } + this._shaderTileset = tileset; + } + return { ok: true }; + } + + /** + * Why the isometric shader path cannot draw the tiles of a tileset, if + * it cannot. + * @param {TMXTileset} tileset - the tileset + * @returns {string|undefined} the reason, or undefined when it can + * @ignore + * @internal + */ + _isometricTilesetReason(tileset) { + if (tileset.isCollection) { + return "collection-of-image tileset"; + } + if (tileset.tilerendersize !== "tile") { + return `tilerendersize "${tileset.tilerendersize}" not supported`; + } + // the shader looks, for each pixel, at every cell whose tile image + // could cover it: a bounded number + if ( + tileset.tilewidth > this.tilewidth * ISOMETRIC_MAX_TILE_WIDTHS || + tileset.tileheight > this.tileheight * ISOMETRIC_MAX_TILE_HEIGHTS + ) { + return `tiles larger than the shader's limit (${ISOMETRIC_MAX_TILE_WIDTHS} grid widths by ${ISOMETRIC_MAX_TILE_HEIGHTS} grid heights)`; + } + return undefined; + } + + /** + * An isometric layer on the shader path draws with one tileset: a tile + * set at run time from another one, or from one the path cannot draw, + * moves the layer back to the legacy renderer. + * @param {number} gid - the gid of the tile just set + * @ignore + * @internal + */ + _keepIsometricShaderTileset(gid) { + const current = this._shaderTileset; + if (current?.contains(gid)) { + return; + } + let reason = ISOMETRIC_ONE_TILESET; + if (current === undefined) { + const tileset = this.tilesets.getTilesetByGid(gid); + reason = this._isometricTilesetReason(tileset); + if (reason === undefined) { + this._shaderTileset = tileset; + return; + } + } + console.warn( + `melonJS: layer "${this.name}" back to the legacy tile renderer (${reason})`, + ); + this.renderMode = "perTile"; + } + // called when the layer is removed from the game world or a container onDeactivateEvent() { // renderer-side caches keyed by this layer (e.g. the WebGL2 shader @@ -825,6 +931,9 @@ export default class TMXLayer extends Renderable { this.dataVersion++; this.isDirty = true; this._preRenderCell(x, y); + if (this.renderMode === "shader" && this.orientation === "isometric") { + this._keepIsometricShaderTileset(cleanGid); + } return tile; } diff --git a/packages/melonjs/src/video/gpu/tmxlayer_region.js b/packages/melonjs/src/video/gpu/tmxlayer_region.js new file mode 100644 index 0000000000..b83f5211bf --- /dev/null +++ b/packages/melonjs/src/video/gpu/tmxlayer_region.js @@ -0,0 +1,143 @@ +/** + * The part of a tile layer a view shows, as the GPU tile layer renderers + * need it: the same on WebGL and WebGPU, so both read it from here. + * + * A region is the quad to draw (`x`, `y`, `width`, `height`, in the layer's + * pixels), the shader's `uVisibleStart` and `uVisibleSize` (the quad's + * corner and size, in the units the orientation's fragment shader works in) + * and its `uOverflow`. + * @import { default as TMXLayer } from "../../level/tiled/TMXLayer.js"; + */ + +/** + * @returns {object} an empty region, for a renderer to fill again and again + * @ignore + */ +export function createRegion() { + return { + x: 0, + y: 0, + width: 0, + height: 0, + startX: 0, + startY: 0, + sizeX: 0, + sizeY: 0, + overflowX: 0, + overflowY: 0, + }; +} + +/** + * The cells of an orthogonal layer a view shows, for the tiles of one + * tileset: the quad covers them, and the shader works in cells. + * @param {TMXLayer} layer - the layer + * @param {object} rect - the visible viewport rect (world coords) + * @param {object} tileset - the tileset whose tiles are drawn + * @param {object} out - the region to fill + * @returns {boolean} false when the view shows nothing of the layer + * @ignore + * @internal + */ +export function orthogonalRegion(layer, rect, tileset, out) { + const tileWidth = layer.tilewidth; + const tileHeight = layer.tileheight; + + // compute the visible region in tile-coord space + const startTileX = Math.max(0, Math.floor(rect.pos.x / tileWidth)); + const startTileY = Math.max(0, Math.floor(rect.pos.y / tileHeight)); + const endTileX = Math.min( + layer.cols, + Math.ceil((rect.pos.x + rect.width) / tileWidth), + ); + const endTileY = Math.min( + layer.rows, + Math.ceil((rect.pos.y + rect.height) / tileHeight), + ); + + // visible rect entirely outside the layer — nothing to draw + if (endTileX <= startTileX || endTileY <= startTileY) { + return false; + } + + // quad world coords + out.x = startTileX * tileWidth; + out.y = startTileY * tileHeight; + out.width = (endTileX - startTileX) * tileWidth; + out.height = (endTileY - startTileY) * tileHeight; + + // visible region in tile units (matches the quad's [0..1] UV span — + // shader recovers the per-fragment tile coord as + // `uVisibleStart + uv * uVisibleSize`) + out.startX = startTileX; + out.startY = startTileY; + out.sizeX = endTileX - startTileX; + out.sizeY = endTileY - startTileY; + + // how many cells a tile reaches past its own, across and up + out.overflowX = Math.max(0, Math.ceil(tileset.tilewidth / tileWidth) - 1); + out.overflowY = Math.max(0, Math.ceil(tileset.tileheight / tileHeight) - 1); + return true; +} + +/** + * The part of an isometric layer a view shows, for the tiles of one tileset: + * the same on every GPU backend, so their renderers share it. + * + * The quad is the view, cut to where the tileset's tiles can be: a tile is + * drawn as its whole image, bottom-aligned on its cell's diamond and moved by + * the tileset's `tileoffset`, so tall tiles reach above the map's top corner + * and wide ones past its right one. + * + * The shader works in the layer's pixels, measured across from the map's top + * corner and with the `tileoffset` taken off: both are in `startX` / + * `startY`, and the shader needs neither. + * @param {TMXLayer} layer - the layer + * @param {object} rect - the visible viewport rect (world coords) + * @param {object} tileset - the tileset whose tiles are drawn + * @param {object} out - the region to fill + * @returns {boolean} false when the view shows nothing of the layer + * @ignore + * @internal + */ +export function isometricRegion(layer, rect, tileset, out) { + const halfWidth = layer.tilewidth / 2; + const halfHeight = layer.tileheight / 2; + const originX = layer.getRenderer().originX; + const offsetX = tileset.tileoffset.x; + const offsetY = tileset.tileoffset.y; + const cols = layer.cols; + const rows = layer.rows; + + // where the tiles can be: from the left of the cell (0, rows - 1) to the + // right of the image of (cols - 1, 0), from the top of the image of + // (0, 0) to the bottom of the diamond of (cols - 1, rows - 1) + const left = originX - rows * halfWidth + offsetX; + const right = originX + (cols - 2) * halfWidth + tileset.tilewidth + offsetX; + const top = layer.tileheight - tileset.tileheight + offsetY; + const bottom = (cols + rows) * halfHeight + offsetY; + + // the view, cut to it, on whole pixels + const x0 = Math.floor(Math.max(rect.pos.x, left)); + const y0 = Math.floor(Math.max(rect.pos.y, top)); + const x1 = Math.ceil(Math.min(rect.pos.x + rect.width, right)); + const y1 = Math.ceil(Math.min(rect.pos.y + rect.height, bottom)); + if (x1 <= x0 || y1 <= y0) { + return false; + } + + out.x = x0; + out.y = y0; + out.width = x1 - x0; + out.height = y1 - y0; + out.startX = x0 - originX - offsetX; + out.startY = y0 - offsetY; + out.sizeX = out.width; + out.sizeY = out.height; + // how many cells' images reach a pixel: as many across as the image is + // wide in half grid widths, as many down as it is tall in half grid + // heights + out.overflowX = Math.ceil(tileset.tilewidth / halfWidth); + out.overflowY = Math.ceil(tileset.tileheight / halfHeight); + return true; +} diff --git a/packages/melonjs/src/video/webgl/renderers/tmxlayer/isometric.js b/packages/melonjs/src/video/webgl/renderers/tmxlayer/isometric.js new file mode 100644 index 0000000000..3483c8e8ce --- /dev/null +++ b/packages/melonjs/src/video/webgl/renderers/tmxlayer/isometric.js @@ -0,0 +1,54 @@ +import { isometricRegion } from "../../../gpu/tmxlayer_region.js"; +import isometricTMXLayerFragment from "../../shaders/isometric-tmxlayer.frag"; +import TMXLayerGPURenderer from "./tmxlayer.js"; + +/** + * additional imports for TypeScript + * @import { default as TMXLayer } from "../../../../level/tiled/TMXLayer.js"; + * @import { default as WebGLRenderer } from "../../webgl_renderer.js"; + */ + +/** + * GPU-accelerated renderer for isometric TMX tile layers (WebGL2): the + * shared {@link TMXLayerGPURenderer}, with an isometric grid's geometry. + * The quad covers the view, and the shader works in pixels. + * + * Isometric tiles overlap, and the shader keeps the one drawn last among + * those of ONE tileset: a layer takes this path only when all its tiles + * come from the same one (`layer._shaderTileset`, set by the layer when it + * is found eligible). + * @ignore + * @internal + */ +export default class IsometricTMXLayerGPURenderer extends TMXLayerGPURenderer { + /** + * @param {WebGLRenderer} renderer - the WebGL renderer instance + */ + constructor(renderer) { + super(renderer, isometricTMXLayerFragment); + this._tilesets = []; + } + + /** + * @ignore + * @internal + */ + _region(layer, rect, tileset, out) { + return isometricRegion(layer, rect, tileset, out); + } + + /** + * The one tileset an isometric layer on this path draws with. + * @param {TMXLayer} layer - the layer + * @returns {object[]} that tileset, or none for an empty layer + * @ignore + * @internal + */ + _tilesetsOf(layer) { + this._tilesets.length = 0; + if (layer._shaderTileset) { + this._tilesets.push(layer._shaderTileset); + } + return this._tilesets; + } +} diff --git a/packages/melonjs/src/video/webgl/renderers/tmxlayer/orthogonal.js b/packages/melonjs/src/video/webgl/renderers/tmxlayer/orthogonal.js index 1d7e8a79f3..f6e6f6fd74 100644 --- a/packages/melonjs/src/video/webgl/renderers/tmxlayer/orthogonal.js +++ b/packages/melonjs/src/video/webgl/renderers/tmxlayer/orthogonal.js @@ -1,7 +1,6 @@ -import { BufferTextureResource } from "../../../texture/resource.js"; -import GLShader from "../../glshader.js"; +import { orthogonalRegion } from "../../../gpu/tmxlayer_region.js"; import orthogonalTMXLayerFragment from "../../shaders/orthogonal-tmxlayer.frag"; -import orthogonalTMXLayerVertex from "../../shaders/orthogonal-tmxlayer.vert"; +import TMXLayerGPURenderer from "./tmxlayer.js"; /** * additional imports for TypeScript @@ -9,445 +8,26 @@ import orthogonalTMXLayerVertex from "../../shaders/orthogonal-tmxlayer.vert"; * @import { default as WebGLRenderer } from "../../webgl_renderer.js"; */ -// Default `uTint` when the layer has no tint set. Module-scoped so the -// fallback path doesn't allocate per frame. -const DEFAULT_TINT = new Float32Array([1, 1, 1, 1]); - /** - * GPU-accelerated renderer for orthogonal TMX tile layers (WebGL2). Draws - * the visible region of a layer as one screen-aligned quad per tileset - * referenced by the layer — the fragment shader samples a per-layer GID - * index texture and the tileset atlas, eliminating the per-tile draw - * loop. The visible rect, GID range, tile size, opacity, and tint are - * pushed as uniforms; the index texture is uploaded once at activation - * and re-uploaded only when `layer.dataVersion` changes (mutations from - * `setTile`/`clearTile`). - * - * The per-layer index texture is a `BufferTextureResource` flowing - * through the standard `TextureCache` / batcher path — same lane as - * every other texture in the engine. Dynamic unit allocation, correct - * `boundTextures` bookkeeping, and per-resource premultiplied-alpha / - * filter all come for free; nothing here pokes `gl.bindTexture` or - * `gl.activeTexture` directly. - * - * Cache lifecycle: one `BufferTextureResource` per `TMXLayer`. Tile - * layers don't come and go individually — they only churn on game reset - * — so the cache is freed in bulk via `reset()`, called from - * `WebGLRenderer.reset()` (which the `GAME_RESET` event already - * triggers). - * + * GPU-accelerated renderer for orthogonal TMX tile layers (WebGL2): the + * shared {@link TMXLayerGPURenderer}, with an orthogonal grid's geometry. + * The quad covers the cells in view, and the shader works in cells. * @ignore * @internal */ -export default class OrthogonalTMXLayerGPURenderer { +export default class OrthogonalTMXLayerGPURenderer extends TMXLayerGPURenderer { /** * @param {WebGLRenderer} renderer - the WebGL renderer instance */ constructor(renderer) { - this.renderer = renderer; - this.gl = renderer.gl; - // Standalone `GLShader` (not `ShaderEffect`) so we own both the - // vertex and the fragment source — they're paired in GLSL ES 3.00, - // which lets the fragment shader use `texelFetch` for byte-exact - // reads from the index / animation lookup textures (samplers stay - // `sampler2D` and decode the 8-bit channels as normalized floats; - // `usampler2D` would conflict with the engine's multi-texture - // batching cache). `setBatcher("quad", this.shader)` integrates - // the program with the quad batcher just like a `ShaderEffect` — - // only the per-fragment math gets cleaner. - this.shader = new GLShader( - renderer.gl, - orthogonalTMXLayerVertex, - orthogonalTMXLayerFragment, - renderer.shaderPrecision, - ); - // per-layer `BufferTextureResource`. `Map` (not `WeakMap`) so we - // can iterate on `reset()` to walk through the global texture - // cache and free GL handles. The world's container holds the - // strong ref to each layer anyway, so this doesn't change layer - // lifetime. - this.resources = new Map(); - // per-tileset animation-lookup resources, keyed by `TMXTileset`. - // Only allocated for tilesets that actually have animated tiles - // (`tileset.isAnimated === true`). - this.animLookups = new Map(); - // pre-allocated scratch for uniform uploads — avoids per-frame - // allocation in the hot path - /** - * @ignore - * @internal - */ - this._v2 = new Float32Array(2); - /** - * @ignore - * @internal - */ - this._v4 = new Float32Array(4); - } - - /** - * Free every cached per-layer index texture and empty the local - * resource map. Called from `WebGLRenderer.reset()` (which - * `GAME_RESET` triggers) so each level transition starts clean. - * @ignore - * @internal - */ - reset() { - // Uploads happen via the quad batcher (see `_drawLayer`), so pin - // the cleanup to that same batcher rather than whatever happens - // to be `currentBatcher` at reset time. `currentBatcher` may - // legitimately be a `PrimitiveBatcher` (e.g. debug-plugin drew - // its quadtree overlay on the previous frame) which has no - // `deleteTexture2D` and would crash the reset. Issue #1471. - const batcher = this.renderer.batchers.get("quad"); - const cache = this.renderer.cache; - const drop = (resource) => { - // route through the batcher so its `boundTextures` bookkeeping - // stays in sync. When no batcher is available (context tear- - // down) or the registered "quad" batcher is a user-supplied - // custom class that doesn't extend `MaterialBatcher`, we - // don't have a clean GL deletion path — still need to free - // the unit assignment, since `cache.delete()` only touches - // the image→atlas map and would leave the unit slot held - // forever otherwise. - if (typeof batcher?.deleteTexture2D === "function") { - batcher.deleteTexture2D(resource); - } else { - cache.freeTextureUnit(resource); - cache.delete(resource); - } - }; - for (const resource of this.resources.values()) { - drop(resource); - } - for (const entry of this.animLookups.values()) { - drop(entry.resource); - } - this.resources.clear(); - this.animLookups.clear(); - } - - /** - * Write a `vec2` uniform without allocating a fresh Float32Array per - * call. Both components flow into the shared `_v2` scratch buffer, - * which `setUniform` reads synchronously and forwards to - * `gl.uniform2fv` — so reusing the buffer across calls is safe. - * @param {string} name - * @param {number} x - * @param {number} y - * @private - * @ignore - * @internal - */ - _setV2(name, x, y) { - this._v2[0] = x; - this._v2[1] = y; - this.shader.setUniform(name, this._v2); + super(renderer, orthogonalTMXLayerFragment); } /** - * `vec4` counterpart to {@link _setV2}. - * @param {string} name - * @param {number} x - * @param {number} y - * @param {number} z - * @param {number} w - * @private * @ignore * @internal */ - _setV4(name, x, y, z, w) { - this._v4[0] = x; - this._v4[1] = y; - this._v4[2] = z; - this._v4[3] = w; - this.shader.setUniform(name, this._v4); - } - - /** - * Get-or-create the per-tileset animation-lookup entry. Returns - * `undefined` for tilesets that have no animated tiles (the shader's - * `uAnimEnabled` uniform is then set to 0 and the lookup texture is - * not bound). - * - * The entry holds a `tileCount × 1` RGBA8 `BufferTextureResource` - * where texel `localId` encodes the CURRENT frame's local id as - * `R = lo byte, G = hi byte` (same encoding as the GID index - * texture). Each call walks `tileset.animations` and rewrites - * dirty texels — `tileset.update(dt)` (driven by the layer) advances - * `anim.cur.tileid` independently of this renderer. - * - * @param {object} tileset - * @param {number} tileCount - tiles in the tileset's atlas grid - * (`atlasCols * atlasRows`) - * @returns {{resource: BufferTextureResource, data: Uint8Array, - * tileCount: number, dirty: boolean}|undefined} - * @ignore - * @internal - */ - _getOrUpdateAnimLookup(tileset, tileCount) { - if (!tileset.isAnimated || tileset.animations.size === 0) { - return undefined; - } - let entry = this.animLookups.get(tileset); - if (entry === undefined) { - // allocate the lookup texture and initialize it to identity - // (localId → localId); animated entries get overwritten below - const data = new Uint8Array(tileCount * 4); - for (let id = 0; id < tileCount; id++) { - data[id * 4 + 0] = id & 0xff; - data[id * 4 + 1] = (id >> 8) & 0xff; - } - const resource = new BufferTextureResource(data, { - width: tileCount, - height: 1, - premultipliedAlpha: false, - repeat: "no-repeat", - // NEAREST so the shader's `texelFetch` reads byte-exact - // channel values back as normalized floats - filter: this.gl.NEAREST, - format: "rgba8", - }); - // new entry → first upload happens unconditionally via the - // batcher's `boundTextures[unit] === undefined` path - entry = { resource, data, tileCount, dirty: false }; - this.animLookups.set(tileset, entry); - } - // walk animations and rewrite the current-frame ids. The dirty - // flag flips true only when at least one texel actually changed - // — the batcher then force-reuploads on the next bind, otherwise - // reuses the existing GL texture untouched. - const data = entry.data; - for (const [localId, anim] of tileset.animations) { - const off = localId * 4; - const cur = anim.cur.tileid; - const lo = cur & 0xff; - const hi = (cur >> 8) & 0xff; - if (data[off] !== lo || data[off + 1] !== hi) { - data[off] = lo; - data[off + 1] = hi; - entry.dirty = true; - } - } - return entry; - } - - /** - * Get-or-create the per-layer index `BufferTextureResource`. - * @param {TMXLayer} layer - * @returns {BufferTextureResource} - * @ignore - * @internal - */ - _getResource(layer) { - let resource = this.resources.get(layer); - if (resource === undefined) { - // reinterpret the layer's `Uint16Array` payload as RGBA bytes - // (zero-copy on every little-endian platform = every browser) - resource = new BufferTextureResource( - new Uint8Array(layer.layerData.buffer), - { - width: layer.cols, - height: layer.rows, - // raw GID bytes — must NOT have alpha pre-multiplied - // into RGB, otherwise A=0 cells wipe their R/G/B - premultipliedAlpha: false, - repeat: "no-repeat", - // NEAREST so `texelFetch` returns the original byte - // values (as normalized floats) for the GID/flip - // decode below - filter: this.gl.NEAREST, - format: "rgba8", - }, - ); - // track the last-uploaded version so we know when to force a - // re-upload after `setTile` / `clearTile` mutations - resource._uploadedVersion = -1; - this.resources.set(layer, resource); - } - return resource; - } - - /** - * Draw an orthogonal TMX layer through the shader path. - * @param {TMXLayer} layer - * @param {object} rect - the visible viewport rect (world coords) - */ - draw(layer, rect) { - const renderer = this.renderer; - const tileWidth = layer.tilewidth; - const tileHeight = layer.tileheight; - const cols = layer.cols; - const rows = layer.rows; - - // compute the visible region in tile-coord space - const startTileX = Math.max(0, Math.floor(rect.pos.x / tileWidth)); - const startTileY = Math.max(0, Math.floor(rect.pos.y / tileHeight)); - const endTileX = Math.min( - cols, - Math.ceil((rect.pos.x + rect.width) / tileWidth), - ); - const endTileY = Math.min( - rows, - Math.ceil((rect.pos.y + rect.height) / tileHeight), - ); - - // visible rect entirely outside the layer — nothing to draw - if (endTileX <= startTileX || endTileY <= startTileY) { - return; - } - - // quad world coords - const worldX = startTileX * tileWidth; - const worldY = startTileY * tileHeight; - const worldW = (endTileX - startTileX) * tileWidth; - const worldH = (endTileY - startTileY) * tileHeight; - - // visible region size in tile units (matches the quad's [0..1] UV - // span — shader recovers the per-fragment tile coord as - // `uVisibleStart + uv * uVisibleSize`) - const visStartX = startTileX; - const visStartY = startTileY; - const visSizeX = endTileX - startTileX; - const visSizeY = endTileY - startTileY; - - // switch the quad batcher to our custom shader. This flushes any - // queued sprite vertices first so they render under their original - // shader, then re-binds for our pass. - const batcher = renderer.setBatcher("quad", this.shader); - - // upload (or rebind) the index texture through the standard cache - // path: dynamic unit allocation, correct `boundTextures` tracking, - // per-resource premultiply / filter applied automatically - const resource = this._getResource(layer); - const indexUnit = batcher.uploadTexture( - resource, - cols, - rows, - resource._uploadedVersion !== layer.dataVersion, - ); - resource._uploadedVersion = layer.dataVersion; - - this.shader.setUniform("uTileIndex", indexUnit); - this._setV2("uMapSize", cols, rows); - this._setV2("uVisibleStart", visStartX, visStartY); - this._setV2("uVisibleSize", visSizeX, visSizeY); - this._setV2("uCellSize", tileWidth, tileHeight); - // the layer's opacity, and the fade of everything it is drawn in: - // set on the renderer by the layer's preDraw - this.shader.setUniform("uOpacity", renderer.getGlobalAlpha()); - this.shader.setUniform( - "uTint", - layer.tint ? layer.tint.toArray() : DEFAULT_TINT, - ); - - // one pass per tileset — the shader's uGidRange uniform + discard - // hides cells outside the active tileset's GID range - const tilesets = layer.tilesets.tilesets; - for (let i = 0; i < tilesets.length; i++) { - const tileset = tilesets[i]; - // skip collection-of-image tilesets (caller's eligibility check - // should have downgraded these layers to the legacy path, but - // guard defensively — we can't compute uTilesetCols for them) - if (tileset.isCollection || tileset.image === undefined) { - continue; - } - - const tsW = tileset.tilewidth; - const tsH = tileset.tileheight; - const margin = tileset.margin; - const spacing = tileset.spacing; - const atlasW = tileset.image.width; - const atlasH = tileset.image.height; - - // atlas geometry (margin + spacing) — number of tiles per row/col - // in the atlas image - const atlasCols = Math.max( - 1, - Math.floor((atlasW - margin * 2 + spacing) / (tsW + spacing)), - ); - const atlasRows = Math.max( - 1, - Math.floor((atlasH - margin * 2 + spacing) / (tsH + spacing)), - ); - - // per-tileset animation lookup. Only allocated/bound for - // tilesets that actually have animated tiles — static tilesets - // pay neither a texture-unit nor an upload. - const tileCount = atlasCols * atlasRows; - const animEntry = this._getOrUpdateAnimLookup(tileset, tileCount); - if (animEntry !== undefined) { - const animUnit = batcher.uploadTexture( - animEntry.resource, - animEntry.tileCount, - 1, - animEntry.dirty, - ); - animEntry.dirty = false; - this.shader.setUniform("uAnimLookup", animUnit); - this.shader.setUniform("uAnimSize", animEntry.tileCount); - } else { - // shader skips the animation lookup when uAnimSize == 0, - // but every declared sampler must still point at a valid - // unit — reuse the index unit so WebGL's draw-time - // validation is happy (the sampler is never read because - // of the uAnimSize guard) - this.shader.setUniform("uAnimLookup", indexUnit); - this.shader.setUniform("uAnimSize", 0); - } - - this._setV2("uTileSize", tsW, tsH); - this._setV2("uTilesetCols", atlasCols, atlasRows); - this._setV2("uInvTilesetSize", 1 / atlasW, 1 / atlasH); - this._setV4("uTilesetMargin", margin, margin, spacing, spacing); - this._setV2( - "uOverflow", - Math.max(0, Math.ceil(tsW / tileWidth) - 1), - Math.max(0, Math.ceil(tsH / tileHeight) - 1), - ); - this._setV2("uGidRange", tileset.firstgid, tileset.lastgid); - // the tileset samples its mipmaps when the batcher binds it a - // mip sampler: the "linear-mipmap" filter, on a linear texture - const filter = - typeof tileset.texture.filter !== "undefined" - ? tileset.texture.filter - : batcher.renderer._glTextureFilter(); - this.shader.setUniform( - "uMipmaps", - (filter === this.gl.LINEAR || filter === "linear") && - batcher.renderer.wantsMipmaps(tileset.texture) - ? 1 - : 0, - ); - - // emit one screen-quad. The batcher will bind the tileset's - // TextureAtlas to uSampler (single-texture fallback path is - // active because we have a custom ShaderEffect). - // on whole pixels unless `subPixel` is set, as a sprite is: the - // tiles move with the quad (see Renderer#_pixelSnapBegin) - const snapped = batcher.renderer._pixelSnapBegin( - worldX, - worldY, - worldW, - worldH, - ); - batcher.addQuad( - tileset.texture, - worldX, - worldY, - worldW, - worldH, - 0, - 0, - 1, - 1, - 0xffffffff, - ); - batcher.renderer._pixelSnapEnd(snapped); - - // flush per-pass so each tileset's draw call uses its own - // uniforms (uGidRange / uTileSize / uTilesetCols differ) - batcher.flush(); - } + _region(layer, rect, tileset, out) { + return orthogonalRegion(layer, rect, tileset, out); } } diff --git a/packages/melonjs/src/video/webgl/renderers/tmxlayer/tmxlayer.js b/packages/melonjs/src/video/webgl/renderers/tmxlayer/tmxlayer.js new file mode 100644 index 0000000000..b740e8591f --- /dev/null +++ b/packages/melonjs/src/video/webgl/renderers/tmxlayer/tmxlayer.js @@ -0,0 +1,488 @@ +import { createRegion } from "../../../gpu/tmxlayer_region.js"; +import { BufferTextureResource } from "../../../texture/resource.js"; +import GLShader from "../../glshader.js"; +import tmxLayerVertex from "../../shaders/tmxlayer.vert"; +import tmxLayerCommonFragment from "../../shaders/tmxlayer-common.frag"; + +/** + * additional imports for TypeScript + * @import { default as TMXLayer } from "../../../../level/tiled/TMXLayer.js"; + * @import { default as WebGLRenderer } from "../../webgl_renderer.js"; + */ + +// Default `uTint` when the layer has no tint set. Module-scoped so the +// fallback path doesn't allocate per frame. +const DEFAULT_TINT = new Float32Array([1, 1, 1, 1]); + +/** + * GPU-accelerated renderer for TMX tile layers (WebGL2): what every map + * orientation shares. Draws the visible region of a layer as one + * screen-aligned quad per tileset — the fragment shader samples a per-layer + * GID index texture and the tileset atlas, eliminating the per-tile draw + * loop. The visible rect, GID range, tile size, opacity, and tint are + * pushed as uniforms; the index texture is uploaded once at activation + * and re-uploaded only when `layer.dataVersion` changes (mutations from + * `setTile`/`clearTile`). + * + * An orientation's renderer extends this one with the two things that + * differ: its part of the fragment shader (where a cell's tile is, and which + * cells can cover a fragment), and {@link TMXLayerGPURenderer#_region}, the + * part of the layer a view shows. + * + * The per-layer index texture is a `BufferTextureResource` flowing + * through the standard `TextureCache` / batcher path — same lane as + * every other texture in the engine. Dynamic unit allocation, correct + * `boundTextures` bookkeeping, and per-resource premultiplied-alpha / + * filter all come for free; nothing here pokes `gl.bindTexture` or + * `gl.activeTexture` directly. + * + * Cache lifecycle: one `BufferTextureResource` per `TMXLayer`. Tile + * layers don't come and go individually — they only churn on game reset + * — so the cache is freed in bulk via `reset()`, called from + * `WebGLRenderer.reset()` (which the `GAME_RESET` event already + * triggers). + * + * @ignore + * @internal + */ +export default class TMXLayerGPURenderer { + /** + * @param {WebGLRenderer} renderer - the WebGL renderer instance + * @param {string} fragment - the orientation's part of the fragment + * shader, appended to the shared one + */ + constructor(renderer, fragment) { + this.renderer = renderer; + this.gl = renderer.gl; + // Standalone `GLShader` (not `ShaderEffect`) so we own both the + // vertex and the fragment source — they're paired in GLSL ES 3.00, + // which lets the fragment shader use `texelFetch` for byte-exact + // reads from the index / animation lookup textures (samplers stay + // `sampler2D` and decode the 8-bit channels as normalized floats; + // `usampler2D` would conflict with the engine's multi-texture + // batching cache). `setBatcher("quad", this.shader)` integrates + // the program with the quad batcher just like a `ShaderEffect` — + // only the per-fragment math gets cleaner. + this.shader = new GLShader( + renderer.gl, + tmxLayerVertex, + `${tmxLayerCommonFragment}\n${fragment}`, + renderer.shaderPrecision, + ); + // per-layer `BufferTextureResource`. `Map` (not `WeakMap`) so we + // can iterate on `reset()` to walk through the global texture + // cache and free GL handles. The world's container holds the + // strong ref to each layer anyway, so this doesn't change layer + // lifetime. + this.resources = new Map(); + // per-tileset animation-lookup resources, keyed by `TMXTileset`. + // Only allocated for tilesets that actually have animated tiles + // (`tileset.isAnimated === true`). + this.animLookups = new Map(); + // pre-allocated scratch for uniform uploads — avoids per-frame + // allocation in the hot path + /** + * @ignore + * @internal + */ + this._v2 = new Float32Array(2); + /** + * @ignore + * @internal + */ + this._v4 = new Float32Array(4); + /** + * the texture unit of the index texture of the layer being drawn + * @ignore + * @internal + */ + this._indexUnit = 0; + /** + * what {@link TMXLayerGPURenderer#_region} fills: the quad to draw, + * in the layer's pixels, and what the shader needs to find a + * fragment's place in the layer from its place in the quad + * @ignore + * @internal + */ + this._regionScratch = createRegion(); + } + + /** + * The part of a layer a view shows, for the tiles of one tileset: the + * quad to draw (`x`, `y`, `width`, `height`, in the layer's pixels), the + * shader's `uVisibleStart` and `uVisibleSize` (the quad's corner and + * size, in the units its fragment shader works in) and `uOverflow`. + * @param {TMXLayer} layer - the layer + * @param {object} rect - the visible viewport rect (world coords) + * @param {object} tileset - the tileset whose tiles are drawn + * @param {object} out - the region to fill + * @returns {boolean} false when the view shows nothing of the layer + * @abstract + * @ignore + * @internal + */ + // eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars + _region(layer, rect, tileset, out) { + return false; + } + + /** + * The tilesets a layer is drawn with, one pass each. + * @param {TMXLayer} layer - the layer + * @returns {object[]} the tilesets + * @ignore + * @internal + */ + _tilesetsOf(layer) { + return layer.tilesets.tilesets; + } + + /** + * Free every cached per-layer index texture and empty the local + * resource map. Called from `WebGLRenderer.reset()` (which + * `GAME_RESET` triggers) so each level transition starts clean. + * @ignore + * @internal + */ + reset() { + // Uploads happen via the quad batcher (see `_drawLayer`), so pin + // the cleanup to that same batcher rather than whatever happens + // to be `currentBatcher` at reset time. `currentBatcher` may + // legitimately be a `PrimitiveBatcher` (e.g. debug-plugin drew + // its quadtree overlay on the previous frame) which has no + // `deleteTexture2D` and would crash the reset. Issue #1471. + const batcher = this.renderer.batchers.get("quad"); + const cache = this.renderer.cache; + const drop = (resource) => { + // route through the batcher so its `boundTextures` bookkeeping + // stays in sync. When no batcher is available (context tear- + // down) or the registered "quad" batcher is a user-supplied + // custom class that doesn't extend `MaterialBatcher`, we + // don't have a clean GL deletion path — still need to free + // the unit assignment, since `cache.delete()` only touches + // the image→atlas map and would leave the unit slot held + // forever otherwise. + if (typeof batcher?.deleteTexture2D === "function") { + batcher.deleteTexture2D(resource); + } else { + cache.freeTextureUnit(resource); + cache.delete(resource); + } + }; + for (const resource of this.resources.values()) { + drop(resource); + } + for (const entry of this.animLookups.values()) { + drop(entry.resource); + } + this.resources.clear(); + this.animLookups.clear(); + } + + /** + * Write a `vec2` uniform without allocating a fresh Float32Array per + * call. Both components flow into the shared `_v2` scratch buffer, + * which `setUniform` reads synchronously and forwards to + * `gl.uniform2fv` — so reusing the buffer across calls is safe. + * @param {string} name + * @param {number} x + * @param {number} y + * @private + * @ignore + * @internal + */ + _setV2(name, x, y) { + this._v2[0] = x; + this._v2[1] = y; + this.shader.setUniform(name, this._v2); + } + + /** + * `vec4` counterpart to {@link _setV2}. + * @param {string} name + * @param {number} x + * @param {number} y + * @param {number} z + * @param {number} w + * @private + * @ignore + * @internal + */ + _setV4(name, x, y, z, w) { + this._v4[0] = x; + this._v4[1] = y; + this._v4[2] = z; + this._v4[3] = w; + this.shader.setUniform(name, this._v4); + } + + /** + * Get-or-create the per-tileset animation-lookup entry. Returns + * `undefined` for tilesets that have no animated tiles (the shader's + * `uAnimEnabled` uniform is then set to 0 and the lookup texture is + * not bound). + * + * The entry holds a `tileCount × 1` RGBA8 `BufferTextureResource` + * where texel `localId` encodes the CURRENT frame's local id as + * `R = lo byte, G = hi byte` (same encoding as the GID index + * texture). Each call walks `tileset.animations` and rewrites + * dirty texels — `tileset.update(dt)` (driven by the layer) advances + * `anim.cur.tileid` independently of this renderer. + * + * @param {object} tileset + * @param {number} tileCount - tiles in the tileset's atlas grid + * (`atlasCols * atlasRows`) + * @returns {{resource: BufferTextureResource, data: Uint8Array, + * tileCount: number, dirty: boolean}|undefined} + * @ignore + * @internal + */ + _getOrUpdateAnimLookup(tileset, tileCount) { + if (!tileset.isAnimated || tileset.animations.size === 0) { + return undefined; + } + let entry = this.animLookups.get(tileset); + if (entry === undefined) { + // allocate the lookup texture and initialize it to identity + // (localId → localId); animated entries get overwritten below + const data = new Uint8Array(tileCount * 4); + for (let id = 0; id < tileCount; id++) { + data[id * 4 + 0] = id & 0xff; + data[id * 4 + 1] = (id >> 8) & 0xff; + } + const resource = new BufferTextureResource(data, { + width: tileCount, + height: 1, + premultipliedAlpha: false, + repeat: "no-repeat", + // NEAREST so the shader's `texelFetch` reads byte-exact + // channel values back as normalized floats + filter: this.gl.NEAREST, + format: "rgba8", + }); + // new entry → first upload happens unconditionally via the + // batcher's `boundTextures[unit] === undefined` path + entry = { resource, data, tileCount, dirty: false }; + this.animLookups.set(tileset, entry); + } + // walk animations and rewrite the current-frame ids. The dirty + // flag flips true only when at least one texel actually changed + // — the batcher then force-reuploads on the next bind, otherwise + // reuses the existing GL texture untouched. + const data = entry.data; + for (const [localId, anim] of tileset.animations) { + const off = localId * 4; + const cur = anim.cur.tileid; + const lo = cur & 0xff; + const hi = (cur >> 8) & 0xff; + if (data[off] !== lo || data[off + 1] !== hi) { + data[off] = lo; + data[off + 1] = hi; + entry.dirty = true; + } + } + return entry; + } + + /** + * Get-or-create the per-layer index `BufferTextureResource`. + * @param {TMXLayer} layer + * @returns {BufferTextureResource} + * @ignore + * @internal + */ + _getResource(layer) { + let resource = this.resources.get(layer); + if (resource === undefined) { + // reinterpret the layer's `Uint16Array` payload as RGBA bytes + // (zero-copy on every little-endian platform = every browser) + resource = new BufferTextureResource( + new Uint8Array(layer.layerData.buffer), + { + width: layer.cols, + height: layer.rows, + // raw GID bytes — must NOT have alpha pre-multiplied + // into RGB, otherwise A=0 cells wipe their R/G/B + premultipliedAlpha: false, + repeat: "no-repeat", + // NEAREST so `texelFetch` returns the original byte + // values (as normalized floats) for the GID/flip + // decode below + filter: this.gl.NEAREST, + format: "rgba8", + }, + ); + // track the last-uploaded version so we know when to force a + // re-upload after `setTile` / `clearTile` mutations + resource._uploadedVersion = -1; + this.resources.set(layer, resource); + } + return resource; + } + + /** + * Bind the layer's shader and what all its tilesets share: called for + * the first tileset the view shows something of. + * @param {TMXLayer} layer - the layer + * @returns {object} the quad batcher, on this renderer's shader + * @ignore + * @internal + */ + _begin(layer) { + const renderer = this.renderer; + const cols = layer.cols; + const rows = layer.rows; + + // switch the quad batcher to our custom shader. This flushes any + // queued sprite vertices first so they render under their original + // shader, then re-binds for our pass. + const batcher = renderer.setBatcher("quad", this.shader); + + // upload (or rebind) the index texture through the standard cache + // path: dynamic unit allocation, correct `boundTextures` tracking, + // per-resource premultiply / filter applied automatically + const resource = this._getResource(layer); + this._indexUnit = batcher.uploadTexture( + resource, + cols, + rows, + resource._uploadedVersion !== layer.dataVersion, + ); + resource._uploadedVersion = layer.dataVersion; + + this.shader.setUniform("uTileIndex", this._indexUnit); + this._setV2("uMapSize", cols, rows); + this._setV2("uCellSize", layer.tilewidth, layer.tileheight); + // the layer's opacity, and the fade of everything it is drawn in: + // set on the renderer by the layer's preDraw + this.shader.setUniform("uOpacity", renderer.getGlobalAlpha()); + this.shader.setUniform( + "uTint", + layer.tint ? layer.tint.toArray() : DEFAULT_TINT, + ); + return batcher; + } + + /** + * Draw a TMX layer through the shader path. + * @param {TMXLayer} layer + * @param {object} rect - the visible viewport rect (world coords) + */ + draw(layer, rect) { + const region = this._regionScratch; + let batcher; + + // one pass per tileset — the shader's uGidRange uniform + discard + // hides cells outside the active tileset's GID range + const tilesets = this._tilesetsOf(layer); + for (let i = 0; i < tilesets.length; i++) { + const tileset = tilesets[i]; + // skip collection-of-image tilesets (caller's eligibility check + // should have downgraded these layers to the legacy path, but + // guard defensively — we can't compute uTilesetCols for them) + if (tileset.isCollection || tileset.image === undefined) { + continue; + } + // the view shows nothing of this tileset's tiles + if (!this._region(layer, rect, tileset, region)) { + continue; + } + batcher ??= this._begin(layer); + const indexUnit = this._indexUnit; + + const tsW = tileset.tilewidth; + const tsH = tileset.tileheight; + const margin = tileset.margin; + const spacing = tileset.spacing; + const atlasW = tileset.image.width; + const atlasH = tileset.image.height; + + // atlas geometry (margin + spacing) — number of tiles per row/col + // in the atlas image + const atlasCols = Math.max( + 1, + Math.floor((atlasW - margin * 2 + spacing) / (tsW + spacing)), + ); + const atlasRows = Math.max( + 1, + Math.floor((atlasH - margin * 2 + spacing) / (tsH + spacing)), + ); + + // per-tileset animation lookup. Only allocated/bound for + // tilesets that actually have animated tiles — static tilesets + // pay neither a texture-unit nor an upload. + const tileCount = atlasCols * atlasRows; + const animEntry = this._getOrUpdateAnimLookup(tileset, tileCount); + if (animEntry !== undefined) { + const animUnit = batcher.uploadTexture( + animEntry.resource, + animEntry.tileCount, + 1, + animEntry.dirty, + ); + animEntry.dirty = false; + this.shader.setUniform("uAnimLookup", animUnit); + this.shader.setUniform("uAnimSize", animEntry.tileCount); + } else { + // shader skips the animation lookup when uAnimSize == 0, + // but every declared sampler must still point at a valid + // unit — reuse the index unit so WebGL's draw-time + // validation is happy (the sampler is never read because + // of the uAnimSize guard) + this.shader.setUniform("uAnimLookup", indexUnit); + this.shader.setUniform("uAnimSize", 0); + } + + this._setV2("uTileSize", tsW, tsH); + this._setV2("uTilesetCols", atlasCols, atlasRows); + this._setV2("uInvTilesetSize", 1 / atlasW, 1 / atlasH); + this._setV4("uTilesetMargin", margin, margin, spacing, spacing); + this._setV2("uVisibleStart", region.startX, region.startY); + this._setV2("uVisibleSize", region.sizeX, region.sizeY); + this._setV2("uOverflow", region.overflowX, region.overflowY); + this._setV2("uGidRange", tileset.firstgid, tileset.lastgid); + // the tileset samples its mipmaps when the batcher binds it a + // mip sampler: the "linear-mipmap" filter, on a linear texture + const filter = + typeof tileset.texture.filter !== "undefined" + ? tileset.texture.filter + : batcher.renderer._glTextureFilter(); + this.shader.setUniform( + "uMipmaps", + (filter === this.gl.LINEAR || filter === "linear") && + batcher.renderer.wantsMipmaps(tileset.texture) + ? 1 + : 0, + ); + + // emit one screen-quad. The batcher will bind the tileset's + // TextureAtlas to uSampler (single-texture fallback path is + // active because we have a custom ShaderEffect). + // on whole pixels unless `subPixel` is set, as a sprite is: the + // tiles move with the quad (see Renderer#_pixelSnapBegin) + const snapped = batcher.renderer._pixelSnapBegin( + region.x, + region.y, + region.width, + region.height, + ); + batcher.addQuad( + tileset.texture, + region.x, + region.y, + region.width, + region.height, + 0, + 0, + 1, + 1, + 0xffffffff, + ); + batcher.renderer._pixelSnapEnd(snapped); + + // flush per-pass so each tileset's draw call uses its own + // uniforms (uGidRange / uTileSize / uTilesetCols differ) + batcher.flush(); + } + } +} diff --git a/packages/melonjs/src/video/webgl/shaders/isometric-tmxlayer.frag b/packages/melonjs/src/video/webgl/shaders/isometric-tmxlayer.frag new file mode 100644 index 0000000000..6c7f1ac61f --- /dev/null +++ b/packages/melonjs/src/video/webgl/shaders/isometric-tmxlayer.frag @@ -0,0 +1,67 @@ +// The isometric part of the TMX layer fragment shader: appended to +// `tmxlayer-common.frag`. +// +// A fragment's position is in the layer's pixels, measured across from the +// map's top corner (the renderer takes the map's `originX` and the +// tileset's `tileoffset` off before it gets here), so the cell maths below +// is that of `TMXIsometricRenderer` with no origin in it. +// +// The tile of cell (col, row) is drawn as its whole image, bottom-aligned on +// the cell's diamond: with u = col - row and s = col + row, its left edge is +// at (u - 1) half grid widths and its bottom edge at (s + 2) half grid +// heights. So the cells whose image covers a fragment are a run of `u`, as +// long as the image is wide in half grid widths, and a run of `s`, as long +// as it is tall in half grid heights: `uOverflow` holds the two lengths. +// Only pairs of the same parity are cells. +// +// The CPU renderer draws rows of constant `s` from the top of the screen +// down, each from left to right. The last tile drawn is the one seen, so the +// scan here goes the other way, s high→low then u high→low, and the FIRST +// cell with an opaque texel is the fragment. + +// bounds of the two runs: tiles up to 8 grid heights tall and 4 grid widths +// wide (the layer falls back to the CPU renderer past them) +const int MAX_ROWS = 16; +const int MAX_COLS = 4; + +// the left of the cell's diamond, and as far above its bottom as the tile +// is tall +vec2 tileOrigin(int cx, int cy) { + return vec2( + float(cx - cy - 1) * uCellSize.x * 0.5, + float(cx + cy + 2) * uCellSize.y * 0.5 - uTileSize.y + ); +} + +void main(void) { + vec2 worldPx = uVisibleStart + vRegion * uVisibleSize; + vec2 dWorldX = dFdx(worldPx); + vec2 dWorldY = dFdy(worldPx); + + vec2 halfCell = uCellSize * 0.5; + // the rightmost and the lowest cell whose image can reach the fragment + int uMax = int(floor(worldPx.x / halfCell.x)) + 1; + int sMax = int(floor((worldPx.y + uTileSize.y) / halfCell.y)) - 2; + int spanU = int(uOverflow.x + 0.5); + int spanS = int(uOverflow.y + 0.5); + + vec4 result; + bool found = false; + for (int j = 0; j < MAX_ROWS; j++) { + if (j >= spanS) break; + int s = sMax - j; + // the first u of this row's parity, then every other one + int u = uMax - ((uMax + s) & 1); + for (int i = 0; i < MAX_COLS; i++) { + if (uMax - u >= spanU) break; + if (tryRenderCell((s + u) / 2, (s - u) / 2, worldPx, dWorldX, dWorldY, result)) { + found = true; + break; + } + u -= 2; + } + if (found) break; + } + if (!found) discard; + fragColor = layerColor(result); +} diff --git a/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag b/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag index e81ed0f6fb..b7c7afd905 100644 --- a/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag +++ b/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag @@ -1,14 +1,5 @@ -#version 300 es - -// Fragment shader for the orthogonal TMX layer GPU renderer (WebGL2 / -// GLSL ES 3.00). -// -// Per fragment the shader: -// 1. recovers the world-pixel position from the host UV, -// 2. walks candidate cells (geometric cell + cells whose oversized, -// bottom-aligned tiles could reach this fragment), -// 3. fetches GIDs from the per-layer index texture, and -// 4. samples the tileset atlas at the correct sub-region. +// The orthogonal part of the TMX layer fragment shader: appended to +// `tmxlayer-common.frag`. // // Fast path: when the tileset has no oversized tiles (`uOverflow == (0, 0)`) // — the common case for grid-aligned maps — only the geometric cell can @@ -20,160 +11,17 @@ // Slow path: tiles drawing larger than the cell are bottom-aligned // vertically and left-aligned horizontally. Render order is "right-down": // later cells end up on top, so the candidate loop scans dy high→low, -// dx low→high and picks the FIRST match. -// -// Index texture encoding (`RGBA8`, one cell per texel): -// R = GID low byte -// G = GID high byte (combined: R | (G << 8) = 16-bit GID) -// B = flip mask (bit 0 = H, bit 1 = V, bit 2 = AD) -// A = unused -// -// Animation lookup (`RGBA8`, 1 row, `tileCount` texels wide): per local -// tile id, the CURRENT frame's local id, same R/G byte-pair encoding. -// -// Why `sampler2D` + float decode rather than `usampler2D`: the engine's -// multi-texture default shader declares `uSampler0..uSamplerN-1` as -// `sampler2D` — all of them are active for WebGL's draw-time validation. -// A `usampler2D`-backed `RGBA8UI` texture cached at any of those units -// (units 0..15 on a typical 16-unit fragment stage) would mismatch when -// the default shader next draws sprites, killing every quad with -// `GL_INVALID_OPERATION`. Staying on regular RGBA8 keeps the cache -// path coherent — the cost is one `floor(c * 255 + 0.5)` per fetch. -// -// `texelFetch` is still used (vs `texture()`) for byte-exact reads — -// it bypasses interpolation, so the integer byte values come out -// unmolested even on a normalized-float sampler. - -// The engine's `setPrecision` step injects precision declarations for -// float and int after the `#version` line, using whatever precision the -// renderer was configured with (`highPrecisionShader` setting on the -// Application). Individual shader files don't hardcode precision so the -// engine-wide preference applies. - -in vec2 vRegion; -in vec4 vColor; - -uniform sampler2D uSampler; // tileset atlas (RGBA) -uniform sampler2D uTileIndex; // per-layer GID index (RGBA8) -uniform sampler2D uAnimLookup; // per-tileset animation table (RGBA8) - -uniform vec2 uMapSize; -uniform vec2 uCellSize; -uniform vec2 uTileSize; -uniform vec2 uOverflow; -uniform vec2 uTilesetCols; -uniform vec2 uInvTilesetSize; -uniform vec4 uTilesetMargin; // (marginX, marginY, spacingX, spacingY) -uniform vec2 uGidRange; // (firstgid, lastgid) -uniform vec2 uVisibleStart; -uniform vec2 uVisibleSize; -uniform int uAnimSize; // number of entries in uAnimLookup, 0 if disabled -uniform float uOpacity; -uniform vec4 uTint; -uniform float uMipmaps; // 1 under the "linear-mipmap" filter, else 0 - -out vec4 fragColor; +// dx low→high and picks the FIRST match. `uOverflow` is how many cells a +// tile reaches past its own, across and up. const int MAX_OVERFLOW = 4; -// Try to render the tile at cell (cx, cy) for the current fragment. -// Returns true and writes the sampled color to `outColor` when the cell -// contains a visible, in-range tile whose pixel covers this fragment. -// Identical logic for both fast and slow paths — GLSL inlines this -// trivially so there's no function-call overhead at runtime. -// -// `dWorldX` / `dWorldY` are the world position's screen derivatives, taken -// in `main` where control flow is still uniform: the atlas coordinate jumps -// from one tile to the next, so its own derivatives would pick a tiny mip -// level along every tile edge. A texel of the atlas is a world pixel, so -// the world's derivatives are the atlas's. -bool tryRenderCell(int cx, int cy, vec2 worldPx, vec2 dWorldX, vec2 dWorldY, out vec4 outColor) { - int mapW = int(uMapSize.x); - int mapH = int(uMapSize.y); - if (cx < 0 || cx >= mapW || cy < 0 || cy >= mapH) { - return false; - } - - // `texelFetch` skips filtering — the 8-bit channel values come back - // as normalized floats, decoded to byte ints below - vec4 cellF = texelFetch(uTileIndex, ivec2(cx, cy), 0); - uvec4 cell = uvec4(cellF * 255.0 + 0.5); - int firstGid = int(uGidRange.x); - int gid = int(cell.r) | (int(cell.g) << 8); - if (gid < firstGid || gid > int(uGidRange.y)) { - return false; - } - - vec2 tileWorldOrigin = vec2( +// a tile is bottom-aligned on its cell, and left-aligned +vec2 tileOrigin(int cx, int cy) { + return vec2( float(cx) * uCellSize.x, (float(cy) + 1.0) * uCellSize.y - uTileSize.y ); - vec2 inTile = (worldPx - tileWorldOrigin) / uTileSize; - if (inTile.x < 0.0 || inTile.x >= 1.0 || inTile.y < 0.0 || inTile.y >= 1.0) { - return false; - } - - // flip mask + axis-swap trick: see TMX shader-path flip spec. - // AD performs a transpose; with AD set, H and V swap their effective - // axes (matches the legacy `buildFlipTransform`). - int flipMask = int(cell.b); - float flipH = float(flipMask & 1); - float flipV = float((flipMask >> 1) & 1); - float flipAD = float((flipMask >> 2) & 1); - inTile = mix(inTile, inTile.yx, flipAD); - float effH = mix(flipH, flipV, flipAD); - float effV = mix(flipV, flipH, flipAD); - inTile.x = mix(inTile.x, 1.0 - inTile.x, effH); - inTile.y = mix(inTile.y, 1.0 - inTile.y, effV); - - int localId = gid - firstGid; - - // animation: if the tileset has animated tiles, swap the local id for - // its current frame's id via the lookup texture (CPU updates the - // lookup in lockstep with `tileset.update(dt)`). - if (uAnimSize > 0) { - vec4 animF = texelFetch(uAnimLookup, ivec2(localId, 0), 0); - uvec4 animTexel = uvec4(animF * 255.0 + 0.5); - localId = int(animTexel.r) | (int(animTexel.g) << 8); - } - - float row = floor(float(localId) / uTilesetCols.x); - float col = float(localId) - row * uTilesetCols.x; - vec2 tileOriginPx = uTilesetMargin.xy - + vec2(col, row) * (uTileSize + uTilesetMargin.zw); - vec2 texelPx = tileOriginPx + inTile * uTileSize; - // keep the sample half a texel of the level it reads inside the tile, - // so filtering never blends in the next tile of the tileset, or the - // spacing around it, along the tile's edge: a seam between two tiles - // (never past the tile's centre). Without mipmaps that level is the - // full-size art; under them, the deepest level read is the next one up - // once the level is past a whole number, by more than a rounding error: - // drawn at its size, the derivatives are 1 give or take a few ulps, and - // level 0 it is. Sampled nearest, the sample stays in the same texel - float lod = ceil(log2(max(max(length(dWorldX), length(dWorldY)), 1.0)) - 0.01); - vec2 inset = min(vec2(0.5 * exp2(lod * uMipmaps)), uTileSize * 0.5); - texelPx = clamp(texelPx, tileOriginPx + inset, tileOriginPx + uTileSize - inset); - vec2 texelUV = texelPx * uInvTilesetSize; - - vec4 sampled = textureGrad( - uSampler, - texelUV, - dWorldX * uInvTilesetSize, - dWorldY * uInvTilesetSize - ); - if (sampled.a <= 0.0) { - return false; - } - outColor = sampled; - return true; -} - -// the layer's opacity and tint applied to a texel. The texel is -// premultiplied, as the blending expects: the opacity scales its color as -// well as its alpha. The tint colors it; its alpha is not an opacity, as on -// a sprite -vec4 layerColor(vec4 texel) { - return texel * uOpacity * vec4(uTint.rgb, 1.0); } void main(void) { diff --git a/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag b/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag new file mode 100644 index 0000000000..4458270c3e --- /dev/null +++ b/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag @@ -0,0 +1,168 @@ +#version 300 es + +// Fragment shader for the TMX layer GPU renderers (WebGL2 / GLSL ES 3.00): +// the part every map orientation shares. An orientation's own file follows +// it in the program (`orthogonal-tmxlayer.frag`, `isometric-tmxlayer.frag`) +// and gives the two things that differ: where a cell's tile is drawn +// (`tileOrigin`), and which cells could cover a fragment, front to back +// (`main`). +// +// Per fragment, `main`: +// 1. recovers the fragment's position in the layer from the host UV, +// 2. walks the cells whose tile could cover it, the one drawn last first, +// 3. and for each, `tryRenderCell` fetches the GID from the per-layer +// index texture and samples the tileset atlas at the right sub-region. +// The first cell with a texel that is not transparent is the fragment. +// +// Index texture encoding (`RGBA8`, one cell per texel): +// R = GID low byte +// G = GID high byte (combined: R | (G << 8) = 16-bit GID) +// B = flip mask (bit 0 = H, bit 1 = V, bit 2 = AD) +// A = unused +// +// Animation lookup (`RGBA8`, 1 row, `tileCount` texels wide): per local +// tile id, the CURRENT frame's local id, same R/G byte-pair encoding. +// +// Why `sampler2D` + float decode rather than `usampler2D`: the engine's +// multi-texture default shader declares `uSampler0..uSamplerN-1` as +// `sampler2D` — all of them are active for WebGL's draw-time validation. +// A `usampler2D`-backed `RGBA8UI` texture cached at any of those units +// (units 0..15 on a typical 16-unit fragment stage) would mismatch when +// the default shader next draws sprites, killing every quad with +// `GL_INVALID_OPERATION`. Staying on regular RGBA8 keeps the cache +// path coherent — the cost is one `floor(c * 255 + 0.5)` per fetch. +// +// `texelFetch` is still used (vs `texture()`) for byte-exact reads — +// it bypasses interpolation, so the integer byte values come out +// unmolested even on a normalized-float sampler. + +// The engine's `setPrecision` step injects precision declarations for +// float and int after the `#version` line, using whatever precision the +// renderer was configured with (`highPrecisionShader` setting on the +// Application). Individual shader files don't hardcode precision so the +// engine-wide preference applies. + +in vec2 vRegion; +in vec4 vColor; + +uniform sampler2D uSampler; // tileset atlas (RGBA) +uniform sampler2D uTileIndex; // per-layer GID index (RGBA8) +uniform sampler2D uAnimLookup; // per-tileset animation table (RGBA8) + +uniform vec2 uMapSize; +uniform vec2 uCellSize; +uniform vec2 uTileSize; +uniform vec2 uOverflow; // how far a tile reaches past its cell (see each `main`) +uniform vec2 uTilesetCols; +uniform vec2 uInvTilesetSize; +uniform vec4 uTilesetMargin; // (marginX, marginY, spacingX, spacingY) +uniform vec2 uGidRange; // (firstgid, lastgid) +uniform vec2 uVisibleStart; +uniform vec2 uVisibleSize; +uniform int uAnimSize; // number of entries in uAnimLookup, 0 if disabled +uniform float uOpacity; +uniform vec4 uTint; +uniform float uMipmaps; // 1 under the "linear-mipmap" filter, else 0 + +out vec4 fragColor; + +// Where the tile of cell (cx, cy) is drawn from: the top left corner of its +// image, in the same pixels as `worldPx`. Defined per orientation. +vec2 tileOrigin(int cx, int cy); + +// Try to render the tile at cell (cx, cy) for the current fragment. +// Returns true and writes the sampled color to `outColor` when the cell +// contains a visible, in-range tile whose pixel covers this fragment. +// Identical logic for both fast and slow paths — GLSL inlines this +// trivially so there's no function-call overhead at runtime. +// +// `dWorldX` / `dWorldY` are the world position's screen derivatives, taken +// in `main` where control flow is still uniform: the atlas coordinate jumps +// from one tile to the next, so its own derivatives would pick a tiny mip +// level along every tile edge. A texel of the atlas is a world pixel, so +// the world's derivatives are the atlas's. +bool tryRenderCell(int cx, int cy, vec2 worldPx, vec2 dWorldX, vec2 dWorldY, out vec4 outColor) { + int mapW = int(uMapSize.x); + int mapH = int(uMapSize.y); + if (cx < 0 || cx >= mapW || cy < 0 || cy >= mapH) { + return false; + } + + // `texelFetch` skips filtering — the 8-bit channel values come back + // as normalized floats, decoded to byte ints below + vec4 cellF = texelFetch(uTileIndex, ivec2(cx, cy), 0); + uvec4 cell = uvec4(cellF * 255.0 + 0.5); + int firstGid = int(uGidRange.x); + int gid = int(cell.r) | (int(cell.g) << 8); + if (gid < firstGid || gid > int(uGidRange.y)) { + return false; + } + + vec2 inTile = (worldPx - tileOrigin(cx, cy)) / uTileSize; + if (inTile.x < 0.0 || inTile.x >= 1.0 || inTile.y < 0.0 || inTile.y >= 1.0) { + return false; + } + + // flip mask + axis-swap trick: see TMX shader-path flip spec. + // AD performs a transpose; with AD set, H and V swap their effective + // axes (matches the legacy `buildFlipTransform`). + int flipMask = int(cell.b); + float flipH = float(flipMask & 1); + float flipV = float((flipMask >> 1) & 1); + float flipAD = float((flipMask >> 2) & 1); + inTile = mix(inTile, inTile.yx, flipAD); + float effH = mix(flipH, flipV, flipAD); + float effV = mix(flipV, flipH, flipAD); + inTile.x = mix(inTile.x, 1.0 - inTile.x, effH); + inTile.y = mix(inTile.y, 1.0 - inTile.y, effV); + + int localId = gid - firstGid; + + // animation: if the tileset has animated tiles, swap the local id for + // its current frame's id via the lookup texture (CPU updates the + // lookup in lockstep with `tileset.update(dt)`). + if (uAnimSize > 0) { + vec4 animF = texelFetch(uAnimLookup, ivec2(localId, 0), 0); + uvec4 animTexel = uvec4(animF * 255.0 + 0.5); + localId = int(animTexel.r) | (int(animTexel.g) << 8); + } + + float row = floor(float(localId) / uTilesetCols.x); + float col = float(localId) - row * uTilesetCols.x; + vec2 tileOriginPx = uTilesetMargin.xy + + vec2(col, row) * (uTileSize + uTilesetMargin.zw); + vec2 texelPx = tileOriginPx + inTile * uTileSize; + // keep the sample half a texel of the level it reads inside the tile, + // so filtering never blends in the next tile of the tileset, or the + // spacing around it, along the tile's edge: a seam between two tiles + // (never past the tile's centre). Without mipmaps that level is the + // full-size art; under them, the deepest level read is the next one up + // once the level is past a whole number, by more than a rounding error: + // drawn at its size, the derivatives are 1 give or take a few ulps, and + // level 0 it is. Sampled nearest, the sample stays in the same texel + float lod = ceil(log2(max(max(length(dWorldX), length(dWorldY)), 1.0)) - 0.01); + vec2 inset = min(vec2(0.5 * exp2(lod * uMipmaps)), uTileSize * 0.5); + texelPx = clamp(texelPx, tileOriginPx + inset, tileOriginPx + uTileSize - inset); + vec2 texelUV = texelPx * uInvTilesetSize; + + vec4 sampled = textureGrad( + uSampler, + texelUV, + dWorldX * uInvTilesetSize, + dWorldY * uInvTilesetSize + ); + if (sampled.a <= 0.0) { + return false; + } + outColor = sampled; + return true; +} + +// the layer's opacity and tint applied to a texel. The texel is +// premultiplied, as the blending expects: the opacity scales its color as +// well as its alpha. The tint colors it; its alpha is not an opacity, as on +// a sprite +vec4 layerColor(vec4 texel) { + return texel * uOpacity * vec4(uTint.rgb, 1.0); +} + diff --git a/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.vert b/packages/melonjs/src/video/webgl/shaders/tmxlayer.vert similarity index 100% rename from packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.vert rename to packages/melonjs/src/video/webgl/shaders/tmxlayer.vert diff --git a/packages/melonjs/src/video/webgl/webgl_renderer.js b/packages/melonjs/src/video/webgl/webgl_renderer.js index 2e19c26cb6..173d773aff 100644 --- a/packages/melonjs/src/video/webgl/webgl_renderer.js +++ b/packages/melonjs/src/video/webgl/webgl_renderer.js @@ -46,6 +46,7 @@ import PrimitiveBatcher from "./batchers/primitive_batcher"; import QuadBatcher from "./batchers/quad_batcher"; import { markContextLost } from "./buffer/deletable.js"; import { createLightUniformScratch, packLights } from "./lighting/pack.ts"; +import IsometricTMXLayerGPURenderer from "./renderers/tmxlayer/isometric.js"; import OrthogonalTMXLayerGPURenderer from "./renderers/tmxlayer/orthogonal.js"; import { WebGLTextureStore } from "./texture/store.js"; import { resolveMaxTextures } from "./utils/maxtextures.js"; @@ -71,6 +72,15 @@ import { GLSamplerCache } from "./utils/samplercache.js"; */ // reusable constants for 2D→3D matrix operations +/** + * the GPU tile layer renderer of each map orientation that has one + * @ignore + */ +const TMX_GPU_RENDERERS = { + orthogonal: OrthogonalTMXLayerGPURenderer, + isometric: IsometricTMXLayerGPURenderer, +}; + const _tempMatrix = new Matrix3d(); // pre-allocated matrices for blitEffect (avoids per-frame allocation) @@ -1341,7 +1351,9 @@ export default class WebGLRenderer extends Renderer { // drop every per-layer GPU tilemap texture — tile layers churn on // game reset and we'd otherwise leak a WebGLTexture per layer // across each level transition - this._orthogonalTMXGPURenderer?.reset(); + this._tmxGPURenderers?.forEach((gpu) => { + gpu?.reset(); + }); // initial viewport size this.setViewport(); @@ -1430,7 +1442,7 @@ export default class WebGLRenderer extends Renderer { * @ignore * @internal */ - this._orthogonalTMXGPURenderer = undefined; + this._tmxGPURenderers = undefined; } } @@ -1487,30 +1499,32 @@ export default class WebGLRenderer extends Renderer { * @internal */ _getTMXGPURendererFor(orientation) { - if (orientation === "orthogonal") { - if (this._orthogonalTMXGPURenderer === undefined) { - try { - this._orthogonalTMXGPURenderer = new OrthogonalTMXLayerGPURenderer( - this, - ); - } catch (err) { - // shader compile / link failure on this driver — disable - // the GPU path permanently for the rest of the session - // (the next `drawTileLayer` call falls through to the - // legacy renderer). Stored as `null` so subsequent - // attempts short-circuit without re-trying the failing - // compile every frame. - console.warn( - "melonJS: GPU tilemap shader failed to compile, falling back to legacy renderer", - err, - ); - this._orthogonalTMXGPURenderer = null; - } + const Class = TMX_GPU_RENDERERS[orientation]; + if (Class === undefined) { + // staggered, hexagonal, oblique: drawn by the legacy renderer + return undefined; + } + this._tmxGPURenderers ??= new Map(); + let gpu = this._tmxGPURenderers.get(orientation); + if (gpu === undefined) { + try { + gpu = new Class(this); + } catch (err) { + // shader compile / link failure on this driver — disable + // the GPU path permanently for the rest of the session + // (the next `drawTileLayer` call falls through to the + // legacy renderer). Stored as `null` so subsequent + // attempts short-circuit without re-trying the failing + // compile every frame. + console.warn( + "melonJS: GPU tilemap shader failed to compile, falling back to legacy renderer", + err, + ); + gpu = null; } - return this._orthogonalTMXGPURenderer || undefined; + this._tmxGPURenderers.set(orientation, gpu); } - // isometric / staggered / hexagonal: phase 2 - return undefined; + return gpu || undefined; } /** diff --git a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/isometric.js b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/isometric.js new file mode 100644 index 0000000000..232d8676f6 --- /dev/null +++ b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/isometric.js @@ -0,0 +1,54 @@ +import { isometricRegion } from "../../../gpu/tmxlayer_region.js"; +import isometricWGSL from "../../shaders/tmxlayer-isometric.wgsl"; +import TMXLayerGPURenderer from "./tmxlayer.js"; + +/** + * additional imports for TypeScript + * @import { default as TMXLayer } from "../../../../level/tiled/TMXLayer.js"; + * @import { default as WebGPURenderer } from "../../webgpu_renderer.js"; + */ + +/** + * GPU-accelerated renderer for isometric TMX tile layers on the WebGPU + * backend: the shared {@link TMXLayerGPURenderer}, with an isometric grid's + * geometry. The quad covers the view, and the shader works in pixels. + * + * Isometric tiles overlap, and the shader keeps the one drawn last among + * those of ONE tileset: a layer takes this path only when all its tiles + * come from the same one (`layer._shaderTileset`, set by the layer when it + * is found eligible). + * @ignore + * @internal + */ +export default class IsometricTMXLayerGPURenderer extends TMXLayerGPURenderer { + /** + * @param {WebGPURenderer} renderer - the WebGPU renderer instance + */ + constructor(renderer) { + super(renderer, isometricWGSL); + this.tilesets = []; + } + + /** + * @ignore + * @internal + */ + region(layer, rect, tileset, out) { + return isometricRegion(layer, rect, tileset, out); + } + + /** + * The one tileset an isometric layer on this path draws with. + * @param {TMXLayer} layer - the layer + * @returns {object[]} that tileset, or none for an empty layer + * @ignore + * @internal + */ + tilesetsOf(layer) { + this.tilesets.length = 0; + if (layer._shaderTileset) { + this.tilesets.push(layer._shaderTileset); + } + return this.tilesets; + } +} diff --git a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/orthogonal.js b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/orthogonal.js index bd861bc1ec..3131648d73 100644 --- a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/orthogonal.js +++ b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/orthogonal.js @@ -1,462 +1,33 @@ -import VertexArrayBuffer from "../../../buffer/vertex.js"; -import { transformQuadCorners } from "../../../gpu/quadcorners.ts"; -import tmxLayerWGSL from "../../shaders/tmxlayer.wgsl"; +import { orthogonalRegion } from "../../../gpu/tmxlayer_region.js"; +import orthogonalWGSL from "../../shaders/tmxlayer-orthogonal.wgsl"; +import TMXLayerGPURenderer from "./tmxlayer.js"; /** * additional imports for TypeScript - * @import { default as TMXLayer } from "../../../../level/tiled/TMXLayer.js"; * @import { default as WebGPURenderer } from "../../webgpu_renderer.js"; */ -/** - * byte size of the TMXUniforms block (see tmxlayer.wgsl): ten vec2f (80) - * interleaved with one vec4f at a 16-aligned offset, two f32, the vec4f - * tint (ending at 112) and the mipmap flag, rounded up to the block's - * 16-byte alignment → 128 bytes - * @ignore - * @internal - */ -const TMX_UNIFORM_SIZE = 128; - /** * GPU-accelerated renderer for orthogonal TMX tile layers on the WebGPU - * backend — the WGSL realization of the WebGL shader tile path: the - * visible region draws as ONE quad per tileset, with the fragment shader - * sampling a per-layer GID index texture and the tileset atlas. - * - * Structure mirrors the WebGL twin; the mechanisms are the backend's: - * - the GID index / animation lookup are renderer-owned `rgba8unorm` - * textures written with `queue.writeTexture` (byte-exact `textureLoad` - * reads, no sampler needed) — re-uploaded only when `layer.dataVersion` - * moves or an animation frame advances - * - the per-draw uniforms snapshot into the effect uniform arena with a - * dynamic offset (each tileset pass gets its own region, per the - * queue-write-before-draws ordering) - * - the tileset atlas rides the standard texture-store material path - * - pipelines flow through the registered `tmxlayer.wgsl` family, keyed - * on the frame's blend/stencil state like every other draw + * backend: the shared {@link TMXLayerGPURenderer}, with an orthogonal grid's + * geometry. The quad covers the cells in view, and the shader works in + * cells. * @ignore * @internal */ -export default class OrthogonalTMXLayerGPURenderer { +export default class OrthogonalTMXLayerGPURenderer extends TMXLayerGPURenderer { /** * @param {WebGPURenderer} renderer - the WebGPU renderer instance */ constructor(renderer) { - this.renderer = renderer; - const cache = renderer.pipelineCache; - - // group-3 shape: the uniform block + the two lookup textures - this.tmxLayout = cache.getEffectLayout("tmx", [ - { - binding: 0, - visibility: GPUShaderStage.FRAGMENT, - buffer: { - type: "uniform", - hasDynamicOffset: true, - minBindingSize: TMX_UNIFORM_SIZE, - }, - }, - { binding: 1, visibility: GPUShaderStage.FRAGMENT, texture: {} }, - { binding: 2, visibility: GPUShaderStage.FRAGMENT, texture: {} }, - ]); - this.key = cache.registerShader(tmxLayerWGSL, { - bindGroupLayouts: [ - cache.frameLayout, - cache.materialLayout, - cache.emptyLayout, - this.tmxLayout, - ], - vertexLayoutKey: "quad", - label: "melonJS tmx layer shader", - }); - // the pipeline cache this state was built against — a device loss - // rebuilds the cache, and the renderer drops this instance with it - this.epoch = cache.epoch; - - /** - * per-layer GID index texture: layer → {texture, view, version} - * @type {Map} - */ - this.resources = new Map(); - /** - * per-tileset animation lookup: tileset → {texture, view, data, tileCount} - * @type {Map} - */ - this.animLookups = new Map(); - /** - * per-(layer, tileset) cached group-3 bind group, keyed on the - * uniform arena page it binds - * @type {Map>} - */ - this.bindGroups = new Map(); - - // CPU staging for the uniform block; the quad's vertices are staged - // in the quad batcher's own layout, sized on first draw - this.uniformScratch = new Float32Array(TMX_UNIFORM_SIZE / 4); - /** - * @type {VertexArrayBuffer|undefined} - */ - this.vertexScratch = undefined; + super(renderer, orthogonalWGSL); } /** - * Retire every lookup texture and empty the local maps — called from - * `WebGPURenderer.reset()` (GAME_RESET) so each level starts clean. * @ignore * @internal */ - reset() { - for (const resource of this.resources.values()) { - this.renderer.retireTexture(resource.texture); - } - for (const entry of this.animLookups.values()) { - this.renderer.retireTexture(entry.texture); - } - this.resources.clear(); - this.animLookups.clear(); - this.bindGroups.clear(); - } - - /** - * Get-or-create the per-layer GID index texture, re-uploading when the - * layer's data version moved (`setTile`/`clearTile` mutations). - * @param {TMXLayer} layer - the layer - * @returns {{texture: GPUTexture, view: GPUTextureView, version: number}} - * @ignore - * @internal - */ - getResource(layer) { - const device = this.renderer.device; - let resource = this.resources.get(layer); - if (resource === undefined) { - const texture = device.createTexture({ - label: "melonJS tmx index", - size: [layer.cols, layer.rows], - format: "rgba8unorm", - usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST, - }); - resource = { texture, view: texture.createView(), version: -1 }; - this.resources.set(layer, resource); - } - if (resource.version !== layer.dataVersion) { - // reinterpret the layer's Uint16Array payload as RGBA bytes - // (zero-copy view; little-endian = every browser) - device.queue.writeTexture( - { texture: resource.texture }, - new Uint8Array(layer.layerData.buffer), - { bytesPerRow: layer.cols * 4, rowsPerImage: layer.rows }, - [layer.cols, layer.rows], - ); - resource.version = layer.dataVersion; - } - return resource; - } - - /** - * Get-or-update the per-tileset animation lookup (undefined for - * tilesets without animated tiles). Texel `localId` encodes the - * CURRENT frame's local id as (R = lo byte, G = hi byte). - * @param {object} tileset - the tileset - * @param {number} tileCount - atlas grid tile count - * @returns {object|undefined} the lookup entry - * @ignore - * @internal - */ - getOrUpdateAnimLookup(tileset, tileCount) { - if (!tileset.isAnimated || tileset.animations.size === 0) { - return undefined; - } - const device = this.renderer.device; - let entry = this.animLookups.get(tileset); - if (entry === undefined) { - // identity mapping (localId → localId); animated entries get - // overwritten below - const data = new Uint8Array(tileCount * 4); - for (let id = 0; id < tileCount; id++) { - data[id * 4 + 0] = id & 0xff; - data[id * 4 + 1] = (id >> 8) & 0xff; - } - const texture = device.createTexture({ - label: "melonJS tmx anim lookup", - size: [tileCount, 1], - format: "rgba8unorm", - usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST, - }); - entry = { - texture, - view: texture.createView(), - data, - tileCount, - uploaded: false, - }; - this.animLookups.set(tileset, entry); - } - // rewrite current-frame ids; upload only when a texel changed - // (tileset.update advanced a frame). At most one upload per frame — - // queue writes execute before every recorded draw, and animation - // state only moves in the update phase, so the frame stays coherent. - const data = entry.data; - let dirty = !entry.uploaded; - for (const [localId, anim] of tileset.animations) { - const off = localId * 4; - const cur = anim.cur.tileid; - const lo = cur & 0xff; - const hi = (cur >> 8) & 0xff; - if (data[off] !== lo || data[off + 1] !== hi) { - data[off] = lo; - data[off + 1] = hi; - dirty = true; - } - } - if (dirty) { - device.queue.writeTexture( - { texture: entry.texture }, - data, - { bytesPerRow: entry.tileCount * 4, rowsPerImage: 1 }, - [entry.tileCount, 1], - ); - entry.uploaded = true; - } - return entry; - } - - /** - * Draw an orthogonal TMX layer through the shader path. - * @param {TMXLayer} layer - the layer to draw - * @param {object} rect - the visible viewport rect (world coords) - */ - draw(layer, rect) { - const renderer = this.renderer; - const tileWidth = layer.tilewidth; - const tileHeight = layer.tileheight; - const cols = layer.cols; - const rows = layer.rows; - - // visible region in tile-coord space - const startTileX = Math.max(0, Math.floor(rect.pos.x / tileWidth)); - const startTileY = Math.max(0, Math.floor(rect.pos.y / tileHeight)); - const endTileX = Math.min( - cols, - Math.ceil((rect.pos.x + rect.width) / tileWidth), - ); - const endTileY = Math.min( - rows, - Math.ceil((rect.pos.y + rect.height) / tileHeight), - ); - if (endTileX <= startTileX || endTileY <= startTileY) { - return; - } - - const worldX = startTileX * tileWidth; - const worldY = startTileY * tileHeight; - const worldW = (endTileX - startTileX) * tileWidth; - const worldH = (endTileY - startTileY) * tileHeight; - - // drain pending sprite vertices under their own pipeline, then use - // the quad batcher's frozen machinery (staging layout, index buffer) - const batcher = renderer.setBatcher("quad"); - batcher.flush(); - const pass = renderer.ensurePass(); - const device = renderer.device; - - const resource = this.getResource(layer); - - // the transformed quad, shared by every tileset pass, in the quad - // batcher's layout (its pipelines ride that layout). The shader keeps - // to its own tiles, so the quad carries no frame - // on whole pixels unless `subPixel` is set, as a sprite is: the tiles - // move with the quad (see Renderer#_pixelSnapBegin) - const snapped = renderer._pixelSnapBegin(worldX, worldY, worldW, worldH); - const corners = transformQuadCorners( - renderer.currentTransform, - worldX, - worldY, - worldW, - worldH, - renderer.currentDepth, - ); - renderer._pixelSnapEnd(snapped); - if (this.vertexScratch?.vertexSize !== batcher.vertexSize) { - this.vertexScratch = new VertexArrayBuffer(batcher.vertexSize, 4); - } - const vertices = this.vertexScratch; - vertices.clear(); - vertices.pushSpriteQuad( - corners[0], - corners[1], - corners[2], - corners[3], - 0, - 0, - 1, - 1, - 0xffffffff, - 0, - false, - ); - const byteLength = 4 * batcher.stride; - const vertexRegion = renderer.vertexArena.alloc(byteLength); - device.queue.writeBuffer( - vertexRegion.buffer, - vertexRegion.offset, - vertices.toUint8(), - 0, - byteLength, - ); - - // layer-level uniform values (identical for every tileset pass) - const scratch = this.uniformScratch; - scratch[0] = cols; - scratch[1] = rows; - scratch[2] = tileWidth; - scratch[3] = tileHeight; - scratch[18] = startTileX; - scratch[19] = startTileY; - scratch[20] = endTileX - startTileX; - scratch[21] = endTileY - startTileY; - // the layer's opacity, and the fade of everything it is drawn in: - // set on the renderer by the layer's preDraw - scratch[22] = renderer.getGlobalAlpha(); - const tint = layer.tint ? layer.tint.toArray() : null; - scratch[24] = tint ? tint[0] : 1; - scratch[25] = tint ? tint[1] : 1; - scratch[26] = tint ? tint[2] : 1; - scratch[27] = tint ? tint[3] : 1; - - const pipeline = renderer.pipelineCache.get( - this.key, - "triangle-list", - renderer.currentBlendMode, - renderer.premultipliedAlpha, - renderer.stencilMode, - ); - if (pipeline !== renderer.currentPipeline) { - pass.setPipeline(pipeline); - renderer.currentPipeline = pipeline; - } - const frame = renderer.currentFrameBinding; - pass.setBindGroup(0, frame.bindGroup, [frame.dynamicOffset]); - pass.setBindGroup(2, renderer.pipelineCache.emptyBindGroup); - pass.setVertexBuffer( - 0, - vertexRegion.buffer, - vertexRegion.offset, - byteLength, - ); - pass.setIndexBuffer(batcher.indexBuffer, "uint32"); - - // one pass per tileset — the GID-range guard hides other tilesets - const tilesets = layer.tilesets.tilesets; - let layerGroups = this.bindGroups.get(layer); - if (layerGroups === undefined) { - layerGroups = new Map(); - this.bindGroups.set(layer, layerGroups); - } - for (let i = 0; i < tilesets.length; i++) { - const tileset = tilesets[i]; - // eligibility should have excluded these — guard defensively - if (tileset.isCollection || tileset.image === undefined) { - continue; - } - - const tsW = tileset.tilewidth; - const tsH = tileset.tileheight; - const margin = tileset.margin; - const spacing = tileset.spacing; - const atlasW = tileset.image.width; - const atlasH = tileset.image.height; - const atlasCols = Math.max( - 1, - Math.floor((atlasW - margin * 2 + spacing) / (tsW + spacing)), - ); - const atlasRows = Math.max( - 1, - Math.floor((atlasH - margin * 2 + spacing) / (tsH + spacing)), - ); - - const animEntry = this.getOrUpdateAnimLookup( - tileset, - atlasCols * atlasRows, - ); - // every declared binding needs a view — reuse the index texture - // as the filler (never read: uAnimSize stays 0) - const animView = animEntry ? animEntry.view : resource.view; - - // per-tileset uniform values + snapshot into the arena - scratch[4] = tsW; - scratch[5] = tsH; - scratch[6] = Math.max(0, Math.ceil(tsW / tileWidth) - 1); - scratch[7] = Math.max(0, Math.ceil(tsH / tileHeight) - 1); - scratch[8] = atlasCols; - scratch[9] = atlasRows; - scratch[10] = 1 / atlasW; - scratch[11] = 1 / atlasH; - scratch[12] = margin; - scratch[13] = margin; - scratch[14] = spacing; - scratch[15] = spacing; - scratch[16] = tileset.firstgid; - scratch[17] = tileset.lastgid; - scratch[23] = animEntry ? animEntry.tileCount : 0; - // the "linear-mipmap" filter, on a linear tileset: the binding - // below asks the store for a chain, and the shader keeps each - // sample clear of the neighbouring tiles at the level it reads - const filter = - typeof tileset.texture.filter === "string" - ? tileset.texture.filter - : renderer.getDefaultTextureFilter(); - const mipmaps = - filter === "linear" && renderer.wantsMipmaps(tileset.texture); - scratch[28] = mipmaps ? 1 : 0; - const region = renderer.effectUniformArena.alloc( - TMX_UNIFORM_SIZE, - device.limits?.minUniformBufferOffsetAlignment ?? 256, - ); - device.queue.writeBuffer( - region.buffer, - region.offset, - scratch.buffer, - 0, - TMX_UNIFORM_SIZE, - ); - - // group-3 bind group, cached per (layer, tileset) while the - // arena page and lookup views stay the same - let cached = layerGroups.get(tileset); - if ( - cached === undefined || - cached.pageLabel !== region.buffer.label || - cached.animView !== animView - ) { - cached = { - pageLabel: region.buffer.label, - animView, - bindGroup: device.createBindGroup({ - label: "melonJS tmx binding", - layout: this.tmxLayout, - entries: [ - { - binding: 0, - resource: { - buffer: region.buffer, - size: TMX_UNIFORM_SIZE, - }, - }, - { binding: 1, resource: resource.view }, - { binding: 2, resource: animView }, - ], - }), - }; - layerGroups.set(tileset, cached); - } - - pass.setBindGroup( - 1, - renderer.textureStore.getBinding(tileset.texture, { mipmaps }), - ); - pass.setBindGroup(3, cached.bindGroup, [region.offset]); - pass.drawIndexed(6); - } + region(layer, rect, tileset, out) { + return orthogonalRegion(layer, rect, tileset, out); } } diff --git a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js new file mode 100644 index 0000000000..9ca29e901a --- /dev/null +++ b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js @@ -0,0 +1,515 @@ +import VertexArrayBuffer from "../../../buffer/vertex.js"; +import { transformQuadCorners } from "../../../gpu/quadcorners.ts"; +import { createRegion } from "../../../gpu/tmxlayer_region.js"; +import tmxLayerCommonWGSL from "../../shaders/tmxlayer-common.wgsl"; + +/** + * additional imports for TypeScript + * @import { default as TMXLayer } from "../../../../level/tiled/TMXLayer.js"; + * @import { default as WebGPURenderer } from "../../webgpu_renderer.js"; + */ + +/** + * byte size of the TMXUniforms block (see tmxlayer-common.wgsl): ten vec2f (80) + * interleaved with one vec4f at a 16-aligned offset, two f32, the vec4f + * tint (ending at 112) and the mipmap flag, rounded up to the block's + * 16-byte alignment → 128 bytes + * @ignore + * @internal + */ +const TMX_UNIFORM_SIZE = 128; + +/** + * GPU-accelerated renderer for TMX tile layers on the WebGPU backend, the + * part every map orientation shares — the WGSL realization of the WebGL + * shader tile path: the visible region draws as ONE quad per tileset, with + * the fragment shader sampling a per-layer GID index texture and the + * tileset atlas. + * + * An orientation's renderer extends this one with the two things that + * differ: its part of the shader (where a cell's tile is, and which cells + * can cover a fragment), and {@link TMXLayerGPURenderer#region}, the part of + * the layer a view shows. + * + * Structure mirrors the WebGL twin; the mechanisms are the backend's: + * - the GID index / animation lookup are renderer-owned `rgba8unorm` + * textures written with `queue.writeTexture` (byte-exact `textureLoad` + * reads, no sampler needed) — re-uploaded only when `layer.dataVersion` + * moves or an animation frame advances + * - the per-draw uniforms snapshot into the effect uniform arena with a + * dynamic offset (each tileset pass gets its own region, per the + * queue-write-before-draws ordering) + * - the tileset atlas rides the standard texture-store material path + * - pipelines flow through the registered shader family (the shared + * module text plus the orientation's), keyed on the frame's blend/stencil + * state like every other draw + * @ignore + * @internal + */ +export default class TMXLayerGPURenderer { + /** + * @param {WebGPURenderer} renderer - the WebGPU renderer instance + * @param {string} wgsl - the orientation's part of the shader module, + * appended to the shared one + */ + constructor(renderer, wgsl) { + this.renderer = renderer; + const cache = renderer.pipelineCache; + + // group-3 shape: the uniform block + the two lookup textures + this.tmxLayout = cache.getEffectLayout("tmx", [ + { + binding: 0, + visibility: GPUShaderStage.FRAGMENT, + buffer: { + type: "uniform", + hasDynamicOffset: true, + minBindingSize: TMX_UNIFORM_SIZE, + }, + }, + { binding: 1, visibility: GPUShaderStage.FRAGMENT, texture: {} }, + { binding: 2, visibility: GPUShaderStage.FRAGMENT, texture: {} }, + ]); + this.key = cache.registerShader(`${tmxLayerCommonWGSL}\n${wgsl}`, { + bindGroupLayouts: [ + cache.frameLayout, + cache.materialLayout, + cache.emptyLayout, + this.tmxLayout, + ], + vertexLayoutKey: "quad", + label: "melonJS tmx layer shader", + }); + // the pipeline cache this state was built against — a device loss + // rebuilds the cache, and the renderer drops this instance with it + this.epoch = cache.epoch; + + /** + * per-layer GID index texture: layer → {texture, view, version} + * @type {Map} + */ + this.resources = new Map(); + /** + * per-tileset animation lookup: tileset → {texture, view, data, tileCount} + * @type {Map} + */ + this.animLookups = new Map(); + /** + * per-(layer, tileset) cached group-3 bind group, keyed on the + * uniform arena page it binds + * @type {Map>} + */ + this.bindGroups = new Map(); + + // CPU staging for the uniform block; the quad's vertices are staged + // in the quad batcher's own layout, sized on first draw + this.uniformScratch = new Float32Array(TMX_UNIFORM_SIZE / 4); + /** + * @type {VertexArrayBuffer|undefined} + */ + this.vertexScratch = undefined; + /** + * what {@link TMXLayerGPURenderer#region} fills + * @ignore + * @internal + */ + this.regionScratch = createRegion(); + } + + /** + * The part of a layer a view shows, for the tiles of one tileset (see + * `gpu/tmxlayer_region.js`). The quad it gives must be the same for + * every tileset of a layer: it is written once, for the first one. + * @param {TMXLayer} layer - the layer + * @param {object} rect - the visible viewport rect (world coords) + * @param {object} tileset - the tileset whose tiles are drawn + * @param {object} out - the region to fill + * @returns {boolean} false when the view shows nothing of the layer + * @abstract + * @ignore + * @internal + */ + // eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars + region(layer, rect, tileset, out) { + return false; + } + + /** + * The tilesets a layer is drawn with, one pass each. + * @param {TMXLayer} layer - the layer + * @returns {object[]} the tilesets + * @ignore + * @internal + */ + tilesetsOf(layer) { + return layer.tilesets.tilesets; + } + + /** + * Retire every lookup texture and empty the local maps — called from + * `WebGPURenderer.reset()` (GAME_RESET) so each level starts clean. + * @ignore + * @internal + */ + reset() { + for (const resource of this.resources.values()) { + this.renderer.retireTexture(resource.texture); + } + for (const entry of this.animLookups.values()) { + this.renderer.retireTexture(entry.texture); + } + this.resources.clear(); + this.animLookups.clear(); + this.bindGroups.clear(); + } + + /** + * Get-or-create the per-layer GID index texture, re-uploading when the + * layer's data version moved (`setTile`/`clearTile` mutations). + * @param {TMXLayer} layer - the layer + * @returns {{texture: GPUTexture, view: GPUTextureView, version: number}} + * @ignore + * @internal + */ + getResource(layer) { + const device = this.renderer.device; + let resource = this.resources.get(layer); + if (resource === undefined) { + const texture = device.createTexture({ + label: "melonJS tmx index", + size: [layer.cols, layer.rows], + format: "rgba8unorm", + usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST, + }); + resource = { texture, view: texture.createView(), version: -1 }; + this.resources.set(layer, resource); + } + if (resource.version !== layer.dataVersion) { + // reinterpret the layer's Uint16Array payload as RGBA bytes + // (zero-copy view; little-endian = every browser) + device.queue.writeTexture( + { texture: resource.texture }, + new Uint8Array(layer.layerData.buffer), + { bytesPerRow: layer.cols * 4, rowsPerImage: layer.rows }, + [layer.cols, layer.rows], + ); + resource.version = layer.dataVersion; + } + return resource; + } + + /** + * Get-or-update the per-tileset animation lookup (undefined for + * tilesets without animated tiles). Texel `localId` encodes the + * CURRENT frame's local id as (R = lo byte, G = hi byte). + * @param {object} tileset - the tileset + * @param {number} tileCount - atlas grid tile count + * @returns {object|undefined} the lookup entry + * @ignore + * @internal + */ + getOrUpdateAnimLookup(tileset, tileCount) { + if (!tileset.isAnimated || tileset.animations.size === 0) { + return undefined; + } + const device = this.renderer.device; + let entry = this.animLookups.get(tileset); + if (entry === undefined) { + // identity mapping (localId → localId); animated entries get + // overwritten below + const data = new Uint8Array(tileCount * 4); + for (let id = 0; id < tileCount; id++) { + data[id * 4 + 0] = id & 0xff; + data[id * 4 + 1] = (id >> 8) & 0xff; + } + const texture = device.createTexture({ + label: "melonJS tmx anim lookup", + size: [tileCount, 1], + format: "rgba8unorm", + usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST, + }); + entry = { + texture, + view: texture.createView(), + data, + tileCount, + uploaded: false, + }; + this.animLookups.set(tileset, entry); + } + // rewrite current-frame ids; upload only when a texel changed + // (tileset.update advanced a frame). At most one upload per frame — + // queue writes execute before every recorded draw, and animation + // state only moves in the update phase, so the frame stays coherent. + const data = entry.data; + let dirty = !entry.uploaded; + for (const [localId, anim] of tileset.animations) { + const off = localId * 4; + const cur = anim.cur.tileid; + const lo = cur & 0xff; + const hi = (cur >> 8) & 0xff; + if (data[off] !== lo || data[off + 1] !== hi) { + data[off] = lo; + data[off + 1] = hi; + dirty = true; + } + } + if (dirty) { + device.queue.writeTexture( + { texture: entry.texture }, + data, + { bytesPerRow: entry.tileCount * 4, rowsPerImage: 1 }, + [entry.tileCount, 1], + ); + entry.uploaded = true; + } + return entry; + } + + /** + * Start a layer's draw, for the first tileset the view shows something + * of: the quad (shared by every tileset pass), the layer's uniform + * values, the pipeline and the bindings that do not change per tileset. + * @param {TMXLayer} layer - the layer + * @param {object} region - the region of that first tileset + * @returns {object} what the tileset passes need: the pass, the index + * texture's resource and the layer's bind group cache + * @ignore + * @internal + */ + begin(layer, region) { + const renderer = this.renderer; + + // drain pending sprite vertices under their own pipeline, then use + // the quad batcher's frozen machinery (staging layout, index buffer) + const batcher = renderer.setBatcher("quad"); + batcher.flush(); + const pass = renderer.ensurePass(); + const device = renderer.device; + + const resource = this.getResource(layer); + + // the transformed quad, shared by every tileset pass, in the quad + // batcher's layout (its pipelines ride that layout). The shader keeps + // to its own tiles, so the quad carries no frame + // on whole pixels unless `subPixel` is set, as a sprite is: the tiles + // move with the quad (see Renderer#_pixelSnapBegin) + const snapped = renderer._pixelSnapBegin( + region.x, + region.y, + region.width, + region.height, + ); + const corners = transformQuadCorners( + renderer.currentTransform, + region.x, + region.y, + region.width, + region.height, + renderer.currentDepth, + ); + renderer._pixelSnapEnd(snapped); + if (this.vertexScratch?.vertexSize !== batcher.vertexSize) { + this.vertexScratch = new VertexArrayBuffer(batcher.vertexSize, 4); + } + const vertices = this.vertexScratch; + vertices.clear(); + vertices.pushSpriteQuad( + corners[0], + corners[1], + corners[2], + corners[3], + 0, + 0, + 1, + 1, + 0xffffffff, + 0, + false, + ); + const byteLength = 4 * batcher.stride; + const vertexRegion = renderer.vertexArena.alloc(byteLength); + device.queue.writeBuffer( + vertexRegion.buffer, + vertexRegion.offset, + vertices.toUint8(), + 0, + byteLength, + ); + + // layer-level uniform values (identical for every tileset pass) + const scratch = this.uniformScratch; + scratch[0] = layer.cols; + scratch[1] = layer.rows; + scratch[2] = layer.tilewidth; + scratch[3] = layer.tileheight; + // the layer's opacity, and the fade of everything it is drawn in: + // set on the renderer by the layer's preDraw + scratch[22] = renderer.getGlobalAlpha(); + const tint = layer.tint ? layer.tint.toArray() : null; + scratch[24] = tint ? tint[0] : 1; + scratch[25] = tint ? tint[1] : 1; + scratch[26] = tint ? tint[2] : 1; + scratch[27] = tint ? tint[3] : 1; + + const pipeline = renderer.pipelineCache.get( + this.key, + "triangle-list", + renderer.currentBlendMode, + renderer.premultipliedAlpha, + renderer.stencilMode, + ); + if (pipeline !== renderer.currentPipeline) { + pass.setPipeline(pipeline); + renderer.currentPipeline = pipeline; + } + const frame = renderer.currentFrameBinding; + pass.setBindGroup(0, frame.bindGroup, [frame.dynamicOffset]); + pass.setBindGroup(2, renderer.pipelineCache.emptyBindGroup); + pass.setVertexBuffer( + 0, + vertexRegion.buffer, + vertexRegion.offset, + byteLength, + ); + pass.setIndexBuffer(batcher.indexBuffer, "uint32"); + + let layerGroups = this.bindGroups.get(layer); + if (layerGroups === undefined) { + layerGroups = new Map(); + this.bindGroups.set(layer, layerGroups); + } + return { pass, resource, layerGroups }; + } + + /** + * Draw a TMX layer through the shader path. + * @param {TMXLayer} layer - the layer to draw + * @param {object} rect - the visible viewport rect (world coords) + */ + draw(layer, rect) { + const renderer = this.renderer; + const device = renderer.device; + const scratch = this.uniformScratch; + const region = this.regionScratch; + let begun; + + // one pass per tileset — the GID-range guard hides other tilesets + const tilesets = this.tilesetsOf(layer); + for (let i = 0; i < tilesets.length; i++) { + const tileset = tilesets[i]; + // eligibility should have excluded these — guard defensively + if (tileset.isCollection || tileset.image === undefined) { + continue; + } + // the view shows nothing of this tileset's tiles + if (!this.region(layer, rect, tileset, region)) { + continue; + } + begun ??= this.begin(layer, region); + const { pass, resource, layerGroups } = begun; + + const tsW = tileset.tilewidth; + const tsH = tileset.tileheight; + const margin = tileset.margin; + const spacing = tileset.spacing; + const atlasW = tileset.image.width; + const atlasH = tileset.image.height; + const atlasCols = Math.max( + 1, + Math.floor((atlasW - margin * 2 + spacing) / (tsW + spacing)), + ); + const atlasRows = Math.max( + 1, + Math.floor((atlasH - margin * 2 + spacing) / (tsH + spacing)), + ); + + const animEntry = this.getOrUpdateAnimLookup( + tileset, + atlasCols * atlasRows, + ); + // every declared binding needs a view — reuse the index texture + // as the filler (never read: uAnimSize stays 0) + const animView = animEntry ? animEntry.view : resource.view; + + // per-tileset uniform values + snapshot into the arena + scratch[4] = tsW; + scratch[5] = tsH; + scratch[6] = region.overflowX; + scratch[7] = region.overflowY; + scratch[8] = atlasCols; + scratch[9] = atlasRows; + scratch[10] = 1 / atlasW; + scratch[11] = 1 / atlasH; + scratch[12] = margin; + scratch[13] = margin; + scratch[14] = spacing; + scratch[15] = spacing; + scratch[16] = tileset.firstgid; + scratch[17] = tileset.lastgid; + scratch[18] = region.startX; + scratch[19] = region.startY; + scratch[20] = region.sizeX; + scratch[21] = region.sizeY; + scratch[23] = animEntry ? animEntry.tileCount : 0; + // the "linear-mipmap" filter, on a linear tileset: the binding + // below asks the store for a chain, and the shader keeps each + // sample clear of the neighbouring tiles at the level it reads + const filter = + typeof tileset.texture.filter === "string" + ? tileset.texture.filter + : renderer.getDefaultTextureFilter(); + const mipmaps = + filter === "linear" && renderer.wantsMipmaps(tileset.texture); + scratch[28] = mipmaps ? 1 : 0; + const uniforms = renderer.effectUniformArena.alloc( + TMX_UNIFORM_SIZE, + device.limits?.minUniformBufferOffsetAlignment ?? 256, + ); + device.queue.writeBuffer( + uniforms.buffer, + uniforms.offset, + scratch.buffer, + 0, + TMX_UNIFORM_SIZE, + ); + + // group-3 bind group, cached per (layer, tileset) while the + // arena page and lookup views stay the same + let cached = layerGroups.get(tileset); + if ( + cached === undefined || + cached.pageLabel !== uniforms.buffer.label || + cached.animView !== animView + ) { + cached = { + pageLabel: uniforms.buffer.label, + animView, + bindGroup: device.createBindGroup({ + label: "melonJS tmx binding", + layout: this.tmxLayout, + entries: [ + { + binding: 0, + resource: { + buffer: uniforms.buffer, + size: TMX_UNIFORM_SIZE, + }, + }, + { binding: 1, resource: resource.view }, + { binding: 2, resource: animView }, + ], + }), + }; + layerGroups.set(tileset, cached); + } + + pass.setBindGroup( + 1, + renderer.textureStore.getBinding(tileset.texture, { mipmaps }), + ); + pass.setBindGroup(3, cached.bindGroup, [uniforms.offset]); + pass.drawIndexed(6); + } + } +} diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl similarity index 73% rename from packages/melonjs/src/video/webgpu/shaders/tmxlayer.wgsl rename to packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl index d892c921bb..fda81795e1 100644 --- a/packages/melonjs/src/video/webgpu/shaders/tmxlayer.wgsl +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl @@ -1,12 +1,15 @@ -// melonJS WebGPU orthogonal TMX layer shader — the WGSL port of -// orthogonal-tmxlayer.vert/frag (WebGL2). +// melonJS WebGPU TMX layer shader — the WGSL port of tmxlayer.vert and +// tmxlayer-common.frag (WebGL2): the part every map orientation shares. An +// orientation's own file follows it in the module (tmxlayer-orthogonal.wgsl, +// tmxlayer-isometric.wgsl) and gives the two things that differ: where a +// cell's tile is drawn (`tileOrigin`), and which cells could cover a +// fragment, front to back (`fragment_main`). // -// Per fragment: recover the world-pixel position from the quad UV, walk -// candidate cells (the geometric cell, plus cells whose oversized -// bottom-aligned tiles could reach this fragment), fetch GIDs from the -// per-layer index texture, and sample the tileset atlas at the correct -// sub-region. One draw per tileset — the GID-range guard discards cells -// belonging to other tilesets. +// Per fragment: recover the fragment's position in the layer from the quad +// UV, walk the cells whose tile could cover it, the one drawn last first, +// fetch GIDs from the per-layer index texture, and sample the tileset atlas +// at the correct sub-region. One draw per tileset — the GID-range guard +// discards cells belonging to other tilesets. // // Index texture encoding (rgba8unorm, one cell per texel): // R = GID low byte, G = GID high byte, B = flip mask (H|V<<1|AD<<2) @@ -27,7 +30,7 @@ struct TMXUniforms { uMapSize : vec2f, uCellSize : vec2f, uTileSize : vec2f, - uOverflow : vec2f, + uOverflow : vec2f, // how far a tile reaches past its cell (see each fragment_main) uTilesetCols : vec2f, uInvTilesetSize : vec2f, uTilesetMargin : vec4f, // (marginX, marginY, spacingX, spacingY) @@ -69,8 +72,6 @@ fn vertex_main( return out; } -const MAX_OVERFLOW : i32 = 4; - struct CellSample { hit : bool, color : vec4f, @@ -110,12 +111,8 @@ fn tryRenderCell( return miss; } - // oversized tiles are bottom-aligned vertically, left-aligned horizontally - let tileWorldOrigin = vec2f( - f32(cx) * tmx.uCellSize.x, - (f32(cy) + 1.0) * tmx.uCellSize.y - tmx.uTileSize.y, - ); - var inTile = (worldPx - tileWorldOrigin) / tmx.uTileSize; + // where the cell's tile is drawn from: the orientation's own + var inTile = (worldPx - tileOrigin(cx, cy)) / tmx.uTileSize; if (inTile.x < 0.0 || inTile.x >= 1.0 || inTile.y < 0.0 || inTile.y >= 1.0) { return miss; } @@ -186,57 +183,3 @@ fn layerColor(texel : vec4f) -> vec4f { return texel * tmx.uOpacity * vec4f(tmx.uTint.rgb, 1.0); } -@fragment -fn fragment_main(in : VSOut) -> @location(0) vec4f { - let tileCoord = tmx.uVisibleStart + in.vRegion * tmx.uVisibleSize; - let geomCell = floor(tileCoord); - let worldPx = tileCoord * tmx.uCellSize; - let dWorldX = dpdx(worldPx); - let dWorldY = dpdy(worldPx); - - let gx = i32(geomCell.x); - let gy = i32(geomCell.y); - let overflowX = i32(tmx.uOverflow.x + 0.5); - let overflowY = i32(tmx.uOverflow.y + 0.5); - - var result : CellSample; - - // fast path: tiles fit the cell exactly — only the geometric cell can - // contain this fragment's tile - if (overflowX == 0 && overflowY == 0) { - result = tryRenderCell(gx, gy, worldPx, dWorldX, dWorldY); - if (!result.hit) { - discard; - } - return layerColor(result.color); - } - - // slow path: oversized tiles. Scan candidates dy high→low, dx low→high - // and take the FIRST match — "right-down" render order puts later - // cells on top - var found = false; - for (var idy = 0; idy <= MAX_OVERFLOW; idy++) { - let dy = MAX_OVERFLOW - idy; - if (dy > overflowY) { - continue; - } - for (var dx = 0; dx <= MAX_OVERFLOW; dx++) { - if (dx > overflowX) { - break; - } - let candidate = tryRenderCell(gx - dx, gy + dy, worldPx, dWorldX, dWorldY); - if (candidate.hit) { - result = candidate; - found = true; - break; - } - } - if (found) { - break; - } - } - if (!found) { - discard; - } - return layerColor(result.color); -} diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-isometric.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-isometric.wgsl new file mode 100644 index 0000000000..8baea0fdbc --- /dev/null +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-isometric.wgsl @@ -0,0 +1,67 @@ +// The isometric part of the TMX layer shader: appended to +// tmxlayer-common.wgsl. The WGSL port of isometric-tmxlayer.frag, where the +// cell maths is explained. +// +// A fragment's position is in the layer's pixels, measured across from the +// map's top corner. With u = col - row and s = col + row, the cells whose +// tile image covers it are a run of `u` and a run of `s`, of the lengths in +// `uOverflow`, scanned s high→low then u high→low: the reverse of the order +// the CPU renderer draws in, so the FIRST opaque texel is the fragment. + +// bounds of the two runs: tiles up to 8 grid heights tall and 4 grid widths +// wide (the layer falls back to the CPU renderer past them) +const MAX_ROWS : i32 = 16; +const MAX_COLS : i32 = 4; + +// the left of the cell's diamond, and as far above its bottom as the tile +// is tall +fn tileOrigin(cx : i32, cy : i32) -> vec2f { + return vec2f( + f32(cx - cy - 1) * tmx.uCellSize.x * 0.5, + f32(cx + cy + 2) * tmx.uCellSize.y * 0.5 - tmx.uTileSize.y, + ); +} + +@fragment +fn fragment_main(in : VSOut) -> @location(0) vec4f { + let worldPx = tmx.uVisibleStart + in.vRegion * tmx.uVisibleSize; + let dWorldX = dpdx(worldPx); + let dWorldY = dpdy(worldPx); + + let halfCell = tmx.uCellSize * 0.5; + // the rightmost and the lowest cell whose image can reach the fragment + let uMax = i32(floor(worldPx.x / halfCell.x)) + 1; + let sMax = i32(floor((worldPx.y + tmx.uTileSize.y) / halfCell.y)) - 2; + let spanU = i32(tmx.uOverflow.x + 0.5); + let spanS = i32(tmx.uOverflow.y + 0.5); + + var result : CellSample; + var found = false; + for (var j = 0; j < MAX_ROWS; j++) { + if (j >= spanS) { + break; + } + let s = sMax - j; + // the first u of this row's parity, then every other one + var u = uMax - ((uMax + s) & 1); + for (var i = 0; i < MAX_COLS; i++) { + if (uMax - u >= spanU) { + break; + } + let candidate = tryRenderCell((s + u) / 2, (s - u) / 2, worldPx, dWorldX, dWorldY); + if (candidate.hit) { + result = candidate; + found = true; + break; + } + u -= 2; + } + if (found) { + break; + } + } + if (!found) { + discard; + } + return layerColor(result.color); +} diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl new file mode 100644 index 0000000000..644a16d72d --- /dev/null +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl @@ -0,0 +1,69 @@ +// The orthogonal part of the TMX layer shader: appended to +// tmxlayer-common.wgsl. The WGSL port of orthogonal-tmxlayer.frag. +// +// `uOverflow` is how many cells a tile reaches past its own, across and up. + +const MAX_OVERFLOW : i32 = 4; + +// a tile is bottom-aligned on its cell, and left-aligned +fn tileOrigin(cx : i32, cy : i32) -> vec2f { + return vec2f( + f32(cx) * tmx.uCellSize.x, + (f32(cy) + 1.0) * tmx.uCellSize.y - tmx.uTileSize.y, + ); +} + +@fragment +fn fragment_main(in : VSOut) -> @location(0) vec4f { + let tileCoord = tmx.uVisibleStart + in.vRegion * tmx.uVisibleSize; + let geomCell = floor(tileCoord); + let worldPx = tileCoord * tmx.uCellSize; + let dWorldX = dpdx(worldPx); + let dWorldY = dpdy(worldPx); + + let gx = i32(geomCell.x); + let gy = i32(geomCell.y); + let overflowX = i32(tmx.uOverflow.x + 0.5); + let overflowY = i32(tmx.uOverflow.y + 0.5); + + var result : CellSample; + + // fast path: tiles fit the cell exactly — only the geometric cell can + // contain this fragment's tile + if (overflowX == 0 && overflowY == 0) { + result = tryRenderCell(gx, gy, worldPx, dWorldX, dWorldY); + if (!result.hit) { + discard; + } + return layerColor(result.color); + } + + // slow path: oversized tiles. Scan candidates dy high→low, dx low→high + // and take the FIRST match — "right-down" render order puts later + // cells on top + var found = false; + for (var idy = 0; idy <= MAX_OVERFLOW; idy++) { + let dy = MAX_OVERFLOW - idy; + if (dy > overflowY) { + continue; + } + for (var dx = 0; dx <= MAX_OVERFLOW; dx++) { + if (dx > overflowX) { + break; + } + let candidate = tryRenderCell(gx - dx, gy + dy, worldPx, dWorldX, dWorldY); + if (candidate.hit) { + result = candidate; + found = true; + break; + } + } + if (found) { + break; + } + } + if (!found) { + discard; + } + return layerColor(result.color); +} diff --git a/packages/melonjs/src/video/webgpu/webgpu_renderer.js b/packages/melonjs/src/video/webgpu/webgpu_renderer.js index 5d506dcebf..e6c92eaffb 100644 --- a/packages/melonjs/src/video/webgpu/webgpu_renderer.js +++ b/packages/melonjs/src/video/webgpu/webgpu_renderer.js @@ -54,6 +54,7 @@ import WebGPUBufferArena from "./buffer/arena.js"; import WebGPUUniformRing from "./buffer/uniformring.js"; import { buildEffectGPU } from "./effect_binding.js"; import WebGPUPipelineCache, { DEPTH_STENCIL_FORMAT } from "./pipeline/cache.js"; +import IsometricTMXLayerGPURenderer from "./renderers/tmxlayer/isometric.js"; import OrthogonalTMXLayerGPURenderer from "./renderers/tmxlayer/orthogonal.js"; import { COMPRESSION_FEATURES } from "./texture/compressed.js"; import { WebGPUFrameTexture } from "./texture/frametexture.js"; @@ -104,6 +105,15 @@ export function resolveDepthOps(meshDepthActive, pendingDepthClear) { }; } +/** + * the GPU tile layer renderer of each map orientation that has one + * @ignore + */ +const TMX_GPU_RENDERERS = { + orthogonal: OrthogonalTMXLayerGPURenderer, + isometric: IsometricTMXLayerGPURenderer, +}; + /** * The **experimental** WebGPU renderer. * @@ -213,11 +223,11 @@ export default class WebGPURenderer extends Renderer { this._resolveToneMapping(); // presentation is settled again by init(), once the surface exists this.setHDROutput(this.settings.hdrOutput === true); - // lazy orientation-specific GPU tilemap renderer (device-scoped: - // dropped on device loss, rebuilt on first use) + // lazy orientation-specific GPU tilemap renderers, by orientation + // (device-scoped: dropped on device loss, rebuilt on first use) /** @ignore * @internal */ - this.orthogonalTMXRenderer = undefined; + this.tmxRenderers = new Map(); // create a texture cache /** @@ -2509,16 +2519,16 @@ export default class WebGPURenderer extends Renderer { * @override */ drawTileLayer(layer, rect) { - if (layer.renderMode === "shader" && layer.orientation === "orthogonal") { + const Class = TMX_GPU_RENDERERS[layer.orientation]; + if (layer.renderMode === "shader" && Class !== undefined) { // device-scoped, lazily (re)built: a device loss rebuilds the // pipeline cache, detected via the epoch stamp - if ( - this.orthogonalTMXRenderer === undefined || - this.orthogonalTMXRenderer.epoch !== this.pipelineCache.epoch - ) { - this.orthogonalTMXRenderer = new OrthogonalTMXLayerGPURenderer(this); + let tmx = this.tmxRenderers.get(layer.orientation); + if (tmx === undefined || tmx.epoch !== this.pipelineCache.epoch) { + tmx = new Class(this); + this.tmxRenderers.set(layer.orientation, tmx); } - this.orthogonalTMXRenderer.draw(layer, rect); + tmx.draw(layer, rect); return; } super.drawTileLayer(layer, rect); @@ -4744,8 +4754,8 @@ export default class WebGPURenderer extends Renderer { this.stubTextureView = null; this.currentRenderTarget = null; // tile-path lookup textures died with the device; the epoch check - // in drawTileLayer rebuilds the renderer lazily - this.orthogonalTMXRenderer = undefined; + // in drawTileLayer rebuilds the renderers lazily + this.tmxRenderers.clear(); this.depthTexture = null; this.msaaDepthTexture = null; this.msaaColorTexture = null; @@ -4802,7 +4812,9 @@ export default class WebGPURenderer extends Renderer { this.effectPassDepth = 0; this.customShader = undefined; // level transition: free the per-layer/per-tileset lookup textures - this.orthogonalTMXRenderer?.reset(); + this.tmxRenderers.forEach((tmx) => { + tmx.reset(); + }); super.reset(); // re-init batchers when recovering from a device loss, plain reset // otherwise — the same split as the WebGL restore path. A reset @@ -4867,7 +4879,7 @@ export default class WebGPURenderer extends Renderer { this.pipelineCache?.clear(); this.captureTexture?.destroy(); this.captureTexture = undefined; - this.orthogonalTMXRenderer = undefined; + this.tmxRenderers.clear(); this._renderTargetPool?.destroy(); this._renderTargetPool = null; this.effectUniformArena?.destroy(); diff --git a/packages/melonjs/tests/helpers/tile-layer-compare.js b/packages/melonjs/tests/helpers/tile-layer-compare.js new file mode 100644 index 0000000000..683d2792b0 --- /dev/null +++ b/packages/melonjs/tests/helpers/tile-layer-compare.js @@ -0,0 +1,333 @@ +/** + * Draw the same tile layers through the CPU renderer (`renderMode` + * "perTile") and through the GPU one ("shader"), with the whole engine in + * between (world, camera, renderer), and compare the two frames. + * + * The tiles are painted pixel by pixel, with no soft edges, and every pixel + * of every tile has a colour of its own: a tile that lands one pixel off, is + * flipped the wrong way, or shows in front of the wrong neighbour, changes + * pixels. So the two paths are held to the same picture, exactly. + */ +import { vi } from "vitest"; +import { Application, boot, TMXTileMap } from "../../src/index.js"; +import { imgList } from "../../src/loader/cache.js"; +import Renderer from "../../src/video/renderer.js"; +import { drawOffscreen, grabFrame } from "./pixels.js"; + +export const FLIP_H = 0x80000000; +export const FLIP_V = 0x40000000; +export const FLIP_AD = 0x20000000; + +let nextName = 0; + +/** + * Whether a pixel of a `w` × `h` tile image belongs to an isometric block + * standing on a `tw` × `th` diamond at the bottom of the image: the diamond, + * the same diamond at the top, and everything between them. A tile as tall + * as its diamond is the diamond alone. + * @param {number} x - pixel column in the tile + * @param {number} y - pixel row in the tile + * @param {number} w - tile image width + * @param {number} h - tile image height + * @param {number} tw - diamond width + * @param {number} th - diamond height + * @returns {boolean} whether the pixel is opaque + */ +export function inBlock(x, y, w, h, tw, th) { + // across: how far from the diamond's vertical axis, in half widths + const dx = Math.abs(x + 0.5 - w / 2) / (tw / 2); + if (dx > 1) { + return false; + } + // the diamond's edge at this column, from its centre line + const edge = (1 - dx) * (th / 2); + const top = th / 2 - edge; + const bottom = h - th / 2 + edge; + return y + 0.5 >= top && y + 0.5 <= bottom; +} + +/** + * Make a tileset image, registered with the loader, and its Tiled JSON. + * @param {object} options + * @param {number} options.tw - tile image width + * @param {number} options.th - tile image height + * @param {number} [options.count=8] - number of tiles + * @param {number} [options.columns=4] - tiles per row of the image + * @param {number} [options.margin=0] - margin round the image + * @param {number} [options.spacing=0] - spacing between tiles + * @param {(x: number, y: number, id: number) => boolean} [options.opaque] - + * which pixels of a tile are opaque (all of them by default) + * @param {object} [options.extra] - more Tiled tileset properties + * (`tileoffset`, `tiles` with animations...) + * @returns {object} the tileset's Tiled JSON, without its `firstgid` + */ +export function makeTileset(options) { + const { + tw, + th, + count = 8, + columns = 4, + margin = 0, + spacing = 0, + opaque, + extra = {}, + } = options; + const rows = Math.ceil(count / columns); + const canvas = document.createElement("canvas"); + canvas.width = margin * 2 + columns * tw + (columns - 1) * spacing; + canvas.height = margin * 2 + rows * th + (rows - 1) * spacing; + const ctx = canvas.getContext("2d"); + const image = ctx.createImageData(canvas.width, canvas.height); + const seed = nextName * 37; + // the margin and spacing are a colour no tile has: sampled, they show + for (let i = 0; i < image.data.length; i += 4) { + image.data[i] = 255; + image.data[i + 1] = 0; + image.data[i + 2] = 255; + image.data[i + 3] = 255; + } + for (let id = 0; id < count; id++) { + const ox = margin + (id % columns) * (tw + spacing); + const oy = margin + Math.floor(id / columns) * (th + spacing); + for (let y = 0; y < th; y++) { + for (let x = 0; x < tw; x++) { + const o = ((oy + y) * canvas.width + ox + x) * 4; + if (opaque !== undefined && !opaque(x, y, id)) { + image.data[o + 3] = 0; + image.data[o] = image.data[o + 1] = image.data[o + 2] = 0; + continue; + } + // a colour per tile, and per pixel in it: never the + // margin's, never the background's + image.data[o] = 40 + ((id * 53 + seed) % 180); + image.data[o + 1] = 30 + ((x * 7 + id * 11) % 200); + image.data[o + 2] = 30 + ((y * 5 + id * 29) % 200); + image.data[o + 3] = 255; + } + } + } + ctx.putImageData(image, 0, 0); + const name = `tile-compare-${nextName++}`; + imgList[name] = canvas; + return { + name, + tilewidth: tw, + tileheight: th, + spacing, + margin, + tilecount: count, + columns, + image: `${name}.png`, + imagewidth: canvas.width, + imageheight: canvas.height, + ...extra, + }; +} + +/** + * Build a map from tilesets and layers. + * @param {object} options + * @param {string} options.orientation - Tiled orientation + * @param {number} options.cols - map width, in tiles + * @param {number} options.rows - map height, in tiles + * @param {number} options.tw - grid tile width + * @param {number} options.th - grid tile height + * @param {object[]} options.tilesets - from {@link makeTileset}; their + * `firstgid` is set here, one after the other + * @param {object[]} options.layers - `{ data }` (gids, row by row) or + * `{ chunks }` for an infinite map, plus any Tiled layer property + * @param {object} [options.extra] - more Tiled map properties + * @returns {TMXTileMap} the map + */ +export function makeMap(options) { + const { + orientation, + cols, + rows, + tw, + th, + tilesets, + layers, + extra = {}, + } = options; + let firstgid = 1; + const sets = tilesets.map((tileset) => { + const set = { ...tileset, firstgid }; + firstgid += tileset.tilecount; + return set; + }); + return new TMXTileMap(`tile-compare-map-${nextName++}`, { + width: cols, + height: rows, + tilewidth: tw, + tileheight: th, + orientation, + renderorder: "right-down", + infinite: false, + version: "1.10", + tiledversion: "1.10.2", + tilesets: sets, + layers: layers.map((layer, i) => { + return { + type: "tilelayer", + name: `layer${i}`, + width: cols, + height: rows, + visible: true, + opacity: 1, + ...layer, + }; + }), + ...extra, + }); +} + +/** + * A layer's data, filled by a function of each cell. + * @param {number} cols - columns + * @param {number} rows - rows + * @param {(col: number, row: number) => number} gidOf - the cell's gid, 0 for none + * @returns {number[]} the gids, row by row + */ +export function fill(cols, rows, gidOf) { + const data = []; + for (let row = 0; row < rows; row++) { + for (let col = 0; col < cols; col++) { + data.push(gidOf(col, row)); + } + } + return data; +} + +/** + * The application the comparisons draw with, its loop held still so that + * frames are drawn only when a test asks. `null` when the renderer asked + * for is not available (a WebGPU device, on a machine without one). + * @param {number} renderer - `video.WEBGL` or `video.WEBGPU` + * @param {string} type - the `renderer.type` expected + * @param {number} [width=320] - canvas width + * @param {number} [height=240] - canvas height + * @returns {Promise} the application + */ +export async function makeApp(renderer, type, width = 320, height = 240) { + // no frame of the engine's own: a test draws each one + vi.spyOn(globalThis, "requestAnimationFrame").mockImplementation(() => { + return 1; + }); + vi.spyOn(globalThis, "cancelAnimationFrame").mockImplementation(() => {}); + boot(); + let app; + try { + app = new Application(width, height, { + parent: "screen", + renderer, + consoleHeader: false, + }); + await app.init(); + } catch { + return null; + } + if (!app.renderer.type.startsWith(type)) { + app.destroy(); + return null; + } + drawOffscreen(app.renderer); + // not black: a tile that is not drawn shows + app.renderer.backgroundColor.setColor(16, 24, 48); + app.viewport.setBounds(-100000, -100000, 200000, 200000); + return app; +} + +/** + * Put a map's layers in the world, in place of whatever was there. + * @param {Application} app - the application + * @param {TMXTileMap} map - the map + * @returns {object[]} the map's tile layers + */ +export function show(app, map) { + app.world.reset(); + app.renderer.reset?.(); + app.renderer.backgroundColor.setColor(16, 24, 48); + map.addTo(app.world, true, false); + app.viewport.setBounds(-100000, -100000, 200000, 200000); + return map.getLayers(); +} + +let clock = 1000; + +/** + * Draw one frame with every layer in the given mode, and return it with how + * many times the CPU tile renderer was reached while drawing it. + * @param {Application} app - the application + * @param {object[]} layers - the layers + * @param {string} mode - "perTile", "shader" or "prerender" + * @param {number} [dt=0] - game time to pass before the frame, in ms + * @returns {Promise<{frame: ImageData, cpuDraws: number}>} the frame + */ +export async function drawIn(app, layers, mode, dt = 0) { + for (const layer of layers) { + layer.renderMode = mode; + } + // the world updated (what is in view, and animated tiles when time + // passes), then drawn + clock += dt; + app.world.update(dt); + app.viewport.update(dt); + const cpu = vi.spyOn(Renderer.prototype, "drawTileLayer"); + app.repaint(); + app.draw(); + const cpuDraws = cpu.mock.calls.length; + cpu.mockRestore(); + const frame = await grabFrame(app.renderer); + return { frame, cpuDraws }; +} + +/** + * Compare two frames. + * @param {ImageData} a - one frame + * @param {ImageData} b - the other + * @param {number} [tolerance=0] - largest difference of a channel that is + * not counted + * @returns {{different: number, worst: number, first: string}} how many + * pixels differ, by how much at most, and the first one + */ +export function diff(a, b, tolerance = 0) { + let different = 0; + let worst = 0; + let first = ""; + for (let i = 0; i < a.data.length; i += 4) { + const d = Math.max( + Math.abs(a.data[i] - b.data[i]), + Math.abs(a.data[i + 1] - b.data[i + 1]), + Math.abs(a.data[i + 2] - b.data[i + 2]), + ); + if (d > tolerance) { + different++; + if (first === "") { + const p = i / 4; + first = `(${p % a.width}, ${Math.floor(p / a.width)}): [${a.data.slice(i, i + 3)}] against [${b.data.slice(i, i + 3)}]`; + } + } + worst = Math.max(worst, d); + } + return { different, worst, first }; +} + +/** + * How many pixels of a frame are not the background: how much was drawn. + * @param {ImageData} frame - the frame + * @returns {number} the count + */ +export function drawn(frame) { + let count = 0; + for (let i = 0; i < frame.data.length; i += 4) { + if ( + frame.data[i] !== 16 || + frame.data[i + 1] !== 24 || + frame.data[i + 2] !== 48 + ) { + count++; + } + } + return count; +} From 74c3e6c2afe3310ef854e93c427d4f22bfe6c4b0 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 00:47:54 +0000 Subject: [PATCH 03/11] GPU tile layers: blend soft tile edges, and hold both paths to a reference (#1897) The GPU path took the first texel that was not transparent, front to back. With drawn art that is wrong wherever tiles overlap, which on an isometric map is everywhere: the soft edge of a tile showed the background in place of the tile behind it. Each texel now goes behind the ones before it until the fragment is opaque, which is what drawing the tiles one over the other comes to. The orthogonal shader shares it, for tiles larger than the grid. Found by a scenario suite that draws every case through the CPU renderer and the GPU one, on WebGL and on WebGPU, and compares both with a reference painted with no engine at all. It also found, and this fixes: - the isometric CPU renderer left out tiles by the first tileset's size, and by no tileset's `tileoffset` - the orthogonal CPU renderer widened the wrong side of the view for tiles taller than the grid - the orthogonal GPU quad cut tiles larger than the grid above the first row and right of the last column - a tile that is not square, flipped diagonally, is wrong on the GPU path: such a layer is left to the CPU renderer Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01JnzwA4a7ZyjLBpbeEpFLu6 --- packages/melonjs/src/level/tiled/TMXLayer.js | 107 +- .../tiled/renderer/TMXIsometricRenderer.js | 18 +- .../tiled/renderer/TMXOrthogonalRenderer.js | 26 +- .../melonjs/src/video/gpu/tmxlayer_region.js | 17 +- .../webgl/shaders/isometric-tmxlayer.frag | 25 +- .../webgl/shaders/orthogonal-tmxlayer.frag | 28 +- .../video/webgl/shaders/tmxlayer-common.frag | 17 +- .../video/webgpu/shaders/tmxlayer-common.wgsl | 13 + .../webgpu/shaders/tmxlayer-isometric.wgsl | 18 +- .../webgpu/shaders/tmxlayer-orthogonal.wgsl | 28 +- .../tests/helpers/tile-layer-compare.js | 181 ++- .../helpers/tile-layer-isometric-scenarios.js | 1296 +++++++++++++++++ packages/melonjs/tests/texture-mipmap.spec.js | 12 +- .../tests/tmxlayer-shader-isometric.spec.js | 8 + .../tests/webgpu_tmxlayer_isometric.spec.js | 9 + 15 files changed, 1703 insertions(+), 100 deletions(-) create mode 100644 packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js create mode 100644 packages/melonjs/tests/tmxlayer-shader-isometric.spec.js create mode 100644 packages/melonjs/tests/webgpu_tmxlayer_isometric.spec.js diff --git a/packages/melonjs/src/level/tiled/TMXLayer.js b/packages/melonjs/src/level/tiled/TMXLayer.js index 54c7fb89d9..98585e4196 100644 --- a/packages/melonjs/src/level/tiled/TMXLayer.js +++ b/packages/melonjs/src/level/tiled/TMXLayer.js @@ -32,6 +32,10 @@ const ISOMETRIC_MAX_TILE_WIDTHS = 4; const ISOMETRIC_MAX_TILE_HEIGHTS = 8; const ISOMETRIC_ONE_TILESET = "tiles of more than one tileset in an isometric layer"; +// A diagonal flip turns a tile a quarter: one that is not square then covers +// other pixels than its own image's rectangle, which is all the shader looks +// at. +const DIAGONAL_NOT_SQUARE = "a tile that is not square, flipped diagonally"; // the area an offset camera shows, handed to the tile renderers in its place // (made on first use) @@ -218,8 +222,23 @@ export default class TMXLayer extends Renderable { width: 0, height: 0, }; + /** + * how far a tileset's `tileoffset` moves its tiles, at most, across + * and down: with `maxTileSize`, how far from its cell a tile of this + * layer can be drawn + * @ignore + */ + this.maxTileOffset = { x: 0, y: 0 }; for (let i = 0; i < this.tilesets.length; i++) { const tileset = this.tilesets.getTilesetByIndex(i); + this.maxTileOffset.x = Math.max( + this.maxTileOffset.x, + Math.abs(tileset.tileoffset.x), + ); + this.maxTileOffset.y = Math.max( + this.maxTileOffset.y, + Math.abs(tileset.tileoffset.y), + ); this.maxTileSize.width = Math.max( this.maxTileSize.width, tileset.tilewidth, @@ -726,9 +745,41 @@ export default class TMXLayer extends Renderable { }; } } + if (this._hasDiagonalFlipNotSquare()) { + return { ok: false, reason: DIAGONAL_NOT_SQUARE }; + } return { ok: true }; } + /** + * Whether a tile of this layer is flipped diagonally and not square, + * which the shader path cannot draw. Only looks at the tiles when a + * tileset has tiles that are not square. + * @returns {boolean} + * @ignore + * @internal + */ + _hasDiagonalFlipNotSquare() { + const tilesets = this.tilesets.tilesets; + if ( + tilesets.every((tileset) => { + return tileset.tilewidth === tileset.tileheight; + }) + ) { + return false; + } + const data = this.layerData; + for (let i = 0; i < data.length; i += 2) { + if (data[i] !== 0 && (data[i + 1] & FLIP_AD_BIT) !== 0) { + const tileset = this.tilesets.getTilesetByGid(data[i]); + if (tileset.tilewidth !== tileset.tileheight) { + return true; + } + } + } + return false; + } + /** * Check whether this isometric layer can take the shader path, and note * the tileset it would draw with. @@ -768,6 +819,9 @@ export default class TMXLayer extends Renderable { } this._shaderTileset = tileset; } + if (this._hasDiagonalFlipNotSquare()) { + return { ok: false, reason: DIAGONAL_NOT_SQUARE }; + } return { ok: true }; } @@ -798,31 +852,46 @@ export default class TMXLayer extends Renderable { } /** - * An isometric layer on the shader path draws with one tileset: a tile - * set at run time from another one, or from one the path cannot draw, - * moves the layer back to the legacy renderer. + * A tile set at run time on a layer on the shader path: one that path + * cannot draw moves the layer back to the legacy renderer. + * + * That is a tile flipped diagonally and not square, and on an isometric + * layer, which draws with one tileset, a tile of another one, or of one + * the path cannot draw. * @param {number} gid - the gid of the tile just set + * @param {number} flipMask - its flips * @ignore * @internal */ - _keepIsometricShaderTileset(gid) { - const current = this._shaderTileset; - if (current?.contains(gid)) { - return; + _keepShaderEligible(gid, flipMask) { + let reason; + if ( + this.orientation === "isometric" && + this._shaderTileset?.contains(gid) !== true + ) { + if (this._shaderTileset !== undefined) { + reason = ISOMETRIC_ONE_TILESET; + } else { + // an empty layer takes its tileset from its first tile + const tileset = this.tilesets.getTilesetByGid(gid); + reason = this._isometricTilesetReason(tileset); + if (reason === undefined) { + this._shaderTileset = tileset; + } + } } - let reason = ISOMETRIC_ONE_TILESET; - if (current === undefined) { + if (reason === undefined && (flipMask & FLIP_AD_BIT) !== 0) { const tileset = this.tilesets.getTilesetByGid(gid); - reason = this._isometricTilesetReason(tileset); - if (reason === undefined) { - this._shaderTileset = tileset; - return; + if (tileset.tilewidth !== tileset.tileheight) { + reason = DIAGONAL_NOT_SQUARE; } } - console.warn( - `melonJS: layer "${this.name}" back to the legacy tile renderer (${reason})`, - ); - this.renderMode = "perTile"; + if (reason !== undefined) { + console.warn( + `melonJS: layer "${this.name}" back to the legacy tile renderer (${reason})`, + ); + this.renderMode = "perTile"; + } } // called when the layer is removed from the game world or a container @@ -931,8 +1000,8 @@ export default class TMXLayer extends Renderable { this.dataVersion++; this.isDirty = true; this._preRenderCell(x, y); - if (this.renderMode === "shader" && this.orientation === "isometric") { - this._keepIsometricShaderTileset(cleanGid); + if (this.renderMode === "shader") { + this._keepShaderEligible(cleanGid, this.layerData[idx + 1]); } return tile; } diff --git a/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js b/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js index 3c884b086c..b0fd0514ba 100644 --- a/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js +++ b/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js @@ -159,15 +159,23 @@ export default class TMXIsometricRenderer extends TMXRenderer { // cache a couple of useful references let tileset = layer.tileset; - // get top-left and bottom-right tile position + // get top-left and bottom-right tile position: the view, widened by + // the largest tile of ANY of the layer's tilesets, as a tile is drawn + // from its cell up, and by how far a `tileoffset` moves one. By the + // first tileset's size alone, the tall tiles of another one went + // missing along the bottom of the view, where their cell is below it + // and their image still reaches in, and so did tiles moved into view + // by an offset larger than a tile + const reachX = layer.maxTileSize.width + layer.maxTileOffset.x; + const reachY = layer.maxTileSize.height + layer.maxTileOffset.y; const rowItr = this.pixelToTileCoords( - rect.pos.x - tileset.tilewidth, - rect.pos.y - tileset.tileheight, + rect.pos.x - reachX, + rect.pos.y - reachY, vector2dPool.get(), ).floorSelf(); const tileEnd = this.pixelToTileCoords( - rect.pos.x + rect.width + tileset.tilewidth, - rect.pos.y + rect.height + tileset.tileheight, + rect.pos.x + rect.width + reachX, + rect.pos.y + rect.height + reachY, vector2dPool.get(), ).ceilSelf(); diff --git a/packages/melonjs/src/level/tiled/renderer/TMXOrthogonalRenderer.js b/packages/melonjs/src/level/tiled/renderer/TMXOrthogonalRenderer.js index 5de0ae9ff9..a6cb4412dd 100644 --- a/packages/melonjs/src/level/tiled/renderer/TMXOrthogonalRenderer.js +++ b/packages/melonjs/src/level/tiled/renderer/TMXOrthogonalRenderer.js @@ -87,22 +87,26 @@ export default class TMXOrthogonalRenderer extends TMXRenderer { let incX = 1; let incY = 1; - // get top-left and bottom-right tile position + // get top-left and bottom-right tile position: the cells whose tile + // can reach the view. A tile larger than its cell is drawn from the + // cell's bottom left corner, so it reaches to the RIGHT of its cell + // and ABOVE it: the cells that matter past the view are those to its + // left and BELOW it. (The rows above were the ones added, so a tile + // more than two cells tall went missing along the bottom of the + // view.) A tileset's `tileoffset` moves its tiles either way. + const overX = Math.max(layer.maxTileSize.width - layer.tilewidth, 0); + const overY = Math.max(layer.maxTileSize.height - layer.tileheight, 0); + const offsetX = layer.maxTileOffset.x; + const offsetY = layer.maxTileOffset.y; const start = this.pixelToTileCoords( - Math.max( - rect.pos.x - Math.max(layer.maxTileSize.width - layer.tilewidth, 0), - 0, - ), - Math.max( - rect.pos.y - Math.max(layer.maxTileSize.height - layer.tileheight, 0), - 0, - ), + Math.max(rect.pos.x - overX - offsetX, 0), + Math.max(rect.pos.y - offsetY, 0), vector2dPool.get(), ).floorSelf(); const end = this.pixelToTileCoords( - rect.pos.x + rect.width + this.tilewidth, - rect.pos.y + rect.height + this.tileheight, + rect.pos.x + rect.width + this.tilewidth + offsetX, + rect.pos.y + rect.height + this.tileheight + overY + offsetY, vector2dPool.get(), ).ceilSelf(); diff --git a/packages/melonjs/src/video/gpu/tmxlayer_region.js b/packages/melonjs/src/video/gpu/tmxlayer_region.js index b83f5211bf..34ffe5318b 100644 --- a/packages/melonjs/src/video/gpu/tmxlayer_region.js +++ b/packages/melonjs/src/video/gpu/tmxlayer_region.js @@ -43,11 +43,24 @@ export function orthogonalRegion(layer, rect, tileset, out) { const tileWidth = layer.tilewidth; const tileHeight = layer.tileheight; + // A tile larger than its cell is drawn from the cell's bottom left + // corner: it reaches to the right of its cell and above it, so the tiles + // of the last column and of the first row reach past the grid. The quad + // covers that too, by the layer's LARGEST tile, so that it is the same + // for every tileset of the layer (the shader drops what a pass has no + // tile for). + const largest = layer.maxTileSize ?? { + width: tileset.tilewidth, + height: tileset.tileheight, + }; + const reachX = Math.max(0, Math.ceil(largest.width / tileWidth) - 1); + const reachY = Math.max(0, Math.ceil(largest.height / tileHeight) - 1); + // compute the visible region in tile-coord space const startTileX = Math.max(0, Math.floor(rect.pos.x / tileWidth)); - const startTileY = Math.max(0, Math.floor(rect.pos.y / tileHeight)); + const startTileY = Math.max(-reachY, Math.floor(rect.pos.y / tileHeight)); const endTileX = Math.min( - layer.cols, + layer.cols + reachX, Math.ceil((rect.pos.x + rect.width) / tileWidth), ); const endTileY = Math.min( diff --git a/packages/melonjs/src/video/webgl/shaders/isometric-tmxlayer.frag b/packages/melonjs/src/video/webgl/shaders/isometric-tmxlayer.frag index 6c7f1ac61f..befc47ff0c 100644 --- a/packages/melonjs/src/video/webgl/shaders/isometric-tmxlayer.frag +++ b/packages/melonjs/src/video/webgl/shaders/isometric-tmxlayer.frag @@ -15,9 +15,11 @@ // Only pairs of the same parity are cells. // // The CPU renderer draws rows of constant `s` from the top of the screen -// down, each from left to right. The last tile drawn is the one seen, so the -// scan here goes the other way, s high→low then u high→low, and the FIRST -// cell with an opaque texel is the fragment. +// down, each from left to right. The last tile drawn is the one in front, +// so the scan here goes the other way, s high→low then u high→low, and puts +// each texel behind the ones before it until the fragment is opaque: an +// opaque texel ends it at once, and the soft edge of a tile shows the tiles +// behind it. // bounds of the two runs: tiles up to 8 grid heights tall and 4 grid widths // wide (the layer falls back to the CPU renderer past them) @@ -45,8 +47,9 @@ void main(void) { int spanU = int(uOverflow.x + 0.5); int spanS = int(uOverflow.y + 0.5); - vec4 result; - bool found = false; + vec4 result = vec4(0.0); + vec4 texel; + bool opaque = false; for (int j = 0; j < MAX_ROWS; j++) { if (j >= spanS) break; int s = sMax - j; @@ -54,14 +57,16 @@ void main(void) { int u = uMax - ((uMax + s) & 1); for (int i = 0; i < MAX_COLS; i++) { if (uMax - u >= spanU) break; - if (tryRenderCell((s + u) / 2, (s - u) / 2, worldPx, dWorldX, dWorldY, result)) { - found = true; - break; + if (tryRenderCell((s + u) / 2, (s - u) / 2, worldPx, dWorldX, dWorldY, texel)) { + if (under(result, texel)) { + opaque = true; + break; + } } u -= 2; } - if (found) break; + if (opaque) break; } - if (!found) discard; + if (result.a <= 0.0) discard; fragColor = layerColor(result); } diff --git a/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag b/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag index b7c7afd905..3e29c4a3ad 100644 --- a/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag +++ b/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag @@ -11,8 +11,9 @@ // Slow path: tiles drawing larger than the cell are bottom-aligned // vertically and left-aligned horizontally. Render order is "right-down": // later cells end up on top, so the candidate loop scans dy high→low, -// dx low→high and picks the FIRST match. `uOverflow` is how many cells a -// tile reaches past its own, across and up. +// dx low→high, the tile in front first, and puts each texel behind the +// ones before it until the fragment is opaque. `uOverflow` is how many +// cells a tile reaches past its own, across and up. const int MAX_OVERFLOW = 4; @@ -36,7 +37,7 @@ void main(void) { int overflowX = int(uOverflow.x + 0.5); int overflowY = int(uOverflow.y + 0.5); - vec4 result; + vec4 result = vec4(0.0); // Fast path: tiles fit the cell exactly (the common case) — only the // geometric cell can contain this fragment's tile. @@ -48,22 +49,25 @@ void main(void) { return; } - // Slow path: oversized tiles. Walk candidates dy high→low, dx low→high - // and pick the FIRST match — render order is "right-down" so later - // cells go on top. - bool found = false; + // Slow path: oversized tiles. Walk candidates dy high→low, dx low→high, + // front to back — render order is "right-down" so later cells go on + // top — until the fragment is opaque. + vec4 texel; + bool opaque = false; for (int idy = 0; idy <= MAX_OVERFLOW; idy++) { int dy = MAX_OVERFLOW - idy; if (dy > overflowY) continue; for (int dx = 0; dx <= MAX_OVERFLOW; dx++) { if (dx > overflowX) break; - if (tryRenderCell(gx - dx, gy + dy, worldPx, dWorldX, dWorldY, result)) { - found = true; - break; + if (tryRenderCell(gx - dx, gy + dy, worldPx, dWorldX, dWorldY, texel)) { + if (under(result, texel)) { + opaque = true; + break; + } } } - if (found) break; + if (opaque) break; } - if (!found) discard; + if (result.a <= 0.0) discard; fragColor = layerColor(result); } diff --git a/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag b/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag index 4458270c3e..7e91ff18f1 100644 --- a/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag +++ b/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag @@ -12,7 +12,9 @@ // 2. walks the cells whose tile could cover it, the one drawn last first, // 3. and for each, `tryRenderCell` fetches the GID from the per-layer // index texture and samples the tileset atlas at the right sub-region. -// The first cell with a texel that is not transparent is the fragment. +// Each texel found goes behind the ones before it (`under`), until the +// fragment is opaque: the soft edge of a tile shows the tile behind it, as +// it does when the tiles are drawn one over the other. // // Index texture encoding (`RGBA8`, one cell per texel): // R = GID low byte @@ -158,6 +160,19 @@ bool tryRenderCell(int cx, int cy, vec2 worldPx, vec2 dWorldX, vec2 dWorldY, out return true; } +// A fragment is opaque from this alpha on: nothing behind it would change a +// channel by a whole step of 8 bits +const float OPAQUE = 0.996; + +// Put a texel behind what a fragment has so far, both premultiplied: what +// drawing the tiles back to front, each over the last, comes to, taken from +// the front. Returns whether the fragment is opaque now, and nothing behind +// it can show +bool under(inout vec4 color, vec4 texel) { + color += (1.0 - color.a) * texel; + return color.a >= OPAQUE; +} + // the layer's opacity and tint applied to a texel. The texel is // premultiplied, as the blending expects: the opacity scales its color as // well as its alpha. The tint colors it; its alpha is not an opacity, as on diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl index fda81795e1..ee5758c0c4 100644 --- a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl @@ -175,6 +175,19 @@ fn tryRenderCell( return out; } +// A fragment is opaque from this alpha on: nothing behind it would change a +// channel by a whole step of 8 bits +const OPAQUE : f32 = 0.996; + +// Put a texel behind what a fragment has so far, both premultiplied: what +// drawing the tiles back to front, each over the last, comes to, taken from +// the front. Returns whether the fragment is opaque now, and nothing behind +// it can show +fn under(color : ptr, texel : vec4f) -> bool { + *color += (1.0 - (*color).a) * texel; + return (*color).a >= OPAQUE; +} + // the layer's opacity and tint applied to a texel. The texel is // premultiplied, as the blending expects: the opacity scales its color as // well as its alpha. The tint colors it; its alpha is not an opacity, as on diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-isometric.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-isometric.wgsl index 8baea0fdbc..2e3b35ed7b 100644 --- a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-isometric.wgsl +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-isometric.wgsl @@ -6,7 +6,8 @@ // map's top corner. With u = col - row and s = col + row, the cells whose // tile image covers it are a run of `u` and a run of `s`, of the lengths in // `uOverflow`, scanned s high→low then u high→low: the reverse of the order -// the CPU renderer draws in, so the FIRST opaque texel is the fragment. +// the CPU renderer draws in, each texel put behind the ones before it until +// the fragment is opaque. // bounds of the two runs: tiles up to 8 grid heights tall and 4 grid widths // wide (the layer falls back to the CPU renderer past them) @@ -35,8 +36,8 @@ fn fragment_main(in : VSOut) -> @location(0) vec4f { let spanU = i32(tmx.uOverflow.x + 0.5); let spanS = i32(tmx.uOverflow.y + 0.5); - var result : CellSample; - var found = false; + var result = vec4f(0.0); + var opaque = false; for (var j = 0; j < MAX_ROWS; j++) { if (j >= spanS) { break; @@ -49,19 +50,18 @@ fn fragment_main(in : VSOut) -> @location(0) vec4f { break; } let candidate = tryRenderCell((s + u) / 2, (s - u) / 2, worldPx, dWorldX, dWorldY); - if (candidate.hit) { - result = candidate; - found = true; + if (candidate.hit && under(&result, candidate.color)) { + opaque = true; break; } u -= 2; } - if (found) { + if (opaque) { break; } } - if (!found) { + if (result.a <= 0.0) { discard; } - return layerColor(result.color); + return layerColor(result); } diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl index 644a16d72d..1585827ba8 100644 --- a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl @@ -26,22 +26,21 @@ fn fragment_main(in : VSOut) -> @location(0) vec4f { let overflowX = i32(tmx.uOverflow.x + 0.5); let overflowY = i32(tmx.uOverflow.y + 0.5); - var result : CellSample; - // fast path: tiles fit the cell exactly — only the geometric cell can // contain this fragment's tile if (overflowX == 0 && overflowY == 0) { - result = tryRenderCell(gx, gy, worldPx, dWorldX, dWorldY); - if (!result.hit) { + let only = tryRenderCell(gx, gy, worldPx, dWorldX, dWorldY); + if (!only.hit) { discard; } - return layerColor(result.color); + return layerColor(only.color); } - // slow path: oversized tiles. Scan candidates dy high→low, dx low→high - // and take the FIRST match — "right-down" render order puts later - // cells on top - var found = false; + // slow path: oversized tiles. Scan candidates dy high→low, dx low→high, + // front to back — "right-down" render order puts later cells on top — + // each texel behind the ones before it, until the fragment is opaque + var result = vec4f(0.0); + var opaque = false; for (var idy = 0; idy <= MAX_OVERFLOW; idy++) { let dy = MAX_OVERFLOW - idy; if (dy > overflowY) { @@ -52,18 +51,17 @@ fn fragment_main(in : VSOut) -> @location(0) vec4f { break; } let candidate = tryRenderCell(gx - dx, gy + dy, worldPx, dWorldX, dWorldY); - if (candidate.hit) { - result = candidate; - found = true; + if (candidate.hit && under(&result, candidate.color)) { + opaque = true; break; } } - if (found) { + if (opaque) { break; } } - if (!found) { + if (result.a <= 0.0) { discard; } - return layerColor(result.color); + return layerColor(result); } diff --git a/packages/melonjs/tests/helpers/tile-layer-compare.js b/packages/melonjs/tests/helpers/tile-layer-compare.js index 683d2792b0..3b3021ffb9 100644 --- a/packages/melonjs/tests/helpers/tile-layer-compare.js +++ b/packages/melonjs/tests/helpers/tile-layer-compare.js @@ -3,13 +3,14 @@ * "perTile") and through the GPU one ("shader"), with the whole engine in * between (world, camera, renderer), and compare the two frames. * - * The tiles are painted pixel by pixel, with no soft edges, and every pixel - * of every tile has a colour of its own: a tile that lands one pixel off, is - * flipped the wrong way, or shows in front of the wrong neighbour, changes - * pixels. So the two paths are held to the same picture, exactly. + * The tiles are painted pixel by pixel, and every pixel of every tile has a + * colour of its own: a tile that lands one pixel off, is flipped the wrong + * way, or shows in front of the wrong neighbour, changes pixels. With hard + * edges (the default) the two paths are held to the same picture, exactly; + * with soft ones (`alpha`), to a rounding step or two. */ import { vi } from "vitest"; -import { Application, boot, TMXTileMap } from "../../src/index.js"; +import { Application, boot, TMXTileMap, timer } from "../../src/index.js"; import { imgList } from "../../src/loader/cache.js"; import Renderer from "../../src/video/renderer.js"; import { drawOffscreen, grabFrame } from "./pixels.js"; @@ -55,8 +56,11 @@ export function inBlock(x, y, w, h, tw, th) { * @param {number} [options.columns=4] - tiles per row of the image * @param {number} [options.margin=0] - margin round the image * @param {number} [options.spacing=0] - spacing between tiles - * @param {(x: number, y: number, id: number) => boolean} [options.opaque] - + * @param {(x: number, y: number, id: number) => boolean} [options.opaque] * which pixels of a tile are opaque (all of them by default) + * @param {(x: number, y: number, id: number) => number} [options.alpha] + * how opaque each pixel is, 0 to 255, for soft edges and see-through tiles + * (in place of `opaque`) * @param {object} [options.extra] - more Tiled tileset properties * (`tileoffset`, `tiles` with animations...) * @returns {object} the tileset's Tiled JSON, without its `firstgid` @@ -70,6 +74,7 @@ export function makeTileset(options) { margin = 0, spacing = 0, opaque, + alpha, extra = {}, } = options; const rows = Math.ceil(count / columns); @@ -92,7 +97,8 @@ export function makeTileset(options) { for (let y = 0; y < th; y++) { for (let x = 0; x < tw; x++) { const o = ((oy + y) * canvas.width + ox + x) * 4; - if (opaque !== undefined && !opaque(x, y, id)) { + const a = alpha !== undefined ? alpha(x, y, id) : 255; + if (a <= 0 || (opaque !== undefined && !opaque(x, y, id))) { image.data[o + 3] = 0; image.data[o] = image.data[o + 1] = image.data[o + 2] = 0; continue; @@ -102,7 +108,7 @@ export function makeTileset(options) { image.data[o] = 40 + ((id * 53 + seed) % 180); image.data[o + 1] = 30 + ((x * 7 + id * 11) % 200); image.data[o + 2] = 30 + ((y * 5 + id * 29) % 200); - image.data[o + 3] = 255; + image.data[o + 3] = a; } } } @@ -182,6 +188,142 @@ export function makeMap(options) { }); } +/** + * The picture of a map, painted here with a 2D canvas and no engine: every + * tile of every layer, in the order Tiled draws them (rows from the top, + * each from the left), where Tiled draws them, with nothing left out for + * being out of view. What both of the engine's renderers are held to. + * + * Tiles are drawn whole, bottom-aligned on their cell and moved by their + * tileset's `tileoffset`; flipped across, down or along the diagonal (the + * last on square tiles only, where a flip is a quarter turn in place). + * Layer opacity and tint are for the caller to apply to the picture. A + * layer is drawn at the map's origin: the engine's tile layers are, whatever + * their `pos` or Tiled's layer offset. + * + * One rule here is the engine's and not Tiled's: a layer whose grid is + * wholly out of view is not drawn, though a tile taller than its cell, in + * the map's first rows, reaches above the grid and could still show. The + * engine draws what is in view going by a layer's bounds, which are its + * grid's. + * @param {object} scene - what {@link makeMap} was given + * @param {number} viewX - the view's left, in the map's pixels + * @param {number} viewY - the view's top + * @param {number} width - the view's width + * @param {number} height - the view's height + * @returns {ImageData} the picture + */ +export function reference(scene, viewX, viewY, width, height) { + const { orientation, cols, rows, tw, th, tilesets, layers } = scene; + const canvas = document.createElement("canvas"); + canvas.width = width; + canvas.height = height; + const ctx = canvas.getContext("2d", { willReadFrequently: true }); + ctx.imageSmoothingEnabled = false; + ctx.fillStyle = "rgb(16, 24, 48)"; + ctx.fillRect(0, 0, width, height); + ctx.translate(-viewX, -viewY); + + let firstgid = 1; + const sets = tilesets.map((tileset) => { + const set = { ...tileset, firstgid, image: imgList[tileset.name] }; + firstgid += tileset.tilecount; + return set; + }); + const drawTile = (raw, left, bottom) => { + const gid = raw & 0x1fffffff; + const set = sets.findLast((candidate) => { + return gid >= candidate.firstgid; + }); + const id = gid - set.firstgid; + const w = set.tilewidth; + const h = set.tileheight; + const sx = set.margin + (id % set.columns) * (w + set.spacing); + const sy = set.margin + Math.floor(id / set.columns) * (h + set.spacing); + const x = left + (set.tileoffset?.x ?? 0); + const y = bottom - h + (set.tileoffset?.y ?? 0); + ctx.save(); + ctx.translate(x + w / 2, y + h / 2); + if ((raw & FLIP_AD) !== 0) { + ctx.rotate(-Math.PI / 2); + ctx.scale(-1, 1); + } + const ad = (raw & FLIP_AD) !== 0; + if ((raw & FLIP_H) !== 0) { + ctx.scale(ad ? 1 : -1, ad ? -1 : 1); + } + if ((raw & FLIP_V) !== 0) { + ctx.scale(ad ? -1 : 1, ad ? 1 : -1); + } + ctx.drawImage(set.image, sx, sy, w, h, -w / 2, -h / 2, w, h); + ctx.restore(); + }; + + for (const layer of layers) { + if (layer.chunks === undefined) { + // the grid's bounds: an isometric map's are its diamond's + const isIso = orientation === "isometric"; + const gridWidth = isIso ? ((cols + rows) * tw) / 2 : cols * tw; + const gridHeight = isIso ? ((cols + rows) * th) / 2 : rows * th; + if ( + viewX + width <= 0 || + gridWidth <= viewX || + viewY + height <= 0 || + gridHeight <= viewY + ) { + continue; + } + } + // the layer's cells: a plain grid, or an infinite map's chunks + const cells = new Map(); + const put = (col, row, gid) => { + if (gid !== 0) { + cells.set(`${col},${row}`, gid); + } + }; + if (layer.chunks !== undefined) { + for (const chunk of layer.chunks) { + chunk.data.forEach((gid, i) => { + put( + chunk.x + (i % chunk.width), + chunk.y + Math.floor(i / chunk.width), + gid, + ); + }); + } + } else { + layer.data.forEach((gid, i) => { + put(i % cols, Math.floor(i / cols), gid); + }); + } + const ordered = [...cells.entries()].map(([key, gid]) => { + const [col, row] = key.split(",").map(Number); + return { col, row, gid }; + }); + if (orientation === "isometric") { + // rows of constant col + row, each from the left + ordered.sort((a, b) => { + return a.col + a.row - (b.col + b.row) || a.col - b.col; + }); + for (const { col, row, gid } of ordered) { + drawTile( + gid, + ((col - row - 1) * tw) / 2 + (rows * tw) / 2, + ((col + row + 2) * th) / 2, + ); + } + } else { + ordered.sort((a, b) => { + return a.row - b.row || a.col - b.col; + }); + for (const { col, row, gid } of ordered) { + drawTile(gid, col * tw, (row + 1) * th); + } + } + } + return ctx.getImageData(0, 0, width, height); +} + /** * A layer's data, filled by a function of each cell. * @param {number} cols - columns @@ -242,18 +384,28 @@ export async function makeApp(renderer, type, width = 320, height = 240) { * Put a map's layers in the world, in place of whatever was there. * @param {Application} app - the application * @param {TMXTileMap} map - the map + * @param {string} [renderMode] - a render mode to ask of every layer before + * it is activated ("prerender" needs asking then: it bakes at activation) * @returns {object[]} the map's tile layers */ -export function show(app, map) { +export function show(app, map, renderMode) { app.world.reset(); app.renderer.reset?.(); app.renderer.backgroundColor.setColor(16, 24, 48); + if (renderMode !== undefined) { + for (const layer of map.getLayers()) { + layer.renderMode = renderMode; + } + } map.addTo(app.world, true, false); + // the layers in their order now: the world sorts its children later, on + // a timer no frame of these tests waits for + app.world.sortNow(true); app.viewport.setBounds(-100000, -100000, 200000, 200000); return map.getLayers(); } -let clock = 1000; +let clock = 0; /** * Draw one frame with every layer in the given mode, and return it with how @@ -268,9 +420,12 @@ export async function drawIn(app, layers, mode, dt = 0) { for (const layer of layers) { layer.renderMode = mode; } - // the world updated (what is in view, and animated tiles when time - // passes), then drawn - clock += dt; + // time passes (animated tiles advance once per tick of the engine's + // clock), then the world is updated: what is in view, what has changed + if (dt > 0) { + clock = Math.max(clock, timer.getTime()) + dt; + timer.update(clock); + } app.world.update(dt); app.viewport.update(dt); const cpu = vi.spyOn(Renderer.prototype, "drawTileLayer"); diff --git a/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js b/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js new file mode 100644 index 0000000000..6c9bb42d2a --- /dev/null +++ b/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js @@ -0,0 +1,1296 @@ +/** + * Tile layers on the GPU ("shader" render mode), isometric ones above all + * (#1897), held to the picture Tiled draws. + * + * Every scenario draws the same layers through the CPU renderer ("perTile") + * and through the GPU one, with the whole engine in between, and compares + * both frames pixel by pixel with a reference painted here with no engine + * at all (see `tile-layer-compare.js`). Where the reference does not reach + * (a faded or tinted layer, a zoomed camera, animated tiles), the two paths + * are compared with each other. + * + * The scenarios are the same on every GPU backend, so each one's spec file + * calls this with its renderer: an application with a GPU device takes the + * place of the one before it, and a spec file has its own page. + */ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { grabFrame } from "./pixels.js"; +import { + diff, + drawIn, + drawn, + FLIP_AD, + FLIP_H, + FLIP_V, + fill, + inBlock, + makeApp, + makeMap, + makeTileset, + reference, + show, +} from "./tile-layer-compare.js"; + +const TW = 64; +const TH = 32; + +/** a tileset of isometric blocks, `tw` × `th`, on the 64 × 32 grid */ +const blocks = (tw = TW, th = TH, options = {}) => { + return makeTileset({ + tw, + th, + opaque: (x, y) => { + return inBlock(x, y, tw, th, Math.min(tw, TW), TH); + }, + ...options, + }); +}; + +/** + * a tileset of blocks with soft edges, as drawn art has: a pixel is as + * opaque as the share of its 3 × 3 neighbours that are in the block. Every + * fourth tile is see-through all over, as glass or water is + */ +const softBlocks = (tw, th, options = {}) => { + const dw = Math.min(tw, TW); + return makeTileset({ + tw, + th, + alpha: (x, y, id) => { + let inside = 0; + for (let dy = -1; dy <= 1; dy++) { + for (let dx = -1; dx <= 1; dx++) { + inside += inBlock(x + dx, y + dy, tw, th, dw, TH) ? 1 : 0; + } + } + return Math.round((inside / 9) * (id % 4 === 3 ? 120 : 255)); + }, + ...options, + }); +}; + +/** a map with most cells filled, from one tileset, and a few holes */ +const scattered = (cols, rows, count = 8, firstgid = 1) => { + return fill(cols, rows, (c, r) => { + return (c * 3 + r) % 7 === 0 ? 0 : firstgid + ((c + r * 2) % count); + }); +}; + +/** an isometric scene, on the 64 × 32 grid */ +const iso = (options) => { + return { orientation: "isometric", tw: TW, th: TH, ...options }; +}; + +const WIDTH = 320; +const HEIGHT = 240; + +/** views over a map: inside, over each edge, and scrolled part way */ +const VIEWS = [ + [0, 0], + [70, 20], + [-40, -80], + [230, 110], + [-150, 30], + [100, -200], +]; + +/** the frames' background, as `makeApp` sets it */ +const BACKGROUND = [16, 24, 48]; + +/** + * What a picture becomes when its layer is faded or tinted as a whole: each + * drawn pixel multiplied by the tint, then blended with the background. + * @param {ImageData} picture - the reference picture + * @param {number} opacity - the layer's opacity + * @param {number[]} [tint] - the layer's tint, 0 to 255 a channel + * @returns {ImageData} the picture expected + */ +const layered = (picture, opacity, tint = [255, 255, 255]) => { + const out = new ImageData(picture.width, picture.height); + for (let i = 0; i < picture.data.length; i += 4) { + const isBackground = + picture.data[i] === BACKGROUND[0] && + picture.data[i + 1] === BACKGROUND[1] && + picture.data[i + 2] === BACKGROUND[2]; + for (let c = 0; c < 3; c++) { + out.data[i + c] = isBackground + ? BACKGROUND[c] + : Math.round( + ((picture.data[i + c] * tint[c]) / 255) * opacity + + BACKGROUND[c] * (1 - opacity), + ); + } + out.data[i + 3] = 255; + } + return out; +}; + +const ALL_FLIPS = [ + 0, + FLIP_H, + FLIP_V, + FLIP_H + FLIP_V, + FLIP_AD, + FLIP_H + FLIP_AD, + FLIP_V + FLIP_AD, + FLIP_H + FLIP_V + FLIP_AD, +]; + +/** + * Declare the scenarios for one GPU backend. + * @param {number} renderer - `video.WEBGL` or `video.WEBGPU` + * @param {string} type - the `renderer.type` expected ("WebGL", "WebGPU") + */ +export function isometricScenarios(renderer, type) { + describe(`tile layers on the GPU, against the CPU renderer and a reference (${type})`, () => { + let app; + beforeAll(async () => { + app = await makeApp(renderer, type, WIDTH, HEIGHT); + }); + afterAll(() => { + app?.destroy(); + vi.restoreAllMocks(); + }); + + const need = (ctx) => { + if (app === null || app === undefined) { + ctx.skip(`${type} not available in this environment`); + } + }; + + /** + * Draw a scene both ways from each view, and expect the reference + * picture from both, the GPU path drawing alone. + * @param {object} scene - what `makeMap` takes + * @param {object} [options] + * @param {number[][]} [options.views] - where to look from + * @param {object[]} [options.layers] - the scene's layers, when + * they are in the world already (and were changed there). Without + * them the scene is put in the world here, and every layer of it is + * expected to take the GPU path of its own accord + * @param {boolean} [options.oracle=true] - false where the reference + * does not reach (a zoomed camera): the two paths are then compared + * with each other alone + * @param {(picture: ImageData) => ImageData} [options.expected] + * what the reference becomes (a faded or tinted layer) + * @param {number} [options.tolerance=0] - the largest difference of a + * channel that is not counted + * @param {number} [options.share=0] - the largest share of the pixels + * drawn that the two paths may differ by + * @returns {Promise} how many pixels were drawn, in all + */ + const same = async (scene, options = {}) => { + const { + views = VIEWS, + oracle = true, + expected, + tolerance = 0, + share = 0, + } = options; + let layers = options.layers; + if (layers === undefined) { + layers = show(app, makeMap(scene)); + for (const layer of layers) { + expect(layer.renderMode, layer.name).toBe("shader"); + } + } + let total = 0; + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + const cpu = await drawIn(app, layers, "perTile"); + const gpu = await drawIn(app, layers, "shader"); + // a layer out of view is not drawn at all; one in view is + // drawn tile by tile by the CPU path, and never by the GPU one + expect(cpu.cpuDraws).toBeLessThanOrEqual(layers.length); + expect(gpu.cpuDraws, "the GPU path reaches no CPU renderer").toBe(0); + const at = `view (${x}, ${y})`; + const count = drawn(gpu.frame); + const allowed = Math.floor(count * share); + if (oracle) { + let ref = reference(scene, x, y, WIDTH, HEIGHT); + if (expected !== undefined) { + ref = expected(ref); + } + const c = diff(ref, cpu.frame, tolerance); + expect(c.different, `${at}, CPU path: ${c.first}`).toBe(0); + const g = diff(ref, gpu.frame, tolerance); + expect(g.different, `${at}, GPU path: ${g.first}`).toBe(0); + } + const d = diff(cpu.frame, gpu.frame, tolerance); + expect( + d.different, + `${at}, CPU against GPU: ${d.first}`, + ).toBeLessThanOrEqual(allowed); + total += count; + } + return total; + }; + + /** how a layer of the scene ends up drawn, and what was warned */ + const modeOf = (scene) => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + const layer = show(app, makeMap(scene))[0]; + return [layer.renderMode, warn.mock.calls.flat().join(" ")]; + } finally { + warn.mockRestore(); + } + }; + + describe("isometric: tiles of every size", () => { + for (const [label, tw, th] of [ + ["as tall as the grid (flat diamonds)", 64, 32], + ["twice as tall", 64, 64], + ["four times as tall, as the Isometric Dungeon's", 64, 128], + ["eight times as tall, the limit", 64, 256], + ["a height that is no multiple of the grid", 64, 50], + ["shorter than the grid", 64, 20], + ["narrower than the grid", 40, 64], + ["twice as wide", 128, 32], + ["four times as wide, the limit", 256, 64], + ["wider and taller, no multiple of the grid", 90, 70], + ]) { + it(label, async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(tw, th)], + layers: [{ data: scattered(8, 6) }], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + } + + for (const [tw, th] of [ + [64, 96], + [128, 128], + [100, 30], + ]) { + it(`${tw} × ${th} tiles that fill their whole image, so every overlap shows`, async (ctx) => { + need(ctx); + // no transparent pixel: which tile is in front decides + // every pixel where two images overlap + const scene = iso({ + cols: 7, + rows: 9, + tilesets: [makeTileset({ tw, th })], + layers: [{ data: scattered(7, 9) }], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + } + + it("on a grid of another size, and an odd one", async (ctx) => { + need(ctx); + for (const [tw, th] of [ + [32, 16], + [100, 50], + [30, 14], + [48, 48], + ]) { + const scene = { + orientation: "isometric", + cols: 9, + rows: 7, + tw, + th, + tilesets: [ + makeTileset({ + tw, + th: th * 2, + opaque: (x, y) => { + return inBlock(x, y, tw, th * 2, tw, th); + }, + }), + ], + layers: [{ data: scattered(9, 7) }], + }; + expect(await same(scene), `${tw} × ${th}`).toBeGreaterThan(0); + } + }); + }); + + describe("isometric: maps of every shape", () => { + for (const [cols, rows] of [ + [1, 1], + [1, 7], + [7, 1], + [3, 11], + [12, 4], + [20, 20], + ]) { + it(`${cols} × ${rows}`, async (ctx) => { + need(ctx); + const scene = iso({ + cols, + rows, + tilesets: [blocks(64, 64)], + layers: [ + { + data: fill(cols, rows, (c, r) => { + return 1 + ((c + r * 3) % 8); + }), + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + } + + it("a large map, of tiles like the Isometric Dungeon's", async (ctx) => { + need(ctx); + const scene = { + orientation: "isometric", + cols: 64, + rows: 64, + tw: 128, + th: 64, + tilesets: [ + makeTileset({ + tw: 128, + th: 256, + count: 16, + columns: 8, + opaque: (x, y) => { + return inBlock(x, y, 128, 256, 128, 64); + }, + }), + ], + layers: [{ data: scattered(64, 64, 16) }], + }; + const views = [ + [4096 - 160, 0], + [4096 - 160, 2000], + [100, 2040], + [7900, 2040], + [4096, 4000], + [3000, 1500], + ]; + expect(await same(scene, { views })).toBeGreaterThan(0); + }); + + it("an empty layer draws nothing, either way", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 6, + rows: 6, + tilesets: [blocks()], + layers: [ + { + data: fill(6, 6, () => { + return 0; + }), + }, + ], + }); + expect(await same(scene, { views: [[0, 0]] })).toBe(0); + }); + + it("a single tile, and tiles in the corners alone", async (ctx) => { + need(ctx); + const corners = iso({ + cols: 9, + rows: 5, + tilesets: [blocks(64, 128)], + layers: [ + { + data: fill(9, 5, (c, r) => { + return (c === 0 || c === 8) && (r === 0 || r === 4) ? 3 : 0; + }), + }, + ], + }); + expect(await same(corners)).toBeGreaterThan(0); + const one = iso({ + cols: 9, + rows: 5, + tilesets: [blocks(64, 128)], + layers: [ + { + data: fill(9, 5, (c, r) => { + return c === 4 && r === 2 ? 6 : 0; + }), + }, + ], + }); + expect(await same(one)).toBeGreaterThan(0); + }); + }); + + describe("isometric: what a tile can be", () => { + it("flipped, every way", async (ctx) => { + need(ctx); + // square tiles: a diagonal flip turns the image a quarter + const tileset = makeTileset({ + tw: 64, + th: 64, + opaque: (x, y) => { + return x + y > 20; + }, + }); + const scene = iso({ + cols: 8, + rows: 8, + tilesets: [tileset], + layers: [ + { + data: fill(8, 8, (c, r) => { + return 1 + ((c + r) % 8) + ALL_FLIPS[(c * 3 + r) % 8]; + }), + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("flipped across and down, taller than wide", async (ctx) => { + need(ctx); + const flips = [0, FLIP_H, FLIP_V, FLIP_H + FLIP_V]; + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 128)], + layers: [ + { + data: fill(8, 6, (c, r) => { + return 1 + ((c + r) % 8) + flips[(c + r * 3) % 4]; + }), + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("animated: each frame, as time passes", async (ctx) => { + need(ctx); + const tileset = blocks(64, 64, { + extra: { + tiles: [ + { + id: 0, + animation: [ + { tileid: 0, duration: 100 }, + { tileid: 5, duration: 100 }, + { tileid: 6, duration: 100 }, + ], + }, + { + id: 3, + animation: [ + { tileid: 3, duration: 150 }, + { tileid: 7, duration: 150 }, + ], + }, + ], + }, + }); + const layers = show( + app, + makeMap( + iso({ + cols: 8, + rows: 6, + tilesets: [tileset], + layers: [ + { + data: fill(8, 6, (c, r) => { + return 1 + ((c + r) % 4); + }), + }, + ], + }), + ), + ); + expect(layers[0].isAnimated).toBe(true); + expect(layers[0].renderMode).toBe("shader"); + app.viewport.moveTo(0, 0); + const frames = []; + for (let step = 0; step < 6; step++) { + // 60 ms a step: the two animations change at their own times + const cpu = await drawIn(app, layers, "perTile", 60); + const gpu = await drawIn(app, layers, "shader"); + expect(gpu.cpuDraws).toBe(0); + const d = diff(cpu.frame, gpu.frame); + expect(d.different, `step ${step}: ${d.first}`).toBe(0); + frames.push(gpu.frame); + } + // and the picture did change along the way + expect(diff(frames[0], frames[2]).different).toBeGreaterThan(0); + expect(diff(frames[2], frames[5]).different).toBeGreaterThan(0); + }); + + it("from a tileset with a margin and spacing", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 64, { margin: 3, spacing: 5 })], + layers: [{ data: scattered(8, 6) }], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + for (const [x, y] of [ + [0, 16], + [-12, 0], + [7, -23], + // further than a tile is large: the tile shows in cells that + // are not its own, and from views its cell is not in + [0, -80], + [150, 150], + [-200, 90], + ]) { + it(`moved by a tileset tileoffset of (${x}, ${y})`, async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 64, { extra: { tileoffset: { x, y } } })], + layers: [{ data: scattered(8, 6) }], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + } + + for (const [tw, th] of [ + [64, 32], + [64, 64], + [64, 128], + [128, 96], + ]) { + it(`with soft edges, and see-through, ${tw} × ${th}: each shows the tiles behind it`, async (ctx) => { + need(ctx); + // a texel that is not opaque is blended over the tiles + // behind it, as many as there are: one over the other + // in 8 bits a step tile by tile, in one go on the GPU, + // a rounding step or two apart + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [softBlocks(tw, th)], + layers: [{ data: scattered(8, 6) }], + }); + expect(await same(scene, { tolerance: 3 })).toBeGreaterThan(0); + }); + } + + it("from the map's second tileset", async (ctx) => { + need(ctx); + // the first tileset is not used by this layer at all, and is + // of another size: nothing of it may count + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 32), blocks(64, 96)], + layers: [{ data: scattered(8, 6, 8, 9) }], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("from a tileset smaller than another of the map's", async (ctx) => { + need(ctx); + // the other way round: the unused tileset is the large one + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(128, 256), blocks(64, 48)], + layers: [{ data: scattered(8, 6, 8, 9) }], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + }); + + describe("isometric: what a layer can be", () => { + it("half transparent, its tiles side by side", async (ctx) => { + need(ctx); + // flat diamonds do not overlap: each pixel is blended once, + // a rounding step apart at most + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 32)], + layers: [{ data: scattered(8, 6), opacity: 0.5 }], + }); + const expected = (picture) => { + return layered(picture, 0.5); + }; + expect(await same(scene, { expected, tolerance: 1 })).toBeGreaterThan( + 0, + ); + }); + + it("half transparent, its tiles overlapping: faded as one picture", async (ctx) => { + need(ctx); + // The GPU path draws the layer flat, then fades it: what is + // behind a tile does not show through it, as with a layer + // baked into one image ("prerender"). Tile by tile, each + // tile is blended on the ones behind it instead. + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 128)], + layers: [{ data: scattered(8, 6), opacity: 0.5 }], + }); + const views = VIEWS.slice(0, 3); + const baked = []; + let layers = show(app, makeMap(scene), "prerender"); + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + baked.push((await drawIn(app, layers, "prerender")).frame); + } + layers = show(app, makeMap(scene)); + expect(layers[0].renderMode).toBe("shader"); + for (const [i, [x, y]] of views.entries()) { + app.viewport.moveTo(x, y); + const gpu = await drawIn(app, layers, "shader"); + expect(gpu.cpuDraws).toBe(0); + const flat = layered(reference(scene, x, y, WIDTH, HEIGHT), 0.5); + const d = diff(flat, gpu.frame, 1); + expect(d.different, `view (${x}, ${y}): ${d.first}`).toBe(0); + const b = diff(baked[i], gpu.frame, 1); + expect(b.different, `as prerender, view (${x}, ${y})`).toBe(0); + expect(drawn(gpu.frame)).toBeGreaterThan(0); + } + }); + + it("tinted", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 128)], + layers: [{ data: scattered(8, 6), tintcolor: "#ff8040" }], + }); + const expected = (picture) => { + return layered(picture, 1, [255, 128, 64]); + }; + expect(await same(scene, { expected, tolerance: 1 })).toBeGreaterThan( + 0, + ); + }); + + it("one of several, each on a tileset of its own", async (ctx) => { + need(ctx); + // a floor of flat tiles under walls of tall ones + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 32), blocks(64, 128)], + layers: [ + { + data: fill(8, 6, (c, r) => { + return 1 + ((c + r) % 8); + }), + }, + { + data: fill(8, 6, (c, r) => { + return (c + r) % 3 === 0 ? 9 + ((c * 2 + r) % 8) : 0; + }), + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("one of several, on tilesets of several sizes moved by a tileoffset", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [ + blocks(64, 32, { extra: { tileoffset: { x: 5, y: 9 } } }), + blocks(64, 128, { extra: { tileoffset: { x: -20, y: 40 } } }), + blocks(128, 64), + ], + layers: [ + { data: scattered(8, 6) }, + { data: scattered(8, 6, 8, 9) }, + { + data: fill(8, 6, (c, r) => { + return (c + r) % 4 === 1 ? 17 + ((c + r * 3) % 8) : 0; + }), + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("of an infinite map, starting left of and above the origin", async (ctx) => { + need(ctx); + const chunk = (x, y) => { + return { + x, + y, + width: 4, + height: 4, + data: fill(4, 4, (c, r) => { + return (c + r + x) % 5 === 0 ? 0 : 1 + ((c + r * 2) % 8); + }), + }; + }; + const scene = iso({ + cols: 8, + rows: 8, + tilesets: [blocks(64, 64)], + extra: { infinite: true }, + layers: [ + { + startx: -4, + starty: -4, + width: 12, + height: 8, + data: undefined, + chunks: [chunk(-4, -4), chunk(0, -4), chunk(4, 0), chunk(0, 0)], + }, + ], + }); + const layers = show(app, makeMap(scene)); + expect(layers[0].startx).toBe(-4); + expect(layers[0].renderMode).toBe("shader"); + const views = [ + [0, 0], + [-200, -150], + [150, -100], + [-60, 40], + ]; + expect(await same(scene, { views, layers })).toBeGreaterThan(0); + }); + }); + + describe("isometric: what a camera can do", () => { + const tall = () => { + return iso({ + cols: 10, + rows: 8, + tilesets: [blocks(64, 128)], + layers: [{ data: scattered(10, 8) }], + }); + }; + + it("look from anywhere, and from where nothing shows", async (ctx) => { + need(ctx); + const scene = tall(); + const layers = show(app, makeMap(scene)); + const views = []; + for (let x = -400; x <= 700; x += 137) { + for (let y = -300; y <= 400; y += 97) { + views.push([x, y]); + } + } + expect(await same(scene, { views, layers })).toBeGreaterThan(0); + // far off the map: nothing, either way + const far = [ + [5000, 5000], + [-5000, 0], + ]; + expect(await same(scene, { views: far, layers })).toBe(0); + }); + + it("scroll a pixel at a time, across a tile", async (ctx) => { + need(ctx); + const scene = tall(); + const layers = show(app, makeMap(scene)); + const views = []; + for (let step = 0; step <= 64; step += 3) { + views.push([40 + step, 10 + Math.floor(step / 2)]); + } + expect(await same(scene, { views, layers })).toBeGreaterThan(0); + }); + + it("stand between pixels", async (ctx) => { + need(ctx); + // the camera's position is rounded to whole pixels when the + // world is drawn: the reference is painted from there + const scene = tall(); + const layers = show(app, makeMap(scene)); + for (const [x, y] of [ + [10.5, 7.25], + [-33.75, 60.5], + [120.1, -45.9], + ]) { + app.viewport.moveTo(x, y); + const cpu = await drawIn(app, layers, "perTile"); + const gpu = await drawIn(app, layers, "shader"); + expect(gpu.cpuDraws).toBe(0); + const d = diff(cpu.frame, gpu.frame); + expect(d.different, `view (${x}, ${y}): ${d.first}`).toBe(0); + expect(drawn(gpu.frame)).toBeGreaterThan(0); + } + }); + + it("be shifted by its offset, as a shake does", async (ctx) => { + need(ctx); + const scene = tall(); + app.viewport.offset.set(13, -9); + try { + expect(await same(scene, { oracle: false })).toBeGreaterThan(0); + } finally { + app.viewport.offset.set(0, 0); + } + }); + + for (const zoom of [2, 3]) { + it(`zoom ${zoom} times`, async (ctx) => { + need(ctx); + const scene = tall(); + app.viewport.zoom = zoom; + try { + expect(await same(scene, { oracle: false })).toBeGreaterThan(0); + } finally { + app.viewport.zoom = 1; + } + }); + } + + for (const zoom of [0.5, 1.5]) { + it(`zoom ${zoom} times, a texel apart at most but for a few pixels`, async (ctx) => { + need(ctx); + // Tiles are sampled without smoothing: where a screen + // pixel's centre falls exactly between two texels, a + // quad per tile and one quad for the layer do not round + // the same way. A texel apart is within the tolerance + // (colours change by 7 a texel at most here); the + // outlines of the tiles, where the next texel is another + // tile's, are the share left. An orthogonal layer's two + // paths are as far apart. + const scene = tall(); + app.viewport.zoom = zoom; + try { + expect( + await same(scene, { oracle: false, tolerance: 8, share: 0.06 }), + ).toBeGreaterThan(0); + } finally { + app.viewport.zoom = 1; + } + }); + } + }); + + describe("isometric: tiles changed while the game runs", () => { + /** set a cell in the world and in the scene the reference reads */ + const setCell = (scene, layer, col, row, gid) => { + scene.layers[0].data[row * scene.cols + col] = gid; + if (gid === 0) { + layer.clearTile(col, row); + } else { + layer.setTile(layer.getTileById(gid, col, row), col, row); + } + }; + + it("setTile and clearTile show at once", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 128)], + layers: [{ data: scattered(8, 6) }], + }); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + const options = { views: [[0, 0]], layers }; + await same(scene, options); + const before = (await drawIn(app, layers, "shader")).frame; + + setCell(scene, layer, 3, 2, 5); + setCell(scene, layer, 0, 5, 2 + FLIP_H); + await same(scene, options); + const set = (await drawIn(app, layers, "shader")).frame; + expect(diff(before, set).different).toBeGreaterThan(0); + + setCell(scene, layer, 3, 2, 0); + setCell(scene, layer, 7, 0, 0); + await same(scene, options); + const cleared = (await drawIn(app, layers, "shader")).frame; + expect(diff(set, cleared).different).toBeGreaterThan(0); + expect(layer.renderMode).toBe("shader"); + }); + + it("every cell changed, one frame after the other", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 6, + rows: 5, + tilesets: [blocks(64, 96)], + layers: [{ data: scattered(6, 5) }], + }); + const layers = show(app, makeMap(scene)); + const options = { views: [[0, 0]], layers }; + for (let step = 0; step < 4; step++) { + for (let row = 0; row < 5; row++) { + for (let col = 0; col < 6; col++) { + const gid = + (col + row + step) % 4 === 0 ? 0 : 1 + ((col * step + row) % 8); + setCell(scene, layers[0], col, row, gid); + } + } + await same(scene, options); + } + expect(layers[0].renderMode).toBe("shader"); + }); + + it("an empty layer takes its tileset from the first tile set", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 6, + rows: 6, + tilesets: [blocks(64, 32), blocks(64, 96)], + layers: [ + { + data: fill(6, 6, () => { + return 0; + }), + }, + ], + }); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + expect(layer._shaderTileset).toBeUndefined(); + setCell(scene, layer, 2, 3, 12); + setCell(scene, layer, 3, 3, 10); + expect(layer.renderMode).toBe("shader"); + expect(layer._shaderTileset.firstgid).toBe(9); + expect(await same(scene, { views: [[0, 0]], layers })).toBeGreaterThan( + 0, + ); + }); + + it("a tile of a second tileset moves the layer back to the CPU renderer", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 6, + rows: 6, + tilesets: [blocks(64, 32), blocks(64, 96)], + layers: [{ data: scattered(6, 6) }], + }); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + expect(layer.renderMode).toBe("shader"); + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + // one of the same tileset: nothing changes + setCell(scene, layer, 1, 1, 3); + expect(layer.renderMode).toBe("shader"); + expect(warn).not.toHaveBeenCalled(); + // one of the other + setCell(scene, layer, 2, 2, 10); + expect(layer.renderMode).toBe("perTile"); + expect(warn.mock.calls.flat().join(" ")).toMatch( + /more than one tileset/, + ); + } finally { + warn.mockRestore(); + } + // and the layer is drawn right from there on + app.viewport.moveTo(0, 0); + app.world.update(0); + app.repaint(); + app.draw(); + const frame = await grabFrame(app.renderer); + const d = diff(reference(scene, 0, 0, WIDTH, HEIGHT), frame); + expect(d.different, d.first).toBe(0); + }); + + it("a tile that is not square, flipped diagonally, does too", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 6, + rows: 6, + tilesets: [blocks(64, 96)], + layers: [{ data: scattered(6, 6) }], + }); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + expect(layer.renderMode).toBe("shader"); + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + setCell(scene, layer, 1, 1, 3 + FLIP_H + FLIP_V); + expect(layer.renderMode).toBe("shader"); + setCell(scene, layer, 2, 2, 4 + FLIP_AD); + expect(layer.renderMode).toBe("perTile"); + expect(warn.mock.calls.flat().join(" ")).toMatch( + /not square, flipped diagonally/, + ); + } finally { + warn.mockRestore(); + } + }); + }); + + describe("isometric: layers the GPU path does not take", () => { + it("tiles of two tilesets in one layer", (ctx) => { + need(ctx); + const [mode, said] = modeOf( + iso({ + cols: 6, + rows: 6, + tilesets: [blocks(64, 32), blocks(64, 96)], + layers: [ + { + data: fill(6, 6, (c, r) => { + return 1 + ((c + r) % 16); + }), + }, + ], + }), + ); + expect(mode).toBe("perTile"); + expect(said).toMatch(/more than one tileset in an isometric layer/); + }); + + it("tiles past the size the shader walks", (ctx) => { + need(ctx); + const [tall, saidTall] = modeOf( + iso({ + cols: 4, + rows: 4, + tilesets: [blocks(64, 257)], + layers: [{ data: scattered(4, 4) }], + }), + ); + expect(tall).toBe("perTile"); + expect(saidTall).toMatch(/larger than the shader's limit/); + const [wide] = modeOf( + iso({ + cols: 4, + rows: 4, + tilesets: [makeTileset({ tw: 257, th: 32 })], + layers: [{ data: scattered(4, 4) }], + }), + ); + expect(wide).toBe("perTile"); + }); + + it("a tileset drawn at the map's grid size", (ctx) => { + need(ctx); + const [mode, said] = modeOf( + iso({ + cols: 4, + rows: 4, + tilesets: [blocks(64, 64, { extra: { tilerendersize: "grid" } })], + layers: [{ data: scattered(4, 4) }], + }), + ); + expect(mode).toBe("perTile"); + expect(said).toMatch(/tilerendersize/); + }); + + it("a tile that is not square, flipped diagonally", (ctx) => { + need(ctx); + const [mode, said] = modeOf( + iso({ + cols: 4, + rows: 4, + tilesets: [blocks(64, 96)], + layers: [ + { + data: fill(4, 4, (c, r) => { + return 1 + ((c + r) % 8) + (c === 2 && r === 1 ? FLIP_AD : 0); + }), + }, + ], + }), + ); + expect(mode).toBe("perTile"); + expect(said).toMatch(/not square, flipped diagonally/); + }); + + it("a staggered map, still", (ctx) => { + need(ctx); + const [mode, said] = modeOf({ + orientation: "staggered", + cols: 4, + rows: 4, + tw: TW, + th: TH, + tilesets: [blocks()], + layers: [{ data: scattered(4, 4) }], + extra: { staggeraxis: "y", staggerindex: "odd" }, + }); + expect(mode).toBe("perTile"); + expect(said).toMatch(/no gpu renderer supported yet for "staggered"/); + }); + + it("any layer, with gpuTilemap off", (ctx) => { + need(ctx); + app.world.gpuTilemap = false; + try { + const [mode] = modeOf( + iso({ + cols: 4, + rows: 4, + tilesets: [blocks()], + layers: [{ data: scattered(4, 4) }], + }), + ); + expect(mode).toBe("perTile"); + } finally { + app.world.gpuTilemap = true; + } + }); + }); + + describe("orthogonal: the same picture, both ways", () => { + const ortho = (options) => { + return { orientation: "orthogonal", tw: 32, th: 32, ...options }; + }; + const ORTHO_VIEWS = [ + [0, 0], + [40, 30], + [-50, -60], + [200, 150], + [-250, 20], + [90, -150], + ]; + + it("tiles the size of the grid, flipped every way", async (ctx) => { + need(ctx); + const scene = ortho({ + cols: 12, + rows: 10, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [ + { + data: fill(12, 10, (c, r) => { + return (c * 3 + r) % 5 === 0 + ? 0 + : 1 + ((c + r * 2) % 8) + ALL_FLIPS[(c + r) % 8]; + }), + }, + ], + }); + expect(await same(scene, { views: ORTHO_VIEWS })).toBeGreaterThan(0); + }); + + // tiles larger than the grid stand on their cell and reach above + // it and to its right: above the map's first row and right of + // its last column too + for (const [tw, th] of [ + [32, 64], + [32, 128], + [64, 32], + [96, 96], + [48, 40], + ]) { + it(`${tw} × ${th} tiles on a 32 × 32 grid`, async (ctx) => { + need(ctx); + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [ + makeTileset({ + tw, + th, + opaque: (x, y) => { + return (x + y) % 9 !== 0 || x < 4; + }, + }), + ], + layers: [{ data: scattered(10, 8) }], + }); + expect(await same(scene, { views: ORTHO_VIEWS })).toBeGreaterThan(0); + }); + } + + it("layers on tilesets of several sizes", async (ctx) => { + need(ctx); + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [ + makeTileset({ tw: 32, th: 32 }), + makeTileset({ + tw: 64, + th: 96, + opaque: (x, y) => { + return x > 10 && y > 6; + }, + }), + ], + layers: [ + { data: scattered(10, 8) }, + { + data: fill(10, 8, (c, r) => { + return (c + r * 2) % 4 === 0 ? 9 + ((c + r) % 8) : 0; + }), + }, + ], + }); + expect(await same(scene, { views: ORTHO_VIEWS })).toBeGreaterThan(0); + }); + + it("tiles larger than the grid, with soft edges and see-through", async (ctx) => { + need(ctx); + for (const [tw, th] of [ + [64, 64], + [32, 96], + [80, 48], + ]) { + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [ + makeTileset({ + tw, + th, + alpha: (x, y, id) => { + // a soft border, and every third tile + // see-through all over + const edge = Math.min(x, y, tw - 1 - x, th - 1 - y); + const a = id % 3 === 2 ? 110 : 255; + return Math.round(a * Math.min(1, (edge + 1) / 4)); + }, + }), + ], + layers: [{ data: scattered(10, 8) }], + }); + expect( + await same(scene, { views: ORTHO_VIEWS, tolerance: 3 }), + `${tw} × ${th}`, + ).toBeGreaterThan(0); + } + }); + + it("a tile that is not square, flipped diagonally, is left to the CPU renderer", (ctx) => { + need(ctx); + const [mode, said] = modeOf( + ortho({ + cols: 4, + rows: 4, + tilesets: [makeTileset({ tw: 32, th: 64 })], + layers: [ + { + data: fill(4, 4, (c, r) => { + return 1 + ((c + r) % 8) + (c === 1 && r === 2 ? FLIP_AD : 0); + }), + }, + ], + }), + ); + expect(mode).toBe("perTile"); + expect(said).toMatch(/not square, flipped diagonally/); + }); + }); + + it("a level change frees what the GPU path kept for its layers", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 6, + rows: 6, + tilesets: [blocks(64, 64)], + layers: [{ data: scattered(6, 6) }], + }); + const layers = show(app, makeMap(scene)); + const options = { views: [[0, 0]], layers }; + await same(scene, options); + const gpu = + app.renderer._tmxGPURenderers?.get("isometric") ?? + app.renderer.tmxRenderers?.get("isometric"); + expect(gpu.resources.size).toBe(1); + app.renderer.reset(); + expect(gpu.resources.size).toBe(0); + // and the layer draws again after it + expect(await same(scene, options)).toBeGreaterThan(0); + }); + }); +} diff --git a/packages/melonjs/tests/texture-mipmap.spec.js b/packages/melonjs/tests/texture-mipmap.spec.js index 9e11496a88..9d9a64dcc8 100644 --- a/packages/melonjs/tests/texture-mipmap.spec.js +++ b/packages/melonjs/tests/texture-mipmap.spec.js @@ -23,13 +23,17 @@ import { createAtlas, TextureAtlas } from "../src/video/texture/atlas.js"; import WebGPULitQuadBatcher from "../src/video/webgpu/batchers/lit_quad_batcher.js"; import WebGPUQuadBatcher from "../src/video/webgpu/batchers/quad_batcher.js"; import WebGPUTMXRenderer from "../src/video/webgpu/renderers/tmxlayer/orthogonal.js"; -import tmxlayerWGSL from "../src/video/webgpu/shaders/tmxlayer.wgsl"; +import tmxlayerCommonWGSL from "../src/video/webgpu/shaders/tmxlayer-common.wgsl"; +import tmxlayerOrthogonalWGSL from "../src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl"; import { getWebGLRenderer, releaseWebGLRenderer, } from "./helpers/webgl-context.js"; import { createMockWebGPURenderer } from "./helpers/webgpu-mock-renderer.js"; +// the orthogonal tile shader's module, as its renderer puts it together +const tmxlayerWGSL = `${tmxlayerCommonWGSL}\n${tmxlayerOrthogonalWGSL}`; + const SIZE = 64; // a checkerboard of single texels, `size` square, at (x, y) of `ctx` @@ -582,8 +586,10 @@ describe("linear-mipmap texture filter (WebGPU, mock renderer)", () => { expect(code).toContain( "return texel * tmx.uOpacity * vec4f(tmx.uTint.rgb, 1.0);", ); - expect(code.match(/layerColor\(result\.color\)/g)).toHaveLength(2); - expect(code).not.toMatch(/result\.color\.a \* tmx\.uOpacity/); + // every color the fragment stage returns goes through it: the + // single cell of the fast path, and what the cells add up to + expect(code.match(/return layerColor\(/g)).toHaveLength(2); + expect(code).not.toMatch(/\.a \* tmx\.uOpacity/); }); it("keeps each sample half a texel inside its tile, mipmaps or not", () => { diff --git a/packages/melonjs/tests/tmxlayer-shader-isometric.spec.js b/packages/melonjs/tests/tmxlayer-shader-isometric.spec.js new file mode 100644 index 0000000000..e8363cc0f4 --- /dev/null +++ b/packages/melonjs/tests/tmxlayer-shader-isometric.spec.js @@ -0,0 +1,8 @@ +/** + * Isometric tile layers on the GPU, on WebGL: see + * `helpers/tile-layer-isometric-scenarios.js`. + */ +import { video } from "../src/index.js"; +import { isometricScenarios } from "./helpers/tile-layer-isometric-scenarios.js"; + +isometricScenarios(video.WEBGL, "WebGL"); diff --git a/packages/melonjs/tests/webgpu_tmxlayer_isometric.spec.js b/packages/melonjs/tests/webgpu_tmxlayer_isometric.spec.js new file mode 100644 index 0000000000..c4ae0bf009 --- /dev/null +++ b/packages/melonjs/tests/webgpu_tmxlayer_isometric.spec.js @@ -0,0 +1,9 @@ +/** + * Isometric tile layers on the GPU, on WebGPU: see + * `helpers/tile-layer-isometric-scenarios.js`. Skipped, visibly, where the + * browser has no WebGPU adapter. + */ +import { video } from "../src/index.js"; +import { isometricScenarios } from "./helpers/tile-layer-isometric-scenarios.js"; + +isometricScenarios(video.WEBGPU, "WebGPU"); From b5f1e0dfe4235a8dcee9b6764b96379da07bbfb3 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 01:42:04 +0000 Subject: [PATCH 04/11] GPU tile layers: review fixes, shared orientation module, benchmark (#1897) From an independent review of the isometric GPU path: - a grid of odd size, and a blend mode other than the normal one, are left to the CPU renderer: the first stands half the tiles half a pixel off, the second shows where tiles overlap - a tileset image with no margin after its last tiles was counted a column short (the orthogonal path too) - a layer asked for "shader" by hand after it was found unfit for it is drawn tile by tile, where an isometric one drew nothing - the tile shader declares full precision itself, and finds a tile's place in the atlas in whole numbers Faster per fragment: what sampling needs of a fragment is taken once instead of for every cell tried, a cell whose tile cannot reach the fragment is dropped before its index is fetched, and the isometric walk skips the rows outside the map. What differs with a map's orientation is in one module for both backends (`gpu/tmxlayer_orientation.js`: the region, the tilesets, the shader's limits, which the layer's checks read too), so each backend has one renderer class and the shader text. `tests/tmxlayer_bench.spec.js` measures the main thread's share of a frame tile by tile and on the GPU path, orthogonal and isometric. Changelog, API docs and the tilemaps skill. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01JnzwA4a7ZyjLBpbeEpFLu6 --- packages/melonjs/CHANGELOG.md | 14 ++ .../melonjs/skills/melonjs-tilemaps/SKILL.md | 30 ++- packages/melonjs/src/application/settings.ts | 20 +- packages/melonjs/src/level/tiled/TMXLayer.js | 53 +++-- packages/melonjs/src/physics/world.js | 25 ++- ...ayer_region.js => tmxlayer_orientation.js} | 113 +++++++++- .../webgl/renderers/tmxlayer/isometric.js | 54 ----- .../webgl/renderers/tmxlayer/orthogonal.js | 33 --- .../webgl/renderers/tmxlayer/tmxlayer.js | 99 ++++----- .../webgl/shaders/isometric-tmxlayer.frag | 19 +- .../webgl/shaders/orthogonal-tmxlayer.frag | 11 +- .../video/webgl/shaders/tmxlayer-common.frag | 97 +++++---- .../melonjs/src/video/webgl/webgl_renderer.js | 22 +- .../webgpu/renderers/tmxlayer/isometric.js | 54 ----- .../webgpu/renderers/tmxlayer/orthogonal.js | 33 --- .../webgpu/renderers/tmxlayer/tmxlayer.js | 90 ++++---- .../video/webgpu/shaders/tmxlayer-common.wgsl | 82 +++---- .../webgpu/shaders/tmxlayer-isometric.wgsl | 20 +- .../webgpu/shaders/tmxlayer-orthogonal.wgsl | 10 +- .../src/video/webgpu/webgpu_renderer.js | 23 +- .../tests/helpers/tile-layer-compare.js | 9 +- .../helpers/tile-layer-isometric-scenarios.js | 104 +++++++++ .../melonjs/tests/sprite-frame-clamp.spec.js | 4 +- packages/melonjs/tests/texture-mipmap.spec.js | 6 +- .../melonjs/tests/tmxlayer-shader.spec.js | 4 +- packages/melonjs/tests/tmxlayer_bench.spec.js | 201 ++++++++++++++++++ .../melonjs/tests/webgpu_tmxlayer.spec.js | 100 ++++++++- 27 files changed, 863 insertions(+), 467 deletions(-) rename packages/melonjs/src/video/gpu/{tmxlayer_region.js => tmxlayer_orientation.js} (59%) delete mode 100644 packages/melonjs/src/video/webgl/renderers/tmxlayer/isometric.js delete mode 100644 packages/melonjs/src/video/webgl/renderers/tmxlayer/orthogonal.js delete mode 100644 packages/melonjs/src/video/webgpu/renderers/tmxlayer/isometric.js delete mode 100644 packages/melonjs/src/video/webgpu/renderers/tmxlayer/orthogonal.js create mode 100644 packages/melonjs/tests/tmxlayer_bench.spec.js diff --git a/packages/melonjs/CHANGELOG.md b/packages/melonjs/CHANGELOG.md index 3419546c1e..774ae5ab0b 100644 --- a/packages/melonjs/CHANGELOG.md +++ b/packages/melonjs/CHANGELOG.md @@ -5,7 +5,21 @@ ### Added - Transitions: a mask transition can be shaped like an image or a `Sprite`, close on the player with `center`, and fill the shape with `invert`, on `state.transition`, `MaskEffect` and `Trigger` ([#1399](https://github.com/melonjs/melonJS/issues/1399)) +### Changed +- Tiled: an isometric tile layer with an `opacity` below 1 fades as one flat picture on WebGL and WebGPU, where each tile was faded over the ones behind it and showed through the one in front. Set the layer's `renderMode` to `"perTile"` for the look it had + +### Performance +- Tiled: isometric tile layers are drawn by the GPU on WebGL and WebGPU, as orthogonal ones are: one quad for the layer, its tiles found by the fragment shader, in place of a quad per tile in view found and placed in JavaScript every frame. On a 100 × 100 map with about 4000 tiles in view, drawing the layer takes under **0.1 ms** of main-thread time a frame, down from about **2 ms**, and no longer grows with the number of tiles shown. A layer takes this path by itself when all its tiles come from one tileset, up to 4 grid widths wide and 8 grid heights tall (a `tileoffset`, flipped and animated tiles, and `setTile` are all fine); any other is drawn as before, and the console says why ([#1897](https://github.com/melonjs/melonJS/issues/1897)) +- Tiled: a reminder that orthogonal tile layers have been drawn this way since 19.4, with the same gain (about 1.9 ms down to under 0.1 ms for 3600 tiles in view). A layer that is not says why in the console when it is activated: most often a tileset with a `tileoffset`, a collection of images, or the Canvas renderer. + ### Fixed +- Tiled, GPU tile layers: the soft edge of a tile larger than the grid shows the tile behind it; it was drawn over the background instead, a thin outline round such tiles +- Tiled, GPU tile layers: tiles larger than the grid are drawn whole above the map's first row and right of its last column, where they were cut off +- Tiled, GPU tile layers: a tileset with a `margin` whose image stops at its last tiles, with no margin after them, draws the right tiles; they were counted a column or a row short +- Tiled, GPU tile layers: a layer with a tile that is not square, flipped diagonally, is drawn tile by tile; the shader drew such a tile wrong +- Tiled, GPU tile layers: the shader runs at full precision whatever `highPrecisionShader` says; at a lower one a phone could misplace the tiles of a large map +- Tiled, isometric maps drawn tile by tile: tiles of a tileset larger than the map's first one, or moved by a `tileoffset`, went missing along the edges of the view +- Tiled, orthogonal maps drawn tile by tile: tiles more than two cells tall went missing along the bottom of the view - `NineSliceSprite` blinks with `flicker()`, as a `Sprite` does; it ignored it - glTF: a model compressed with Draco or meshopt, which the loader cannot decode, says so in the console, naming the extension, instead of failing on an accessor error that did not mention it diff --git a/packages/melonjs/skills/melonjs-tilemaps/SKILL.md b/packages/melonjs/skills/melonjs-tilemaps/SKILL.md index 51481cf2af..bb5933e1a6 100644 --- a/packages/melonjs/skills/melonjs-tilemaps/SKILL.md +++ b/packages/melonjs/skills/melonjs-tilemaps/SKILL.md @@ -453,11 +453,28 @@ per-pixel GID lookup in the fragment shader. It requires **all** of: - a renderer that reports GPU tile-layer support (WebGL 2 / WebGPU — not Canvas), - `world.gpuTilemap !== false` (the `gpuTilemap` application setting, default `true`), -- an **orthogonal** layer — isometric, hexagonal, staggered and oblique always - fall back, -- no collection-of-image tileset, no non-zero `tileoffset`, a tileset tile - render size of "Tile size" (`tilerendersize`, not "Map grid size"), and tile - overflow within the shader's 4-cell limit. +- an **orthogonal** or **isometric** layer — hexagonal, staggered and oblique + always fall back, +- no collection-of-image tileset, and a tileset tile render size of "Tile + size" (`tilerendersize`, not "Map grid size"), +- on an orthogonal map: no non-zero `tileoffset`, and tiles that reach at most + 4 cells past their own, +- on an isometric map: every tile of the layer from **one tileset** (tiles + overlap, and their order is kept within a tileset, not across two: split + floor and walls into a layer each, which a map usually does already), tiles + at most 4 grid widths wide and 8 grid heights tall, a grid of even size + (64 × 32, not 65 × 33), and the normal blend mode. A `tileoffset` is fine, +- no tile that is not square flipped diagonally (rotated, in Tiled). + +A tile set at run time with `setTile` that breaks one of these moves the layer +back to `"perTile"`, with a warning. The two paths draw the same picture, with +one difference: a layer with `opacity` below 1 is faded as one flat picture on +the shader path (as `preRender` does), where tile by tile each tile is faded +over the ones behind it, which shows where tiles overlap. + +The gain is main-thread time: tile by tile, every tile in view is found and +placed in JavaScript each frame (about 2 ms for 4000 tiles); the shader path +draws one quad whatever is in view. When a layer falls back, melonJS `console.warn`s the reason each time the layer is activated (once per page when the renderer has no GPU tile support at @@ -509,6 +526,9 @@ layer into the offscreen-bake path instead. | changing tiles at runtime is slow on a pre-rendered layer | outside an orthogonal map whose tiles fit their cells, a changed tile re-bakes the whole layer before it next draws: keep layers you edit often off `preRender` | | thin lines between tiles on the Canvas renderer, with smoothing on | filtering blends in the next tile of the tileset or its spacing, which a canvas cannot prevent: set `antiAlias: false`, or `preRender` the layer. WebGL and WebGPU keep each tile to itself | | tilemap slow on some machines | layer fell back off the shader path — check the console warning for the reason | +| an isometric layer warns `tiles of more than one tileset in an isometric layer` and is slow | the shader path draws an isometric layer with one tileset: put the tiles of each tileset on a layer of their own, or pack them into one tileset | +| a half-transparent isometric layer of tall tiles looks flatter than before 20.17 | on the shader path a layer fades as one picture, and a tile no longer shows through the one in front: set that layer's `renderMode` to `"perTile"` (a `renderMode` custom property in Tiled) for the old look | +| the soft edges of tiles larger than the grid showed a thin dark outline on the shader path | before 20.17 the edge of such a tile was not blended over the tile behind it: update | ## Related skills diff --git a/packages/melonjs/src/application/settings.ts b/packages/melonjs/src/application/settings.ts index 6010e6f77b..f4733069dc 100644 --- a/packages/melonjs/src/application/settings.ts +++ b/packages/melonjs/src/application/settings.ts @@ -476,15 +476,17 @@ export type ApplicationSettings = { physic: PhysicsType; /** - * Enable the GPU procedural shader path for orthogonal tile layers - * (backends advertising `renderer.supportsShaderTileLayers` — WebGL 2 - * and WebGPU). When `true` (default), eligible layers render via a - * single quad per tileset + a fragment shader doing per-fragment GID - * lookup, bypassing the per-tile draw loop entirely. Layers that - * don't qualify (Canvas renderer, non-orthogonal, - * collection-of-image tilesets, tilerendersize "grid", non-zero - * tileoffset, oversampled beyond the shader's overflow window) fall - * back to the legacy path automatically. + * Enable the GPU procedural shader path for orthogonal and isometric + * tile layers (backends advertising + * `renderer.supportsShaderTileLayers` — WebGL 2 and WebGPU). When + * `true` (default), eligible layers render via a single quad per + * tileset + a fragment shader doing per-fragment GID lookup, bypassing + * the per-tile draw loop entirely. Layers that don't qualify (Canvas + * renderer, a staggered, hexagonal or oblique map, collection-of-image + * tilesets, tilerendersize "grid", tiles larger than the shader walks; + * on an orthogonal map a non-zero tileoffset, on an isometric one + * tiles of several tilesets in one layer) fall back to the legacy path + * automatically, saying why in the console. * Set to `false` to disable globally. * @default true */ diff --git a/packages/melonjs/src/level/tiled/TMXLayer.js b/packages/melonjs/src/level/tiled/TMXLayer.js index 98585e4196..a18991eff2 100644 --- a/packages/melonjs/src/level/tiled/TMXLayer.js +++ b/packages/melonjs/src/level/tiled/TMXLayer.js @@ -5,6 +5,7 @@ import { boundsPool } from "../../physics/bounds.ts"; import Renderable from "../../renderable/renderable.js"; import CanvasRenderer from "../../video/canvas/canvas_renderer"; import { createCanvas } from "../../video/canvas_factory.js"; +import { TMX_ORIENTATIONS } from "../../video/gpu/tmxlayer_orientation.js"; import { TMX_CLEAR_BIT_MASK, TMX_FLIP_AD, @@ -26,10 +27,14 @@ const FLIP_H_BIT = 1 << 0; const FLIP_V_BIT = 1 << 1; const FLIP_AD_BIT = 1 << 2; -// The largest tiles the isometric shader path draws, in grid cells: the -// bounds of the loops in `isometric-tmxlayer.frag` and its WGSL twin. -const ISOMETRIC_MAX_TILE_WIDTHS = 4; -const ISOMETRIC_MAX_TILE_HEIGHTS = 8; +// The largest tiles the shader path draws: the bounds of the loops of the +// fragment shaders, which are declared from the same numbers. An orthogonal +// tile can reach so many cells past its own; an isometric one be so many +// grid cells wide and tall. +const ORTHOGONAL_MAX_OVERFLOW = TMX_ORIENTATIONS.orthogonal.limits.MAX_OVERFLOW; +const ISOMETRIC_MAX_TILE_WIDTHS = TMX_ORIENTATIONS.isometric.limits.MAX_COLS; +const ISOMETRIC_MAX_TILE_HEIGHTS = + TMX_ORIENTATIONS.isometric.limits.MAX_ROWS / 2; const ISOMETRIC_ONE_TILESET = "tiles of more than one tileset in an isometric layer"; // A diagonal flip turns a tile a quarter: one that is not square then covers @@ -229,6 +234,14 @@ export default class TMXLayer extends Renderable { * @ignore */ this.maxTileOffset = { x: 0, y: 0 }; + /** + * The one tileset an isometric layer on the shader path draws with: + * `undefined` for an empty layer (it takes the tileset of the first + * tile set), `null` for a layer that path cannot draw, or that was + * not checked for it. + * @ignore + */ + this._shaderTileset = null; for (let i = 0; i < this.tilesets.length; i++) { const tileset = this.tilesets.getTilesetByIndex(i); this.maxTileOffset.x = Math.max( @@ -385,7 +398,7 @@ export default class TMXLayer extends Renderable { /** * How this layer is rendered. Resolved by `onActivateEvent` to one of: - * - `"shader"` — WebGL2 procedural shader path (single quad per tileset, fragment GID lookup) + * - `"shader"` — GPU procedural shader path, on WebGL 2 and WebGPU, for orthogonal and isometric maps (single quad per tileset, fragment GID lookup) * - `"prerender"`— offscreen-canvas bake at activation, blitted as one drawImage per frame * - `"perTile"` — per-frame loop, one drawImage per visible tile * @@ -722,7 +735,7 @@ export default class TMXLayer extends Renderable { // support oversized tiles (tile dim > cell dim) drawn bottom-aligned. // Loop bound is MAX_OVERFLOW + 1; anything beyond would silently clip, // so refuse the shader path for layers with extreme oversampling. - const MAX_OVERFLOW_CELLS = 4; + const MAX_OVERFLOW_CELLS = ORTHOGONAL_MAX_OVERFLOW; for (const ts of this.tilesets.tilesets) { if (ts.isCollection) { return { ok: false, reason: "collection-of-image tileset" }; @@ -795,11 +808,22 @@ export default class TMXLayer extends Renderable { * @internal */ _checkIsometricShaderEligibility() { - /** - * the one tileset an isometric layer on the shader path draws with - * @ignore - */ - this._shaderTileset = undefined; + this._shaderTileset = null; + // Half the tiles of a grid of odd size stand half a pixel off. Tile + // by tile, each is moved to a whole pixel; the shader has one quad + // for all of them, and would draw those a pixel away. + if (this.tilewidth % 2 !== 0 || this.tileheight % 2 !== 0) { + return { ok: false, reason: "an isometric grid of odd size" }; + } + // Tiles overlap, and the shader draws the layer flat, then blends it + // as one picture: where tile by tile each tile is blended over the + // ones behind it, which shows with every mode but the normal one. + if (this.blendMode !== "normal") { + return { + ok: false, + reason: `blend mode "${this.blendMode}" on an isometric layer`, + }; + } const data = this.layerData; let tileset = null; for (let i = 0; i < data.length; i += 2) { @@ -811,17 +835,17 @@ export default class TMXLayer extends Renderable { tileset = this.tilesets.getTilesetByGid(gid); } } - // an empty layer: its tileset is the first one a tile is set from if (tileset !== null) { const reason = this._isometricTilesetReason(tileset); if (reason !== undefined) { return { ok: false, reason }; } - this._shaderTileset = tileset; } if (this._hasDiagonalFlipNotSquare()) { return { ok: false, reason: DIAGONAL_NOT_SQUARE }; } + // an empty layer: its tileset is the first one a tile is set from + this._shaderTileset = tileset ?? undefined; return { ok: true }; } @@ -869,7 +893,7 @@ export default class TMXLayer extends Renderable { this.orientation === "isometric" && this._shaderTileset?.contains(gid) !== true ) { - if (this._shaderTileset !== undefined) { + if (this._shaderTileset) { reason = ISOMETRIC_ONE_TILESET; } else { // an empty layer takes its tileset from its first tile @@ -891,6 +915,7 @@ export default class TMXLayer extends Renderable { `melonJS: layer "${this.name}" back to the legacy tile renderer (${reason})`, ); this.renderMode = "perTile"; + this._shaderTileset = null; } } diff --git a/packages/melonjs/src/physics/world.js b/packages/melonjs/src/physics/world.js index a16706e07b..d26e8eb103 100644 --- a/packages/melonjs/src/physics/world.js +++ b/packages/melonjs/src/physics/world.js @@ -215,17 +215,20 @@ export default class World extends Container { this.preRender = false; /** - * Enable the WebGL2 procedural shader path for orthogonal tile - * layers. When `true` (default), eligible layers render via a - * single quad per tileset + a fragment shader doing per-pixel GID - * lookup — bypassing the per-tile drawImage loop entirely. - * Supported features on the shader path: animated tiles, all - * three flip bits (H/V/AD), per-layer opacity/tint/blend mode, - * and oversized bottom-aligned tiles up to 4 cells of overflow. - * Layers that don't qualify (Canvas/WebGL1, non-orthogonal, - * collection-of-image tilesets, non-zero `tileoffset`, or tile - * overflow beyond the shader's 4-cell limit) fall back to the - * legacy path automatically. Set to `false` to disable globally. + * Enable the GPU procedural shader path (WebGL 2 and WebGPU) for + * orthogonal and isometric tile layers. When `true` (default), + * eligible layers render via a single quad per tileset + a + * fragment shader doing per-pixel GID lookup — bypassing the + * per-tile drawImage loop entirely. + * Supported features on the shader path: animated tiles, flipped + * tiles, per-layer opacity and tint, and tiles larger than the + * grid: up to 4 cells of overflow on an orthogonal map, up to 4 + * grid widths by 8 grid heights on an isometric one. + * Layers that don't qualify (Canvas, a staggered, hexagonal or + * oblique map, collection-of-image tilesets, larger tiles; on an + * orthogonal map a non-zero `tileoffset`, on an isometric one + * tiles of several tilesets in one layer) fall back to the legacy + * path automatically. Set to `false` to disable globally. * @type {boolean} * @default true */ diff --git a/packages/melonjs/src/video/gpu/tmxlayer_region.js b/packages/melonjs/src/video/gpu/tmxlayer_orientation.js similarity index 59% rename from packages/melonjs/src/video/gpu/tmxlayer_region.js rename to packages/melonjs/src/video/gpu/tmxlayer_orientation.js index 34ffe5318b..13b08fdbf1 100644 --- a/packages/melonjs/src/video/gpu/tmxlayer_region.js +++ b/packages/melonjs/src/video/gpu/tmxlayer_orientation.js @@ -1,11 +1,19 @@ /** - * The part of a tile layer a view shows, as the GPU tile layer renderers - * need it: the same on WebGL and WebGPU, so both read it from here. + * What the GPU tile layer renderers need to know of a map orientation, the + * same on WebGL and WebGPU: each backend brings the shader text, and reads + * the rest from here ({@link TMX_ORIENTATIONS}). * - * A region is the quad to draw (`x`, `y`, `width`, `height`, in the layer's - * pixels), the shader's `uVisibleStart` and `uVisibleSize` (the quad's - * corner and size, in the units the orientation's fragment shader works in) - * and its `uOverflow`. + * - `region`: the part of a layer a view shows, for the tiles of one + * tileset. A region is the quad to draw (`x`, `y`, `width`, `height`, in + * the layer's pixels), the shader's `uVisibleStart` and `uVisibleSize` + * (the quad's corner and size, in the units the orientation's fragment + * shader works in) and its `uOverflow`. + * - `tilesetsOf`: the tilesets a layer is drawn with, one pass each. + * - `draws`: whether a layer can be drawn at all (its `renderMode` can be + * set to "shader" by hand, after it was found unfit for it). + * - `limits`: the bounds of the loops of the orientation's fragment shader, + * declared in it as constants of the same names, and what a layer is + * checked against before it takes the GPU path. * @import { default as TMXLayer } from "../../level/tiled/TMXLayer.js"; */ @@ -154,3 +162,96 @@ export function isometricRegion(layer, rect, tileset, out) { out.overflowY = Math.ceil(tileset.tileheight / halfHeight); return true; } + +/** + * How many tiles a tileset's image has across: counted from the margin, a + * tile plus the spacing apart, the last column going without its outer + * margin, as Tiled and the tileset's own atlas count them. + * @param {object} tileset - the tileset + * @returns {number} the number of columns + * @ignore + * @internal + */ +export function atlasColumns(tileset) { + return Math.max( + 1, + Math.floor( + (tileset.image.width - tileset.margin + tileset.spacing) / + (tileset.tilewidth + tileset.spacing), + ), + ); +} + +/** + * How many tiles a tileset's image has down (see {@link atlasColumns}). + * @param {object} tileset - the tileset + * @returns {number} the number of rows + * @ignore + * @internal + */ +export function atlasRows(tileset) { + return Math.max( + 1, + Math.floor( + (tileset.image.height - tileset.margin + tileset.spacing) / + (tileset.tileheight + tileset.spacing), + ), + ); +} + +/** + * The map orientations the GPU tile layer renderers draw. + * @ignore + * @internal + */ +export const TMX_ORIENTATIONS = { + orthogonal: { + region: orthogonalRegion, + /** + * every tileset of the map: a pass shows the tiles of its own + * @param {TMXLayer} layer - the layer + * @returns {object[]} the tilesets + */ + tilesetsOf(layer) { + return layer.tilesets.tilesets; + }, + draws() { + return true; + }, + // how many cells a tile can reach past its own, across and up + limits: { MAX_OVERFLOW: 4 }, + }, + isometric: { + region: isometricRegion, + /** + * The one tileset an isometric layer draws with: its tiles overlap, + * and a pass keeps the order of the tiles of one tileset, not across + * two (`layer._shaderTileset`, found by the layer when it is checked + * for this path). + * @param {TMXLayer} layer - the layer + * @param {object[]} scratch - an array to fill + * @returns {object[]} that tileset, or none for an empty layer + */ + tilesetsOf(layer, scratch) { + scratch.length = 0; + if (layer._shaderTileset) { + scratch.push(layer._shaderTileset); + } + return scratch; + }, + /** + * not a layer with no tileset to draw with: one that was never + * checked, or whose tiles come from several (`null`; an empty layer + * has `undefined`, and draws nothing) + * @param {TMXLayer} layer - the layer + * @returns {boolean} whether the layer can be drawn + */ + draws(layer) { + return layer._shaderTileset !== null; + }, + // the cells whose image can reach a pixel: rows of them, half a + // grid height apart, and in each, cells a grid width apart. So + // tiles up to MAX_ROWS / 2 grid heights tall, MAX_COLS wide + limits: { MAX_ROWS: 16, MAX_COLS: 4 }, + }, +}; diff --git a/packages/melonjs/src/video/webgl/renderers/tmxlayer/isometric.js b/packages/melonjs/src/video/webgl/renderers/tmxlayer/isometric.js deleted file mode 100644 index 3483c8e8ce..0000000000 --- a/packages/melonjs/src/video/webgl/renderers/tmxlayer/isometric.js +++ /dev/null @@ -1,54 +0,0 @@ -import { isometricRegion } from "../../../gpu/tmxlayer_region.js"; -import isometricTMXLayerFragment from "../../shaders/isometric-tmxlayer.frag"; -import TMXLayerGPURenderer from "./tmxlayer.js"; - -/** - * additional imports for TypeScript - * @import { default as TMXLayer } from "../../../../level/tiled/TMXLayer.js"; - * @import { default as WebGLRenderer } from "../../webgl_renderer.js"; - */ - -/** - * GPU-accelerated renderer for isometric TMX tile layers (WebGL2): the - * shared {@link TMXLayerGPURenderer}, with an isometric grid's geometry. - * The quad covers the view, and the shader works in pixels. - * - * Isometric tiles overlap, and the shader keeps the one drawn last among - * those of ONE tileset: a layer takes this path only when all its tiles - * come from the same one (`layer._shaderTileset`, set by the layer when it - * is found eligible). - * @ignore - * @internal - */ -export default class IsometricTMXLayerGPURenderer extends TMXLayerGPURenderer { - /** - * @param {WebGLRenderer} renderer - the WebGL renderer instance - */ - constructor(renderer) { - super(renderer, isometricTMXLayerFragment); - this._tilesets = []; - } - - /** - * @ignore - * @internal - */ - _region(layer, rect, tileset, out) { - return isometricRegion(layer, rect, tileset, out); - } - - /** - * The one tileset an isometric layer on this path draws with. - * @param {TMXLayer} layer - the layer - * @returns {object[]} that tileset, or none for an empty layer - * @ignore - * @internal - */ - _tilesetsOf(layer) { - this._tilesets.length = 0; - if (layer._shaderTileset) { - this._tilesets.push(layer._shaderTileset); - } - return this._tilesets; - } -} diff --git a/packages/melonjs/src/video/webgl/renderers/tmxlayer/orthogonal.js b/packages/melonjs/src/video/webgl/renderers/tmxlayer/orthogonal.js deleted file mode 100644 index f6e6f6fd74..0000000000 --- a/packages/melonjs/src/video/webgl/renderers/tmxlayer/orthogonal.js +++ /dev/null @@ -1,33 +0,0 @@ -import { orthogonalRegion } from "../../../gpu/tmxlayer_region.js"; -import orthogonalTMXLayerFragment from "../../shaders/orthogonal-tmxlayer.frag"; -import TMXLayerGPURenderer from "./tmxlayer.js"; - -/** - * additional imports for TypeScript - * @import { default as TMXLayer } from "../../../../level/tiled/TMXLayer.js"; - * @import { default as WebGLRenderer } from "../../webgl_renderer.js"; - */ - -/** - * GPU-accelerated renderer for orthogonal TMX tile layers (WebGL2): the - * shared {@link TMXLayerGPURenderer}, with an orthogonal grid's geometry. - * The quad covers the cells in view, and the shader works in cells. - * @ignore - * @internal - */ -export default class OrthogonalTMXLayerGPURenderer extends TMXLayerGPURenderer { - /** - * @param {WebGLRenderer} renderer - the WebGL renderer instance - */ - constructor(renderer) { - super(renderer, orthogonalTMXLayerFragment); - } - - /** - * @ignore - * @internal - */ - _region(layer, rect, tileset, out) { - return orthogonalRegion(layer, rect, tileset, out); - } -} diff --git a/packages/melonjs/src/video/webgl/renderers/tmxlayer/tmxlayer.js b/packages/melonjs/src/video/webgl/renderers/tmxlayer/tmxlayer.js index b740e8591f..0ad3e0fbf0 100644 --- a/packages/melonjs/src/video/webgl/renderers/tmxlayer/tmxlayer.js +++ b/packages/melonjs/src/video/webgl/renderers/tmxlayer/tmxlayer.js @@ -1,6 +1,13 @@ -import { createRegion } from "../../../gpu/tmxlayer_region.js"; +import { + atlasColumns, + atlasRows, + createRegion, + TMX_ORIENTATIONS, +} from "../../../gpu/tmxlayer_orientation.js"; import { BufferTextureResource } from "../../../texture/resource.js"; import GLShader from "../../glshader.js"; +import isometricFragment from "../../shaders/isometric-tmxlayer.frag"; +import orthogonalFragment from "../../shaders/orthogonal-tmxlayer.frag"; import tmxLayerVertex from "../../shaders/tmxlayer.vert"; import tmxLayerCommonFragment from "../../shaders/tmxlayer-common.frag"; @@ -10,6 +17,12 @@ import tmxLayerCommonFragment from "../../shaders/tmxlayer-common.frag"; * @import { default as WebGLRenderer } from "../../webgl_renderer.js"; */ +// each orientation's part of the fragment shader, appended to the shared one +const FRAGMENTS = { + orthogonal: orthogonalFragment, + isometric: isometricFragment, +}; + // Default `uTint` when the layer has no tint set. Module-scoped so the // fallback path doesn't allocate per frame. const DEFAULT_TINT = new Float32Array([1, 1, 1, 1]); @@ -24,10 +37,10 @@ const DEFAULT_TINT = new Float32Array([1, 1, 1, 1]); * and re-uploaded only when `layer.dataVersion` changes (mutations from * `setTile`/`clearTile`). * - * An orientation's renderer extends this one with the two things that - * differ: its part of the fragment shader (where a cell's tile is, and which - * cells can cover a fragment), and {@link TMXLayerGPURenderer#_region}, the - * part of the layer a view shows. + * There is one of these per map orientation drawn (see `TMX_ORIENTATIONS` + * in `gpu/tmxlayer_orientation.js`). Two things differ from one to the next: + * its part of the fragment shader (where a cell's tile is, and which cells + * can cover a fragment), and the part of a layer a view shows. * * The per-layer index texture is a `BufferTextureResource` flowing * through the standard `TextureCache` / batcher path — same lane as @@ -48,11 +61,23 @@ const DEFAULT_TINT = new Float32Array([1, 1, 1, 1]); export default class TMXLayerGPURenderer { /** * @param {WebGLRenderer} renderer - the WebGL renderer instance - * @param {string} fragment - the orientation's part of the fragment - * shader, appended to the shared one + * @param {string} orientation - the map orientation to draw, one of + * `TMX_ORIENTATIONS` */ - constructor(renderer, fragment) { + constructor(renderer, orientation) { this.renderer = renderer; + /** + * what differs with the map's orientation + * @ignore + * @internal + */ + this.orientation = TMX_ORIENTATIONS[orientation]; + // the bounds of the shader's loops, declared ahead of its own part + const limits = Object.entries(this.orientation.limits) + .map(([name, value]) => { + return `const int ${name} = ${value};`; + }) + .join("\n"); this.gl = renderer.gl; // Standalone `GLShader` (not `ShaderEffect`) so we own both the // vertex and the fragment source — they're paired in GLSL ES 3.00, @@ -66,7 +91,7 @@ export default class TMXLayerGPURenderer { this.shader = new GLShader( renderer.gl, tmxLayerVertex, - `${tmxLayerCommonFragment}\n${fragment}`, + `${tmxLayerCommonFragment}\n${limits}\n${FRAGMENTS[orientation]}`, renderer.shaderPrecision, ); // per-layer `BufferTextureResource`. `Map` (not `WeakMap`) so we @@ -98,43 +123,19 @@ export default class TMXLayerGPURenderer { */ this._indexUnit = 0; /** - * what {@link TMXLayerGPURenderer#_region} fills: the quad to draw, + * what the orientation's `region` fills: the quad to draw, * in the layer's pixels, and what the shader needs to find a * fragment's place in the layer from its place in the quad * @ignore * @internal */ this._regionScratch = createRegion(); - } - - /** - * The part of a layer a view shows, for the tiles of one tileset: the - * quad to draw (`x`, `y`, `width`, `height`, in the layer's pixels), the - * shader's `uVisibleStart` and `uVisibleSize` (the quad's corner and - * size, in the units its fragment shader works in) and `uOverflow`. - * @param {TMXLayer} layer - the layer - * @param {object} rect - the visible viewport rect (world coords) - * @param {object} tileset - the tileset whose tiles are drawn - * @param {object} out - the region to fill - * @returns {boolean} false when the view shows nothing of the layer - * @abstract - * @ignore - * @internal - */ - // eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars - _region(layer, rect, tileset, out) { - return false; - } - - /** - * The tilesets a layer is drawn with, one pass each. - * @param {TMXLayer} layer - the layer - * @returns {object[]} the tilesets - * @ignore - * @internal - */ - _tilesetsOf(layer) { - return layer.tilesets.tilesets; + /** + * what the orientation's `tilesetsOf` fills, when it needs to + * @ignore + * @internal + */ + this._tilesetsScratch = []; } /** @@ -374,7 +375,7 @@ export default class TMXLayerGPURenderer { // one pass per tileset — the shader's uGidRange uniform + discard // hides cells outside the active tileset's GID range - const tilesets = this._tilesetsOf(layer); + const tilesets = this.orientation.tilesetsOf(layer, this._tilesetsScratch); for (let i = 0; i < tilesets.length; i++) { const tileset = tilesets[i]; // skip collection-of-image tilesets (caller's eligibility check @@ -384,7 +385,7 @@ export default class TMXLayerGPURenderer { continue; } // the view shows nothing of this tileset's tiles - if (!this._region(layer, rect, tileset, region)) { + if (!this.orientation.region(layer, rect, tileset, region)) { continue; } batcher ??= this._begin(layer); @@ -399,19 +400,13 @@ export default class TMXLayerGPURenderer { // atlas geometry (margin + spacing) — number of tiles per row/col // in the atlas image - const atlasCols = Math.max( - 1, - Math.floor((atlasW - margin * 2 + spacing) / (tsW + spacing)), - ); - const atlasRows = Math.max( - 1, - Math.floor((atlasH - margin * 2 + spacing) / (tsH + spacing)), - ); + const atlasCols = atlasColumns(tileset); + const atlasRowCount = atlasRows(tileset); // per-tileset animation lookup. Only allocated/bound for // tilesets that actually have animated tiles — static tilesets // pay neither a texture-unit nor an upload. - const tileCount = atlasCols * atlasRows; + const tileCount = atlasCols * atlasRowCount; const animEntry = this._getOrUpdateAnimLookup(tileset, tileCount); if (animEntry !== undefined) { const animUnit = batcher.uploadTexture( @@ -434,7 +429,7 @@ export default class TMXLayerGPURenderer { } this._setV2("uTileSize", tsW, tsH); - this._setV2("uTilesetCols", atlasCols, atlasRows); + this._setV2("uTilesetCols", atlasCols, atlasRowCount); this._setV2("uInvTilesetSize", 1 / atlasW, 1 / atlasH); this._setV4("uTilesetMargin", margin, margin, spacing, spacing); this._setV2("uVisibleStart", region.startX, region.startY); diff --git a/packages/melonjs/src/video/webgl/shaders/isometric-tmxlayer.frag b/packages/melonjs/src/video/webgl/shaders/isometric-tmxlayer.frag index befc47ff0c..ea42ad41a0 100644 --- a/packages/melonjs/src/video/webgl/shaders/isometric-tmxlayer.frag +++ b/packages/melonjs/src/video/webgl/shaders/isometric-tmxlayer.frag @@ -21,10 +21,9 @@ // opaque texel ends it at once, and the soft edge of a tile shows the tiles // behind it. -// bounds of the two runs: tiles up to 8 grid heights tall and 4 grid widths -// wide (the layer falls back to the CPU renderer past them) -const int MAX_ROWS = 16; -const int MAX_COLS = 4; +// `MAX_ROWS` and `MAX_COLS`, the bounds of the two runs, are declared ahead +// of this file (see `TMX_ORIENTATIONS` in `gpu/tmxlayer_orientation.js`): a +// layer of larger tiles is left to the CPU renderer. // the left of the cell's diamond, and as far above its bottom as the tile // is tall @@ -37,8 +36,7 @@ vec2 tileOrigin(int cx, int cy) { void main(void) { vec2 worldPx = uVisibleStart + vRegion * uVisibleSize; - vec2 dWorldX = dFdx(worldPx); - vec2 dWorldY = dFdy(worldPx); + Footprint fp = footprint(worldPx); vec2 halfCell = uCellSize * 0.5; // the rightmost and the lowest cell whose image can reach the fragment @@ -50,14 +48,17 @@ void main(void) { vec4 result = vec4(0.0); vec4 texel; bool opaque = false; - for (int j = 0; j < MAX_ROWS; j++) { - if (j >= spanS) break; + // rows past the map's last one, or before its first, hold no cell: most + // of a fragment's run, along the bottom of the map and above its top + int sLast = int(uMapSize.x) + int(uMapSize.y) - 2; + for (int j = max(sMax - sLast, 0); j < MAX_ROWS; j++) { int s = sMax - j; + if (j >= spanS || s < 0) break; // the first u of this row's parity, then every other one int u = uMax - ((uMax + s) & 1); for (int i = 0; i < MAX_COLS; i++) { if (uMax - u >= spanU) break; - if (tryRenderCell((s + u) / 2, (s - u) / 2, worldPx, dWorldX, dWorldY, texel)) { + if (tryRenderCell((s + u) / 2, (s - u) / 2, worldPx, fp, texel)) { if (under(result, texel)) { opaque = true; break; diff --git a/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag b/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag index 3e29c4a3ad..ef36adad72 100644 --- a/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag +++ b/packages/melonjs/src/video/webgl/shaders/orthogonal-tmxlayer.frag @@ -15,7 +15,9 @@ // ones before it until the fragment is opaque. `uOverflow` is how many // cells a tile reaches past its own, across and up. -const int MAX_OVERFLOW = 4; +// `MAX_OVERFLOW`, the bound of both loops, is declared ahead of this file +// (see `TMX_ORIENTATIONS` in `gpu/tmxlayer_orientation.js`): a layer whose +// tiles reach further is left to the CPU renderer. // a tile is bottom-aligned on its cell, and left-aligned vec2 tileOrigin(int cx, int cy) { @@ -29,8 +31,7 @@ void main(void) { vec2 tileCoord = uVisibleStart + vRegion * uVisibleSize; vec2 geomCell = floor(tileCoord); vec2 worldPx = tileCoord * uCellSize; - vec2 dWorldX = dFdx(worldPx); - vec2 dWorldY = dFdy(worldPx); + Footprint fp = footprint(worldPx); int gx = int(geomCell.x); int gy = int(geomCell.y); @@ -42,7 +43,7 @@ void main(void) { // Fast path: tiles fit the cell exactly (the common case) — only the // geometric cell can contain this fragment's tile. if (overflowX == 0 && overflowY == 0) { - if (!tryRenderCell(gx, gy, worldPx, dWorldX, dWorldY, result)) { + if (!tryRenderCell(gx, gy, worldPx, fp, result)) { discard; } fragColor = layerColor(result); @@ -59,7 +60,7 @@ void main(void) { if (dy > overflowY) continue; for (int dx = 0; dx <= MAX_OVERFLOW; dx++) { if (dx > overflowX) break; - if (tryRenderCell(gx - dx, gy + dy, worldPx, dWorldX, dWorldY, texel)) { + if (tryRenderCell(gx - dx, gy + dy, worldPx, fp, texel)) { if (under(result, texel)) { opaque = true; break; diff --git a/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag b/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag index 7e91ff18f1..79f5bc8f6e 100644 --- a/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag +++ b/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag @@ -1,4 +1,11 @@ #version 300 es +// Full precision, whatever the application asked for its other shaders +// (`highPrecisionShader`): a fragment's position is in pixels of the map, +// and a gid has 16 bits, which the 16 bits a phone can give a `mediump` +// value do not hold past a small map. The engine's `setPrecision` step +// leaves a shader that declares its own alone. +precision highp float; +precision highp int; // Fragment shader for the TMX layer GPU renderers (WebGL2 / GLSL ES 3.00): // the part every map orientation shares. An orientation's own file follows @@ -38,12 +45,6 @@ // it bypasses interpolation, so the integer byte values come out // unmolested even on a normalized-float sampler. -// The engine's `setPrecision` step injects precision declarations for -// float and int after the `#version` line, using whatever precision the -// renderer was configured with (`highPrecisionShader` setting on the -// Application). Individual shader files don't hardcode precision so the -// engine-wide preference applies. - in vec2 vRegion; in vec4 vColor; @@ -72,24 +73,55 @@ out vec4 fragColor; // image, in the same pixels as `worldPx`. Defined per orientation. vec2 tileOrigin(int cx, int cy); +// What sampling the atlas needs of a fragment, the same for every cell +// tried for it: taken once, in `main`, where control flow is still uniform. +// +// `gradX` / `gradY` are the atlas coordinate's screen derivatives. The atlas +// coordinate itself jumps from one tile to the next, so its own derivatives +// would pick a tiny mip level along every tile edge; a texel of the atlas is +// a world pixel, so the world's derivatives are the atlas's. +// +// `inset` keeps a sample half a texel of the level it reads inside its tile, +// so filtering never blends in the next tile of the tileset, or the spacing +// around it, along the tile's edge: a seam between two tiles (never past the +// tile's centre). Without mipmaps that level is the full-size art; under +// them, the deepest level read is the next one up once the level is past a +// whole number, by more than a rounding error: drawn at its size, the +// derivatives are 1 give or take a few ulps, and level 0 it is. Sampled +// nearest, the sample stays in the same texel +struct Footprint { + vec2 gradX; + vec2 gradY; + vec2 inset; +}; + +Footprint footprint(vec2 worldPx) { + vec2 dWorldX = dFdx(worldPx); + vec2 dWorldY = dFdy(worldPx); + float lod = ceil(log2(max(max(length(dWorldX), length(dWorldY)), 1.0)) - 0.01); + vec2 inset = min(vec2(0.5 * exp2(lod * uMipmaps)), uTileSize * 0.5); + return Footprint(dWorldX * uInvTilesetSize, dWorldY * uInvTilesetSize, inset); +} + // Try to render the tile at cell (cx, cy) for the current fragment. // Returns true and writes the sampled color to `outColor` when the cell // contains a visible, in-range tile whose pixel covers this fragment. // Identical logic for both fast and slow paths — GLSL inlines this // trivially so there's no function-call overhead at runtime. -// -// `dWorldX` / `dWorldY` are the world position's screen derivatives, taken -// in `main` where control flow is still uniform: the atlas coordinate jumps -// from one tile to the next, so its own derivatives would pick a tiny mip -// level along every tile edge. A texel of the atlas is a world pixel, so -// the world's derivatives are the atlas's. -bool tryRenderCell(int cx, int cy, vec2 worldPx, vec2 dWorldX, vec2 dWorldY, out vec4 outColor) { +bool tryRenderCell(int cx, int cy, vec2 worldPx, Footprint fp, out vec4 outColor) { int mapW = int(uMapSize.x); int mapH = int(uMapSize.y); if (cx < 0 || cx >= mapW || cy < 0 || cy >= mapH) { return false; } + // the cell's tile does not reach the fragment: known before anything + // is fetched + vec2 inTile = (worldPx - tileOrigin(cx, cy)) / uTileSize; + if (inTile.x < 0.0 || inTile.x >= 1.0 || inTile.y < 0.0 || inTile.y >= 1.0) { + return false; + } + // `texelFetch` skips filtering — the 8-bit channel values come back // as normalized floats, decoded to byte ints below vec4 cellF = texelFetch(uTileIndex, ivec2(cx, cy), 0); @@ -100,11 +132,6 @@ bool tryRenderCell(int cx, int cy, vec2 worldPx, vec2 dWorldX, vec2 dWorldY, out return false; } - vec2 inTile = (worldPx - tileOrigin(cx, cy)) / uTileSize; - if (inTile.x < 0.0 || inTile.x >= 1.0 || inTile.y < 0.0 || inTile.y >= 1.0) { - return false; - } - // flip mask + axis-swap trick: see TMX shader-path flip spec. // AD performs a transpose; with AD set, H and V swap their effective // axes (matches the legacy `buildFlipTransform`). @@ -129,30 +156,16 @@ bool tryRenderCell(int cx, int cy, vec2 worldPx, vec2 dWorldX, vec2 dWorldY, out localId = int(animTexel.r) | (int(animTexel.g) << 8); } - float row = floor(float(localId) / uTilesetCols.x); - float col = float(localId) - row * uTilesetCols.x; + // the tile's place in the atlas, in whole numbers: a float division + // may land a hair under the row it should + int cols = int(uTilesetCols.x); + int row = localId / cols; vec2 tileOriginPx = uTilesetMargin.xy - + vec2(col, row) * (uTileSize + uTilesetMargin.zw); + + vec2(float(localId - row * cols), float(row)) * (uTileSize + uTilesetMargin.zw); vec2 texelPx = tileOriginPx + inTile * uTileSize; - // keep the sample half a texel of the level it reads inside the tile, - // so filtering never blends in the next tile of the tileset, or the - // spacing around it, along the tile's edge: a seam between two tiles - // (never past the tile's centre). Without mipmaps that level is the - // full-size art; under them, the deepest level read is the next one up - // once the level is past a whole number, by more than a rounding error: - // drawn at its size, the derivatives are 1 give or take a few ulps, and - // level 0 it is. Sampled nearest, the sample stays in the same texel - float lod = ceil(log2(max(max(length(dWorldX), length(dWorldY)), 1.0)) - 0.01); - vec2 inset = min(vec2(0.5 * exp2(lod * uMipmaps)), uTileSize * 0.5); - texelPx = clamp(texelPx, tileOriginPx + inset, tileOriginPx + uTileSize - inset); - vec2 texelUV = texelPx * uInvTilesetSize; - - vec4 sampled = textureGrad( - uSampler, - texelUV, - dWorldX * uInvTilesetSize, - dWorldY * uInvTilesetSize - ); + texelPx = clamp(texelPx, tileOriginPx + fp.inset, tileOriginPx + uTileSize - fp.inset); + + vec4 sampled = textureGrad(uSampler, texelPx * uInvTilesetSize, fp.gradX, fp.gradY); if (sampled.a <= 0.0) { return false; } @@ -161,8 +174,8 @@ bool tryRenderCell(int cx, int cy, vec2 worldPx, vec2 dWorldX, vec2 dWorldY, out } // A fragment is opaque from this alpha on: nothing behind it would change a -// channel by a whole step of 8 bits -const float OPAQUE = 0.996; +// channel by half a step of 8 bits +const float OPAQUE = 0.998; // Put a texel behind what a fragment has so far, both premultiplied: what // drawing the tiles back to front, each over the last, comes to, taken from diff --git a/packages/melonjs/src/video/webgl/webgl_renderer.js b/packages/melonjs/src/video/webgl/webgl_renderer.js index 173d773aff..1ffd68dcfd 100644 --- a/packages/melonjs/src/video/webgl/webgl_renderer.js +++ b/packages/melonjs/src/video/webgl/webgl_renderer.js @@ -24,6 +24,7 @@ import { import AmbientLightEffect from "../effects/ambientlight.js"; import BlendEffect from "../effects/blendEffect.js"; import RadialGradientEffect from "../effects/radialGradient.js"; +import { TMX_ORIENTATIONS } from "../gpu/tmxlayer_orientation.js"; import { Gradient } from "../gradient.js"; import Renderer from "./../renderer.js"; import RenderTargetPool from "../rendertarget/render_target_pool.js"; @@ -46,8 +47,7 @@ import PrimitiveBatcher from "./batchers/primitive_batcher"; import QuadBatcher from "./batchers/quad_batcher"; import { markContextLost } from "./buffer/deletable.js"; import { createLightUniformScratch, packLights } from "./lighting/pack.ts"; -import IsometricTMXLayerGPURenderer from "./renderers/tmxlayer/isometric.js"; -import OrthogonalTMXLayerGPURenderer from "./renderers/tmxlayer/orthogonal.js"; +import TMXLayerGPURenderer from "./renderers/tmxlayer/tmxlayer.js"; import { WebGLTextureStore } from "./texture/store.js"; import { resolveMaxTextures } from "./utils/maxtextures.js"; import { getMaxShaderPrecision } from "./utils/precision.js"; @@ -72,15 +72,6 @@ import { GLSamplerCache } from "./utils/samplercache.js"; */ // reusable constants for 2D→3D matrix operations -/** - * the GPU tile layer renderer of each map orientation that has one - * @ignore - */ -const TMX_GPU_RENDERERS = { - orthogonal: OrthogonalTMXLayerGPURenderer, - isometric: IsometricTMXLayerGPURenderer, -}; - const _tempMatrix = new Matrix3d(); // pre-allocated matrices for blitEffect (avoids per-frame allocation) @@ -1458,7 +1449,9 @@ export default class WebGLRenderer extends Renderer { drawTileLayer(layer, rect) { if (layer.renderMode === "shader") { const gpu = this._getTMXGPURendererFor(layer.orientation); - if (gpu !== undefined) { + // a layer asked for this path after it was found unfit for it + // is still drawn, tile by tile + if (gpu !== undefined && gpu.orientation.draws(layer)) { gpu.draw(layer, rect); return; } @@ -1499,8 +1492,7 @@ export default class WebGLRenderer extends Renderer { * @internal */ _getTMXGPURendererFor(orientation) { - const Class = TMX_GPU_RENDERERS[orientation]; - if (Class === undefined) { + if (TMX_ORIENTATIONS[orientation] === undefined) { // staggered, hexagonal, oblique: drawn by the legacy renderer return undefined; } @@ -1508,7 +1500,7 @@ export default class WebGLRenderer extends Renderer { let gpu = this._tmxGPURenderers.get(orientation); if (gpu === undefined) { try { - gpu = new Class(this); + gpu = new TMXLayerGPURenderer(this, orientation); } catch (err) { // shader compile / link failure on this driver — disable // the GPU path permanently for the rest of the session diff --git a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/isometric.js b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/isometric.js deleted file mode 100644 index 232d8676f6..0000000000 --- a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/isometric.js +++ /dev/null @@ -1,54 +0,0 @@ -import { isometricRegion } from "../../../gpu/tmxlayer_region.js"; -import isometricWGSL from "../../shaders/tmxlayer-isometric.wgsl"; -import TMXLayerGPURenderer from "./tmxlayer.js"; - -/** - * additional imports for TypeScript - * @import { default as TMXLayer } from "../../../../level/tiled/TMXLayer.js"; - * @import { default as WebGPURenderer } from "../../webgpu_renderer.js"; - */ - -/** - * GPU-accelerated renderer for isometric TMX tile layers on the WebGPU - * backend: the shared {@link TMXLayerGPURenderer}, with an isometric grid's - * geometry. The quad covers the view, and the shader works in pixels. - * - * Isometric tiles overlap, and the shader keeps the one drawn last among - * those of ONE tileset: a layer takes this path only when all its tiles - * come from the same one (`layer._shaderTileset`, set by the layer when it - * is found eligible). - * @ignore - * @internal - */ -export default class IsometricTMXLayerGPURenderer extends TMXLayerGPURenderer { - /** - * @param {WebGPURenderer} renderer - the WebGPU renderer instance - */ - constructor(renderer) { - super(renderer, isometricWGSL); - this.tilesets = []; - } - - /** - * @ignore - * @internal - */ - region(layer, rect, tileset, out) { - return isometricRegion(layer, rect, tileset, out); - } - - /** - * The one tileset an isometric layer on this path draws with. - * @param {TMXLayer} layer - the layer - * @returns {object[]} that tileset, or none for an empty layer - * @ignore - * @internal - */ - tilesetsOf(layer) { - this.tilesets.length = 0; - if (layer._shaderTileset) { - this.tilesets.push(layer._shaderTileset); - } - return this.tilesets; - } -} diff --git a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/orthogonal.js b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/orthogonal.js deleted file mode 100644 index 3131648d73..0000000000 --- a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/orthogonal.js +++ /dev/null @@ -1,33 +0,0 @@ -import { orthogonalRegion } from "../../../gpu/tmxlayer_region.js"; -import orthogonalWGSL from "../../shaders/tmxlayer-orthogonal.wgsl"; -import TMXLayerGPURenderer from "./tmxlayer.js"; - -/** - * additional imports for TypeScript - * @import { default as WebGPURenderer } from "../../webgpu_renderer.js"; - */ - -/** - * GPU-accelerated renderer for orthogonal TMX tile layers on the WebGPU - * backend: the shared {@link TMXLayerGPURenderer}, with an orthogonal grid's - * geometry. The quad covers the cells in view, and the shader works in - * cells. - * @ignore - * @internal - */ -export default class OrthogonalTMXLayerGPURenderer extends TMXLayerGPURenderer { - /** - * @param {WebGPURenderer} renderer - the WebGPU renderer instance - */ - constructor(renderer) { - super(renderer, orthogonalWGSL); - } - - /** - * @ignore - * @internal - */ - region(layer, rect, tileset, out) { - return orthogonalRegion(layer, rect, tileset, out); - } -} diff --git a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js index 9ca29e901a..a0ccbd8eaf 100644 --- a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js +++ b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js @@ -1,7 +1,14 @@ import VertexArrayBuffer from "../../../buffer/vertex.js"; import { transformQuadCorners } from "../../../gpu/quadcorners.ts"; -import { createRegion } from "../../../gpu/tmxlayer_region.js"; +import { + atlasColumns, + atlasRows, + createRegion, + TMX_ORIENTATIONS, +} from "../../../gpu/tmxlayer_orientation.js"; import tmxLayerCommonWGSL from "../../shaders/tmxlayer-common.wgsl"; +import isometricWGSL from "../../shaders/tmxlayer-isometric.wgsl"; +import orthogonalWGSL from "../../shaders/tmxlayer-orthogonal.wgsl"; /** * additional imports for TypeScript @@ -19,6 +26,12 @@ import tmxLayerCommonWGSL from "../../shaders/tmxlayer-common.wgsl"; */ const TMX_UNIFORM_SIZE = 128; +// each orientation's part of the shader module, appended to the shared one +const ORIENTATION_WGSL = { + orthogonal: orthogonalWGSL, + isometric: isometricWGSL, +}; + /** * GPU-accelerated renderer for TMX tile layers on the WebGPU backend, the * part every map orientation shares — the WGSL realization of the WebGL @@ -49,11 +62,25 @@ const TMX_UNIFORM_SIZE = 128; export default class TMXLayerGPURenderer { /** * @param {WebGPURenderer} renderer - the WebGPU renderer instance - * @param {string} wgsl - the orientation's part of the shader module, - * appended to the shared one + * @param {string} orientation - the map orientation to draw, one of + * `TMX_ORIENTATIONS` */ - constructor(renderer, wgsl) { + constructor(renderer, orientation) { this.renderer = renderer; + /** + * what differs with the map's orientation (see + * `gpu/tmxlayer_orientation.js`) + * @ignore + * @internal + */ + this.orientation = TMX_ORIENTATIONS[orientation]; + // the bounds of the shader's loops, declared ahead of its own part + const limits = Object.entries(this.orientation.limits) + .map(([name, value]) => { + return `const ${name} : i32 = ${value};`; + }) + .join("\n"); + const wgsl = `${limits}\n${ORIENTATION_WGSL[orientation]}`; const cache = renderer.pipelineCache; // group-3 shape: the uniform block + the two lookup textures @@ -109,40 +136,17 @@ export default class TMXLayerGPURenderer { */ this.vertexScratch = undefined; /** - * what {@link TMXLayerGPURenderer#region} fills + * what the orientation's `region` fills * @ignore * @internal */ this.regionScratch = createRegion(); - } - - /** - * The part of a layer a view shows, for the tiles of one tileset (see - * `gpu/tmxlayer_region.js`). The quad it gives must be the same for - * every tileset of a layer: it is written once, for the first one. - * @param {TMXLayer} layer - the layer - * @param {object} rect - the visible viewport rect (world coords) - * @param {object} tileset - the tileset whose tiles are drawn - * @param {object} out - the region to fill - * @returns {boolean} false when the view shows nothing of the layer - * @abstract - * @ignore - * @internal - */ - // eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars - region(layer, rect, tileset, out) { - return false; - } - - /** - * The tilesets a layer is drawn with, one pass each. - * @param {TMXLayer} layer - the layer - * @returns {object[]} the tilesets - * @ignore - * @internal - */ - tilesetsOf(layer) { - return layer.tilesets.tilesets; + /** + * what the orientation's `tilesetsOf` fills, when it needs to + * @ignore + * @internal + */ + this.tilesetsScratch = []; } /** @@ -395,7 +399,7 @@ export default class TMXLayerGPURenderer { let begun; // one pass per tileset — the GID-range guard hides other tilesets - const tilesets = this.tilesetsOf(layer); + const tilesets = this.orientation.tilesetsOf(layer, this.tilesetsScratch); for (let i = 0; i < tilesets.length; i++) { const tileset = tilesets[i]; // eligibility should have excluded these — guard defensively @@ -403,7 +407,7 @@ export default class TMXLayerGPURenderer { continue; } // the view shows nothing of this tileset's tiles - if (!this.region(layer, rect, tileset, region)) { + if (!this.orientation.region(layer, rect, tileset, region)) { continue; } begun ??= this.begin(layer, region); @@ -415,18 +419,12 @@ export default class TMXLayerGPURenderer { const spacing = tileset.spacing; const atlasW = tileset.image.width; const atlasH = tileset.image.height; - const atlasCols = Math.max( - 1, - Math.floor((atlasW - margin * 2 + spacing) / (tsW + spacing)), - ); - const atlasRows = Math.max( - 1, - Math.floor((atlasH - margin * 2 + spacing) / (tsH + spacing)), - ); + const atlasCols = atlasColumns(tileset); + const atlasRowCount = atlasRows(tileset); const animEntry = this.getOrUpdateAnimLookup( tileset, - atlasCols * atlasRows, + atlasCols * atlasRowCount, ); // every declared binding needs a view — reuse the index texture // as the filler (never read: uAnimSize stays 0) @@ -438,7 +436,7 @@ export default class TMXLayerGPURenderer { scratch[6] = region.overflowX; scratch[7] = region.overflowY; scratch[8] = atlasCols; - scratch[9] = atlasRows; + scratch[9] = atlasRowCount; scratch[10] = 1 / atlasW; scratch[11] = 1 / atlasH; scratch[12] = margin; diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl index ee5758c0c4..5cc80843d8 100644 --- a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl @@ -77,21 +77,35 @@ struct CellSample { color : vec4f, } +// What sampling the atlas needs of a fragment, the same for every cell +// tried for it: taken once, in `fragment_main`, where control flow is still +// uniform (see `footprint` in tmxlayer-common.frag). +struct Footprint { + gradX : vec2f, + gradY : vec2f, + inset : vec2f, +} + +fn footprint(worldPx : vec2f) -> Footprint { + let dWorldX = dpdx(worldPx); + let dWorldY = dpdy(worldPx); + // keep the sample half a texel of the level it reads inside the tile, + // so filtering never blends in the next tile of the tileset, or the + // spacing around it, along the tile's edge: a seam between two tiles + // (never past the tile's centre). Without mipmaps that level is the + // full-size art; under them, the deepest level read is the next one up + // once the level is past a whole number, by more than a rounding error: + // drawn at its size, the derivatives are 1 give or take a few ulps, and + // level 0 it is. Sampled nearest, the sample stays in the same texel + let lod = ceil(log2(max(max(length(dWorldX), length(dWorldY)), 1.0)) - 0.01); + let inset = min(vec2f(0.5 * exp2(lod * tmx.uMipmaps)), tmx.uTileSize * 0.5); + return Footprint(dWorldX * tmx.uInvTilesetSize, dWorldY * tmx.uInvTilesetSize, inset); +} + // Try to render the tile at cell (cx, cy) for the current fragment — // identical logic to the GLSL tryRenderCell, returning a hit/color pair // (WGSL has no out parameters). -// `dWorldX` / `dWorldY`: the world position's screen derivatives, taken in -// `fragment_main` while control flow is still uniform. The atlas coordinate -// jumps from one tile to the next, so its own derivatives would pick a tiny -// mip level along every tile edge; a texel of the atlas is a world pixel, -// so the world's derivatives are the atlas's. -fn tryRenderCell( - cx : i32, - cy : i32, - worldPx : vec2f, - dWorldX : vec2f, - dWorldY : vec2f, -) -> CellSample { +fn tryRenderCell(cx : i32, cy : i32, worldPx : vec2f, fp : Footprint) -> CellSample { var miss : CellSample; miss.hit = false; miss.color = vec4f(0.0); @@ -102,6 +116,13 @@ fn tryRenderCell( return miss; } + // the cell's tile does not reach the fragment (where it is drawn from + // is the orientation's own): known before anything is fetched + var inTile = (worldPx - tileOrigin(cx, cy)) / tmx.uTileSize; + if (inTile.x < 0.0 || inTile.x >= 1.0 || inTile.y < 0.0 || inTile.y >= 1.0) { + return miss; + } + // byte-exact read: rgba8unorm texel back to byte ints let cellF = textureLoad(uTileIndex, vec2i(cx, cy), 0); let cell = vec4u(cellF * 255.0 + 0.5); @@ -111,12 +132,6 @@ fn tryRenderCell( return miss; } - // where the cell's tile is drawn from: the orientation's own - var inTile = (worldPx - tileOrigin(cx, cy)) / tmx.uTileSize; - if (inTile.x < 0.0 || inTile.x >= 1.0 || inTile.y < 0.0 || inTile.y >= 1.0) { - return miss; - } - // flip mask + axis-swap trick: AD transposes; with AD set, H and V // swap their effective axes (matches the legacy buildFlipTransform) let flipMask = i32(cell.b); @@ -139,32 +154,23 @@ fn tryRenderCell( localId = i32(animTexel.r) | (i32(animTexel.g) << 8u); } - let row = floor(f32(localId) / tmx.uTilesetCols.x); - let col = f32(localId) - row * tmx.uTilesetCols.x; + // the tile's place in the atlas, in whole numbers: a float division + // may land a hair under the row it should + let cols = i32(tmx.uTilesetCols.x); + let row = localId / cols; let tileOriginPx = tmx.uTilesetMargin.xy - + vec2f(col, row) * (tmx.uTileSize + tmx.uTilesetMargin.zw); + + vec2f(f32(localId - row * cols), f32(row)) * (tmx.uTileSize + tmx.uTilesetMargin.zw); var texelPx = tileOriginPx + inTile * tmx.uTileSize; - // keep the sample half a texel of the level it reads inside the tile, - // so filtering never blends in the next tile of the tileset, or the - // spacing around it, along the tile's edge: a seam between two tiles - // (never past the tile's centre). Without mipmaps that level is the - // full-size art; under them, the deepest level read is the next one up - // once the level is past a whole number, by more than a rounding error: - // drawn at its size, the derivatives are 1 give or take a few ulps, and - // level 0 it is. Sampled nearest, the sample stays in the same texel - let lod = ceil(log2(max(max(length(dWorldX), length(dWorldY)), 1.0)) - 0.01); - let inset = min(vec2f(0.5 * exp2(lod * tmx.uMipmaps)), tmx.uTileSize * 0.5); - texelPx = clamp(texelPx, tileOriginPx + inset, tileOriginPx + tmx.uTileSize - inset); - let texelUV = texelPx * tmx.uInvTilesetSize; + texelPx = clamp(texelPx, tileOriginPx + fp.inset, tileOriginPx + tmx.uTileSize - fp.inset); // explicit gradients, so the sample is legal here (non-uniform control // flow); a sampler without mipmaps clamps to level 0, as before let sampled = textureSampleGrad( uTexture, uSampler, - texelUV, - dWorldX * tmx.uInvTilesetSize, - dWorldY * tmx.uInvTilesetSize, + texelPx * tmx.uInvTilesetSize, + fp.gradX, + fp.gradY, ); if (sampled.a <= 0.0) { return miss; @@ -176,8 +182,8 @@ fn tryRenderCell( } // A fragment is opaque from this alpha on: nothing behind it would change a -// channel by a whole step of 8 bits -const OPAQUE : f32 = 0.996; +// channel by half a step of 8 bits +const OPAQUE : f32 = 0.998; // Put a texel behind what a fragment has so far, both premultiplied: what // drawing the tiles back to front, each over the last, comes to, taken from diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-isometric.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-isometric.wgsl index 2e3b35ed7b..e948a3263e 100644 --- a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-isometric.wgsl +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-isometric.wgsl @@ -9,10 +9,8 @@ // the CPU renderer draws in, each texel put behind the ones before it until // the fragment is opaque. -// bounds of the two runs: tiles up to 8 grid heights tall and 4 grid widths -// wide (the layer falls back to the CPU renderer past them) -const MAX_ROWS : i32 = 16; -const MAX_COLS : i32 = 4; +// `MAX_ROWS` and `MAX_COLS`, the bounds of the two runs, are declared ahead +// of this file (see `TMX_ORIENTATIONS` in gpu/tmxlayer_orientation.js). // the left of the cell's diamond, and as far above its bottom as the tile // is tall @@ -26,8 +24,7 @@ fn tileOrigin(cx : i32, cy : i32) -> vec2f { @fragment fn fragment_main(in : VSOut) -> @location(0) vec4f { let worldPx = tmx.uVisibleStart + in.vRegion * tmx.uVisibleSize; - let dWorldX = dpdx(worldPx); - let dWorldY = dpdy(worldPx); + let fp = footprint(worldPx); let halfCell = tmx.uCellSize * 0.5; // the rightmost and the lowest cell whose image can reach the fragment @@ -38,18 +35,21 @@ fn fragment_main(in : VSOut) -> @location(0) vec4f { var result = vec4f(0.0); var opaque = false; - for (var j = 0; j < MAX_ROWS; j++) { - if (j >= spanS) { + // rows past the map's last one, or before its first, hold no cell: most + // of a fragment's run, along the bottom of the map and above its top + let sLast = i32(tmx.uMapSize.x) + i32(tmx.uMapSize.y) - 2; + for (var j = max(sMax - sLast, 0); j < MAX_ROWS; j++) { + let s = sMax - j; + if (j >= spanS || s < 0) { break; } - let s = sMax - j; // the first u of this row's parity, then every other one var u = uMax - ((uMax + s) & 1); for (var i = 0; i < MAX_COLS; i++) { if (uMax - u >= spanU) { break; } - let candidate = tryRenderCell((s + u) / 2, (s - u) / 2, worldPx, dWorldX, dWorldY); + let candidate = tryRenderCell((s + u) / 2, (s - u) / 2, worldPx, fp); if (candidate.hit && under(&result, candidate.color)) { opaque = true; break; diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl index 1585827ba8..d2ac310b81 100644 --- a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl @@ -3,7 +3,8 @@ // // `uOverflow` is how many cells a tile reaches past its own, across and up. -const MAX_OVERFLOW : i32 = 4; +// `MAX_OVERFLOW`, the bound of both loops, is declared ahead of this file +// (see `TMX_ORIENTATIONS` in gpu/tmxlayer_orientation.js). // a tile is bottom-aligned on its cell, and left-aligned fn tileOrigin(cx : i32, cy : i32) -> vec2f { @@ -18,8 +19,7 @@ fn fragment_main(in : VSOut) -> @location(0) vec4f { let tileCoord = tmx.uVisibleStart + in.vRegion * tmx.uVisibleSize; let geomCell = floor(tileCoord); let worldPx = tileCoord * tmx.uCellSize; - let dWorldX = dpdx(worldPx); - let dWorldY = dpdy(worldPx); + let fp = footprint(worldPx); let gx = i32(geomCell.x); let gy = i32(geomCell.y); @@ -29,7 +29,7 @@ fn fragment_main(in : VSOut) -> @location(0) vec4f { // fast path: tiles fit the cell exactly — only the geometric cell can // contain this fragment's tile if (overflowX == 0 && overflowY == 0) { - let only = tryRenderCell(gx, gy, worldPx, dWorldX, dWorldY); + let only = tryRenderCell(gx, gy, worldPx, fp); if (!only.hit) { discard; } @@ -50,7 +50,7 @@ fn fragment_main(in : VSOut) -> @location(0) vec4f { if (dx > overflowX) { break; } - let candidate = tryRenderCell(gx - dx, gy + dy, worldPx, dWorldX, dWorldY); + let candidate = tryRenderCell(gx - dx, gy + dy, worldPx, fp); if (candidate.hit && under(&result, candidate.color)) { opaque = true; break; diff --git a/packages/melonjs/src/video/webgpu/webgpu_renderer.js b/packages/melonjs/src/video/webgpu/webgpu_renderer.js index e6c92eaffb..dfadff3173 100644 --- a/packages/melonjs/src/video/webgpu/webgpu_renderer.js +++ b/packages/melonjs/src/video/webgpu/webgpu_renderer.js @@ -25,6 +25,7 @@ import BlendEffect from "../effects/blendEffect.js"; import { meshHostingBlocker } from "../effects/mesh_splice.js"; import RadialGradientEffect from "../effects/radialGradient.js"; import { frameClampMode } from "../gpu/frame.js"; +import { TMX_ORIENTATIONS } from "../gpu/tmxlayer_orientation.js"; import { Gradient } from "../gradient.js"; import Renderer from "../renderer.js"; import RenderTargetPool from "../rendertarget/render_target_pool.js"; @@ -54,8 +55,7 @@ import WebGPUBufferArena from "./buffer/arena.js"; import WebGPUUniformRing from "./buffer/uniformring.js"; import { buildEffectGPU } from "./effect_binding.js"; import WebGPUPipelineCache, { DEPTH_STENCIL_FORMAT } from "./pipeline/cache.js"; -import IsometricTMXLayerGPURenderer from "./renderers/tmxlayer/isometric.js"; -import OrthogonalTMXLayerGPURenderer from "./renderers/tmxlayer/orthogonal.js"; +import TMXLayerGPURenderer from "./renderers/tmxlayer/tmxlayer.js"; import { COMPRESSION_FEATURES } from "./texture/compressed.js"; import { WebGPUFrameTexture } from "./texture/frametexture.js"; import WebGPUTextureStore from "./texture/store.js"; @@ -105,15 +105,6 @@ export function resolveDepthOps(meshDepthActive, pendingDepthClear) { }; } -/** - * the GPU tile layer renderer of each map orientation that has one - * @ignore - */ -const TMX_GPU_RENDERERS = { - orthogonal: OrthogonalTMXLayerGPURenderer, - isometric: IsometricTMXLayerGPURenderer, -}; - /** * The **experimental** WebGPU renderer. * @@ -2519,13 +2510,17 @@ export default class WebGPURenderer extends Renderer { * @override */ drawTileLayer(layer, rect) { - const Class = TMX_GPU_RENDERERS[layer.orientation]; - if (layer.renderMode === "shader" && Class !== undefined) { + // a layer asked for this path after it was found unfit for it is + // still drawn, tile by tile + if ( + layer.renderMode === "shader" && + TMX_ORIENTATIONS[layer.orientation]?.draws(layer) === true + ) { // device-scoped, lazily (re)built: a device loss rebuilds the // pipeline cache, detected via the epoch stamp let tmx = this.tmxRenderers.get(layer.orientation); if (tmx === undefined || tmx.epoch !== this.pipelineCache.epoch) { - tmx = new Class(this); + tmx = new TMXLayerGPURenderer(this, layer.orientation); this.tmxRenderers.set(layer.orientation, tmx); } tmx.draw(layer, rect); diff --git a/packages/melonjs/tests/helpers/tile-layer-compare.js b/packages/melonjs/tests/helpers/tile-layer-compare.js index 3b3021ffb9..4275e82c83 100644 --- a/packages/melonjs/tests/helpers/tile-layer-compare.js +++ b/packages/melonjs/tests/helpers/tile-layer-compare.js @@ -61,6 +61,9 @@ export function inBlock(x, y, w, h, tw, th) { * @param {(x: number, y: number, id: number) => number} [options.alpha] * how opaque each pixel is, 0 to 255, for soft edges and see-through tiles * (in place of `opaque`) + * @param {boolean} [options.outerMargin=true] - false for an image that + * stops at its last column and row, with no margin after them, as Tiled + * lets one * @param {object} [options.extra] - more Tiled tileset properties * (`tileoffset`, `tiles` with animations...) * @returns {object} the tileset's Tiled JSON, without its `firstgid` @@ -75,12 +78,14 @@ export function makeTileset(options) { spacing = 0, opaque, alpha, + outerMargin = true, extra = {}, } = options; const rows = Math.ceil(count / columns); const canvas = document.createElement("canvas"); - canvas.width = margin * 2 + columns * tw + (columns - 1) * spacing; - canvas.height = margin * 2 + rows * th + (rows - 1) * spacing; + const outer = outerMargin ? margin : 0; + canvas.width = margin + outer + columns * tw + (columns - 1) * spacing; + canvas.height = margin + outer + rows * th + (rows - 1) * spacing; const ctx = canvas.getContext("2d"); const image = ctx.createImageData(canvas.width, canvas.height); const seed = nextName * 37; diff --git a/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js b/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js index 6c9bb42d2a..0bf2c405bf 100644 --- a/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js +++ b/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js @@ -531,6 +531,21 @@ export function isometricScenarios(renderer, type) { expect(await same(scene)).toBeGreaterThan(0); }); + it("from a tileset whose image has no margin after its last tiles", async (ctx) => { + need(ctx); + // as Tiled lets one be: the tiles are counted from the first + // margin, not as if there were two + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [ + blocks(64, 64, { margin: 7, spacing: 2, outerMargin: false }), + ], + layers: [{ data: scattered(8, 6) }], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + for (const [x, y] of [ [0, 16], [-12, 0], @@ -1095,6 +1110,76 @@ export function isometricScenarios(renderer, type) { expect(said).toMatch(/not square, flipped diagonally/); }); + it("a grid of odd size, whose tiles stand half a pixel off", async (ctx) => { + need(ctx); + for (const [tw, th] of [ + [33, 17], + [64, 31], + [63, 32], + ]) { + const scene = { + orientation: "isometric", + cols: 9, + rows: 7, + tw, + th, + tilesets: [makeTileset({ tw, th: th * 2 })], + layers: [{ data: scattered(9, 7) }], + }; + const [mode, said] = modeOf(scene); + expect(mode, `${tw} × ${th}`).toBe("perTile"); + expect(said).toMatch(/isometric grid of odd size/); + } + }); + + it("a layer blended another way than the normal one", (ctx) => { + need(ctx); + for (const mode of ["add", "multiply", "screen"]) { + const [took, said] = modeOf( + iso({ + cols: 4, + rows: 4, + tilesets: [blocks(64, 128)], + layers: [{ data: scattered(4, 4), mode }], + }), + ); + expect(took, mode).toBe("perTile"); + expect(said).toMatch(/blend mode/); + } + }); + + it("and one asked for by hand afterwards is still drawn, tile by tile", async (ctx) => { + need(ctx); + // two tilesets in one layer: the layer has no tileset to + // draw with on the GPU + const scene = iso({ + cols: 6, + rows: 6, + tilesets: [blocks(64, 32), blocks(64, 96)], + layers: [ + { + data: fill(6, 6, (c, r) => { + return 1 + ((c + r) % 16); + }), + }, + ], + }); + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + let layers; + try { + layers = show(app, makeMap(scene)); + } finally { + warn.mockRestore(); + } + expect(layers[0].renderMode).toBe("perTile"); + app.viewport.moveTo(0, 0); + const forced = await drawIn(app, layers, "shader"); + expect(forced.cpuDraws).toBe(1); + const d = diff(reference(scene, 0, 0, WIDTH, HEIGHT), forced.frame); + expect(d.different, d.first).toBe(0); + expect(drawn(forced.frame)).toBeGreaterThan(0); + }); + it("a staggered map, still", (ctx) => { need(ctx); const [mode, said] = modeOf({ @@ -1192,6 +1277,25 @@ export function isometricScenarios(renderer, type) { }); } + it("a tileset whose image has no margin after its last tiles", async (ctx) => { + need(ctx); + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [ + makeTileset({ + tw: 32, + th: 32, + margin: 5, + spacing: 3, + outerMargin: false, + }), + ], + layers: [{ data: scattered(10, 8) }], + }); + expect(await same(scene, { views: ORTHO_VIEWS })).toBeGreaterThan(0); + }); + it("layers on tilesets of several sizes", async (ctx) => { need(ctx); const scene = ortho({ diff --git a/packages/melonjs/tests/sprite-frame-clamp.spec.js b/packages/melonjs/tests/sprite-frame-clamp.spec.js index 49ad1afbd8..653912a0c0 100644 --- a/packages/melonjs/tests/sprite-frame-clamp.spec.js +++ b/packages/melonjs/tests/sprite-frame-clamp.spec.js @@ -44,7 +44,7 @@ import WebGPULitQuadBatcher from "../src/video/webgpu/batchers/lit_quad_batcher. import WebGPUQuadBatcher from "../src/video/webgpu/batchers/quad_batcher.js"; import WebGPUUniformRing from "../src/video/webgpu/buffer/uniformring.js"; import { FRAME_UNIFORM_SIZE } from "../src/video/webgpu/pipeline/bindgroups.js"; -import WebGPUTMXRenderer from "../src/video/webgpu/renderers/tmxlayer/orthogonal.js"; +import WebGPUTMXRenderer from "../src/video/webgpu/renderers/tmxlayer/tmxlayer.js"; import quadWGSL from "../src/video/webgpu/shaders/quad.wgsl"; import quadLitWGSL from "../src/video/webgpu/shaders/quad-lit.wgsl"; import WebGPURenderer from "../src/video/webgpu/webgpu_renderer.js"; @@ -1191,7 +1191,7 @@ describe("sprites keep to their frame (WebGPU, mock renderer)", () => { renderer.setBatcher = () => { return batcher; }; - const tmx = new WebGPUTMXRenderer(renderer); + const tmx = new WebGPUTMXRenderer(renderer, "orthogonal"); tmx.draw( { cols: 4, diff --git a/packages/melonjs/tests/texture-mipmap.spec.js b/packages/melonjs/tests/texture-mipmap.spec.js index 9d9a64dcc8..3998929914 100644 --- a/packages/melonjs/tests/texture-mipmap.spec.js +++ b/packages/melonjs/tests/texture-mipmap.spec.js @@ -22,7 +22,7 @@ import Renderer from "../src/video/renderer.js"; import { createAtlas, TextureAtlas } from "../src/video/texture/atlas.js"; import WebGPULitQuadBatcher from "../src/video/webgpu/batchers/lit_quad_batcher.js"; import WebGPUQuadBatcher from "../src/video/webgpu/batchers/quad_batcher.js"; -import WebGPUTMXRenderer from "../src/video/webgpu/renderers/tmxlayer/orthogonal.js"; +import WebGPUTMXRenderer from "../src/video/webgpu/renderers/tmxlayer/tmxlayer.js"; import tmxlayerCommonWGSL from "../src/video/webgpu/shaders/tmxlayer-common.wgsl"; import tmxlayerOrthogonalWGSL from "../src/video/webgpu/shaders/tmxlayer-orthogonal.wgsl"; import { @@ -556,7 +556,7 @@ describe("linear-mipmap texture filter (WebGPU, mock renderer)", () => { renderer.setBatcher = () => { return batcher; }; - const tmx = new WebGPUTMXRenderer(renderer); + const tmx = new WebGPUTMXRenderer(renderer, "orthogonal"); tmx.draw(layer(atlas()), { pos: { x: 0, y: 0 }, width: 64, @@ -600,7 +600,7 @@ describe("linear-mipmap texture filter (WebGPU, mock renderer)", () => { "let inset = min(vec2f(0.5 * exp2(lod * tmx.uMipmaps)), tmx.uTileSize * 0.5);", ); expect(code).toContain( - "texelPx = clamp(texelPx, tileOriginPx + inset, tileOriginPx + tmx.uTileSize - inset);", + "texelPx = clamp(texelPx, tileOriginPx + fp.inset, tileOriginPx + tmx.uTileSize - fp.inset);", ); }); }); diff --git a/packages/melonjs/tests/tmxlayer-shader.spec.js b/packages/melonjs/tests/tmxlayer-shader.spec.js index d1e3b61818..d12529101b 100644 --- a/packages/melonjs/tests/tmxlayer-shader.spec.js +++ b/packages/melonjs/tests/tmxlayer-shader.spec.js @@ -1,7 +1,7 @@ import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { boot } from "../src/index.js"; import { BufferTextureResource } from "../src/video/texture/resource.js"; -import OrthogonalTMXLayerGPURenderer from "../src/video/webgl/renderers/tmxlayer/orthogonal.js"; +import TMXLayerGPURenderer from "../src/video/webgl/renderers/tmxlayer/tmxlayer.js"; import { getWebGLRenderer, releaseWebGLRenderer, @@ -44,7 +44,7 @@ describe("TMXLayer shader path", () => { // `_getTMXGPURendererFor` is the lazy factory on WebGLRenderer const r1 = renderer._getTMXGPURendererFor("orthogonal"); const r2 = renderer._getTMXGPURendererFor("orthogonal"); - expect(r1).toBeInstanceOf(OrthogonalTMXLayerGPURenderer); + expect(r1).toBeInstanceOf(TMXLayerGPURenderer); expect(r2).toBe(r1); }); diff --git a/packages/melonjs/tests/tmxlayer_bench.spec.js b/packages/melonjs/tests/tmxlayer_bench.spec.js new file mode 100644 index 0000000000..bd8f38f9ec --- /dev/null +++ b/packages/melonjs/tests/tmxlayer_bench.spec.js @@ -0,0 +1,201 @@ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { video } from "../src/index.js"; +import TMXTileset from "../src/level/tiled/TMXTileset.js"; +import Renderer from "../src/video/renderer.js"; +import { + fill, + inBlock, + makeApp, + makeMap, + makeTileset, + show, +} from "./helpers/tile-layer-compare.js"; + +/** + * Benchmark of the two ways a tile layer is drawn on WebGL: tile by tile + * (`renderMode` "perTile", the CPU renderer: a quad per tile in view, each + * found and placed in JavaScript every frame) against the GPU path + * ("shader": one quad per layer, the tiles found by the fragment shader), + * on a map of medium size, orthogonal and isometric (#1897). + * + * Read the times as **CPU-side** cost, the main thread's share of a frame: + * the run happens under a software rasterizer, whose own time is no measure + * of a graphics card's, and is left out (the frames are timed up to the + * flush, and the rasterizer is let catch up between runs). What the GPU + * path takes off the main thread is the walk over the cells in view and a + * quad for each: its cost does not grow with the number of tiles shown, + * which is what the wider views show. + * + * The canvas is a quarter of the view's size each way, and the camera + * zoomed out to match: as many tiles are in view, and walked, as in the + * view named, with a sixteenth of the pixels for the software rasterizer. + */ + +const SCALE = 4; +// the views measured: what a camera shows, in pixels of the map +const VIEWS = [ + [1280, 720], + [2560, 1440], + [3840, 2160], +]; +const SIZE = 100; // cells a side +const FRAMES = 20; +const RUNS = 3; + +/** most cells filled, a few holes, as a drawn map has */ +const cells = () => { + return fill(SIZE, SIZE, (c, r) => { + return (c * 7 + r * 3) % 23 === 0 ? 0 : 1 + ((c * 5 + r * 11) % 8); + }); +}; + +const MAPS = [ + { + label: "orthogonal, 32 × 32", + scene: () => { + return { + orientation: "orthogonal", + cols: SIZE, + rows: SIZE, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [{ data: cells() }], + }; + }, + // the middle of the map + center: [(SIZE * 32) / 2, (SIZE * 32) / 2], + }, + { + label: "isometric, 64 × 64 on a 64 × 32 grid", + scene: () => { + return { + orientation: "isometric", + cols: SIZE, + rows: SIZE, + tw: 64, + th: 32, + tilesets: [ + makeTileset({ + tw: 64, + th: 64, + opaque: (x, y) => { + return inBlock(x, y, 64, 64, 64, 32); + }, + }), + ], + layers: [{ data: cells() }], + }; + }, + center: [(SIZE * 64) / 2, (SIZE * 32) / 2], + }, +]; + +describe("tile layer benchmark: tile by tile against the GPU path", () => { + let app; + beforeAll(async () => { + app = await makeApp( + video.WEBGL, + "WebGL", + VIEWS[0][0] / SCALE, + VIEWS[0][1] / SCALE, + ); + }); + afterAll(() => { + app?.destroy(); + vi.restoreAllMocks(); + }); + + it(`${SIZE} × ${SIZE} maps, three sizes of view`, (ctx) => { + if (app === null || app === undefined) { + // a bare return reports as a PASS, so a benchmark that never + // ran would look identical to one that did + ctx.skip("WebGL not available: nothing to measure"); + return; + } + const gl = app.renderer.gl; + const pixel = new Uint8Array(4); + const frame = () => { + app.world.update(0); + app.viewport.update(0); + app.repaint(); + app.draw(); + app.renderer.flush(); + }; + // let the rasterizer finish what it was sent, outside the timings + const settle = () => { + gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel); + }; + /** the median time of a frame, in ms */ + const time = () => { + for (let i = 0; i < 5; i++) { + frame(); + } + settle(); + const runs = []; + for (let run = 0; run < RUNS; run++) { + const start = performance.now(); + for (let i = 0; i < FRAMES; i++) { + frame(); + } + runs.push((performance.now() - start) / FRAMES); + settle(); + } + runs.sort((a, b) => { + return a - b; + }); + return runs[Math.floor(RUNS / 2)]; + }; + + const rows = []; + for (const { label, scene, center } of MAPS) { + const layers = show(app, makeMap(scene())); + expect(layers[0].renderMode).toBe("shader"); + for (const [width, height] of VIEWS) { + app.viewport.zoom = VIEWS[0][0] / SCALE / width; + app.viewport.moveTo(center[0] - width / 2, center[1] - height / 2); + + layers[0].renderMode = "perTile"; + const tiles = vi.spyOn(TMXTileset.prototype, "drawTileRaw"); + frame(); + const inView = tiles.mock.calls.length; + tiles.mockRestore(); + const perTile = time(); + + layers[0].renderMode = "shader"; + const cpu = vi.spyOn(Renderer.prototype, "drawTileLayer"); + frame(); + // the GPU path draws alone + expect(cpu).not.toHaveBeenCalled(); + cpu.mockRestore(); + const shader = time(); + + expect(inView).toBeGreaterThan(0); + rows.push({ + label, + view: `${width} × ${height}`, + inView, + perTile, + shader, + }); + } + app.viewport.zoom = 1; + } + + const line = "═".repeat(86); + console.log(`╔${line}╗`); + console.log( + `║ ${`TILE LAYER BENCHMARK — ${SIZE} × ${SIZE} map, CPU ms per frame`.padEnd(85)}║`, + ); + console.log(`╠${line}╣`); + console.log( + `║ ${"map".padEnd(36)}${"view".padStart(13)}${"tiles".padStart(8)}${"perTile".padStart(10)}${"shader".padStart(9)}${"ratio".padStart(8)} ║`, + ); + for (const row of rows) { + console.log( + `║ ${row.label.padEnd(36)}${row.view.padStart(13)}${String(row.inView).padStart(8)}${row.perTile.toFixed(3).padStart(10)}${row.shader.toFixed(3).padStart(9)}${`${(row.perTile / row.shader).toFixed(1)}×`.padStart(8)} ║`, + ); + } + console.log(`╚${line}╝`); + }); +}); diff --git a/packages/melonjs/tests/webgpu_tmxlayer.spec.js b/packages/melonjs/tests/webgpu_tmxlayer.spec.js index f5ebdb8117..bb48fc2765 100644 --- a/packages/melonjs/tests/webgpu_tmxlayer.spec.js +++ b/packages/melonjs/tests/webgpu_tmxlayer.spec.js @@ -1,7 +1,7 @@ import "./helpers/webgpu-globals.js"; import { beforeEach, describe, expect, it } from "vitest"; import WebGPUQuadBatcher from "../src/video/webgpu/batchers/quad_batcher.js"; -import OrthogonalTMXLayerGPURenderer from "../src/video/webgpu/renderers/tmxlayer/orthogonal.js"; +import TMXLayerGPURenderer from "../src/video/webgpu/renderers/tmxlayer/tmxlayer.js"; import { createMockWebGPURenderer } from "./helpers/webgpu-mock-renderer.js"; /** @@ -58,13 +58,13 @@ describe("WebGPU orthogonal TMX layer renderer (mock)", () => { renderer.currentBatcher ??= new WebGPUQuadBatcher(renderer); return renderer.currentBatcher; }; - tmx = new OrthogonalTMXLayerGPURenderer(renderer); + tmx = new TMXLayerGPURenderer(renderer, "orthogonal"); }); it("registers the shader family once with the quad vertex layout", () => { expect(tmx.key).toMatch(/^effect:/); // a second instance (device-loss rebuild) reuses the module text - const again = new OrthogonalTMXLayerGPURenderer(renderer); + const again = new TMXLayerGPURenderer(renderer, "orthogonal"); expect(again.key).toBe(tmx.key); }); @@ -185,3 +185,97 @@ describe("WebGPU orthogonal TMX layer renderer (mock)", () => { expect(tmx.animLookups.size).toBe(0); }); }); + +/** + * The isometric orientation on the same renderer (#1897): its own shader + * module, one pass with the layer's one tileset whatever the map has, and + * the two run lengths its shader walks in place of the orthogonal overflow. + * What it draws is compared with the CPU renderer, pixel by pixel, on a real + * device in `webgpu_tmxlayer_isometric.spec.js`. + */ +describe("WebGPU isometric TMX layer renderer (mock)", () => { + let renderer; + let tmx; + + const tileset = (firstgid, tilewidth, tileheight) => { + return { + isCollection: false, + image: { width: 256, height: 256 }, + tilewidth, + tileheight, + margin: 0, + spacing: 0, + tileoffset: { x: 0, y: 0 }, + firstgid, + lastgid: firstgid + 15, + isAnimated: false, + animations: new Map(), + texture: { name: `iso-atlas-${firstgid}` }, + }; + }; + const floor = tileset(1, 64, 32); + const walls = tileset(17, 64, 128); + + const layerOf = (shaderTileset) => { + return { + orientation: "isometric", + cols: 8, + rows: 8, + tilewidth: 64, + tileheight: 32, + dataVersion: 1, + layerData: new Uint16Array(8 * 8 * 2), + getOpacity() { + return 1; + }, + getRenderer() { + return { originX: 8 * 32 }; + }, + tint: null, + tilesets: { tilesets: [floor, walls] }, + _shaderTileset: shaderTileset, + }; + }; + + const RECT = { pos: { x: 100, y: 40 }, width: 320, height: 240 }; + + beforeEach(() => { + renderer = createMockWebGPURenderer(); + renderer.setBatcher = () => { + renderer.currentBatcher ??= new WebGPUQuadBatcher(renderer); + return renderer.currentBatcher; + }; + tmx = new TMXLayerGPURenderer(renderer, "isometric"); + }); + + it("has a shader module of its own", () => { + const orthogonal = new TMXLayerGPURenderer(renderer, "orthogonal"); + expect(tmx.key).toMatch(/^effect:/); + expect(tmx.key).not.toBe(orthogonal.key); + }); + + it("draws one quad, with the layer's tileset alone", () => { + tmx.draw(layerOf(walls), RECT); + expect(renderer.calls.drawIndexed).toEqual([6]); + // the tiles' size, and how many cells' images reach a pixel: as + // many across as the image is wide in half grid widths, as many + // down as it is tall in half grid heights + const uniforms = tmx.uniformScratch; + expect([uniforms[4], uniforms[5]]).toEqual([64, 128]); + expect([uniforms[6], uniforms[7]]).toEqual([2, 8]); + }); + + it("draws nothing for a layer with no tile yet", () => { + tmx.draw(layerOf(undefined), RECT); + expect(renderer.calls.drawIndexed).toEqual([]); + }); + + it("draws nothing where the view shows none of the layer", () => { + tmx.draw(layerOf(floor), { + pos: { x: 5000, y: 5000 }, + width: 320, + height: 240, + }); + expect(renderer.calls.drawIndexed).toEqual([]); + }); +}); From 3aa084e98d5cc5fbd71790cabefdf60264bc1b32 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 02:17:47 +0000 Subject: [PATCH 05/11] Tiled: tile layers follow their offset, and are in view as far as their tiles reach Two gaps the reference picture of the tile layer tests had to work around (#1897), on every renderer and render mode. A tile layer ignored its offset in Tiled (`offsetx`, `offsety`) and its own `pos`: it was always drawn at the map's origin, where image layers followed theirs. The offset now goes into `pos`, and the layer draws its tiles, and `getTile()` finds them, where `pos` says. A layer was skipped once its grid left the view, though tiles larger than a cell, or moved by a `tileoffset`, still reached in: the tops of tall tiles vanished above the map. The layer's bounds now cover how far its tiles reach; its `width` and `height` stay the grid's. Tests written first and seen to fail: both paths against the reference on WebGL and WebGPU (offset layers, several layers, an infinite map, a layer moved at run time, views that hold only the reach), and on the Canvas renderer tile by tile and baked, with `getTile`, the bounds, and staggered and hexagonal maps. The reference no longer leaves out a layer whose grid is out of view. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01JnzwA4a7ZyjLBpbeEpFLu6 --- packages/melonjs/CHANGELOG.md | 2 + .../melonjs/skills/melonjs-tilemaps/SKILL.md | 12 + packages/melonjs/src/level/tiled/TMXLayer.js | 84 ++++- .../tests/helpers/tile-layer-compare.js | 33 +- .../helpers/tile-layer-isometric-scenarios.js | 311 ++++++++++++++++ .../melonjs/tests/tmxlayer-offset.spec.js | 335 ++++++++++++++++++ 6 files changed, 743 insertions(+), 34 deletions(-) create mode 100644 packages/melonjs/tests/tmxlayer-offset.spec.js diff --git a/packages/melonjs/CHANGELOG.md b/packages/melonjs/CHANGELOG.md index 774ae5ab0b..3f2dcd8acf 100644 --- a/packages/melonjs/CHANGELOG.md +++ b/packages/melonjs/CHANGELOG.md @@ -18,6 +18,8 @@ - Tiled, GPU tile layers: a tileset with a `margin` whose image stops at its last tiles, with no margin after them, draws the right tiles; they were counted a column or a row short - Tiled, GPU tile layers: a layer with a tile that is not square, flipped diagonally, is drawn tile by tile; the shader drew such a tile wrong - Tiled, GPU tile layers: the shader runs at full precision whatever `highPrecisionShader` says; at a lower one a phone could misplace the tiles of a large map +- Tiled: a tile layer follows its offset in Tiled (`offsetx`, `offsety`), and its own `pos` when the game moves it: its tiles are drawn there and `getTile()` finds them there, on every renderer and map orientation. Both were ignored, and the layer drawn at the map's origin; only image layers followed their offset. A map whose tile layers have an offset now looks as it does in Tiled +- Tiled: tiles that reach past their layer's grid (taller or wider than a cell, or moved by a `tileoffset`) are drawn when they are in view and the grid is not; the whole layer was skipped, so the tops of tall tiles vanished once the map scrolled out of view. `getBounds()` of a tile layer covers that reach; its `width` and `height` are still the grid's - Tiled, isometric maps drawn tile by tile: tiles of a tileset larger than the map's first one, or moved by a `tileoffset`, went missing along the edges of the view - Tiled, orthogonal maps drawn tile by tile: tiles more than two cells tall went missing along the bottom of the view - `NineSliceSprite` blinks with `flicker()`, as a `Sprite` does; it ignored it diff --git a/packages/melonjs/skills/melonjs-tilemaps/SKILL.md b/packages/melonjs/skills/melonjs-tilemaps/SKILL.md index bb5933e1a6..0bd9874f8f 100644 --- a/packages/melonjs/skills/melonjs-tilemaps/SKILL.md +++ b/packages/melonjs/skills/melonjs-tilemaps/SKILL.md @@ -301,6 +301,15 @@ collision: solid ground comes from collision object layers (below), so a tile set or cleared changes what is drawn and what `cellAt` returns, not what a body runs into. Keep a layer you edit often off `preRender` (see the symptom table). +A tile layer is where its `pos` says: its offset in Tiled (Layer → Offset) +to start with, and wherever the game moves it afterwards. Its tiles are drawn +there, and `getTile(x, y)` answers for where they are drawn: + +```js +board.pos.y -= 8; // the whole layer, 8 pixels up +board.getTile(pointer.x, pointer.y); // still the tile under the pointer +``` + The map's own custom properties (Map → Map Properties in Tiled) are set on the map object, which `level.getCurrentLevel()` returns once it is loaded: a `theme` string property is `level.getCurrentLevel().theme`. They are set @@ -525,6 +534,9 @@ layer into the offscreen-bake path instead. | the level's `name` is not its id after loading | a map custom property named `name` replaced it: map properties are set over the map's own fields | | changing tiles at runtime is slow on a pre-rendered layer | outside an orthogonal map whose tiles fit their cells, a changed tile re-bakes the whole layer before it next draws: keep layers you edit often off `preRender` | | thin lines between tiles on the Canvas renderer, with smoothing on | filtering blends in the next tile of the tileset or its spacing, which a canvas cannot prevent: set `antiAlias: false`, or `preRender` the layer. WebGL and WebGPU keep each tile to itself | +| a tile layer given an offset in Tiled (a roof nudged up, a shadow shifted) is drawn where the others are | before 20.17 a tile layer ignored its offset, and its `pos`; only image layers followed theirs: update | +| after updating to 20.17 a tile layer sits away from where it did | it has an offset in Tiled that was ignored until then, and now moves it as in the editor: clear the layer's Offset in Tiled if it was not meant | +| the tops of tall tiles vanish when the map itself scrolls out of view | before 20.17 a layer was skipped once its grid left the view, though tiles larger than a cell (or moved by a `tileoffset`) still reached in: update | | tilemap slow on some machines | layer fell back off the shader path — check the console warning for the reason | | an isometric layer warns `tiles of more than one tileset in an isometric layer` and is slow | the shader path draws an isometric layer with one tileset: put the tiles of each tileset on a layer of their own, or pack them into one tileset | | a half-transparent isometric layer of tall tiles looks flatter than before 20.17 | on the shader path a layer fades as one picture, and a tile no longer shows through the one in front: set that layer's `renderMode` to `"perTile"` (a `renderMode` custom property in Tiled) for the old look | diff --git a/packages/melonjs/src/level/tiled/TMXLayer.js b/packages/melonjs/src/level/tiled/TMXLayer.js index a18991eff2..f54f591be6 100644 --- a/packages/melonjs/src/level/tiled/TMXLayer.js +++ b/packages/melonjs/src/level/tiled/TMXLayer.js @@ -242,8 +242,17 @@ export default class TMXLayer extends Renderable { * @ignore */ this._shaderTileset = null; + // how far a `tileoffset` moves tiles, each way + let offsetLeft = 0; + let offsetTop = 0; + let offsetRight = 0; + let offsetBottom = 0; for (let i = 0; i < this.tilesets.length; i++) { const tileset = this.tilesets.getTilesetByIndex(i); + offsetLeft = Math.max(offsetLeft, -tileset.tileoffset.x); + offsetRight = Math.max(offsetRight, tileset.tileoffset.x); + offsetTop = Math.max(offsetTop, -tileset.tileoffset.y); + offsetBottom = Math.max(offsetBottom, tileset.tileoffset.y); this.maxTileOffset.x = Math.max( this.maxTileOffset.x, Math.abs(tileset.tileoffset.x), @@ -261,6 +270,25 @@ export default class TMXLayer extends Renderable { tileset.tileheight, ); } + // A tile larger than its cell reaches past it: up and to the right + // on an orthogonal or an isometric map, where a tile stands on the + // bottom left of its cell (any way on the others) + const overX = Math.max(this.maxTileSize.width - this.tilewidth, 0); + const overY = Math.max(this.maxTileSize.height - this.tileheight, 0); + const upRight = orientation === "orthogonal" || orientation === "isometric"; + /** + * How far past the layer's grid its tiles can be drawn, on each + * side: by tiles larger than a cell, and by a tileset's + * `tileoffset`. The layer is in view as far as they reach (see + * {@link TMXLayer#updateBounds}). + * @ignore + */ + this._reach = { + left: (upRight ? 0 : overX) + offsetLeft, + top: overY + offsetTop, + right: overX + offsetRight, + bottom: (upRight ? 0 : overY) + offsetBottom, + }; /** * All animated tilesets in this layer @@ -446,12 +474,42 @@ export default class TMXLayer extends Renderable { // layer left of and above the map's origin const bounds = this.getRenderer().getBounds(this); const origin = this._cellOrigin(vector2dPool.get()); - this.pos.x = bounds.left + origin.x; - this.pos.y = bounds.top + origin.y; + /** + * where the layer is in its map, with nothing moving it: what `pos` + * is measured from to know how far the layer was moved + * @ignore + */ + this._mapPos = { x: bounds.left + origin.x, y: bounds.top + origin.y }; this.width = bounds.width; this.height = bounds.height; + // the layer's offset in Tiled moves it from there, as setting `pos` + // does afterwards: its tiles are drawn, and found, where it is + this.pos.x = this._mapPos.x + (+data.offsetx || 0); + this.pos.y = this._mapPos.y + (+data.offsety || 0); vector2dPool.release(origin); boundsPool.release(bounds); + this.updateBounds(); + } + + /** + * The layer's bounds: its grid, where it is, and as far past it as its + * tiles reach. A tile larger than its cell, or moved by its tileset's + * `tileoffset`, is drawn outside the grid, and the layer is in view + * while such a tile is. Its `width` and `height` stay the grid's. + * @ignore + */ + updateBounds(absolute = true) { + const bounds = super.updateBounds(absolute); + const reach = this._reach; + if (reach !== undefined && bounds.isFinite()) { + bounds.setMinMax( + bounds.left - reach.left, + bounds.top - reach.top, + bounds.right + reach.right, + bounds.bottom + reach.bottom, + ); + } + return bounds; } /** @@ -977,9 +1035,10 @@ export default class TMXLayer extends Renderable { let tile = null; if (this.contains(x, y)) { + // in the map's pixels: back from where the layer was moved to const coord = this.getRenderer().pixelToTileCoords( - x, - y, + x - (this.pos.x - this._mapPos.x), + y - (this.pos.y - this._mapPos.y), vector2dPool.get(), ); tile = this.cellAt(coord.x, coord.y); @@ -1180,11 +1239,20 @@ export default class TMXLayer extends Renderable { viewRect.pos.set(rect.pos.x + offset.x, rect.pos.y + offset.y); rect = viewRect.setSize(rect.width, rect.height); } - // an infinite map's layer that starts left of or above the map's - // origin: its renderer draws the layer's grid from its first cell, - // moved to where that cell's tile is in the world - if (this.startx !== 0 || this.starty !== 0) { + // A layer moved from its place in the map, by its offset in Tiled or + // by its `pos`, and an infinite map's layer that starts left of or + // above the map's origin: the layer's renderer draws its grid from + // its first cell, moved to where that cell's tile is in the world + const movedX = this.pos.x - this._mapPos.x; + const movedY = this.pos.y - this._mapPos.y; + if ( + movedX !== 0 || + movedY !== 0 || + this.startx !== 0 || + this.starty !== 0 + ) { const origin = this._cellOrigin(vector2dPool.get()); + origin.set(origin.x + movedX, origin.y + movedY); cellRect ??= new Rect(0, 0, 0, 0); cellRect.pos.set(rect.pos.x - origin.x, rect.pos.y - origin.y); cellRect.setSize(rect.width, rect.height); diff --git a/packages/melonjs/tests/helpers/tile-layer-compare.js b/packages/melonjs/tests/helpers/tile-layer-compare.js index 4275e82c83..3b13bba0e9 100644 --- a/packages/melonjs/tests/helpers/tile-layer-compare.js +++ b/packages/melonjs/tests/helpers/tile-layer-compare.js @@ -202,15 +202,8 @@ export function makeMap(options) { * Tiles are drawn whole, bottom-aligned on their cell and moved by their * tileset's `tileoffset`; flipped across, down or along the diagonal (the * last on square tiles only, where a flip is a quarter turn in place). - * Layer opacity and tint are for the caller to apply to the picture. A - * layer is drawn at the map's origin: the engine's tile layers are, whatever - * their `pos` or Tiled's layer offset. - * - * One rule here is the engine's and not Tiled's: a layer whose grid is - * wholly out of view is not drawn, though a tile taller than its cell, in - * the map's first rows, reaches above the grid and could still show. The - * engine draws what is in view going by a layer's bounds, which are its - * grid's. + * A layer is moved by its offset in Tiled (`offsetx`, `offsety`). Layer + * opacity and tint are for the caller to apply to the picture. * @param {object} scene - what {@link makeMap} was given * @param {number} viewX - the view's left, in the map's pixels * @param {number} viewY - the view's top @@ -265,20 +258,8 @@ export function reference(scene, viewX, viewY, width, height) { }; for (const layer of layers) { - if (layer.chunks === undefined) { - // the grid's bounds: an isometric map's are its diamond's - const isIso = orientation === "isometric"; - const gridWidth = isIso ? ((cols + rows) * tw) / 2 : cols * tw; - const gridHeight = isIso ? ((cols + rows) * th) / 2 : rows * th; - if ( - viewX + width <= 0 || - gridWidth <= viewX || - viewY + height <= 0 || - gridHeight <= viewY - ) { - continue; - } - } + const ox = layer.offsetx ?? 0; + const oy = layer.offsety ?? 0; // the layer's cells: a plain grid, or an infinite map's chunks const cells = new Map(); const put = (col, row, gid) => { @@ -313,8 +294,8 @@ export function reference(scene, viewX, viewY, width, height) { for (const { col, row, gid } of ordered) { drawTile( gid, - ((col - row - 1) * tw) / 2 + (rows * tw) / 2, - ((col + row + 2) * th) / 2, + ox + ((col - row - 1) * tw) / 2 + (rows * tw) / 2, + oy + ((col + row + 2) * th) / 2, ); } } else { @@ -322,7 +303,7 @@ export function reference(scene, viewX, viewY, width, height) { return a.row - b.row || a.col - b.col; }); for (const { col, row, gid } of ordered) { - drawTile(gid, col * tw, (row + 1) * th); + drawTile(gid, ox + col * tw, oy + (row + 1) * th); } } } diff --git a/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js b/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js index 0bf2c405bf..7debb5bf20 100644 --- a/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js +++ b/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js @@ -1376,6 +1376,317 @@ export function isometricScenarios(renderer, type) { }); }); + describe("a layer moved, in Tiled or in the game", () => { + const ortho = (options) => { + return { orientation: "orthogonal", tw: 32, th: 32, ...options }; + }; + + /** the scene baked into one image ("prerender"), from each view */ + const baked = async (scene, views = VIEWS.slice(0, 4)) => { + const layers = show(app, makeMap(scene), "prerender"); + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, "prerender"); + const d = diff(reference(scene, x, y, WIDTH, HEIGHT), frame); + expect(d.different, `baked, view (${x}, ${y}): ${d.first}`).toBe(0); + } + }; + + for (const [x, y] of [ + [37, -21], + [-130, 75], + [0, 200], + ]) { + it(`isometric, offset by (${x}, ${y}) in Tiled`, async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 128)], + layers: [{ data: scattered(8, 6), offsetx: x, offsety: y }], + }); + expect(await same(scene)).toBeGreaterThan(0); + await baked(scene); + }); + + it(`orthogonal, offset by (${x}, ${y}) in Tiled`, async (ctx) => { + need(ctx); + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [makeTileset({ tw: 32, th: 64 })], + layers: [{ data: scattered(10, 8), offsetx: x, offsety: y }], + }); + expect(await same(scene)).toBeGreaterThan(0); + await baked(scene); + }); + } + + it("isometric layers, each with an offset of its own, on tilesets moved by a tileoffset", async (ctx) => { + need(ctx); + const scene = iso({ + cols: 8, + rows: 6, + tilesets: [ + blocks(64, 32, { extra: { tileoffset: { x: 5, y: 9 } } }), + blocks(64, 128, { extra: { tileoffset: { x: -20, y: 40 } } }), + blocks(128, 64), + ], + layers: [ + { data: scattered(8, 6), offsetx: -10, offsety: 30 }, + { data: scattered(8, 6, 8, 9), offsetx: 64, offsety: 0 }, + { + data: fill(8, 6, (c, r) => { + return (c + r) % 4 === 1 ? 17 + ((c + r * 3) % 8) : 0; + }), + offsetx: 3, + offsety: -70, + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("orthogonal layers, each with an offset of its own", async (ctx) => { + need(ctx); + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [ + makeTileset({ tw: 32, th: 32 }), + makeTileset({ tw: 64, th: 96 }), + ], + layers: [ + { data: scattered(10, 8), offsetx: 16, offsety: 8 }, + { + data: fill(10, 8, (c, r) => { + return (c + r * 2) % 4 === 0 ? 9 + ((c + r) % 8) : 0; + }), + offsetx: -45, + offsety: 61, + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("a layer of an infinite isometric map, offset", async (ctx) => { + need(ctx); + const chunk = (x, y) => { + return { + x, + y, + width: 4, + height: 4, + data: fill(4, 4, (c, r) => { + return (c + r + x) % 5 === 0 ? 0 : 1 + ((c + r * 2) % 8); + }), + }; + }; + const scene = iso({ + cols: 8, + rows: 8, + tilesets: [blocks(64, 64)], + extra: { infinite: true }, + layers: [ + { + startx: -4, + starty: -4, + width: 12, + height: 8, + data: undefined, + chunks: [chunk(-4, -4), chunk(0, -4), chunk(4, 0), chunk(0, 0)], + offsetx: 48, + offsety: -36, + }, + ], + }); + const layers = show(app, makeMap(scene)); + expect(layers[0].renderMode).toBe("shader"); + const views = [ + [0, 0], + [-200, -150], + [150, -100], + ]; + expect(await same(scene, { views, layers })).toBeGreaterThan(0); + }); + + for (const orientation of ["isometric", "orthogonal"]) { + it(`${orientation}: moved in the game, by its pos`, async (ctx) => { + need(ctx); + const layer0 = { offsetx: 12, offsety: 7 }; + const scene = + orientation === "isometric" + ? iso({ + cols: 8, + rows: 6, + tilesets: [blocks(64, 128)], + layers: [{ data: scattered(8, 6), ...layer0 }], + }) + : ortho({ + cols: 10, + rows: 8, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [{ data: scattered(10, 8), ...layer0 }], + }); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + // where Tiled put it + const startX = layer.pos.x; + const startY = layer.pos.y; + expect(await same(scene, { layers })).toBeGreaterThan(0); + for (const [dx, dy] of [ + [50, -30], + [-170, 90], + [0, 0], + ]) { + layer.pos.x = startX + dx; + layer.pos.y = startY + dy; + // the reference follows + scene.layers[0].offsetx = 12 + dx; + scene.layers[0].offsety = 7 + dy; + expect( + await same(scene, { layers }), + `moved by (${dx}, ${dy})`, + ).toBeGreaterThan(0); + } + }); + } + }); + + describe("tiles that reach past the grid, when only they are in view", () => { + const ortho = (options) => { + return { orientation: "orthogonal", tw: 32, th: 32, ...options }; + }; + const all = (cols, rows) => { + return fill(cols, rows, (c, r) => { + return 1 + ((c + r * 2) % 8); + }); + }; + + /** the scene baked into one image ("prerender"), from each view */ + const baked = async (scene, views) => { + const layers = show(app, makeMap(scene), "prerender"); + let total = 0; + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, "prerender"); + const d = diff(reference(scene, x, y, WIDTH, HEIGHT), frame); + expect(d.different, `baked, view (${x}, ${y}): ${d.first}`).toBe(0); + total += drawn(frame); + } + return total; + }; + + it("isometric: tall tiles, above the map's top corner", async (ctx) => { + need(ctx); + // the 64 × 128 tiles of the first rows reach 96 pixels above + // the grid, which starts at 0: these views end above it + const scene = iso({ + cols: 10, + rows: 8, + tilesets: [blocks(64, 128)], + layers: [{ data: all(10, 8) }], + }); + const views = [ + [160, -HEIGHT - 10], + [100, -HEIGHT - 40], + [220, -HEIGHT - 70], + [-126, -300], + ]; + expect(await same(scene, { views })).toBeGreaterThan(0); + expect(await baked(scene, views)).toBeGreaterThan(0); + }); + + it("orthogonal: tall tiles, above the first row", async (ctx) => { + need(ctx); + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [makeTileset({ tw: 32, th: 128 })], + layers: [{ data: all(10, 8) }], + }); + const views = [ + [0, -HEIGHT - 10], + [40, -HEIGHT - 50], + [-200, -HEIGHT - 80], + ]; + expect(await same(scene, { views })).toBeGreaterThan(0); + expect(await baked(scene, views)).toBeGreaterThan(0); + }); + + it("orthogonal: wide tiles, right of the last column", async (ctx) => { + need(ctx); + // 96 pixels wide on a 32 pixel grid: 64 pixels past the map, + // which ends at 320 + const scene = ortho({ + cols: 10, + rows: 8, + tilesets: [makeTileset({ tw: 96, th: 32 })], + layers: [{ data: all(10, 8) }], + }); + const views = [ + [325, 0], + [350, 40], + [380, -100], + ]; + expect(await same(scene, { views })).toBeGreaterThan(0); + expect(await baked(scene, views)).toBeGreaterThan(0); + }); + + it("isometric tiles moved off the grid by their tileset's tileoffset", async (ctx) => { + need(ctx); + // moved 150 pixels up and to the left: where the grid is not + const scene = iso({ + cols: 6, + rows: 6, + tilesets: [ + blocks(64, 64, { extra: { tileoffset: { x: -150, y: -150 } } }), + ], + layers: [{ data: all(6, 6) }], + }); + const views = [ + [-WIDTH - 10, 0], + [-100, -HEIGHT - 20], + [-WIDTH - 60, -80], + ]; + expect(await same(scene, { views })).toBeGreaterThan(0); + }); + + it("orthogonal tiles moved off the grid by their tileset's tileoffset", async (ctx) => { + need(ctx); + // under the map, which ends at 192. An orthogonal tileoffset + // leaves the layer to the CPU renderer: it is drawn as well + const scene = ortho({ + cols: 8, + rows: 6, + tilesets: [ + makeTileset({ + tw: 32, + th: 32, + extra: { tileoffset: { x: 0, y: 90 } }, + }), + ], + layers: [{ data: all(8, 6) }], + }); + const [mode] = modeOf(scene); + expect(mode).toBe("perTile"); + const layers = app.world.getChildren().filter((child) => { + return child.layerData !== undefined; + }); + for (const [x, y] of [ + [0, 200], + [-40, 250], + ]) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, "perTile"); + const d = diff(reference(scene, x, y, WIDTH, HEIGHT), frame); + expect(d.different, `view (${x}, ${y}): ${d.first}`).toBe(0); + expect(drawn(frame)).toBeGreaterThan(0); + } + }); + }); + it("a level change frees what the GPU path kept for its layers", async (ctx) => { need(ctx); const scene = iso({ diff --git a/packages/melonjs/tests/tmxlayer-offset.spec.js b/packages/melonjs/tests/tmxlayer-offset.spec.js new file mode 100644 index 0000000000..1f25aa597b --- /dev/null +++ b/packages/melonjs/tests/tmxlayer-offset.spec.js @@ -0,0 +1,335 @@ +/** + * Where a tile layer is: moved by its offset in Tiled and by its own `pos`, + * and in view as far as its tiles reach, which is past its grid when they + * are larger than a cell or moved by their tileset's `tileoffset`. + * + * Checked here on the Canvas renderer, tile by tile and baked into one + * image, against the picture painted with no engine (see + * `helpers/tile-layer-compare.js`), and for what `getTile` answers. The GPU + * renderers are held to the same in `helpers/tile-layer-isometric-scenarios.js`. + */ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { video } from "../src/index.js"; +import { + diff, + drawIn, + drawn, + fill, + inBlock, + makeApp, + makeMap, + makeTileset, + reference, + show, +} from "./helpers/tile-layer-compare.js"; + +const WIDTH = 320; +const HEIGHT = 240; + +const filled = (cols, rows) => { + return fill(cols, rows, (c, r) => { + return (c * 3 + r) % 7 === 0 ? 0 : 1 + ((c + r * 2) % 8); + }); +}; + +const orthogonal = (options = {}) => { + return { + orientation: "orthogonal", + cols: 10, + rows: 8, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: options.tall ?? 32 })], + layers: [{ data: filled(10, 8), ...options.layer }], + }; +}; + +const isometric = (options = {}) => { + const th = options.tall ?? 64; + return { + orientation: "isometric", + cols: 8, + rows: 6, + tw: 64, + th: 32, + tilesets: [ + makeTileset({ + tw: 64, + th, + opaque: (x, y) => { + return inBlock(x, y, 64, th, 64, 32); + }, + }), + ], + layers: [{ data: filled(8, 6), ...options.layer }], + }; +}; + +describe("where a tile layer is (Canvas renderer)", () => { + let app; + beforeAll(async () => { + app = await makeApp(video.CANVAS, "CANVAS", WIDTH, HEIGHT); + }); + afterAll(() => { + app?.destroy(); + vi.restoreAllMocks(); + }); + + /** + * Draw a scene tile by tile and baked, from each view, and expect the + * reference picture. + * @returns {Promise} how many pixels were drawn, in all + */ + const asTiled = async (scene, views) => { + let total = 0; + for (const mode of ["perTile", "prerender"]) { + const layers = show(app, makeMap(scene), mode); + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, mode); + const d = diff(reference(scene, x, y, WIDTH, HEIGHT), frame); + expect(d.different, `${mode}, view (${x}, ${y}): ${d.first}`).toBe(0); + total += drawn(frame); + } + } + return total; + }; + + const VIEWS = [ + [0, 0], + [70, 20], + [-40, -80], + [230, 110], + ]; + + describe("moved by its offset in Tiled", () => { + for (const [x, y] of [ + [37, -21], + [-130, 75], + ]) { + it(`an orthogonal layer, by (${x}, ${y})`, async () => { + const scene = orthogonal({ + tall: 64, + layer: { offsetx: x, offsety: y }, + }); + expect(await asTiled(scene, VIEWS)).toBeGreaterThan(0); + }); + + it(`an isometric layer, by (${x}, ${y})`, async () => { + const scene = isometric({ + tall: 128, + layer: { offsetx: x, offsety: y }, + }); + expect(await asTiled(scene, VIEWS)).toBeGreaterThan(0); + }); + } + + for (const [orientation, extra] of [ + ["staggered", { staggeraxis: "y", staggerindex: "odd" }], + [ + "hexagonal", + { staggeraxis: "x", staggerindex: "even", hexsidelength: 16 }, + ], + ]) { + it(`a ${orientation} layer: the same picture, moved`, async () => { + // no reference is painted for these maps: the layer moved by + // (37, -21) is the layer where it was, seen from that much + // the other way + const scene = (layer) => { + return { + orientation, + cols: 8, + rows: 8, + tw: 64, + th: 32, + tilesets: [tileset], + layers: [{ data: filled(8, 8), ...layer }], + extra, + }; + }; + const tileset = makeTileset({ + tw: 64, + th: 48, + opaque: (x, y) => { + return inBlock(x, y, 64, 48, 64, 32); + }, + }); + for (const mode of ["perTile", "prerender"]) { + const plain = show(app, makeMap(scene({})), mode); + const frames = []; + for (const [x, y] of VIEWS) { + app.viewport.moveTo(x - 37, y + 21); + frames.push((await drawIn(app, plain, mode)).frame); + } + const moved = show( + app, + makeMap(scene({ offsetx: 37, offsety: -21 })), + mode, + ); + for (const [i, [x, y]] of VIEWS.entries()) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, moved, mode); + const d = diff(frames[i], frame); + expect(d.different, `${mode}, view (${x}, ${y}): ${d.first}`).toBe( + 0, + ); + expect(drawn(frame)).toBeGreaterThan(0); + } + } + }); + } + + it("the layer's pos is its offset, and its bounds move with it", () => { + const [layer] = show( + app, + makeMap(orthogonal({ layer: { offsetx: 37, offsety: -21 } })), + ); + expect([layer.pos.x, layer.pos.y]).toEqual([37, -21]); + expect([layer.width, layer.height]).toEqual([320, 256]); + const bounds = layer.getBounds(); + expect([bounds.left, bounds.top]).toEqual([37, -21]); + expect([bounds.right, bounds.bottom]).toEqual([357, 235]); + // no offset: where it always was + const [plain] = show(app, makeMap(orthogonal())); + expect([plain.pos.x, plain.pos.y]).toEqual([0, 0]); + }); + + it("getTile answers for where the tiles are drawn", () => { + const [layer] = show( + app, + makeMap(orthogonal({ layer: { offsetx: 40, offsety: -24 } })), + ); + // the cell (2, 3) is drawn from (64 + 40, 96 - 24) + const tile = layer.getTile(64 + 40 + 5, 96 - 24 + 5); + expect([tile.col, tile.row]).toEqual([2, 3]); + expect(tile.tileId).toBe(layer.cellAt(2, 3).tileId); + // its last pixel, and the first of the next cell + expect(layer.getTile(95 + 40, 127 - 24).col).toBe(2); + expect(layer.getTile(96 + 40, 127 - 24).col).toBe(3); + // left of where the layer now starts: no tile, where there was + // one before the layer was moved + expect(layer.getTile(5, 40)).toBeNull(); + const first = layer.getTile(32 + 40 + 5, -20); + expect([first.col, first.row]).toEqual([1, 0]); + + const [iso] = show( + app, + makeMap(isometric({ layer: { offsetx: -30, offsety: 50 } })), + ); + // the middle of the diamond of cell (3, 2): the map's top + // corner is at x = rows × half a tile + const originX = 6 * 32; + const middle = iso.getTile( + (3 - 2) * 32 + originX - 30, + (3 + 2 + 1) * 16 + 50, + ); + expect([middle.col, middle.row]).toEqual([3, 2]); + }); + }); + + describe("moved in the game, by its pos", () => { + for (const make of [orthogonal, isometric]) { + it(`${make.name}: drawn, and found by getTile, where it was moved to`, async () => { + const scene = make({ tall: 64 }); + const layers = show(app, makeMap(scene), "perTile"); + const layer = layers[0]; + const before = layer.getTile(150, 100); + expect(before).not.toBeNull(); + for (const [dx, dy] of [ + [50, -30], + [-170, 90], + [0, 0], + ]) { + layer.pos.x = dx; + layer.pos.y = dy; + scene.layers[0].offsetx = dx; + scene.layers[0].offsety = dy; + for (const [x, y] of VIEWS) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, "perTile"); + const d = diff(reference(scene, x, y, WIDTH, HEIGHT), frame); + expect( + d.different, + `moved by (${dx}, ${dy}), view (${x}, ${y}): ${d.first}`, + ).toBe(0); + } + // the same cell, where it is now + const now = layer.getTile(150 + dx, 100 + dy); + expect([now.col, now.row]).toEqual([before.col, before.row]); + } + }); + } + }); + + describe("in view as far as its tiles reach", () => { + it("isometric: tall tiles, above the map's top corner", async () => { + const scene = isometric({ tall: 128 }); + scene.layers[0].data = fill(8, 6, (c, r) => { + return 1 + ((c + r * 2) % 8); + }); + // the grid starts at y = 0: these views end above it + const views = [ + [40, -HEIGHT - 10], + [100, -HEIGHT - 60], + ]; + expect(await asTiled(scene, views)).toBeGreaterThan(0); + }); + + it("orthogonal: tall tiles above the first row, wide ones right of the last column", async () => { + const tall = orthogonal({ tall: 128 }); + expect( + await asTiled(tall, [ + [0, -HEIGHT - 10], + [-200, -HEIGHT - 80], + ]), + ).toBeGreaterThan(0); + const wide = orthogonal(); + wide.tilesets = [makeTileset({ tw: 96, th: 32 })]; + expect( + await asTiled(wide, [ + [325, 0], + [380, -100], + ]), + ).toBeGreaterThan(0); + }); + + it("tiles moved off the grid by their tileset's tileoffset", async () => { + const scene = orthogonal(); + scene.tilesets = [ + makeTileset({ tw: 32, th: 32, extra: { tileoffset: { x: 0, y: 90 } } }), + ]; + // the grid ends at y = 256 + expect( + await asTiled(scene, [ + [0, 260], + [-40, 300], + ]), + ).toBeGreaterThan(0); + }); + + it("the bounds cover the reach, the size stays the grid's", () => { + const [layer] = show(app, makeMap(orthogonal({ tall: 128 }))); + expect([layer.width, layer.height]).toEqual([320, 256]); + const bounds = layer.getBounds(); + // 128 pixel tiles on a 32 pixel grid stand 96 above their cell + expect(bounds.top).toBe(-96); + expect([bounds.left, bounds.right, bounds.bottom]).toEqual([0, 320, 256]); + // and getTile has nothing above the grid: no cell is there + expect(layer.getTile(10, -50)).toBeNull(); + // tiles the size of the grid: the grid, no more + const plain = show(app, makeMap(orthogonal()))[0].getBounds(); + expect([plain.left, plain.top, plain.right, plain.bottom]).toEqual([ + 0, 0, 320, 256, + ]); + }); + + it("and far from all of it, the layer is not drawn at all", async () => { + const scene = orthogonal({ tall: 128 }); + const layers = show(app, makeMap(scene), "perTile"); + app.viewport.moveTo(0, -HEIGHT - 200); + const far = await drawIn(app, layers, "perTile"); + expect(far.cpuDraws).toBe(0); + expect(drawn(far.frame)).toBe(0); + }); + }); +}); From a3dd9148ddef7b6c4cc079b4339ca590335aec23 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 02:57:47 +0000 Subject: [PATCH 06/11] GPU tile layers for staggered and hexagonal maps (#1897) Both are one geometry, on the base the isometric path shares: lines of cells a row height apart, every other one shifted by a column width, along the map's rows or its columns. One shader chunk (GLSL and WGSL) and one region function draw both; a hexagonal map's cells are longer by its side length, which is in the two lengths the shader is given (in what was the padding of the WGSL uniform block). The layer's checks are those of an isometric layer: one tileset, tiles up to 4 cells wide and 8 tall, a grid of even size. The CPU renderer of these maps is fixed first, as the reference of the tests showed it away from Tiled: its drawing loop hung a tile larger than its cell from the top of the cell and ignored the tileset's `tileoffset`, where its own `drawTile` (and Tiled) stand the tile on the cell and move it by the offset; and it left out tiles of a large tileset along the edges of the view. Tests: the reference paints staggered and hexagonal maps; 24 scenes (both orientations, either axis, either index, three kinds of tiles) on the Canvas renderer tile by tile and baked, and 38 scenarios on WebGL and WebGPU through both paths, with soft edges, flips, animation, layer offsets, infinite maps, setTile and the fallbacks. The comparison of the two paths moves to the shared test helper. Changelog, API docs and the tilemaps skill. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01JnzwA4a7ZyjLBpbeEpFLu6 --- packages/melonjs/CHANGELOG.md | 5 +- .../melonjs/skills/melonjs-tilemaps/SKILL.md | 20 +- packages/melonjs/src/application/settings.ts | 12 +- packages/melonjs/src/level/tiled/TMXLayer.js | 113 ++-- .../tiled/renderer/TMXHexagonalRenderer.js | 52 +- packages/melonjs/src/physics/world.js | 13 +- .../src/video/gpu/tmxlayer_orientation.js | 144 ++++- .../webgl/renderers/tmxlayer/tmxlayer.js | 8 + .../webgl/shaders/staggered-tmxlayer.frag | 85 +++ .../webgpu/renderers/tmxlayer/tmxlayer.js | 9 + .../video/webgpu/shaders/tmxlayer-common.wgsl | 4 + .../webgpu/shaders/tmxlayer-staggered.wgsl | 83 +++ .../tests/helpers/tile-layer-compare.js | 122 +++- .../helpers/tile-layer-isometric-scenarios.js | 85 +-- .../helpers/tile-layer-staggered-scenarios.js | 536 ++++++++++++++++++ .../tests/tmxlayer-shader-staggered.spec.js | 8 + .../melonjs/tests/tmxlayer-staggered.spec.js | 53 ++ .../melonjs/tests/webgpu_tmxlayer.spec.js | 107 ++++ .../tests/webgpu_tmxlayer_staggered.spec.js | 9 + 19 files changed, 1274 insertions(+), 194 deletions(-) create mode 100644 packages/melonjs/src/video/webgl/shaders/staggered-tmxlayer.frag create mode 100644 packages/melonjs/src/video/webgpu/shaders/tmxlayer-staggered.wgsl create mode 100644 packages/melonjs/tests/helpers/tile-layer-staggered-scenarios.js create mode 100644 packages/melonjs/tests/tmxlayer-shader-staggered.spec.js create mode 100644 packages/melonjs/tests/tmxlayer-staggered.spec.js create mode 100644 packages/melonjs/tests/webgpu_tmxlayer_staggered.spec.js diff --git a/packages/melonjs/CHANGELOG.md b/packages/melonjs/CHANGELOG.md index 3f2dcd8acf..7155fc5c06 100644 --- a/packages/melonjs/CHANGELOG.md +++ b/packages/melonjs/CHANGELOG.md @@ -6,10 +6,10 @@ - Transitions: a mask transition can be shaped like an image or a `Sprite`, close on the player with `center`, and fill the shape with `invert`, on `state.transition`, `MaskEffect` and `Trigger` ([#1399](https://github.com/melonjs/melonJS/issues/1399)) ### Changed -- Tiled: an isometric tile layer with an `opacity` below 1 fades as one flat picture on WebGL and WebGPU, where each tile was faded over the ones behind it and showed through the one in front. Set the layer's `renderMode` to `"perTile"` for the look it had +- Tiled: an isometric, staggered or hexagonal tile layer with an `opacity` below 1 fades as one flat picture on WebGL and WebGPU, where each tile was faded over the ones behind it and showed through the one in front. Set the layer's `renderMode` to `"perTile"` for the look it had ### Performance -- Tiled: isometric tile layers are drawn by the GPU on WebGL and WebGPU, as orthogonal ones are: one quad for the layer, its tiles found by the fragment shader, in place of a quad per tile in view found and placed in JavaScript every frame. On a 100 × 100 map with about 4000 tiles in view, drawing the layer takes under **0.1 ms** of main-thread time a frame, down from about **2 ms**, and no longer grows with the number of tiles shown. A layer takes this path by itself when all its tiles come from one tileset, up to 4 grid widths wide and 8 grid heights tall (a `tileoffset`, flipped and animated tiles, and `setTile` are all fine); any other is drawn as before, and the console says why ([#1897](https://github.com/melonjs/melonJS/issues/1897)) +- Tiled: isometric, staggered and hexagonal tile layers are drawn by the GPU on WebGL and WebGPU, as orthogonal ones are: one quad for the layer, its tiles found by the fragment shader, in place of a quad per tile in view found and placed in JavaScript every frame. On a 100 × 100 isometric map with about 4000 tiles in view, drawing the layer takes under **0.1 ms** of main-thread time a frame, down from about **2 ms**, and no longer grows with the number of tiles shown. A layer takes this path by itself when all its tiles come from one tileset, up to 4 cells wide and 8 tall (a `tileoffset`, flipped and animated tiles, and `setTile` are all fine); any other is drawn as before, and the console says why ([#1897](https://github.com/melonjs/melonJS/issues/1897)) - Tiled: a reminder that orthogonal tile layers have been drawn this way since 19.4, with the same gain (about 1.9 ms down to under 0.1 ms for 3600 tiles in view). A layer that is not says why in the console when it is activated: most often a tileset with a `tileoffset`, a collection of images, or the Canvas renderer. ### Fixed @@ -20,6 +20,7 @@ - Tiled, GPU tile layers: the shader runs at full precision whatever `highPrecisionShader` says; at a lower one a phone could misplace the tiles of a large map - Tiled: a tile layer follows its offset in Tiled (`offsetx`, `offsety`), and its own `pos` when the game moves it: its tiles are drawn there and `getTile()` finds them there, on every renderer and map orientation. Both were ignored, and the layer drawn at the map's origin; only image layers followed their offset. A map whose tile layers have an offset now looks as it does in Tiled - Tiled: tiles that reach past their layer's grid (taller or wider than a cell, or moved by a `tileoffset`) are drawn when they are in view and the grid is not; the whole layer was skipped, so the tops of tall tiles vanished once the map scrolled out of view. `getBounds()` of a tile layer covers that reach; its `width` and `height` are still the grid's +- Tiled, staggered and hexagonal maps: a tile larger than its cell stands on the cell, and a tileset's `tileoffset` moves its tiles, as in Tiled and on every other map. Drawn tile by tile, such a tile hung down from the top of its cell and the offset was ignored, so the map sat lower than its objects (and than a tile redrawn in a baked layer); tiles of a large tileset also went missing along the edges of the view - Tiled, isometric maps drawn tile by tile: tiles of a tileset larger than the map's first one, or moved by a `tileoffset`, went missing along the edges of the view - Tiled, orthogonal maps drawn tile by tile: tiles more than two cells tall went missing along the bottom of the view - `NineSliceSprite` blinks with `flicker()`, as a `Sprite` does; it ignored it diff --git a/packages/melonjs/skills/melonjs-tilemaps/SKILL.md b/packages/melonjs/skills/melonjs-tilemaps/SKILL.md index 0bd9874f8f..04cc022a75 100644 --- a/packages/melonjs/skills/melonjs-tilemaps/SKILL.md +++ b/packages/melonjs/skills/melonjs-tilemaps/SKILL.md @@ -462,17 +462,18 @@ per-pixel GID lookup in the fragment shader. It requires **all** of: - a renderer that reports GPU tile-layer support (WebGL 2 / WebGPU — not Canvas), - `world.gpuTilemap !== false` (the `gpuTilemap` application setting, default `true`), -- an **orthogonal** or **isometric** layer — hexagonal, staggered and oblique - always fall back, +- an **orthogonal**, **isometric**, **staggered** or **hexagonal** layer — + oblique always falls back, - no collection-of-image tileset, and a tileset tile render size of "Tile size" (`tilerendersize`, not "Map grid size"), - on an orthogonal map: no non-zero `tileoffset`, and tiles that reach at most 4 cells past their own, -- on an isometric map: every tile of the layer from **one tileset** (tiles - overlap, and their order is kept within a tileset, not across two: split - floor and walls into a layer each, which a map usually does already), tiles - at most 4 grid widths wide and 8 grid heights tall, a grid of even size - (64 × 32, not 65 × 33), and the normal blend mode. A `tileoffset` is fine, +- on an isometric, staggered or hexagonal map: every tile of the layer from + **one tileset** (tiles overlap, and their order is kept within a tileset, + not across two: split floor and walls into a layer each, which a map + usually does already), tiles at most 4 cells wide and 8 tall, a grid of + even size (64 × 32, not 65 × 33; a hexagon side of even length), and the + normal blend mode. A `tileoffset` is fine, - no tile that is not square flipped diagonally (rotated, in Tiled). A tile set at run time with `setTile` that breaks one of these moves the layer @@ -538,8 +539,9 @@ layer into the offscreen-bake path instead. | after updating to 20.17 a tile layer sits away from where it did | it has an offset in Tiled that was ignored until then, and now moves it as in the editor: clear the layer's Offset in Tiled if it was not meant | | the tops of tall tiles vanish when the map itself scrolls out of view | before 20.17 a layer was skipped once its grid left the view, though tiles larger than a cell (or moved by a `tileoffset`) still reached in: update | | tilemap slow on some machines | layer fell back off the shader path — check the console warning for the reason | -| an isometric layer warns `tiles of more than one tileset in an isometric layer` and is slow | the shader path draws an isometric layer with one tileset: put the tiles of each tileset on a layer of their own, or pack them into one tileset | -| a half-transparent isometric layer of tall tiles looks flatter than before 20.17 | on the shader path a layer fades as one picture, and a tile no longer shows through the one in front: set that layer's `renderMode` to `"perTile"` (a `renderMode` custom property in Tiled) for the old look | +| a staggered or hexagonal map sits higher than before 20.17, by how much its tiles are taller than a cell (or by its tileset's `tileoffset`) | it is drawn as Tiled draws it: until then a tile hung from the top of its cell and the offset was ignored, away from the map's objects | +| an isometric (staggered, hexagonal) layer warns `tiles of more than one tileset in an isometric layer` and is slow | the shader path draws such a layer with one tileset: put the tiles of each tileset on a layer of their own, or pack them into one tileset | +| a half-transparent isometric (staggered, hexagonal) layer of tall tiles looks flatter than before 20.17 | on the shader path a layer fades as one picture, and a tile no longer shows through the one in front: set that layer's `renderMode` to `"perTile"` (a `renderMode` custom property in Tiled) for the old look | | the soft edges of tiles larger than the grid showed a thin dark outline on the shader path | before 20.17 the edge of such a tile was not blended over the tile behind it: update | ## Related skills diff --git a/packages/melonjs/src/application/settings.ts b/packages/melonjs/src/application/settings.ts index f4733069dc..d58847735c 100644 --- a/packages/melonjs/src/application/settings.ts +++ b/packages/melonjs/src/application/settings.ts @@ -476,16 +476,16 @@ export type ApplicationSettings = { physic: PhysicsType; /** - * Enable the GPU procedural shader path for orthogonal and isometric - * tile layers (backends advertising + * Enable the GPU procedural shader path for orthogonal, isometric, + * staggered and hexagonal tile layers (backends advertising * `renderer.supportsShaderTileLayers` — WebGL 2 and WebGPU). When * `true` (default), eligible layers render via a single quad per * tileset + a fragment shader doing per-fragment GID lookup, bypassing * the per-tile draw loop entirely. Layers that don't qualify (Canvas - * renderer, a staggered, hexagonal or oblique map, collection-of-image - * tilesets, tilerendersize "grid", tiles larger than the shader walks; - * on an orthogonal map a non-zero tileoffset, on an isometric one - * tiles of several tilesets in one layer) fall back to the legacy path + * renderer, an oblique map, collection-of-image tilesets, + * tilerendersize "grid", tiles larger than the shader walks; on an + * orthogonal map a non-zero tileoffset, on the others tiles of + * several tilesets in one layer) fall back to the legacy path * automatically, saying why in the console. * Set to `false` to disable globally. * @default true diff --git a/packages/melonjs/src/level/tiled/TMXLayer.js b/packages/melonjs/src/level/tiled/TMXLayer.js index f54f591be6..44b6edbfdf 100644 --- a/packages/melonjs/src/level/tiled/TMXLayer.js +++ b/packages/melonjs/src/level/tiled/TMXLayer.js @@ -27,16 +27,18 @@ const FLIP_H_BIT = 1 << 0; const FLIP_V_BIT = 1 << 1; const FLIP_AD_BIT = 1 << 2; -// The largest tiles the shader path draws: the bounds of the loops of the -// fragment shaders, which are declared from the same numbers. An orthogonal -// tile can reach so many cells past its own; an isometric one be so many -// grid cells wide and tall. +// The largest tiles the shader path draws are the bounds of the loops of the +// fragment shaders, which are declared from the same numbers (see +// `TMX_ORIENTATIONS`). An orthogonal tile can reach so many cells past its +// own. const ORTHOGONAL_MAX_OVERFLOW = TMX_ORIENTATIONS.orthogonal.limits.MAX_OVERFLOW; -const ISOMETRIC_MAX_TILE_WIDTHS = TMX_ORIENTATIONS.isometric.limits.MAX_COLS; -const ISOMETRIC_MAX_TILE_HEIGHTS = - TMX_ORIENTATIONS.isometric.limits.MAX_ROWS / 2; -const ISOMETRIC_ONE_TILESET = - "tiles of more than one tileset in an isometric layer"; +// "an isometric", "a staggered", "a hexagonal": a layer's kind, in a reason +const kindOf = (orientation) => { + return `${orientation === "isometric" ? "an" : "a"} ${orientation}`; +}; +const oneTilesetReason = (orientation) => { + return `tiles of more than one tileset in ${kindOf(orientation)} layer`; +}; // A diagonal flip turns a tile a quarter: one that is not square then covers // other pixels than its own image's rectangle, which is all the shader looks // at. @@ -270,12 +272,12 @@ export default class TMXLayer extends Renderable { tileset.tileheight, ); } - // A tile larger than its cell reaches past it: up and to the right - // on an orthogonal or an isometric map, where a tile stands on the - // bottom left of its cell (any way on the others) + // A tile larger than its cell reaches past it: up and to the right, + // as a tile stands on the bottom left of its cell (any way on an + // oblique map) const overX = Math.max(this.maxTileSize.width - this.tilewidth, 0); const overY = Math.max(this.maxTileSize.height - this.tileheight, 0); - const upRight = orientation === "orthogonal" || orientation === "isometric"; + const upRight = orientation !== "oblique"; /** * How far past the layer's grid its tiles can be drawn, on each * side: by tiles larger than a cell, and by a tileset's @@ -426,7 +428,7 @@ export default class TMXLayer extends Renderable { /** * How this layer is rendered. Resolved by `onActivateEvent` to one of: - * - `"shader"` — GPU procedural shader path, on WebGL 2 and WebGPU, for orthogonal and isometric maps (single quad per tileset, fragment GID lookup) + * - `"shader"` — GPU procedural shader path, on WebGL 2 and WebGPU, for orthogonal, isometric, staggered and hexagonal maps (single quad per tileset, fragment GID lookup) * - `"prerender"`— offscreen-canvas bake at activation, blitted as one drawImage per frame * - `"perTile"` — per-frame loop, one drawImage per visible tile * @@ -579,7 +581,7 @@ export default class TMXLayer extends Renderable { const renderer = this.getRenderer(); const bounds = renderer.getBounds(this); // an oversized tile reaches past its cell (which way depends on the - // orientation: up on an orthogonal map, down on a hexagonal one), and + // orientation: up and to the right, any way on an oblique map), and // a tileset's offset moves all of its tiles const overX = Math.max(this.maxTileSize.width - this.tilewidth, 0); const overY = Math.max(this.maxTileSize.height - this.tileheight, 0); @@ -777,7 +779,7 @@ export default class TMXLayer extends Renderable { if (!renderer || renderer.supportsShaderTileLayers !== true) { return { ok: false, reason: "no-gpu-renderer" }; } - if (this.orientation !== "orthogonal" && this.orientation !== "isometric") { + if (TMX_ORIENTATIONS[this.orientation] === undefined) { return { ok: false, reason: `no gpu renderer supported yet for "${this.orientation}" orientation`, @@ -786,8 +788,8 @@ export default class TMXLayer extends Renderable { if (!this.tilesets || this.tilesets.tilesets.length === 0) { return { ok: false, reason: "no tilesets" }; } - if (this.orientation === "isometric") { - return this._checkIsometricShaderEligibility(); + if (this.orientation !== "orthogonal") { + return this._checkOverlappingShaderEligibility(); } // the shader iterates a fixed-size loop over candidate cells to // support oversized tiles (tile dim > cell dim) drawn bottom-aligned. @@ -852,12 +854,12 @@ export default class TMXLayer extends Renderable { } /** - * Check whether this isometric layer can take the shader path, and note - * the tileset it would draw with. + * Check whether this layer of an isometric, staggered or hexagonal map + * can take the shader path, and note the tileset it would draw with. * - * Isometric tiles overlap (each is drawn as its whole image, over the - * rows behind it), and the shader path draws one tileset at a time: it - * can keep the tile drawn last among those of one tileset, not across + * On these maps tiles overlap (each is drawn as its whole image, over + * the ones behind it), and the shader path draws one tileset at a time: + * it can keep the tile drawn last among those of one tileset, not across * two. So the layer's tiles must all come from the same tileset. Which * one is found by looking at the tiles, not at the map's list: a map * commonly has a tileset per layer. @@ -865,13 +867,21 @@ export default class TMXLayer extends Renderable { * @ignore * @internal */ - _checkIsometricShaderEligibility() { + _checkOverlappingShaderEligibility() { this._shaderTileset = null; - // Half the tiles of a grid of odd size stand half a pixel off. Tile - // by tile, each is moved to a whole pixel; the shader has one quad - // for all of them, and would draw those a pixel away. - if (this.tilewidth % 2 !== 0 || this.tileheight % 2 !== 0) { - return { ok: false, reason: "an isometric grid of odd size" }; + const kind = kindOf(this.orientation); + // Half the cells of a grid of odd size stand half a pixel off (on a + // hexagonal map, of a side of odd length). Tile by tile, each tile + // is moved to a whole pixel; the shader has one quad for all of + // them, and would draw those a pixel away. + const map = this.getRenderer(); + if ( + this.tilewidth % 2 !== 0 || + this.tileheight % 2 !== 0 || + (this.orientation !== "isometric" && + !(Number.isInteger(map.columnwidth) && Number.isInteger(map.rowheight))) + ) { + return { ok: false, reason: `${kind} grid of odd size` }; } // Tiles overlap, and the shader draws the layer flat, then blends it // as one picture: where tile by tile each tile is blended over the @@ -879,7 +889,7 @@ export default class TMXLayer extends Renderable { if (this.blendMode !== "normal") { return { ok: false, - reason: `blend mode "${this.blendMode}" on an isometric layer`, + reason: `blend mode "${this.blendMode}" on ${kind} layer`, }; } const data = this.layerData; @@ -888,13 +898,13 @@ export default class TMXLayer extends Renderable { const gid = data[i]; if (gid !== 0 && (tileset === null || !tileset.contains(gid))) { if (tileset !== null) { - return { ok: false, reason: ISOMETRIC_ONE_TILESET }; + return { ok: false, reason: oneTilesetReason(this.orientation) }; } tileset = this.tilesets.getTilesetByGid(gid); } } if (tileset !== null) { - const reason = this._isometricTilesetReason(tileset); + const reason = this._overlappingTilesetReason(tileset); if (reason !== undefined) { return { ok: false, reason }; } @@ -908,27 +918,34 @@ export default class TMXLayer extends Renderable { } /** - * Why the isometric shader path cannot draw the tiles of a tileset, if - * it cannot. + * Why the shader path of an isometric, staggered or hexagonal map cannot + * draw the tiles of a tileset, if it cannot. * @param {TMXTileset} tileset - the tileset * @returns {string|undefined} the reason, or undefined when it can * @ignore * @internal */ - _isometricTilesetReason(tileset) { + _overlappingTilesetReason(tileset) { if (tileset.isCollection) { return "collection-of-image tileset"; } if (tileset.tilerendersize !== "tile") { return `tilerendersize "${tileset.tilerendersize}" not supported`; } - // the shader looks, for each pixel, at every cell whose tile image - // could cover it: a bounded number - if ( - tileset.tilewidth > this.tilewidth * ISOMETRIC_MAX_TILE_WIDTHS || - tileset.tileheight > this.tileheight * ISOMETRIC_MAX_TILE_HEIGHTS - ) { - return `tiles larger than the shader's limit (${ISOMETRIC_MAX_TILE_WIDTHS} grid widths by ${ISOMETRIC_MAX_TILE_HEIGHTS} grid heights)`; + // The shader looks, for each pixel, at every cell whose tile image + // could cover it: a bounded number, of lines of cells and of cells + // in a line. On an isometric map the lines are half a grid height + // apart and their cells a grid width; on the others, a row height + // and two column widths + const { MAX_ROWS, MAX_COLS } = TMX_ORIENTATIONS[this.orientation].limits; + const map = this.getRenderer(); + const isometric = this.orientation === "isometric"; + const maxWidth = + MAX_COLS * (isometric ? this.tilewidth : map.columnwidth * 2); + const maxHeight = + MAX_ROWS * (isometric ? this.tileheight / 2 : map.rowheight); + if (tileset.tilewidth > maxWidth || tileset.tileheight > maxHeight) { + return `tiles larger than the shader's limit (${maxWidth} by ${maxHeight} pixels on this grid)`; } return undefined; } @@ -937,9 +954,9 @@ export default class TMXLayer extends Renderable { * A tile set at run time on a layer on the shader path: one that path * cannot draw moves the layer back to the legacy renderer. * - * That is a tile flipped diagonally and not square, and on an isometric - * layer, which draws with one tileset, a tile of another one, or of one - * the path cannot draw. + * That is a tile flipped diagonally and not square, and on a layer of an + * isometric, staggered or hexagonal map, which draws with one tileset, a + * tile of another one, or of one the path cannot draw. * @param {number} gid - the gid of the tile just set * @param {number} flipMask - its flips * @ignore @@ -948,15 +965,15 @@ export default class TMXLayer extends Renderable { _keepShaderEligible(gid, flipMask) { let reason; if ( - this.orientation === "isometric" && + this.orientation !== "orthogonal" && this._shaderTileset?.contains(gid) !== true ) { if (this._shaderTileset) { - reason = ISOMETRIC_ONE_TILESET; + reason = oneTilesetReason(this.orientation); } else { // an empty layer takes its tileset from its first tile const tileset = this.tilesets.getTilesetByGid(gid); - reason = this._isometricTilesetReason(tileset); + reason = this._overlappingTilesetReason(tileset); if (reason === undefined) { this._shaderTileset = tileset; } diff --git a/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js b/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js index 4d1c27eb9f..63d1bf2935 100644 --- a/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js +++ b/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js @@ -379,12 +379,32 @@ export default class TMXHexagonalRenderer extends TMXRenderer { * @internal */ drawTileLayer(renderer, layer, rect) { + // The cells whose tile can reach the view. A tile stands on the + // bottom left of its cell, as on every map: one larger than the cell + // reaches up and to the right of it, so the cells that matter past + // the view are those to its left and below it, and a tileset's + // `tileoffset` moves its tiles either way. + const overX = Math.max( + (layer.maxTileSize?.width ?? this.tilewidth) - this.tilewidth, + 0, + ); + const overY = Math.max( + (layer.maxTileSize?.height ?? this.tileheight) - this.tileheight, + 0, + ); + const offsetX = layer.maxTileOffset?.x ?? 0; + const offsetY = layer.maxTileOffset?.y ?? 0; + const left = rect.pos.x - overX - offsetX; + const top = rect.pos.y - offsetY; + const right = rect.right + offsetX; + const bottom = rect.bottom + overY + offsetY; + // the tile at the top-left corner of the area, rounded down to its // column and row (a staggered map's renderer gives a point's place // within its tile too) const startTile = this.pixelToTileCoords( - rect.pos.x, - rect.pos.y, + left, + top, vector2dPool.get(), ).floorSelf(); @@ -403,8 +423,8 @@ export default class TMXHexagonalRenderer extends TMXRenderer { * up due to those tiles being visible as well. How we go up one row * depends on whether we're in the left or right half of the tile. */ - const inUpperHalf = rect.pos.y - startPos.y < this.sideoffsety; - const inLeftHalf = rect.pos.x - startPos.x < this.sideoffsetx; + const inUpperHalf = top - startPos.y < this.sideoffsety; + const inLeftHalf = left - startPos.x < this.sideoffsetx; if (inUpperHalf) { startTile.y--; @@ -452,11 +472,11 @@ export default class TMXHexagonalRenderer extends TMXRenderer { let staggeredRow = this.doStaggerX(startTile.x); // main drawing loop - for (; startPos.y < rect.bottom && startTile.y < endY; ) { + for (; startPos.y < bottom && startTile.y < endY; ) { rowTile.setV(startTile); rowPos.setV(startPos); - for (; rowPos.x < rect.right && rowTile.x < endX; rowTile.x += 2) { + for (; rowPos.x < right && rowTile.x < endX; rowTile.x += 2) { if (rowTile.x >= 0 && rowTile.y >= 0 && rowTile.y < layer.rows) { const idx = (rowTile.y * layerCols + rowTile.x) * 2; const gid = data[idx]; @@ -465,10 +485,14 @@ export default class TMXHexagonalRenderer extends TMXRenderer { if (!tilesetCache.contains(gid)) { tilesetCache = tilesets.getTilesetByGid(gid); } + // standing on its cell, and moved by its + // tileset's offset: where `drawTile` draws it tilesetCache.drawTileRaw( renderer, - rowPos.x, - rowPos.y, + tilesetCache.tileoffset.x + rowPos.x, + tilesetCache.tileoffset.y + + rowPos.y + + (this.tileheight - tilesetCache.tileheight), gid, flipMask, ); @@ -505,7 +529,7 @@ export default class TMXHexagonalRenderer extends TMXRenderer { } // main drawing loop - for (; startPos.y < rect.bottom && startTile.y < endY; startTile.y++) { + for (; startPos.y < bottom && startTile.y < endY; startTile.y++) { rowTile.setV(startTile); rowPos.setV(startPos); @@ -513,7 +537,7 @@ export default class TMXHexagonalRenderer extends TMXRenderer { rowPos.x += this.columnwidth; } - for (; rowPos.x < rect.right && rowTile.x < endX; rowTile.x++) { + for (; rowPos.x < right && rowTile.x < endX; rowTile.x++) { if (rowTile.x >= 0 && rowTile.y >= 0 && rowTile.y < layer.rows) { const idx = (rowTile.y * layerCols + rowTile.x) * 2; const gid = data[idx]; @@ -522,10 +546,14 @@ export default class TMXHexagonalRenderer extends TMXRenderer { if (!tilesetCache.contains(gid)) { tilesetCache = tilesets.getTilesetByGid(gid); } + // standing on its cell, and moved by its + // tileset's offset: where `drawTile` draws it tilesetCache.drawTileRaw( renderer, - rowPos.x, - rowPos.y, + tilesetCache.tileoffset.x + rowPos.x, + tilesetCache.tileoffset.y + + rowPos.y + + (this.tileheight - tilesetCache.tileheight), gid, flipMask, ); diff --git a/packages/melonjs/src/physics/world.js b/packages/melonjs/src/physics/world.js index d26e8eb103..601442e88d 100644 --- a/packages/melonjs/src/physics/world.js +++ b/packages/melonjs/src/physics/world.js @@ -216,18 +216,19 @@ export default class World extends Container { /** * Enable the GPU procedural shader path (WebGL 2 and WebGPU) for - * orthogonal and isometric tile layers. When `true` (default), + * orthogonal, isometric, staggered and hexagonal tile layers. When + * `true` (default), * eligible layers render via a single quad per tileset + a * fragment shader doing per-pixel GID lookup — bypassing the * per-tile drawImage loop entirely. * Supported features on the shader path: animated tiles, flipped * tiles, per-layer opacity and tint, and tiles larger than the * grid: up to 4 cells of overflow on an orthogonal map, up to 4 - * grid widths by 8 grid heights on an isometric one. - * Layers that don't qualify (Canvas, a staggered, hexagonal or - * oblique map, collection-of-image tilesets, larger tiles; on an - * orthogonal map a non-zero `tileoffset`, on an isometric one - * tiles of several tilesets in one layer) fall back to the legacy + * cells wide and 8 tall on the others. + * Layers that don't qualify (Canvas, an oblique map, + * collection-of-image tilesets, larger tiles; on an orthogonal map + * a non-zero `tileoffset`, on the others tiles of several tilesets + * in one layer) fall back to the legacy * path automatically. Set to `false` to disable globally. * @type {boolean} * @default true diff --git a/packages/melonjs/src/video/gpu/tmxlayer_orientation.js b/packages/melonjs/src/video/gpu/tmxlayer_orientation.js index 13b08fdbf1..0691c50798 100644 --- a/packages/melonjs/src/video/gpu/tmxlayer_orientation.js +++ b/packages/melonjs/src/video/gpu/tmxlayer_orientation.js @@ -33,6 +33,11 @@ export function createRegion() { sizeY: 0, overflowX: 0, overflowY: 0, + // a staggered or hexagonal map's: its column width and row height, + // and its stagger axis and index as the shader reads them + staggerX: 0, + staggerY: 0, + staggerMode: 0, }; } @@ -163,6 +168,73 @@ export function isometricRegion(layer, rect, tileset, out) { return true; } +/** + * The part of a staggered or hexagonal layer a view shows, for the tiles of + * one tileset: the two are one geometry (see `staggered-tmxlayer.frag`), + * the second with cells longer by its `hexsidelength`. + * + * As on an isometric map, the quad is the view, cut to where the tileset's + * tiles can be, and the shader works in the layer's pixels with the + * `tileoffset` taken off. + * @param {TMXLayer} layer - the layer + * @param {object} rect - the visible viewport rect (world coords) + * @param {object} tileset - the tileset whose tiles are drawn + * @param {object} out - the region to fill + * @returns {boolean} false when the view shows nothing of the layer + * @ignore + * @internal + */ +export function staggeredRegion(layer, rect, tileset, out) { + const map = layer.getRenderer(); + const columnWidth = map.columnwidth; + const rowHeight = map.rowheight; + const offsetX = tileset.tileoffset.x; + const offsetY = tileset.tileoffset.y; + const cols = layer.cols; + const rows = layer.rows; + + // The cells' boxes: lines a row height apart, and in each, cells two + // column widths apart, every other line shifted by one. A row of the map + // is one line, or two when the stagger axis is x + const lines = map.staggerX ? rows * 2 : rows; + // the last box of a line, in column widths from the first (of a shifted + // line, when the axis is y) + const lastBox = map.staggerX ? cols - 1 : cols * 2 - 1; + // where the tiles can be: from the left of the first box to the right of + // the last one's image, from the top of the first line's images to the + // bottom of the last line's boxes + const left = offsetX; + const right = lastBox * columnWidth + tileset.tilewidth + offsetX; + const top = layer.tileheight - tileset.tileheight + offsetY; + const bottom = (lines - 1) * rowHeight + layer.tileheight + offsetY; + + // the view, cut to it, on whole pixels + const x0 = Math.floor(Math.max(rect.pos.x, left)); + const y0 = Math.floor(Math.max(rect.pos.y, top)); + const x1 = Math.ceil(Math.min(rect.pos.x + rect.width, right)); + const y1 = Math.ceil(Math.min(rect.pos.y + rect.height, bottom)); + if (x1 <= x0 || y1 <= y0) { + return false; + } + + out.x = x0; + out.y = y0; + out.width = x1 - x0; + out.height = y1 - y0; + out.startX = x0 - offsetX; + out.startY = y0 - offsetY; + out.sizeX = out.width; + out.sizeY = out.height; + // how many cells' images reach a pixel: as many in a line as the image + // is wide in cell spacings, as many lines as it is tall in row heights + out.overflowX = Math.ceil(tileset.tilewidth / (columnWidth * 2)); + out.overflowY = Math.ceil(tileset.tileheight / rowHeight); + out.staggerX = columnWidth; + out.staggerY = rowHeight; + out.staggerMode = (map.staggerX ? 1 : 0) + (map.staggerEven ? 2 : 0); + return true; +} + /** * How many tiles a tileset's image has across: counted from the margin, a * tile plus the spacing apart, the last column going without its outer @@ -199,6 +271,37 @@ export function atlasRows(tileset) { ); } +/** + * What the orientations whose tiles overlap share: a layer is drawn with + * ONE tileset. A pass keeps the order of the tiles of one tileset, not + * across two (`layer._shaderTileset`, found by the layer when it is checked + * for this path). + */ +const ONE_TILESET = { + /** + * @param {TMXLayer} layer - the layer + * @param {object[]} scratch - an array to fill + * @returns {object[]} that tileset, or none for an empty layer + */ + tilesetsOf(layer, scratch) { + scratch.length = 0; + if (layer._shaderTileset) { + scratch.push(layer._shaderTileset); + } + return scratch; + }, + /** + * not a layer with no tileset to draw with: one that was never + * checked, or whose tiles come from several (`null`; an empty layer + * has `undefined`, and draws nothing) + * @param {TMXLayer} layer - the layer + * @returns {boolean} whether the layer can be drawn + */ + draws(layer) { + return layer._shaderTileset !== null; + }, +}; + /** * The map orientations the GPU tile layer renderers draw. * @ignore @@ -223,35 +326,24 @@ export const TMX_ORIENTATIONS = { }, isometric: { region: isometricRegion, - /** - * The one tileset an isometric layer draws with: its tiles overlap, - * and a pass keeps the order of the tiles of one tileset, not across - * two (`layer._shaderTileset`, found by the layer when it is checked - * for this path). - * @param {TMXLayer} layer - the layer - * @param {object[]} scratch - an array to fill - * @returns {object[]} that tileset, or none for an empty layer - */ - tilesetsOf(layer, scratch) { - scratch.length = 0; - if (layer._shaderTileset) { - scratch.push(layer._shaderTileset); - } - return scratch; - }, - /** - * not a layer with no tileset to draw with: one that was never - * checked, or whose tiles come from several (`null`; an empty layer - * has `undefined`, and draws nothing) - * @param {TMXLayer} layer - the layer - * @returns {boolean} whether the layer can be drawn - */ - draws(layer) { - return layer._shaderTileset !== null; - }, + ...ONE_TILESET, // the cells whose image can reach a pixel: rows of them, half a // grid height apart, and in each, cells a grid width apart. So // tiles up to MAX_ROWS / 2 grid heights tall, MAX_COLS wide limits: { MAX_ROWS: 16, MAX_COLS: 4 }, }, + // one geometry for both: lines of cells, a row height apart, and in + // each, cells two column widths apart + staggered: { + region: staggeredRegion, + ...ONE_TILESET, + staggered: true, + limits: { MAX_ROWS: 16, MAX_COLS: 4 }, + }, + hexagonal: { + region: staggeredRegion, + ...ONE_TILESET, + staggered: true, + limits: { MAX_ROWS: 16, MAX_COLS: 4 }, + }, }; diff --git a/packages/melonjs/src/video/webgl/renderers/tmxlayer/tmxlayer.js b/packages/melonjs/src/video/webgl/renderers/tmxlayer/tmxlayer.js index 0ad3e0fbf0..c2cb1d7017 100644 --- a/packages/melonjs/src/video/webgl/renderers/tmxlayer/tmxlayer.js +++ b/packages/melonjs/src/video/webgl/renderers/tmxlayer/tmxlayer.js @@ -8,6 +8,7 @@ import { BufferTextureResource } from "../../../texture/resource.js"; import GLShader from "../../glshader.js"; import isometricFragment from "../../shaders/isometric-tmxlayer.frag"; import orthogonalFragment from "../../shaders/orthogonal-tmxlayer.frag"; +import staggeredFragment from "../../shaders/staggered-tmxlayer.frag"; import tmxLayerVertex from "../../shaders/tmxlayer.vert"; import tmxLayerCommonFragment from "../../shaders/tmxlayer-common.frag"; @@ -21,6 +22,9 @@ import tmxLayerCommonFragment from "../../shaders/tmxlayer-common.frag"; const FRAGMENTS = { orthogonal: orthogonalFragment, isometric: isometricFragment, + // one geometry: a hexagonal map's cells are longer, no more + staggered: staggeredFragment, + hexagonal: staggeredFragment, }; // Default `uTint` when the layer has no tint set. Module-scoped so the @@ -435,6 +439,10 @@ export default class TMXLayerGPURenderer { this._setV2("uVisibleStart", region.startX, region.startY); this._setV2("uVisibleSize", region.sizeX, region.sizeY); this._setV2("uOverflow", region.overflowX, region.overflowY); + if (this.orientation.staggered === true) { + this._setV2("uStagger", region.staggerX, region.staggerY); + this.shader.setUniform("uStaggerMode", region.staggerMode); + } this._setV2("uGidRange", tileset.firstgid, tileset.lastgid); // the tileset samples its mipmaps when the batcher binds it a // mip sampler: the "linear-mipmap" filter, on a linear texture diff --git a/packages/melonjs/src/video/webgl/shaders/staggered-tmxlayer.frag b/packages/melonjs/src/video/webgl/shaders/staggered-tmxlayer.frag new file mode 100644 index 0000000000..d3324e176e --- /dev/null +++ b/packages/melonjs/src/video/webgl/shaders/staggered-tmxlayer.frag @@ -0,0 +1,85 @@ +// The staggered and hexagonal part of the TMX layer fragment shader: +// appended to `tmxlayer-common.frag`. +// +// Both maps are one geometry, in the layer's pixels (the renderer takes the +// tileset's `tileoffset` off before a position gets here). Cells stand in +// lines, a `rowheight` apart down the screen; in a line they are two +// `columnwidth` apart, and every other line is shifted by one `columnwidth` +// (which ones, `staggerindex` says). A hexagonal map's cells are longer by +// its `hexsidelength`, which is in those two lengths already. +// +// What `staggeraxis` changes is which cell a place in a line is: +// "y": a line is a row of the map, and its cells are that row's columns; +// "x": a row of the map is two lines, its columns alternating between +// them, so a line holds every other column of one row. +// +// The tile of a cell is drawn as its whole image, standing on the bottom +// left of the cell. So the cells whose image covers a fragment are a run of +// lines, as long as the image is tall in row heights, and in each a run of +// cells, as long as it is wide in cell spacings: `uOverflow` holds the two +// lengths. The CPU renderer draws the lines from the top of the screen +// down, each from the left: the scan here goes the other way, and puts each +// texel behind the ones before it until the fragment is opaque. +// +// `MAX_ROWS` and `MAX_COLS`, the bounds of the two runs, are declared ahead +// of this file (see `TMX_ORIENTATIONS` in `gpu/tmxlayer_orientation.js`): a +// layer of larger tiles is left to the CPU renderer. + +uniform vec2 uStagger; // (columnwidth, rowheight) +uniform float uStaggerMode; // 1 when the stagger axis is x, + 2 when the index is even + +// whether a line is one of the shifted ones: 1, or 0 +int shifted(int line) { + return (line & 1) ^ (int(uStaggerMode) >> 1); +} + +// the bottom left of the cell's box, less the tile's height +vec2 tileOrigin(int cx, int cy) { + bool staggerX = (int(uStaggerMode) & 1) == 1; + // the cell's line, and how far along it the cell is + int line = staggerX ? 2 * cy + shifted(cx) : cy; + float x = staggerX + ? float(cx) * uStagger.x + : float(2 * cx + shifted(cy)) * uStagger.x; + return vec2(x, float(line) * uStagger.y + uCellSize.y - uTileSize.y); +} + +void main(void) { + vec2 worldPx = uVisibleStart + vRegion * uVisibleSize; + Footprint fp = footprint(worldPx); + + bool staggerX = (int(uStaggerMode) & 1) == 1; + float spacing = uStagger.x * 2.0; + // the lowest line whose image can reach the fragment + int lineMax = int(floor((worldPx.y - uCellSize.y + uTileSize.y) / uStagger.y)); + int spanK = int(uOverflow.x + 0.5); + int spanL = int(uOverflow.y + 0.5); + // lines past the map's last one, or before its first, hold no cell + int lineLast = (staggerX ? 2 * int(uMapSize.y) : int(uMapSize.y)) - 1; + + vec4 result = vec4(0.0); + vec4 texel; + bool opaque = false; + for (int j = max(lineMax - lineLast, 0); j < MAX_ROWS; j++) { + int line = lineMax - j; + if (j >= spanL || line < 0) break; + int shift = shifted(line); + // the rightmost cell of the line whose image can reach the fragment + int kMax = int(floor((worldPx.x - float(shift) * uStagger.x) / spacing)); + for (int i = 0; i < MAX_COLS; i++) { + if (i >= spanK) break; + int k = kMax - i; + int cx = staggerX ? 2 * k + shift : k; + int cy = staggerX ? line >> 1 : line; + if (tryRenderCell(cx, cy, worldPx, fp, texel)) { + if (under(result, texel)) { + opaque = true; + break; + } + } + } + if (opaque) break; + } + if (result.a <= 0.0) discard; + fragColor = layerColor(result); +} diff --git a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js index a0ccbd8eaf..750487c91e 100644 --- a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js +++ b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js @@ -9,6 +9,7 @@ import { import tmxLayerCommonWGSL from "../../shaders/tmxlayer-common.wgsl"; import isometricWGSL from "../../shaders/tmxlayer-isometric.wgsl"; import orthogonalWGSL from "../../shaders/tmxlayer-orthogonal.wgsl"; +import staggeredWGSL from "../../shaders/tmxlayer-staggered.wgsl"; /** * additional imports for TypeScript @@ -30,6 +31,9 @@ const TMX_UNIFORM_SIZE = 128; const ORIENTATION_WGSL = { orthogonal: orthogonalWGSL, isometric: isometricWGSL, + // one geometry: a hexagonal map's cells are longer, no more + staggered: staggeredWGSL, + hexagonal: staggeredWGSL, }; /** @@ -460,6 +464,11 @@ export default class TMXLayerGPURenderer { const mipmaps = filter === "linear" && renderer.wantsMipmaps(tileset.texture); scratch[28] = mipmaps ? 1 : 0; + // a staggered or hexagonal map's (0 on the others, which have + // no use for them) + scratch[29] = region.staggerMode; + scratch[30] = region.staggerX; + scratch[31] = region.staggerY; const uniforms = renderer.effectUniformArena.alloc( TMX_UNIFORM_SIZE, device.limits?.minUniformBufferOffsetAlignment ?? 256, diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl index 5cc80843d8..711d52a2b3 100644 --- a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl @@ -41,6 +41,10 @@ struct TMXUniforms { uAnimSize : f32, // entries in the anim lookup, 0 when disabled uTint : vec4f, uMipmaps : f32, // 1 under the "linear-mipmap" filter, else 0 + // a staggered or hexagonal map's (see tmxlayer-staggered.wgsl); they + // fill what was the block's padding + uStaggerMode : f32, // 1 when the stagger axis is x, + 2 when the index is even + uStagger : vec2f, // (columnwidth, rowheight) }; @group(0) @binding(0) var uFrame : FrameUniforms; diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-staggered.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-staggered.wgsl new file mode 100644 index 0000000000..90d06b8939 --- /dev/null +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-staggered.wgsl @@ -0,0 +1,83 @@ +// The staggered and hexagonal part of the TMX layer shader: appended to +// tmxlayer-common.wgsl. The WGSL port of staggered-tmxlayer.frag, where the +// geometry is explained. +// +// Cells stand in lines, `tmx.uStagger.y` (a row height) apart down the +// screen; in a line they are two `tmx.uStagger.x` (a column width) apart, +// and every other line is shifted by one. With the stagger axis "y" a line +// is a row of the map; with "x" a row is two lines, its columns alternating +// between them. +// +// `MAX_ROWS` and `MAX_COLS`, the bounds of the two runs, are declared ahead +// of this file (see `TMX_ORIENTATIONS` in gpu/tmxlayer_orientation.js). + +// whether a line is one of the shifted ones: 1, or 0 +fn shifted(line : i32) -> i32 { + return (line & 1) ^ (i32(tmx.uStaggerMode) >> 1u); +} + +// the bottom left of the cell's box, less the tile's height +fn tileOrigin(cx : i32, cy : i32) -> vec2f { + let staggerX = (i32(tmx.uStaggerMode) & 1) == 1; + var line = cy; + var x = f32(2 * cx + shifted(cy)) * tmx.uStagger.x; + if (staggerX) { + line = 2 * cy + shifted(cx); + x = f32(cx) * tmx.uStagger.x; + } + return vec2f(x, f32(line) * tmx.uStagger.y + tmx.uCellSize.y - tmx.uTileSize.y); +} + +@fragment +fn fragment_main(in : VSOut) -> @location(0) vec4f { + let worldPx = tmx.uVisibleStart + in.vRegion * tmx.uVisibleSize; + let fp = footprint(worldPx); + + let staggerX = (i32(tmx.uStaggerMode) & 1) == 1; + let spacing = tmx.uStagger.x * 2.0; + // the lowest line whose image can reach the fragment + let lineMax = i32(floor((worldPx.y - tmx.uCellSize.y + tmx.uTileSize.y) / tmx.uStagger.y)); + let spanK = i32(tmx.uOverflow.x + 0.5); + let spanL = i32(tmx.uOverflow.y + 0.5); + // lines past the map's last one, or before its first, hold no cell + var lineLast = i32(tmx.uMapSize.y) - 1; + if (staggerX) { + lineLast = 2 * i32(tmx.uMapSize.y) - 1; + } + + var result = vec4f(0.0); + var opaque = false; + for (var j = max(lineMax - lineLast, 0); j < MAX_ROWS; j++) { + let line = lineMax - j; + if (j >= spanL || line < 0) { + break; + } + let shift = shifted(line); + // the rightmost cell of the line whose image can reach the fragment + let kMax = i32(floor((worldPx.x - f32(shift) * tmx.uStagger.x) / spacing)); + for (var i = 0; i < MAX_COLS; i++) { + if (i >= spanK) { + break; + } + let k = kMax - i; + var cx = k; + var cy = line; + if (staggerX) { + cx = 2 * k + shift; + cy = line >> 1u; + } + let candidate = tryRenderCell(cx, cy, worldPx, fp); + if (candidate.hit && under(&result, candidate.color)) { + opaque = true; + break; + } + } + if (opaque) { + break; + } + } + if (result.a <= 0.0) { + discard; + } + return layerColor(result); +} diff --git a/packages/melonjs/tests/helpers/tile-layer-compare.js b/packages/melonjs/tests/helpers/tile-layer-compare.js index 3b13bba0e9..2c5afa8521 100644 --- a/packages/melonjs/tests/helpers/tile-layer-compare.js +++ b/packages/melonjs/tests/helpers/tile-layer-compare.js @@ -9,7 +9,7 @@ * edges (the default) the two paths are held to the same picture, exactly; * with soft ones (`alpha`), to a rounding step or two. */ -import { vi } from "vitest"; +import { expect, vi } from "vitest"; import { Application, boot, TMXTileMap, timer } from "../../src/index.js"; import { imgList } from "../../src/loader/cache.js"; import Renderer from "../../src/video/renderer.js"; @@ -199,8 +199,10 @@ export function makeMap(options) { * each from the left), where Tiled draws them, with nothing left out for * being out of view. What both of the engine's renderers are held to. * - * Tiles are drawn whole, bottom-aligned on their cell and moved by their - * tileset's `tileoffset`; flipped across, down or along the diagonal (the + * Orthogonal, isometric, staggered and hexagonal maps (the last two take + * their `staggeraxis`, `staggerindex` and `hexsidelength` from the scene's + * `extra`). Tiles are drawn whole, bottom-aligned on their cell and moved by + * their tileset's `tileoffset`; flipped across, down or along the diagonal (the * last on square tiles only, where a flip is a quarter turn in place). * A layer is moved by its offset in Tiled (`offsetx`, `offsety`). Layer * opacity and tint are for the caller to apply to the picture. @@ -298,6 +300,40 @@ export function reference(scene, viewX, viewY, width, height) { oy + ((col + row + 2) * th) / 2, ); } + } else if (orientation === "staggered" || orientation === "hexagonal") { + // Every other row (or column, with `staggeraxis` "x") is + // shifted by half a cell; a hexagonal map's cells are longer + // by `hexsidelength` along the stagger axis. Tiles are drawn + // from the top of the screen down, each line from the left. + const { staggeraxis, staggerindex, hexsidelength = 0 } = scene.extra; + const staggerX = staggeraxis === "x"; + const even = staggerindex === "even" ? 1 : 0; + const side = orientation === "hexagonal" ? hexsidelength : 0; + const sideX = staggerX ? side : 0; + const sideY = staggerX ? 0 : side; + const columnWidth = (tw - sideX) / 2 + sideX; + const rowHeight = (th - sideY) / 2 + sideY; + const place = ({ col, row }) => { + // `& 1` and not `% 2`: an infinite map has negative cells + return staggerX + ? { + x: col * columnWidth, + y: row * (th + sideY) + ((col & 1) ^ even) * rowHeight, + } + : { + x: col * (tw + sideX) + ((row & 1) ^ even) * columnWidth, + y: row * rowHeight, + }; + }; + const placed = ordered.map((cell) => { + return { ...cell, ...place(cell) }; + }); + placed.sort((a, b) => { + return a.y - b.y || a.x - b.x; + }); + for (const { gid, x, y } of placed) { + drawTile(gid, ox + x, oy + y + th); + } } else { ordered.sort((a, b) => { return a.row - b.row || a.col - b.col; @@ -472,3 +508,83 @@ export function drawn(frame) { } return count; } + +/** views over a map: inside, over each edge, and scrolled part way */ +const DEFAULT_VIEWS = [ + [0, 0], + [70, 20], + [-40, -80], + [230, 110], + [-150, 30], + [100, -200], +]; + +/** + * Draw a scene both ways from each view, and expect the reference + * picture from both, the GPU path drawing alone. + * @param {Application} app - the application, from {@link makeApp} + * @param {object} scene - what {@link makeMap} takes + * @param {object} [options] + * @param {number[][]} [options.views] - where to look from + * @param {object[]} [options.layers] - the scene's layers, when + * they are in the world already (and were changed there). Without + * them the scene is put in the world here, and every layer of it is + * expected to take the GPU path of its own accord + * @param {boolean} [options.oracle=true] - false where the reference + * does not reach (a zoomed camera): the two paths are then compared + * with each other alone + * @param {(picture: ImageData) => ImageData} [options.expected] + * what the reference becomes (a faded or tinted layer) + * @param {number} [options.tolerance=0] - the largest difference of a + * channel that is not counted + * @param {number} [options.share=0] - the largest share of the pixels + * drawn that the two paths may differ by + * @returns {Promise} how many pixels were drawn, in all + */ +export async function comparePaths(app, scene, options = {}) { + const { + views = DEFAULT_VIEWS, + oracle = true, + expected, + tolerance = 0, + share = 0, + } = options; + const { width, height } = app.viewport; + let layers = options.layers; + if (layers === undefined) { + layers = show(app, makeMap(scene)); + for (const layer of layers) { + expect(layer.renderMode, layer.name).toBe("shader"); + } + } + let total = 0; + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + const cpu = await drawIn(app, layers, "perTile"); + const gpu = await drawIn(app, layers, "shader"); + // a layer out of view is not drawn at all; one in view is + // drawn tile by tile by the CPU path, and never by the GPU one + expect(cpu.cpuDraws).toBeLessThanOrEqual(layers.length); + expect(gpu.cpuDraws, "the GPU path reaches no CPU renderer").toBe(0); + const at = `view (${x}, ${y})`; + const count = drawn(gpu.frame); + const allowed = Math.floor(count * share); + if (oracle) { + let ref = reference(scene, x, y, width, height); + if (expected !== undefined) { + ref = expected(ref); + } + const c = diff(ref, cpu.frame, tolerance); + expect(c.different, `${at}, CPU path: ${c.first}`).toBe(0); + const g = diff(ref, gpu.frame, tolerance); + expect(g.different, `${at}, GPU path: ${g.first}`).toBe(0); + } + const d = diff(cpu.frame, gpu.frame, tolerance); + expect( + d.different, + `${at}, CPU against GPU: ${d.first}`, + ).toBeLessThanOrEqual(allowed); + total += count; + } + return total; +} diff --git a/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js b/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js index 7debb5bf20..1764611e2e 100644 --- a/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js +++ b/packages/melonjs/tests/helpers/tile-layer-isometric-scenarios.js @@ -16,6 +16,7 @@ import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; import { grabFrame } from "./pixels.js"; import { + comparePaths, diff, drawIn, drawn, @@ -158,72 +159,8 @@ export function isometricScenarios(renderer, type) { } }; - /** - * Draw a scene both ways from each view, and expect the reference - * picture from both, the GPU path drawing alone. - * @param {object} scene - what `makeMap` takes - * @param {object} [options] - * @param {number[][]} [options.views] - where to look from - * @param {object[]} [options.layers] - the scene's layers, when - * they are in the world already (and were changed there). Without - * them the scene is put in the world here, and every layer of it is - * expected to take the GPU path of its own accord - * @param {boolean} [options.oracle=true] - false where the reference - * does not reach (a zoomed camera): the two paths are then compared - * with each other alone - * @param {(picture: ImageData) => ImageData} [options.expected] - * what the reference becomes (a faded or tinted layer) - * @param {number} [options.tolerance=0] - the largest difference of a - * channel that is not counted - * @param {number} [options.share=0] - the largest share of the pixels - * drawn that the two paths may differ by - * @returns {Promise} how many pixels were drawn, in all - */ - const same = async (scene, options = {}) => { - const { - views = VIEWS, - oracle = true, - expected, - tolerance = 0, - share = 0, - } = options; - let layers = options.layers; - if (layers === undefined) { - layers = show(app, makeMap(scene)); - for (const layer of layers) { - expect(layer.renderMode, layer.name).toBe("shader"); - } - } - let total = 0; - for (const [x, y] of views) { - app.viewport.moveTo(x, y); - const cpu = await drawIn(app, layers, "perTile"); - const gpu = await drawIn(app, layers, "shader"); - // a layer out of view is not drawn at all; one in view is - // drawn tile by tile by the CPU path, and never by the GPU one - expect(cpu.cpuDraws).toBeLessThanOrEqual(layers.length); - expect(gpu.cpuDraws, "the GPU path reaches no CPU renderer").toBe(0); - const at = `view (${x}, ${y})`; - const count = drawn(gpu.frame); - const allowed = Math.floor(count * share); - if (oracle) { - let ref = reference(scene, x, y, WIDTH, HEIGHT); - if (expected !== undefined) { - ref = expected(ref); - } - const c = diff(ref, cpu.frame, tolerance); - expect(c.different, `${at}, CPU path: ${c.first}`).toBe(0); - const g = diff(ref, gpu.frame, tolerance); - expect(g.different, `${at}, GPU path: ${g.first}`).toBe(0); - } - const d = diff(cpu.frame, gpu.frame, tolerance); - expect( - d.different, - `${at}, CPU against GPU: ${d.first}`, - ).toBeLessThanOrEqual(allowed); - total += count; - } - return total; + const same = (scene, options) => { + return comparePaths(app, scene, options); }; /** how a layer of the scene ends up drawn, and what was warned */ @@ -1180,22 +1117,6 @@ export function isometricScenarios(renderer, type) { expect(drawn(forced.frame)).toBeGreaterThan(0); }); - it("a staggered map, still", (ctx) => { - need(ctx); - const [mode, said] = modeOf({ - orientation: "staggered", - cols: 4, - rows: 4, - tw: TW, - th: TH, - tilesets: [blocks()], - layers: [{ data: scattered(4, 4) }], - extra: { staggeraxis: "y", staggerindex: "odd" }, - }); - expect(mode).toBe("perTile"); - expect(said).toMatch(/no gpu renderer supported yet for "staggered"/); - }); - it("any layer, with gpuTilemap off", (ctx) => { need(ctx); app.world.gpuTilemap = false; diff --git a/packages/melonjs/tests/helpers/tile-layer-staggered-scenarios.js b/packages/melonjs/tests/helpers/tile-layer-staggered-scenarios.js new file mode 100644 index 0000000000..a78cb54f47 --- /dev/null +++ b/packages/melonjs/tests/helpers/tile-layer-staggered-scenarios.js @@ -0,0 +1,536 @@ +/** + * Staggered and hexagonal tile layers, held to the picture Tiled draws: the + * scenes, and for a GPU backend the same comparison as the isometric ones + * (`tile-layer-isometric-scenarios.js`): the CPU renderer, the GPU one and + * a reference painted with no engine, pixel by pixel. + * + * Both orientations are one geometry: lines of cells, every other one + * shifted by half a cell, along the map's rows (`staggeraxis` "y") or its + * columns ("x"); a hexagonal map's cells are longer by `hexsidelength`. + */ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { + comparePaths, + diff, + drawIn, + drawn, + FLIP_AD, + FLIP_H, + FLIP_V, + fill, + inBlock, + makeApp, + makeMap, + makeTileset, + reference, + show, +} from "./tile-layer-compare.js"; + +export const WIDTH = 320; +export const HEIGHT = 240; + +const COLS = 9; +const ROWS = 11; + +/** most cells filled, a few holes */ +const scattered = (cols = COLS, rows = ROWS, firstgid = 1) => { + return fill(cols, rows, (c, r) => { + return (c * 3 + r) % 7 === 0 ? 0 : firstgid + ((c + r * 2) % 8); + }); +}; + +/** the grids: a staggered map's diamonds, and hexagons either way up */ +const GRIDS = [ + { orientation: "staggered", tw: 64, th: 32 }, + { orientation: "hexagonal", tw: 28, th: 24, hexsidelength: 12 }, +]; + +/** + * A staggered or hexagonal scene. + * @param {object} grid - one of `GRIDS` + * @param {string} staggeraxis - "x" or "y" + * @param {string} staggerindex - "odd" or "even" + * @param {object} tileset - from `makeTileset` + * @param {object} [more] - what else the scene has (its layers, by default + * one of `scattered` cells) + * @returns {object} the scene, for `makeMap` and `reference` + */ +export const staggeredScene = ( + grid, + staggeraxis, + staggerindex, + tileset, + more = {}, +) => { + const { orientation, tw, th, hexsidelength } = grid; + return { + orientation, + cols: COLS, + rows: ROWS, + tw, + th, + tilesets: [tileset], + layers: [{ data: scattered() }], + ...more, + extra: { staggeraxis, staggerindex, hexsidelength, ...more.extra }, + }; +}; + +/** the kinds of tiles each grid is drawn with */ +const TILES = [ + { + label: "tiles the size of a cell, filling it: every overlap shows", + make: ({ tw, th }) => { + return makeTileset({ tw, th }); + }, + }, + { + label: "tiles taller than a cell, standing on it", + make: ({ tw, th }) => { + return makeTileset({ + tw, + th: th * 3, + opaque: (x, y) => { + return inBlock(x, y, tw, th * 3, tw, th); + }, + }); + }, + }, + { + label: "tiles wider and taller than a cell, moved by a tileoffset", + make: ({ tw, th }) => { + return makeTileset({ + tw: tw + 20, + th: th * 2 + 6, + opaque: (x, y) => { + return (x + y) % 11 !== 0; + }, + extra: { tileoffset: { x: -6, y: 10 } }, + }); + }, + }, +]; + +/** every grid, either axis, either index, each kind of tiles */ +export const STAGGERED_SCENES = []; +for (const grid of GRIDS) { + for (const staggeraxis of ["y", "x"]) { + for (const staggerindex of ["odd", "even"]) { + for (const tiles of TILES) { + STAGGERED_SCENES.push({ + label: `${grid.orientation}, axis ${staggeraxis}, ${staggerindex}: ${tiles.label}`, + scene: () => { + return staggeredScene( + grid, + staggeraxis, + staggerindex, + tiles.make(grid), + ); + }, + }); + } + } + } +} + +export const STAGGERED_VIEWS = [ + [0, 0], + [70, 20], + [-40, -80], + [230, 110], + [-150, 30], + [100, -200], + [33, 57], +]; + +/** + * Declare the scenarios for one GPU backend. + * @param {number} renderer - `video.WEBGL` or `video.WEBGPU` + * @param {string} type - the `renderer.type` expected ("WebGL", "WebGPU") + */ +export function staggeredScenarios(renderer, type) { + describe(`staggered and hexagonal tile layers on the GPU, against the CPU renderer and a reference (${type})`, () => { + let app; + beforeAll(async () => { + app = await makeApp(renderer, type, WIDTH, HEIGHT); + }); + afterAll(() => { + app?.destroy(); + vi.restoreAllMocks(); + }); + + const need = (ctx) => { + if (app === null || app === undefined) { + ctx.skip(`${type} not available in this environment`); + } + }; + const same = (scene, options = {}) => { + return comparePaths(app, scene, { views: STAGGERED_VIEWS, ...options }); + }; + const modeOf = (scene) => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + const layer = show(app, makeMap(scene))[0]; + return [layer.renderMode, warn.mock.calls.flat().join(" ")]; + } finally { + warn.mockRestore(); + } + }; + const [STAGGERED, HEXAGONAL] = GRIDS; + const blocks = (grid, tall = 2, options = {}) => { + return makeTileset({ + tw: grid.tw, + th: grid.th * tall, + opaque: (x, y) => { + return inBlock(x, y, grid.tw, grid.th * tall, grid.tw, grid.th); + }, + ...options, + }); + }; + + describe("every grid, either way", () => { + for (const { label, scene } of STAGGERED_SCENES) { + it(label, async (ctx) => { + need(ctx); + expect(await same(scene())).toBeGreaterThan(0); + }); + } + }); + + describe("what a tile and a layer can be", () => { + it("tiles with soft edges, and see-through: each shows the tiles behind it", async (ctx) => { + need(ctx); + for (const grid of GRIDS) { + const tileset = makeTileset({ + tw: grid.tw, + th: grid.th * 2, + alpha: (x, y, id) => { + const edge = Math.min(x, y, grid.tw - 1 - x, grid.th * 2 - 1 - y); + const a = id % 3 === 2 ? 110 : 255; + return Math.round(a * Math.min(1, (edge + 1) / 4)); + }, + }); + for (const axis of ["y", "x"]) { + expect( + await same(staggeredScene(grid, axis, "odd", tileset), { + tolerance: 3, + }), + `${grid.orientation}, axis ${axis}`, + ).toBeGreaterThan(0); + } + } + }); + + it("tiles flipped, every way", async (ctx) => { + need(ctx); + const flips = [ + 0, + FLIP_H, + FLIP_V, + FLIP_H + FLIP_V, + FLIP_AD, + FLIP_H + FLIP_AD, + FLIP_V + FLIP_AD, + FLIP_H + FLIP_V + FLIP_AD, + ]; + // square tiles: a diagonal flip turns the image a quarter + const tileset = makeTileset({ + tw: 64, + th: 64, + opaque: (x, y) => { + return x + y > 20; + }, + }); + const scene = staggeredScene(STAGGERED, "y", "odd", tileset, { + layers: [ + { + data: fill(COLS, ROWS, (c, r) => { + return 1 + ((c + r) % 8) + flips[(c * 3 + r) % 8]; + }), + }, + ], + }); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("animated tiles, as time passes", async (ctx) => { + need(ctx); + const tileset = blocks(STAGGERED, 2, { + extra: { + tiles: [ + { + id: 0, + animation: [ + { tileid: 0, duration: 100 }, + { tileid: 5, duration: 100 }, + ], + }, + ], + }, + }); + const layers = show( + app, + makeMap(staggeredScene(STAGGERED, "x", "even", tileset)), + ); + expect(layers[0].renderMode).toBe("shader"); + app.viewport.moveTo(0, 0); + const frames = []; + for (let step = 0; step < 4; step++) { + const cpu = await drawIn(app, layers, "perTile", 60); + const gpu = await drawIn(app, layers, "shader"); + expect(gpu.cpuDraws).toBe(0); + const d = diff(cpu.frame, gpu.frame); + expect(d.different, `step ${step}: ${d.first}`).toBe(0); + frames.push(gpu.frame); + } + expect(diff(frames[0], frames[2]).different).toBeGreaterThan(0); + }); + + it("a layer offset in Tiled, and moved in the game", async (ctx) => { + need(ctx); + for (const grid of GRIDS) { + const scene = staggeredScene(grid, "y", "even", blocks(grid), { + layers: [{ data: scattered(), offsetx: 37, offsety: -21 }], + }); + const layers = show(app, makeMap(scene)); + expect(await same(scene, { layers })).toBeGreaterThan(0); + layers[0].pos.x += 50; + layers[0].pos.y += 64; + scene.layers[0].offsetx += 50; + scene.layers[0].offsety += 64; + expect(await same(scene, { layers })).toBeGreaterThan(0); + } + }); + + it("two layers, each on a tileset of its own", async (ctx) => { + need(ctx); + const scene = staggeredScene( + STAGGERED, + "y", + "odd", + blocks(STAGGERED, 1), + { + tilesets: [blocks(STAGGERED, 1), blocks(STAGGERED, 3)], + layers: [ + { data: scattered() }, + { + data: fill(COLS, ROWS, (c, r) => { + return (c + r) % 3 === 0 ? 9 + ((c * 2 + r) % 8) : 0; + }), + }, + ], + }, + ); + expect(await same(scene)).toBeGreaterThan(0); + }); + + it("a layer of an infinite map, starting left of and above the origin", async (ctx) => { + need(ctx); + const chunk = (x, y) => { + return { + x, + y, + width: 4, + height: 4, + data: fill(4, 4, (c, r) => { + return (c + r + x) % 5 === 0 ? 0 : 1 + ((c + r * 2) % 8); + }), + }; + }; + for (const grid of GRIDS) { + for (const axis of ["y", "x"]) { + const scene = staggeredScene(grid, axis, "odd", blocks(grid), { + cols: 8, + rows: 8, + layers: [ + { + startx: -4, + starty: -4, + width: 12, + height: 8, + data: undefined, + chunks: [ + chunk(-4, -4), + chunk(0, -4), + chunk(4, 0), + chunk(0, 0), + ], + }, + ], + extra: { infinite: true }, + }); + const layers = show(app, makeMap(scene)); + expect(layers[0].renderMode).toBe("shader"); + const views = [ + [0, 0], + [-200, -150], + [150, -60], + ]; + expect( + await same(scene, { views, layers }), + `${grid.orientation}, axis ${axis}`, + ).toBeGreaterThan(0); + } + } + }); + + it("tiles set and cleared while the game runs", async (ctx) => { + need(ctx); + for (const grid of GRIDS) { + const scene = staggeredScene(grid, "x", "odd", blocks(grid, 3)); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + const options = { views: [[0, 0]], layers }; + for (let step = 0; step < 3; step++) { + for (let row = 0; row < ROWS; row++) { + for (let col = 0; col < COLS; col++) { + const gid = + (col + row + step) % 4 === 0 + ? 0 + : 1 + ((col * step + row) % 8); + scene.layers[0].data[row * COLS + col] = gid; + if (gid === 0) { + layer.clearTile(col, row); + } else { + layer.setTile(layer.getTileById(gid, col, row), col, row); + } + } + } + await same(scene, options); + } + expect(layer.renderMode).toBe("shader"); + } + }); + + it("views that hold only what reaches past the grid", async (ctx) => { + need(ctx); + for (const grid of GRIDS) { + // tiles four cells tall stand three cells above the grid + const scene = staggeredScene(grid, "y", "odd", blocks(grid, 4), { + layers: [ + { + data: fill(COLS, ROWS, (c, r) => { + return 1 + ((c + r * 2) % 8); + }), + }, + ], + }); + const views = [ + [0, -HEIGHT - 5], + [60, -HEIGHT - grid.th], + ]; + expect( + await same(scene, { views }), + grid.orientation, + ).toBeGreaterThan(0); + } + }); + }); + + describe("layers the GPU path does not take", () => { + it("tiles of two tilesets in one layer", (ctx) => { + need(ctx); + for (const grid of GRIDS) { + const [mode, said] = modeOf( + staggeredScene(grid, "y", "odd", blocks(grid), { + tilesets: [blocks(grid), blocks(grid, 3)], + layers: [ + { + data: fill(COLS, ROWS, (c, r) => { + return 1 + ((c + r) % 16); + }), + }, + ], + }), + ); + expect(mode).toBe("perTile"); + expect(said).toMatch( + new RegExp( + `more than one tileset in an? ${grid.orientation} layer`, + ), + ); + } + }); + + it("tiles past the size the shader walks", (ctx) => { + need(ctx); + const [mode, said] = modeOf( + staggeredScene(STAGGERED, "y", "odd", blocks(STAGGERED, 9)), + ); + expect(mode).toBe("perTile"); + expect(said).toMatch(/larger than the shader's limit/); + }); + + it("a grid whose cells stand half a pixel off", (ctx) => { + need(ctx); + // a hexagon side of odd length: every other column is half a + // pixel off + const grid = { + orientation: "hexagonal", + tw: 28, + th: 24, + hexsidelength: 11, + }; + const [mode, said] = modeOf( + staggeredScene(grid, "x", "odd", makeTileset({ tw: 28, th: 24 })), + ); + expect(mode).toBe("perTile"); + expect(said).toMatch(/odd size/); + }); + + it("a tile of another tileset set while the game runs", (ctx) => { + need(ctx); + const scene = staggeredScene(HEXAGONAL, "y", "odd", blocks(HEXAGONAL), { + tilesets: [blocks(HEXAGONAL), blocks(HEXAGONAL, 2)], + }); + const layer = show(app, makeMap(scene))[0]; + expect(layer.renderMode).toBe("shader"); + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + layer.setTile(layer.getTileById(10, 2, 2), 2, 2); + expect(layer.renderMode).toBe("perTile"); + expect(warn.mock.calls.flat().join(" ")).toMatch( + /more than one tileset/, + ); + } finally { + warn.mockRestore(); + } + }); + + it("an oblique map, still", (ctx) => { + need(ctx); + const [mode, said] = modeOf({ + orientation: "oblique", + cols: 4, + rows: 4, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [{ data: scattered(4, 4) }], + extra: { skewx: 10, skewy: 0 }, + }); + expect(mode).toBe("perTile"); + expect(said).toMatch(/no gpu renderer supported yet for "oblique"/); + }); + }); + + it("and each is drawn the same baked into one image", async (ctx) => { + need(ctx); + for (const { label, scene: build } of STAGGERED_SCENES.filter((_, i) => { + return i % 5 === 1; + })) { + const scene = build(); + const layers = show(app, makeMap(scene), "prerender"); + for (const [x, y] of STAGGERED_VIEWS.slice(0, 4)) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, "prerender"); + const d = diff(reference(scene, x, y, WIDTH, HEIGHT), frame); + expect(d.different, `${label}, view (${x}, ${y}): ${d.first}`).toBe( + 0, + ); + expect(drawn(frame)).toBeGreaterThanOrEqual(0); + } + } + }); + }); +} diff --git a/packages/melonjs/tests/tmxlayer-shader-staggered.spec.js b/packages/melonjs/tests/tmxlayer-shader-staggered.spec.js new file mode 100644 index 0000000000..625d924051 --- /dev/null +++ b/packages/melonjs/tests/tmxlayer-shader-staggered.spec.js @@ -0,0 +1,8 @@ +/** + * Staggered and hexagonal tile layers on the GPU, on WebGL: see + * `helpers/tile-layer-staggered-scenarios.js`. + */ +import { video } from "../src/index.js"; +import { staggeredScenarios } from "./helpers/tile-layer-staggered-scenarios.js"; + +staggeredScenarios(video.WEBGL, "WebGL"); diff --git a/packages/melonjs/tests/tmxlayer-staggered.spec.js b/packages/melonjs/tests/tmxlayer-staggered.spec.js new file mode 100644 index 0000000000..47cc976e3e --- /dev/null +++ b/packages/melonjs/tests/tmxlayer-staggered.spec.js @@ -0,0 +1,53 @@ +/** + * Staggered and hexagonal maps drawn tile by tile, and baked into one image, + * on the Canvas renderer: held to the picture Tiled draws (a tile stands on + * its cell and follows its tileset's `tileoffset`, as on every other map). + * The GPU renderers are held to the same in + * `helpers/tile-layer-staggered-scenarios.js`. + */ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { video } from "../src/index.js"; +import { + diff, + drawIn, + drawn, + makeApp, + makeMap, + reference, + show, +} from "./helpers/tile-layer-compare.js"; +import { + HEIGHT, + STAGGERED_SCENES, + STAGGERED_VIEWS, + WIDTH, +} from "./helpers/tile-layer-staggered-scenarios.js"; + +describe("staggered and hexagonal maps, as Tiled draws them (Canvas renderer)", () => { + let app; + beforeAll(async () => { + app = await makeApp(video.CANVAS, "CANVAS", WIDTH, HEIGHT); + }); + afterAll(() => { + app?.destroy(); + vi.restoreAllMocks(); + }); + + for (const { label, scene: build } of STAGGERED_SCENES) { + it(label, async () => { + const scene = build(); + let total = 0; + for (const mode of ["perTile", "prerender"]) { + const layers = show(app, makeMap(scene), mode); + for (const [x, y] of STAGGERED_VIEWS) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, mode); + const d = diff(reference(scene, x, y, WIDTH, HEIGHT), frame); + expect(d.different, `${mode}, view (${x}, ${y}): ${d.first}`).toBe(0); + total += drawn(frame); + } + } + expect(total).toBeGreaterThan(0); + }); + } +}); diff --git a/packages/melonjs/tests/webgpu_tmxlayer.spec.js b/packages/melonjs/tests/webgpu_tmxlayer.spec.js index bb48fc2765..e8745aee51 100644 --- a/packages/melonjs/tests/webgpu_tmxlayer.spec.js +++ b/packages/melonjs/tests/webgpu_tmxlayer.spec.js @@ -279,3 +279,110 @@ describe("WebGPU isometric TMX layer renderer (mock)", () => { expect(renderer.calls.drawIndexed).toEqual([]); }); }); + +/** + * Staggered and hexagonal maps on the same renderer: one shader module for + * both, with the map's column width, row height, stagger axis and index in + * what was the uniform block's padding. + */ +describe("WebGPU staggered and hexagonal TMX layer renderer (mock)", () => { + let renderer; + + const tileset = { + isCollection: false, + image: { width: 256, height: 256 }, + tilewidth: 28, + tileheight: 48, + margin: 0, + spacing: 0, + tileoffset: { x: 0, y: 0 }, + firstgid: 1, + lastgid: 16, + isAnimated: false, + animations: new Map(), + texture: { name: "hex-atlas" }, + }; + + const layerOf = (orientation, map) => { + return { + orientation, + cols: 8, + rows: 8, + tilewidth: 28, + tileheight: 24, + dataVersion: 1, + layerData: new Uint16Array(8 * 8 * 2), + getOpacity() { + return 1; + }, + getRenderer() { + return map; + }, + tint: null, + tilesets: { tilesets: [tileset] }, + _shaderTileset: tileset, + }; + }; + + const RECT = { pos: { x: 10, y: 20 }, width: 320, height: 240 }; + + beforeEach(() => { + renderer = createMockWebGPURenderer(); + renderer.setBatcher = () => { + renderer.currentBatcher ??= new WebGPUQuadBatcher(renderer); + return renderer.currentBatcher; + }; + }); + + it("one shader module for both, apart from the other orientations'", () => { + const staggered = new TMXLayerGPURenderer(renderer, "staggered"); + const hexagonal = new TMXLayerGPURenderer(renderer, "hexagonal"); + const isometric = new TMXLayerGPURenderer(renderer, "isometric"); + expect(hexagonal.key).toBe(staggered.key); + expect(staggered.key).not.toBe(isometric.key); + }); + + it("hands the shader the map's lines: their spacing, axis and index", () => { + const tmx = new TMXLayerGPURenderer(renderer, "hexagonal"); + // a hexagon side of 12 along x: columns 20 apart, rows of 12 + tmx.draw( + layerOf("hexagonal", { + columnwidth: 20, + rowheight: 12, + staggerX: true, + staggerEven: true, + }), + RECT, + ); + expect(renderer.calls.drawIndexed).toEqual([6]); + const uniforms = tmx.uniformScratch; + // axis x (1) and an even index (2) + expect(uniforms[29]).toBe(3); + expect([uniforms[30], uniforms[31]]).toEqual([20, 12]); + // 28 pixels wide in spacings of 40, 48 tall in rows of 12 + expect([uniforms[6], uniforms[7]]).toEqual([1, 4]); + + const other = new TMXLayerGPURenderer(renderer, "staggered"); + other.draw( + layerOf("staggered", { + columnwidth: 14, + rowheight: 12, + staggerX: false, + staggerEven: false, + }), + RECT, + ); + expect(other.uniformScratch[29]).toBe(0); + expect([other.uniformScratch[30], other.uniformScratch[31]]).toEqual([ + 14, 12, + ]); + }); + + it("leaves them at 0 for the other orientations", () => { + const tmx = new TMXLayerGPURenderer(renderer, "isometric"); + const layer = layerOf("isometric", { originX: 8 * 14 }); + tmx.draw(layer, RECT); + expect(renderer.calls.drawIndexed).toEqual([6]); + expect([...tmx.uniformScratch.slice(29, 32)]).toEqual([0, 0, 0]); + }); +}); diff --git a/packages/melonjs/tests/webgpu_tmxlayer_staggered.spec.js b/packages/melonjs/tests/webgpu_tmxlayer_staggered.spec.js new file mode 100644 index 0000000000..4fd65a742f --- /dev/null +++ b/packages/melonjs/tests/webgpu_tmxlayer_staggered.spec.js @@ -0,0 +1,9 @@ +/** + * Staggered and hexagonal tile layers on the GPU, on WebGPU: see + * `helpers/tile-layer-staggered-scenarios.js`. Skipped, visibly, where the + * browser has no WebGPU adapter. + */ +import { video } from "../src/index.js"; +import { staggeredScenarios } from "./helpers/tile-layer-staggered-scenarios.js"; + +staggeredScenarios(video.WEBGPU, "WebGPU"); From 3b898941f27936efa19aad70d2fb840ec4de12e2 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 03:57:06 +0000 Subject: [PATCH 07/11] Tile layers: final review fixes (#1897) A layer reads the camera's view in its own coordinates. It is handed the view in the world's, and is drawn where its ancestors put it: a map centred in the view by moving the world (`level.load`) lost the tiles reaching above or left of its grid on the GPU and baked paths, and a layer in a moved container lost tiles tile by tile. A layer keeps track of whether the shader path can still draw it whichever way it is drawn: a tile of a second tileset set while the layer was switched to "perTile" by hand was left undrawn once it was switched back. A tile layer's bounds are its grid's again. What its tiles reach past the grid is asked for separately (`getDrawBounds`, read by `Camera2d#isVisible`): growing `getBounds()` moved what read it as the map's area. Pathfinding plugin: a grid from a tile layer stands where the layer was moved to (its offset in Tiled, its `pos`), as the layer is now drawn. One helper cuts the view to a layer's tiles for the isometric and the staggered regions; stale comments and the docs corrected. Tests: a layer in a centred map and in a moved container, on Canvas, WebGL and WebGPU, in every render mode and orientation, against the reference; render modes switched by hand with tiles set in between. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01JnzwA4a7ZyjLBpbeEpFLu6 --- packages/melonjs/CHANGELOG.md | 3 +- .../melonjs/skills/melonjs-tilemaps/SKILL.md | 9 +- packages/melonjs/src/application/settings.ts | 13 +- packages/melonjs/src/camera/camera2d.ts | 10 +- packages/melonjs/src/level/tiled/TMXLayer.js | 90 ++++-- .../tiled/renderer/TMXIsometricRenderer.js | 4 +- .../tiled/renderer/TMXOrthogonalRenderer.js | 4 +- packages/melonjs/src/physics/world.js | 12 +- .../src/video/gpu/tmxlayer_orientation.js | 79 ++--- .../video/webgl/shaders/tmxlayer-common.frag | 4 +- .../melonjs/src/video/webgl/webgl_renderer.js | 2 +- .../webgpu/renderers/tmxlayer/tmxlayer.js | 13 +- .../video/webgpu/shaders/tmxlayer-common.wgsl | 9 +- .../tests/helpers/tile-layer-compare.js | 13 +- .../helpers/tile-layer-placement-checks.js | 277 ++++++++++++++++++ .../melonjs/tests/tmxlayer-offset.spec.js | 22 +- .../tests/tmxlayer-placement-webgl.spec.js | 8 + .../melonjs/tests/tmxlayer-placement.spec.js | 8 + .../tests/webgpu_tmxlayer_placement.spec.js | 9 + packages/pathfinding-plugin/CHANGELOG.md | 5 + packages/pathfinding-plugin/package.json | 2 +- packages/pathfinding-plugin/src/grid.ts | 8 + .../pathfinding-plugin/tests/sources.spec.ts | 21 ++ 23 files changed, 526 insertions(+), 99 deletions(-) create mode 100644 packages/melonjs/tests/helpers/tile-layer-placement-checks.js create mode 100644 packages/melonjs/tests/tmxlayer-placement-webgl.spec.js create mode 100644 packages/melonjs/tests/tmxlayer-placement.spec.js create mode 100644 packages/melonjs/tests/webgpu_tmxlayer_placement.spec.js diff --git a/packages/melonjs/CHANGELOG.md b/packages/melonjs/CHANGELOG.md index 7155fc5c06..fa0d70370c 100644 --- a/packages/melonjs/CHANGELOG.md +++ b/packages/melonjs/CHANGELOG.md @@ -19,7 +19,8 @@ - Tiled, GPU tile layers: a layer with a tile that is not square, flipped diagonally, is drawn tile by tile; the shader drew such a tile wrong - Tiled, GPU tile layers: the shader runs at full precision whatever `highPrecisionShader` says; at a lower one a phone could misplace the tiles of a large map - Tiled: a tile layer follows its offset in Tiled (`offsetx`, `offsety`), and its own `pos` when the game moves it: its tiles are drawn there and `getTile()` finds them there, on every renderer and map orientation. Both were ignored, and the layer drawn at the map's origin; only image layers followed their offset. A map whose tile layers have an offset now looks as it does in Tiled -- Tiled: tiles that reach past their layer's grid (taller or wider than a cell, or moved by a `tileoffset`) are drawn when they are in view and the grid is not; the whole layer was skipped, so the tops of tall tiles vanished once the map scrolled out of view. `getBounds()` of a tile layer covers that reach; its `width` and `height` are still the grid's +- Tiled: tiles that reach past their layer's grid (taller or wider than a cell, or moved by a `tileoffset`) are drawn when they are in view and the grid is not; the whole layer was skipped, so the tops of tall tiles vanished once the map scrolled out of view. The layer's bounds are still its grid's +- Tiled: a map centred in the view because it is smaller (as `level.load` centres it), and a tile layer in a container that was moved, are drawn whole. The layer read the camera's view as if it stood at the world's origin: on WebGL, WebGPU and a pre-rendered layer the tiles reaching above or left of the grid were cut, and tile by tile a layer far from the origin lost tiles - Tiled, staggered and hexagonal maps: a tile larger than its cell stands on the cell, and a tileset's `tileoffset` moves its tiles, as in Tiled and on every other map. Drawn tile by tile, such a tile hung down from the top of its cell and the offset was ignored, so the map sat lower than its objects (and than a tile redrawn in a baked layer); tiles of a large tileset also went missing along the edges of the view - Tiled, isometric maps drawn tile by tile: tiles of a tileset larger than the map's first one, or moved by a `tileoffset`, went missing along the edges of the view - Tiled, orthogonal maps drawn tile by tile: tiles more than two cells tall went missing along the bottom of the view diff --git a/packages/melonjs/skills/melonjs-tilemaps/SKILL.md b/packages/melonjs/skills/melonjs-tilemaps/SKILL.md index 04cc022a75..da107d41cc 100644 --- a/packages/melonjs/skills/melonjs-tilemaps/SKILL.md +++ b/packages/melonjs/skills/melonjs-tilemaps/SKILL.md @@ -301,9 +301,11 @@ collision: solid ground comes from collision object layers (below), so a tile set or cleared changes what is drawn and what `cellAt` returns, not what a body runs into. Keep a layer you edit often off `preRender` (see the symptom table). -A tile layer is where its `pos` says: its offset in Tiled (Layer → Offset) -to start with, and wherever the game moves it afterwards. Its tiles are drawn -there, and `getTile(x, y)` answers for where they are drawn: +A tile layer moves with its `pos`. It starts where its offset in Tiled puts +it (Layer → Offset), and the game moves it from there; its tiles are drawn +where it is, and `getTile(x, y)` answers for where they are drawn. Move it +by a difference, not to a point: `pos` is not (0, 0) to begin with on every +map (an infinite one, an oblique one). ```js board.pos.y -= 8; // the whole layer, 8 pixels up @@ -537,6 +539,7 @@ layer into the offscreen-bake path instead. | thin lines between tiles on the Canvas renderer, with smoothing on | filtering blends in the next tile of the tileset or its spacing, which a canvas cannot prevent: set `antiAlias: false`, or `preRender` the layer. WebGL and WebGPU keep each tile to itself | | a tile layer given an offset in Tiled (a roof nudged up, a shadow shifted) is drawn where the others are | before 20.17 a tile layer ignored its offset, and its `pos`; only image layers followed theirs: update | | after updating to 20.17 a tile layer sits away from where it did | it has an offset in Tiled that was ignored until then, and now moves it as in the editor: clear the layer's Offset in Tiled if it was not meant | +| a map smaller than the screen (centred by `level.load`) had the tops of its tall tiles cut on WebGL or WebGPU | before 20.17 a layer read the camera's view as if it stood at the world's origin, and so did a layer in a container the game had moved: update | | the tops of tall tiles vanish when the map itself scrolls out of view | before 20.17 a layer was skipped once its grid left the view, though tiles larger than a cell (or moved by a `tileoffset`) still reached in: update | | tilemap slow on some machines | layer fell back off the shader path — check the console warning for the reason | | a staggered or hexagonal map sits higher than before 20.17, by how much its tiles are taller than a cell (or by its tileset's `tileoffset`) | it is drawn as Tiled draws it: until then a tile hung from the top of its cell and the offset was ignored, away from the map's objects | diff --git a/packages/melonjs/src/application/settings.ts b/packages/melonjs/src/application/settings.ts index d58847735c..425ab6ca7d 100644 --- a/packages/melonjs/src/application/settings.ts +++ b/packages/melonjs/src/application/settings.ts @@ -481,12 +481,13 @@ export type ApplicationSettings = { * `renderer.supportsShaderTileLayers` — WebGL 2 and WebGPU). When * `true` (default), eligible layers render via a single quad per * tileset + a fragment shader doing per-fragment GID lookup, bypassing - * the per-tile draw loop entirely. Layers that don't qualify (Canvas - * renderer, an oblique map, collection-of-image tilesets, - * tilerendersize "grid", tiles larger than the shader walks; on an - * orthogonal map a non-zero tileoffset, on the others tiles of - * several tilesets in one layer) fall back to the legacy path - * automatically, saying why in the console. + * the per-tile draw loop entirely. Layers that don't qualify fall + * back to the legacy path automatically, saying why in the console: + * among them the Canvas renderer, an oblique map, collection-of-image + * tilesets, tilerendersize "grid", tiles larger than the shader walks; + * on an orthogonal map a non-zero tileoffset; on the others tiles of + * several tilesets in one layer, a grid of odd size, or a blend mode + * other than the normal one. * Set to `false` to disable globally. * @default true */ diff --git a/packages/melonjs/src/camera/camera2d.ts b/packages/melonjs/src/camera/camera2d.ts index 1b8675742c..6dfa51c740 100644 --- a/packages/melonjs/src/camera/camera2d.ts +++ b/packages/melonjs/src/camera/camera2d.ts @@ -1091,8 +1091,14 @@ export default class Camera2d extends Renderable { ); } // non-floating objects use world coordinates, check against - // the camera's visible world area (accounts for position and zoom) - return obj.getBounds().overlaps(this.worldView); + // the camera's visible world area (accounts for position and zoom). + // What a renderable draws can reach past its bounds (a tile layer's + // tiles, taller than their cell, stand above its grid): it says so + // with `getDrawBounds` + const drawn = ( + obj as Renderable & { getDrawBounds?: () => Bounds } + ).getDrawBounds?.(); + return (drawn ?? obj.getBounds()).overlaps(this.worldView); } /** diff --git a/packages/melonjs/src/level/tiled/TMXLayer.js b/packages/melonjs/src/level/tiled/TMXLayer.js index 44b6edbfdf..996375c0fe 100644 --- a/packages/melonjs/src/level/tiled/TMXLayer.js +++ b/packages/melonjs/src/level/tiled/TMXLayer.js @@ -237,7 +237,8 @@ export default class TMXLayer extends Renderable { */ this.maxTileOffset = { x: 0, y: 0 }; /** - * The one tileset an isometric layer on the shader path draws with: + * The one tileset a layer of an isometric, staggered or hexagonal + * map draws with on the shader path: * `undefined` for an empty layer (it takes the tileset of the first * tile set), `null` for a layer that path cannot draw, or that was * not checked for it. @@ -282,7 +283,7 @@ export default class TMXLayer extends Renderable { * How far past the layer's grid its tiles can be drawn, on each * side: by tiles larger than a cell, and by a tileset's * `tileoffset`. The layer is in view as far as they reach (see - * {@link TMXLayer#updateBounds}). + * {@link TMXLayer#getDrawBounds}). * @ignore */ this._reach = { @@ -490,28 +491,30 @@ export default class TMXLayer extends Renderable { this.pos.y = this._mapPos.y + (+data.offsety || 0); vector2dPool.release(origin); boundsPool.release(bounds); - this.updateBounds(); } /** - * The layer's bounds: its grid, where it is, and as far past it as its - * tiles reach. A tile larger than its cell, or moved by its tileset's - * `tileoffset`, is drawn outside the grid, and the layer is in view - * while such a tile is. Its `width` and `height` stay the grid's. + * The area the layer draws in: its bounds, which are its grid's, and as + * far past them as its tiles reach. A tile larger than its cell, or + * moved by its tileset's `tileoffset`, is drawn outside the grid, and a + * camera has the layer in view while such a tile is (see + * `Camera2d#isVisible`). The layer's bounds, `width` and `height` stay + * the grid's. + * @returns {Bounds} that area, in the same coordinates as `getBounds()` * @ignore */ - updateBounds(absolute = true) { - const bounds = super.updateBounds(absolute); + getDrawBounds() { + const bounds = this.getBounds(); const reach = this._reach; - if (reach !== undefined && bounds.isFinite()) { - bounds.setMinMax( - bounds.left - reach.left, - bounds.top - reach.top, - bounds.right + reach.right, - bounds.bottom + reach.bottom, - ); - } - return bounds; + /** @ignore */ + this._drawBounds ??= boundsPool.get(); + this._drawBounds.setMinMax( + bounds.left - reach.left, + bounds.top - reach.top, + bounds.right + reach.right, + bounds.bottom + reach.bottom, + ); + return this._drawBounds; } /** @@ -707,6 +710,13 @@ export default class TMXLayer extends Renderable { typeof this.preRender === "boolean" ? this.preRender : root?.preRender; const elig = this._checkShaderEligibility(renderer, gpuAllowed); + /** + * whether the shader path can draw the layer, as its tiles are now + * (kept up to date by `setTile`): a layer asked for that path by + * hand when it cannot is drawn tile by tile + * @ignore + */ + this._shaderFit = elig.ok; const requested = this.renderMode; // explicit "shader" — honor if eligible, warn otherwise @@ -986,11 +996,15 @@ export default class TMXLayer extends Renderable { } } if (reason !== undefined) { - console.warn( - `melonJS: layer "${this.name}" back to the legacy tile renderer (${reason})`, - ); - this.renderMode = "perTile"; + this._shaderFit = false; this._shaderTileset = null; + // drawn another way for now (by hand): nothing to move back + if (this.renderMode === "shader") { + console.warn( + `melonJS: layer "${this.name}" back to the legacy tile renderer (${reason})`, + ); + this.renderMode = "perTile"; + } } } @@ -1101,7 +1115,9 @@ export default class TMXLayer extends Renderable { this.dataVersion++; this.isDirty = true; this._preRenderCell(x, y); - if (this.renderMode === "shader") { + // whichever way the layer is drawn now: it may be asked for the + // shader path again + if (this._shaderFit === true) { this._keepShaderEligible(cleanGid, this.layerData[idx + 1]); } return tile; @@ -1256,6 +1272,27 @@ export default class TMXLayer extends Renderable { viewRect.pos.set(rect.pos.x + offset.x, rect.pos.y + offset.y); rect = viewRect.setSize(rect.width, rect.height); } + // The view is the camera's, in the world's coordinates, and the layer + // is drawn where its ancestors put it: a world moved to centre a map + // smaller than the view, a container the game moved. Read in the + // layer's own coordinates, the view starts that much earlier. (A + // camera other than the default one draws the world unmoved.) + let shiftX = 0; + let shiftY = 0; + const parent = this.ancestor; + if (parent !== undefined && parent !== null) { + const at = parent.getAbsolutePosition(); + shiftX = at.x; + shiftY = at.y; + if (rect.isDefault === false) { + let root = parent; + while (root.ancestor) { + root = root.ancestor; + } + shiftX -= root.pos.x; + shiftY -= root.pos.y; + } + } // A layer moved from its place in the map, by its offset in Tiled or // by its `pos`, and an infinite map's layer that starts left of or // above the map's origin: the layer's renderer draws its grid from @@ -1265,13 +1302,18 @@ export default class TMXLayer extends Renderable { if ( movedX !== 0 || movedY !== 0 || + shiftX !== 0 || + shiftY !== 0 || this.startx !== 0 || this.starty !== 0 ) { const origin = this._cellOrigin(vector2dPool.get()); origin.set(origin.x + movedX, origin.y + movedY); cellRect ??= new Rect(0, 0, 0, 0); - cellRect.pos.set(rect.pos.x - origin.x, rect.pos.y - origin.y); + cellRect.pos.set( + rect.pos.x - shiftX - origin.x, + rect.pos.y - shiftY - origin.y, + ); cellRect.setSize(rect.width, rect.height); renderer.translate(origin.x, origin.y); renderer.drawTileLayer(this, cellRect); diff --git a/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js b/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js index b0fd0514ba..f81f5de6e0 100644 --- a/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js +++ b/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js @@ -166,8 +166,8 @@ export default class TMXIsometricRenderer extends TMXRenderer { // missing along the bottom of the view, where their cell is below it // and their image still reaches in, and so did tiles moved into view // by an offset larger than a tile - const reachX = layer.maxTileSize.width + layer.maxTileOffset.x; - const reachY = layer.maxTileSize.height + layer.maxTileOffset.y; + const reachX = layer.maxTileSize.width + (layer.maxTileOffset?.x ?? 0); + const reachY = layer.maxTileSize.height + (layer.maxTileOffset?.y ?? 0); const rowItr = this.pixelToTileCoords( rect.pos.x - reachX, rect.pos.y - reachY, diff --git a/packages/melonjs/src/level/tiled/renderer/TMXOrthogonalRenderer.js b/packages/melonjs/src/level/tiled/renderer/TMXOrthogonalRenderer.js index a6cb4412dd..1f5354a43c 100644 --- a/packages/melonjs/src/level/tiled/renderer/TMXOrthogonalRenderer.js +++ b/packages/melonjs/src/level/tiled/renderer/TMXOrthogonalRenderer.js @@ -96,8 +96,8 @@ export default class TMXOrthogonalRenderer extends TMXRenderer { // view.) A tileset's `tileoffset` moves its tiles either way. const overX = Math.max(layer.maxTileSize.width - layer.tilewidth, 0); const overY = Math.max(layer.maxTileSize.height - layer.tileheight, 0); - const offsetX = layer.maxTileOffset.x; - const offsetY = layer.maxTileOffset.y; + const offsetX = layer.maxTileOffset?.x ?? 0; + const offsetY = layer.maxTileOffset?.y ?? 0; const start = this.pixelToTileCoords( Math.max(rect.pos.x - overX - offsetX, 0), Math.max(rect.pos.y - offsetY, 0), diff --git a/packages/melonjs/src/physics/world.js b/packages/melonjs/src/physics/world.js index 601442e88d..dbfa3d58d6 100644 --- a/packages/melonjs/src/physics/world.js +++ b/packages/melonjs/src/physics/world.js @@ -225,11 +225,13 @@ export default class World extends Container { * tiles, per-layer opacity and tint, and tiles larger than the * grid: up to 4 cells of overflow on an orthogonal map, up to 4 * cells wide and 8 tall on the others. - * Layers that don't qualify (Canvas, an oblique map, - * collection-of-image tilesets, larger tiles; on an orthogonal map - * a non-zero `tileoffset`, on the others tiles of several tilesets - * in one layer) fall back to the legacy - * path automatically. Set to `false` to disable globally. + * Layers that don't qualify fall back to the legacy path + * automatically, saying why in the console: among them Canvas, an + * oblique map, collection-of-image tilesets, a `tilerendersize` + * of "grid", larger tiles; on an orthogonal map a non-zero + * `tileoffset`; on the others tiles of several tilesets in one + * layer, a grid of odd size, or a blend mode other than the + * normal one. Set to `false` to disable globally. * @type {boolean} * @default true */ diff --git a/packages/melonjs/src/video/gpu/tmxlayer_orientation.js b/packages/melonjs/src/video/gpu/tmxlayer_orientation.js index 0691c50798..c3fdf54e96 100644 --- a/packages/melonjs/src/video/gpu/tmxlayer_orientation.js +++ b/packages/melonjs/src/video/gpu/tmxlayer_orientation.js @@ -106,6 +106,39 @@ export function orthogonalRegion(layer, rect, tileset, out) { return true; } +/** + * The view, cut to where a layer's tiles can be, on whole pixels: the quad + * to draw, for an orientation whose shader works in the layer's pixels. + * @param {object} rect - the visible viewport rect (world coords) + * @param {number} left - where the tiles can be, in the layer's pixels + * @param {number} top - where the tiles can be + * @param {number} right - where the tiles can be + * @param {number} bottom - where the tiles can be + * @param {number} originX - where the shader's pixels are measured from + * @param {number} originY - where the shader's pixels are measured from + * @param {object} out - the region to fill + * @returns {boolean} false when the view shows none of it + * @ignore + */ +function viewIn(rect, left, top, right, bottom, originX, originY, out) { + const x0 = Math.floor(Math.max(rect.pos.x, left)); + const y0 = Math.floor(Math.max(rect.pos.y, top)); + const x1 = Math.ceil(Math.min(rect.pos.x + rect.width, right)); + const y1 = Math.ceil(Math.min(rect.pos.y + rect.height, bottom)); + if (x1 <= x0 || y1 <= y0) { + return false; + } + out.x = x0; + out.y = y0; + out.width = x1 - x0; + out.height = y1 - y0; + out.startX = x0 - originX; + out.startY = y0 - originY; + out.sizeX = out.width; + out.sizeY = out.height; + return true; +} + /** * The part of an isometric layer a view shows, for the tiles of one tileset: * the same on every GPU backend, so their renderers share it. @@ -143,23 +176,11 @@ export function isometricRegion(layer, rect, tileset, out) { const top = layer.tileheight - tileset.tileheight + offsetY; const bottom = (cols + rows) * halfHeight + offsetY; - // the view, cut to it, on whole pixels - const x0 = Math.floor(Math.max(rect.pos.x, left)); - const y0 = Math.floor(Math.max(rect.pos.y, top)); - const x1 = Math.ceil(Math.min(rect.pos.x + rect.width, right)); - const y1 = Math.ceil(Math.min(rect.pos.y + rect.height, bottom)); - if (x1 <= x0 || y1 <= y0) { + if ( + !viewIn(rect, left, top, right, bottom, originX + offsetX, offsetY, out) + ) { return false; } - - out.x = x0; - out.y = y0; - out.width = x1 - x0; - out.height = y1 - y0; - out.startX = x0 - originX - offsetX; - out.startY = y0 - offsetY; - out.sizeX = out.width; - out.sizeY = out.height; // how many cells' images reach a pixel: as many across as the image is // wide in half grid widths, as many down as it is tall in half grid // heights @@ -208,23 +229,9 @@ export function staggeredRegion(layer, rect, tileset, out) { const top = layer.tileheight - tileset.tileheight + offsetY; const bottom = (lines - 1) * rowHeight + layer.tileheight + offsetY; - // the view, cut to it, on whole pixels - const x0 = Math.floor(Math.max(rect.pos.x, left)); - const y0 = Math.floor(Math.max(rect.pos.y, top)); - const x1 = Math.ceil(Math.min(rect.pos.x + rect.width, right)); - const y1 = Math.ceil(Math.min(rect.pos.y + rect.height, bottom)); - if (x1 <= x0 || y1 <= y0) { + if (!viewIn(rect, left, top, right, bottom, offsetX, offsetY, out)) { return false; } - - out.x = x0; - out.y = y0; - out.width = x1 - x0; - out.height = y1 - y0; - out.startX = x0 - offsetX; - out.startY = y0 - offsetY; - out.sizeX = out.width; - out.sizeY = out.height; // how many cells' images reach a pixel: as many in a line as the image // is wide in cell spacings, as many lines as it is tall in row heights out.overflowX = Math.ceil(tileset.tilewidth / (columnWidth * 2)); @@ -298,7 +305,7 @@ const ONE_TILESET = { * @returns {boolean} whether the layer can be drawn */ draws(layer) { - return layer._shaderTileset !== null; + return layer._shaderTileset !== null && layer._shaderFit !== false; }, }; @@ -318,8 +325,14 @@ export const TMX_ORIENTATIONS = { tilesetsOf(layer) { return layer.tilesets.tilesets; }, - draws() { - return true; + /** + * not a layer found unfit for this path, or made so by a tile set + * since (a layer never checked is drawn) + * @param {TMXLayer} layer - the layer + * @returns {boolean} whether the layer can be drawn + */ + draws(layer) { + return layer._shaderFit !== false; }, // how many cells a tile can reach past its own, across and up limits: { MAX_OVERFLOW: 4 }, diff --git a/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag b/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag index 79f5bc8f6e..214b7a5e84 100644 --- a/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag +++ b/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag @@ -9,8 +9,8 @@ precision highp int; // Fragment shader for the TMX layer GPU renderers (WebGL2 / GLSL ES 3.00): // the part every map orientation shares. An orientation's own file follows -// it in the program (`orthogonal-tmxlayer.frag`, `isometric-tmxlayer.frag`) -// and gives the two things that differ: where a cell's tile is drawn +// it in the program (`orthogonal-tmxlayer.frag`, `isometric-tmxlayer.frag`, +// `staggered-tmxlayer.frag` for staggered and hexagonal maps) and gives the two things that differ: where a cell's tile is drawn // (`tileOrigin`), and which cells could cover a fragment, front to back // (`main`). // diff --git a/packages/melonjs/src/video/webgl/webgl_renderer.js b/packages/melonjs/src/video/webgl/webgl_renderer.js index 1ffd68dcfd..0aaf2676c2 100644 --- a/packages/melonjs/src/video/webgl/webgl_renderer.js +++ b/packages/melonjs/src/video/webgl/webgl_renderer.js @@ -1493,7 +1493,7 @@ export default class WebGLRenderer extends Renderer { */ _getTMXGPURendererFor(orientation) { if (TMX_ORIENTATIONS[orientation] === undefined) { - // staggered, hexagonal, oblique: drawn by the legacy renderer + // an oblique map: drawn by the legacy renderer return undefined; } this._tmxGPURenderers ??= new Map(); diff --git a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js index 750487c91e..868f2d0e63 100644 --- a/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js +++ b/packages/melonjs/src/video/webgpu/renderers/tmxlayer/tmxlayer.js @@ -20,8 +20,9 @@ import staggeredWGSL from "../../shaders/tmxlayer-staggered.wgsl"; /** * byte size of the TMXUniforms block (see tmxlayer-common.wgsl): ten vec2f (80) * interleaved with one vec4f at a 16-aligned offset, two f32, the vec4f - * tint (ending at 112) and the mipmap flag, rounded up to the block's - * 16-byte alignment → 128 bytes + * tint (ending at 112), the mipmap flag (112), and a staggered map's mode + * (116) and two lengths (a vec2f at 120) → 128 bytes, a multiple of the + * block's 16-byte alignment * @ignore * @internal */ @@ -43,10 +44,10 @@ const ORIENTATION_WGSL = { * the fragment shader sampling a per-layer GID index texture and the * tileset atlas. * - * An orientation's renderer extends this one with the two things that - * differ: its part of the shader (where a cell's tile is, and which cells - * can cover a fragment), and {@link TMXLayerGPURenderer#region}, the part of - * the layer a view shows. + * There is one of these per map orientation drawn (see `TMX_ORIENTATIONS` + * in `gpu/tmxlayer_orientation.js`). Two things differ from one to the next: + * its part of the shader (where a cell's tile is, and which cells can cover + * a fragment), and the part of a layer a view shows. * * Structure mirrors the WebGL twin; the mechanisms are the backend's: * - the GID index / animation lookup are renderer-owned `rgba8unorm` diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl index 711d52a2b3..a5b208c24b 100644 --- a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl @@ -1,7 +1,8 @@ // melonJS WebGPU TMX layer shader — the WGSL port of tmxlayer.vert and // tmxlayer-common.frag (WebGL2): the part every map orientation shares. An // orientation's own file follows it in the module (tmxlayer-orthogonal.wgsl, -// tmxlayer-isometric.wgsl) and gives the two things that differ: where a +// tmxlayer-isometric.wgsl, tmxlayer-staggered.wgsl for staggered and +// hexagonal maps) and gives the two things that differ: where a // cell's tile is drawn (`tileOrigin`), and which cells could cover a // fragment, front to back (`fragment_main`). // @@ -17,9 +18,9 @@ // local id, same R/G byte-pair encoding. // // textureLoad replaces texelFetch (byte-exact, no sampler); the atlas -// sample uses textureSampleLevel — the sample sites sit in non-uniform -// control flow, where implicit-derivative sampling is not allowed (WGSL -// uniformity rule; atlases are single-level so the output is identical). +// sample uses textureSampleGrad, with derivatives taken once per fragment — +// the sample sites sit in non-uniform control flow, where +// implicit-derivative sampling is not allowed (WGSL uniformity rule). struct FrameUniforms { projection : mat4x4, diff --git a/packages/melonjs/tests/helpers/tile-layer-compare.js b/packages/melonjs/tests/helpers/tile-layer-compare.js index 2c5afa8521..64e59d8da7 100644 --- a/packages/melonjs/tests/helpers/tile-layer-compare.js +++ b/packages/melonjs/tests/helpers/tile-layer-compare.js @@ -408,10 +408,14 @@ export async function makeApp(renderer, type, width = 320, height = 240) { * @param {TMXTileMap} map - the map * @param {string} [renderMode] - a render mode to ask of every layer before * it is activated ("prerender" needs asking then: it bakes at activation) + * @param {boolean} [centered=false] - as `level.load` adds a map: one + * smaller than the view is centred in it, by moving the world (`world.pos`) * @returns {object[]} the map's tile layers */ -export function show(app, map, renderMode) { +export function show(app, map, renderMode, centered = false) { app.world.reset(); + // where a map centred before this one left it + app.world.pos.set(0, 0, app.world.pos.z); app.renderer.reset?.(); app.renderer.backgroundColor.setColor(16, 24, 48); if (renderMode !== undefined) { @@ -419,7 +423,7 @@ export function show(app, map, renderMode) { layer.renderMode = renderMode; } } - map.addTo(app.world, true, false); + map.addTo(app.world, true, centered); // the layers in their order now: the world sorts its children later, on // a timer no frame of these tests waits for app.world.sortNow(true); @@ -539,6 +543,8 @@ const DEFAULT_VIEWS = [ * channel that is not counted * @param {number} [options.share=0] - the largest share of the pixels * drawn that the two paths may differ by + * @param {number[]} [options.origin] - where the map's origin is drawn, + * when the world or a container the layers are in was moved * @returns {Promise} how many pixels were drawn, in all */ export async function comparePaths(app, scene, options = {}) { @@ -548,6 +554,7 @@ export async function comparePaths(app, scene, options = {}) { expected, tolerance = 0, share = 0, + origin = [0, 0], } = options; const { width, height } = app.viewport; let layers = options.layers; @@ -570,7 +577,7 @@ export async function comparePaths(app, scene, options = {}) { const count = drawn(gpu.frame); const allowed = Math.floor(count * share); if (oracle) { - let ref = reference(scene, x, y, width, height); + let ref = reference(scene, x - origin[0], y - origin[1], width, height); if (expected !== undefined) { ref = expected(ref); } diff --git a/packages/melonjs/tests/helpers/tile-layer-placement-checks.js b/packages/melonjs/tests/helpers/tile-layer-placement-checks.js new file mode 100644 index 0000000000..ddcf86d3f7 --- /dev/null +++ b/packages/melonjs/tests/helpers/tile-layer-placement-checks.js @@ -0,0 +1,277 @@ +/** + * A tile layer drawn where its ancestors put it: in a world moved to centre + * a map smaller than the view (what `level.load` does), or in a container + * that was moved. The layer is handed the camera's view in the world's + * coordinates, and has to read it in its own: the tiles that reach above or + * left of its grid were cut on the GPU and baked paths, and a layer far + * enough from the origin lost tiles tile by tile. + * + * Also here: a layer's `renderMode` switched by hand, and tiles set in + * between. + * + * One spec file per renderer calls this: an application takes the place of + * the one before it, and a spec file has its own page. + */ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { Container } from "../../src/index.js"; +import { + comparePaths, + diff, + drawIn, + drawn, + FLIP_AD, + fill, + inBlock, + makeApp, + makeMap, + makeTileset, + reference, + show, +} from "./tile-layer-compare.js"; + +const WIDTH = 320; +const HEIGHT = 240; + +const all = (cols, rows) => { + return fill(cols, rows, (c, r) => { + return 1 + ((c + r * 2) % 8); + }); +}; + +/** small maps of tiles that reach past their grid, one per orientation */ +const SCENES = { + orthogonal: () => { + return { + orientation: "orthogonal", + cols: 4, + rows: 3, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 48, th: 64 })], + layers: [{ data: all(4, 3) }], + }; + }, + isometric: () => { + return { + orientation: "isometric", + cols: 3, + rows: 3, + tw: 64, + th: 32, + tilesets: [ + makeTileset({ + tw: 64, + th: 96, + opaque: (x, y) => { + return inBlock(x, y, 64, 96, 64, 32); + }, + }), + ], + layers: [{ data: all(3, 3) }], + }; + }, + staggered: () => { + return { + orientation: "staggered", + cols: 3, + rows: 5, + tw: 64, + th: 32, + tilesets: [ + makeTileset({ + tw: 64, + th: 80, + extra: { tileoffset: { x: -20, y: 0 } }, + }), + ], + layers: [{ data: all(3, 5) }], + extra: { staggeraxis: "y", staggerindex: "odd" }, + }; + }, + hexagonal: () => { + return { + orientation: "hexagonal", + cols: 5, + rows: 5, + tw: 28, + th: 24, + tilesets: [makeTileset({ tw: 28, th: 60 })], + layers: [{ data: all(5, 5) }], + extra: { staggeraxis: "x", staggerindex: "even", hexsidelength: 12 }, + }; + }, +}; + +const VIEWS = [ + [0, 0], + [30, 20], + [-45, -60], +]; + +/** + * Declare the checks for one renderer. + * @param {number} renderer - `video.CANVAS`, `video.WEBGL` or `video.WEBGPU` + * @param {string} type - the `renderer.type` expected + * @param {string[]} modes - the render modes that renderer has + */ +export function placementChecks(renderer, type, modes) { + describe(`a tile layer where its ancestors put it (${type})`, () => { + let app; + beforeAll(async () => { + app = await makeApp(renderer, type, WIDTH, HEIGHT); + }); + afterAll(() => { + app?.world.pos.set(0, 0, 0); + app?.destroy(); + vi.restoreAllMocks(); + }); + + const need = (ctx) => { + if (app === null || app === undefined) { + ctx.skip(`${type} not available in this environment`); + } + }; + + for (const [orientation, build] of Object.entries(SCENES)) { + it(`${orientation}: a map centred in the view keeps the tiles that reach past its grid`, async (ctx) => { + need(ctx); + const scene = build(); + let total = 0; + for (const mode of modes) { + const layers = show( + app, + makeMap(scene), + mode === "prerender" ? mode : undefined, + true, + ); + const { x: ox, y: oy } = app.world.pos; + // smaller than the view: centred, by moving the world + expect(ox, "centred across").toBeGreaterThan(0); + expect(oy, "centred down").toBeGreaterThan(0); + for (const [x, y] of VIEWS) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, layers, mode); + const ref = reference(scene, x - ox, y - oy, WIDTH, HEIGHT); + const d = diff(ref, frame); + expect(d.different, `${mode}, view (${x}, ${y}): ${d.first}`).toBe( + 0, + ); + total += drawn(frame); + } + } + expect(total).toBeGreaterThan(0); + }); + + for (const [gx, gy] of [ + [120, 80], + [-700, 900], + ]) { + it(`${orientation}: in a container moved to (${gx}, ${gy})`, async (ctx) => { + need(ctx); + const scene = build(); + let total = 0; + for (const mode of modes) { + app.world.reset(); + app.world.pos.set(0, 0, app.world.pos.z); + app.renderer.reset?.(); + app.renderer.backgroundColor.setColor(16, 24, 48); + const map = makeMap(scene); + const layers = map.getLayers(); + if (mode === "prerender") { + layers[0].renderMode = mode; + } + const group = new Container(); + group.pos.set(gx, gy, 0); + app.world.addChild(group); + map.addTo(group, true, false); + app.world.sortNow(true); + app.viewport.setBounds(-100000, -100000, 200000, 200000); + for (const [x, y] of VIEWS) { + // the view follows the container + app.viewport.moveTo(x + gx, y + gy); + const { frame } = await drawIn(app, layers, mode); + const ref = reference(scene, x, y, WIDTH, HEIGHT); + const d = diff(ref, frame); + expect( + d.different, + `${mode}, view (${x}, ${y}): ${d.first}`, + ).toBe(0); + total += drawn(frame); + } + } + expect(total).toBeGreaterThan(0); + }); + } + } + + if (type !== "CANVAS") { + it("both paths, centred: the same as the reference", async (ctx) => { + need(ctx); + for (const build of Object.values(SCENES)) { + const scene = build(); + const layers = show(app, makeMap(scene), undefined, true); + expect(layers[0].renderMode).toBe("shader"); + const origin = [app.world.pos.x, app.world.pos.y]; + expect( + await comparePaths(app, scene, { views: VIEWS, layers, origin }), + scene.orientation, + ).toBeGreaterThan(0); + } + }); + + describe("a render mode switched by hand, and tiles set in between", () => { + const two = (orientation) => { + const scene = SCENES[orientation](); + const [first] = scene.tilesets; + scene.tilesets = [ + first, + makeTileset({ tw: first.tilewidth, th: first.tileheight }), + ]; + return scene; + }; + + for (const orientation of ["isometric", "staggered", "hexagonal"]) { + it(`${orientation}: a tile of another tileset set while the layer was drawn tile by tile`, async (ctx) => { + need(ctx); + const scene = two(orientation); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + expect(layer.renderMode).toBe("shader"); + layer.renderMode = "perTile"; + // no warning: nothing is moved off the GPU path now + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + scene.layers[0].data[1 * scene.cols + 1] = 11; + layer.setTile(layer.getTileById(11, 1, 1), 1, 1); + expect(warn).not.toHaveBeenCalled(); + warn.mockRestore(); + // asked for the GPU path again: the layer has tiles of + // two tilesets, and is drawn tile by tile, whole + app.viewport.moveTo(0, 0); + const forced = await drawIn(app, layers, "shader"); + expect(forced.cpuDraws).toBe(1); + const d = diff(reference(scene, 0, 0, WIDTH, HEIGHT), forced.frame); + expect(d.different, d.first).toBe(0); + }); + } + + it("orthogonal: a tile not square, flipped diagonally, set while the layer was drawn tile by tile", async (ctx) => { + need(ctx); + const scene = SCENES.orthogonal(); + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + expect(layer.renderMode).toBe("shader"); + app.viewport.moveTo(0, 0); + layer.renderMode = "perTile"; + layer.setTile(layer.getTileById(3 + FLIP_AD, 1, 1), 1, 1); + const cpu = await drawIn(app, layers, "perTile"); + // asked for the GPU path again: still tile by tile, and the + // same picture + const forced = await drawIn(app, layers, "shader"); + expect(forced.cpuDraws).toBe(1); + expect(diff(cpu.frame, forced.frame).different).toBe(0); + expect(drawn(forced.frame)).toBeGreaterThan(0); + }); + }); + } + }); +} diff --git a/packages/melonjs/tests/tmxlayer-offset.spec.js b/packages/melonjs/tests/tmxlayer-offset.spec.js index 1f25aa597b..82e6e1c737 100644 --- a/packages/melonjs/tests/tmxlayer-offset.spec.js +++ b/packages/melonjs/tests/tmxlayer-offset.spec.js @@ -307,17 +307,31 @@ describe("where a tile layer is (Canvas renderer)", () => { ).toBeGreaterThan(0); }); - it("the bounds cover the reach, the size stays the grid's", () => { + it("its bounds stay the grid's: the reach is for what is in view alone", () => { const [layer] = show(app, makeMap(orthogonal({ tall: 128 }))); expect([layer.width, layer.height]).toEqual([320, 256]); const bounds = layer.getBounds(); + expect([bounds.left, bounds.top, bounds.right, bounds.bottom]).toEqual([ + 0, 0, 320, 256, + ]); // 128 pixel tiles on a 32 pixel grid stand 96 above their cell - expect(bounds.top).toBe(-96); - expect([bounds.left, bounds.right, bounds.bottom]).toEqual([0, 320, 256]); + const reach = layer.getDrawBounds(); + expect([reach.left, reach.top, reach.right, reach.bottom]).toEqual([ + 0, -96, 320, 256, + ]); // and getTile has nothing above the grid: no cell is there expect(layer.getTile(10, -50)).toBeNull(); + // moved, both move with it + layer.pos.x = 40; + layer.pos.y = -10; + expect([layer.getBounds().left, layer.getBounds().top]).toEqual([ + 40, -10, + ]); + expect([layer.getDrawBounds().left, layer.getDrawBounds().top]).toEqual([ + 40, -106, + ]); // tiles the size of the grid: the grid, no more - const plain = show(app, makeMap(orthogonal()))[0].getBounds(); + const plain = show(app, makeMap(orthogonal()))[0].getDrawBounds(); expect([plain.left, plain.top, plain.right, plain.bottom]).toEqual([ 0, 0, 320, 256, ]); diff --git a/packages/melonjs/tests/tmxlayer-placement-webgl.spec.js b/packages/melonjs/tests/tmxlayer-placement-webgl.spec.js new file mode 100644 index 0000000000..0b47582a68 --- /dev/null +++ b/packages/melonjs/tests/tmxlayer-placement-webgl.spec.js @@ -0,0 +1,8 @@ +/** + * A tile layer drawn where its ancestors put it, on WebGL, with the GPU + * path: see `helpers/tile-layer-placement-checks.js`. + */ +import { video } from "../src/index.js"; +import { placementChecks } from "./helpers/tile-layer-placement-checks.js"; + +placementChecks(video.WEBGL, "WebGL", ["perTile", "shader", "prerender"]); diff --git a/packages/melonjs/tests/tmxlayer-placement.spec.js b/packages/melonjs/tests/tmxlayer-placement.spec.js new file mode 100644 index 0000000000..d073279d17 --- /dev/null +++ b/packages/melonjs/tests/tmxlayer-placement.spec.js @@ -0,0 +1,8 @@ +/** + * A tile layer drawn where its ancestors put it, on the Canvas renderer: + * see `helpers/tile-layer-placement-checks.js`. + */ +import { video } from "../src/index.js"; +import { placementChecks } from "./helpers/tile-layer-placement-checks.js"; + +placementChecks(video.CANVAS, "CANVAS", ["perTile", "prerender"]); diff --git a/packages/melonjs/tests/webgpu_tmxlayer_placement.spec.js b/packages/melonjs/tests/webgpu_tmxlayer_placement.spec.js new file mode 100644 index 0000000000..6ebc8fba77 --- /dev/null +++ b/packages/melonjs/tests/webgpu_tmxlayer_placement.spec.js @@ -0,0 +1,9 @@ +/** + * A tile layer drawn where its ancestors put it, on WebGPU: see + * `helpers/tile-layer-placement-checks.js`. Skipped where the browser has + * no WebGPU adapter. + */ +import { video } from "../src/index.js"; +import { placementChecks } from "./helpers/tile-layer-placement-checks.js"; + +placementChecks(video.WEBGPU, "WebGPU", ["perTile", "shader", "prerender"]); diff --git a/packages/pathfinding-plugin/CHANGELOG.md b/packages/pathfinding-plugin/CHANGELOG.md index 21c213f86d..6de3ce7d2b 100644 --- a/packages/pathfinding-plugin/CHANGELOG.md +++ b/packages/pathfinding-plugin/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [1.1.1] - _unreleased_ + +### Fixed +- `Grid.fromTileLayer`: the grid stands where the layer's tiles are drawn when the layer has an offset in Tiled or was moved by its `pos`, which melonJS 20.17 follows when it draws the layer; the grid stayed at the map's origin + ## [1.1.0] - _2026-11-15_ ### Added diff --git a/packages/pathfinding-plugin/package.json b/packages/pathfinding-plugin/package.json index e6b114272d..9b894c5672 100644 --- a/packages/pathfinding-plugin/package.json +++ b/packages/pathfinding-plugin/package.json @@ -1,6 +1,6 @@ { "name": "@melonjs/pathfinding-plugin", - "version": "1.1.0", + "version": "1.1.1", "description": "melonJS A* pathfinding plugin", "homepage": "https://www.npmjs.com/package/@melonjs/pathfinding-plugin", "type": "module", diff --git a/packages/pathfinding-plugin/src/grid.ts b/packages/pathfinding-plugin/src/grid.ts index b9f9deb7c6..fa9c4472c8 100644 --- a/packages/pathfinding-plugin/src/grid.ts +++ b/packages/pathfinding-plugin/src/grid.ts @@ -595,6 +595,14 @@ export class Grid { corner.x += at.x - layer.pos.x; corner.y += at.y - layer.pos.y; } + // and where the layer was moved to in its map, by its offset in + // Tiled or by its `pos` (an engine that draws it there says where it + // stands unmoved; an older one drew it unmoved, whatever its `pos`) + const unmoved = (layer as { _mapPos?: { x: number; y: number } })._mapPos; + if (unmoved !== undefined) { + corner.x += layer.pos.x - unmoved.x; + corner.y += layer.pos.y - unmoved.y; + } if (isometric) { // the grid's corner is the box around its diamonds corner.x -= (layer.rows * layer.tilewidth) / 2; diff --git a/packages/pathfinding-plugin/tests/sources.spec.ts b/packages/pathfinding-plugin/tests/sources.spec.ts index 406eb64448..d293d76a50 100644 --- a/packages/pathfinding-plugin/tests/sources.spec.ts +++ b/packages/pathfinding-plugin/tests/sources.spec.ts @@ -230,6 +230,27 @@ describe("grids from a level", () => { grid.refresh(); expect(grid.isSolid(2, 1)).toBe(true); }); + + it("stands where the layer's tiles are drawn, when the layer was moved", () => { + // by its offset in Tiled, or as here by its `pos`: the tiles + // are drawn there, and the grid's cells are theirs + const layer = wallsLayer(); + const before = Grid.fromTileLayer(layer).getPointPosition(2, 1); + const { x, y } = layer.pos; + layer.pos.x = x + 24; + layer.pos.y = y - 10; + try { + const grid = Grid.fromTileLayer(layer); + const at = grid.getPointPosition(2, 1); + expect([at.x - before.x, at.y - before.y]).toEqual([24, -10]); + // the same cells, moved + expect(grid.isSolid(2, 0)).toBe(true); + expect(grid.isSolid(0, 0)).toBe(false); + } finally { + layer.pos.x = x; + layer.pos.y = y; + } + }); }); describe("Grid.fromBodies", () => { From 2e788199dde5947c41e7ee59d726cfeeef0dfbda Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 04:20:03 +0000 Subject: [PATCH 08/11] Tile layers: fixes from an adversarial test audit (#1897) Probes written to break the tile layer code, each against the reference picture, and what they found: - An orthogonal layer whose tiles are larger than the grid lay them over each other in the wrong order on the GPU when they came from more than one tileset (a pass per tileset) or the map's render order was not "right-down": such a layer is drawn tile by tile. - A layer with more columns or rows than the largest texture drew nothing on the GPU: drawn tile by tile. - A hexagonal map whose side leaves an odd number of pixels of a cell is laid out on whole pixels, as Tiled's own numbers are; and so drawn by the GPU too. The baked canvas of a staggered or hexagonal map of odd tile size was a pixel short. - The isometric CPU renderer left out tiles narrower than half a cell at the right edge of the view. - A camera with an offset over a moved world: the layer asked a plain rectangle, in place of the camera, whether it was the default one. - A container with a size of its own (a level loaded into one) is in view by its bounds before its layers are asked: it is told how far their tiles reach. - A layer taken out of its container and added again threw; a tile its tileset's image does not hold threw every frame tile by tile, and is now left undrawn on both paths. The probes are kept as tests (`tile-layer-edge-cases.js`, on WebGL and WebGPU), with the reference taught the orthogonal render orders and Tiled's whole numbers for hexagons. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01JnzwA4a7ZyjLBpbeEpFLu6 --- packages/melonjs/CHANGELOG.md | 5 + .../melonjs/skills/melonjs-tilemaps/SKILL.md | 8 +- packages/melonjs/src/level/tiled/TMXLayer.js | 222 +++++++- .../melonjs/src/level/tiled/TMXTileset.js | 12 +- .../tiled/renderer/TMXHexagonalRenderer.js | 18 +- .../tiled/renderer/TMXIsometricRenderer.js | 11 +- packages/melonjs/src/renderable/container.js | 31 ++ packages/melonjs/src/video/renderer.js | 10 + .../video/webgl/shaders/tmxlayer-common.frag | 5 + .../melonjs/src/video/webgl/webgl_renderer.js | 1 + .../video/webgpu/shaders/tmxlayer-common.wgsl | 5 + .../src/video/webgpu/webgpu_renderer.js | 3 + .../tests/helpers/tile-layer-compare.js | 22 +- .../tests/helpers/tile-layer-edge-cases.js | 501 ++++++++++++++++++ .../helpers/tile-layer-staggered-scenarios.js | 14 +- .../melonjs/tests/tmxlayer-edge-cases.spec.js | 8 + .../tests/webgpu_tmxlayer_edge_cases.spec.js | 9 + 17 files changed, 832 insertions(+), 53 deletions(-) create mode 100644 packages/melonjs/tests/helpers/tile-layer-edge-cases.js create mode 100644 packages/melonjs/tests/tmxlayer-edge-cases.spec.js create mode 100644 packages/melonjs/tests/webgpu_tmxlayer_edge_cases.spec.js diff --git a/packages/melonjs/CHANGELOG.md b/packages/melonjs/CHANGELOG.md index fa0d70370c..56cb535b29 100644 --- a/packages/melonjs/CHANGELOG.md +++ b/packages/melonjs/CHANGELOG.md @@ -16,12 +16,17 @@ - Tiled, GPU tile layers: the soft edge of a tile larger than the grid shows the tile behind it; it was drawn over the background instead, a thin outline round such tiles - Tiled, GPU tile layers: tiles larger than the grid are drawn whole above the map's first row and right of its last column, where they were cut off - Tiled, GPU tile layers: a tileset with a `margin` whose image stops at its last tiles, with no margin after them, draws the right tiles; they were counted a column or a row short +- Tiled, GPU tile layers: an orthogonal layer whose tiles are larger than the grid and come from more than one tileset, or whose map has a render order other than "right-down", is drawn tile by tile; on the GPU such tiles lay over each other in the wrong order +- Tiled, GPU tile layers: a layer with more columns or rows than the largest texture the GPU takes is drawn tile by tile; it drew nothing - Tiled, GPU tile layers: a layer with a tile that is not square, flipped diagonally, is drawn tile by tile; the shader drew such a tile wrong - Tiled, GPU tile layers: the shader runs at full precision whatever `highPrecisionShader` says; at a lower one a phone could misplace the tiles of a large map - Tiled: a tile layer follows its offset in Tiled (`offsetx`, `offsety`), and its own `pos` when the game moves it: its tiles are drawn there and `getTile()` finds them there, on every renderer and map orientation. Both were ignored, and the layer drawn at the map's origin; only image layers followed their offset. A map whose tile layers have an offset now looks as it does in Tiled - Tiled: tiles that reach past their layer's grid (taller or wider than a cell, or moved by a `tileoffset`) are drawn when they are in view and the grid is not; the whole layer was skipped, so the tops of tall tiles vanished once the map scrolled out of view. The layer's bounds are still its grid's - Tiled: a map centred in the view because it is smaller (as `level.load` centres it), and a tile layer in a container that was moved, are drawn whole. The layer read the camera's view as if it stood at the world's origin: on WebGL, WebGPU and a pre-rendered layer the tiles reaching above or left of the grid were cut, and tile by tile a layer far from the origin lost tiles - Tiled, staggered and hexagonal maps: a tile larger than its cell stands on the cell, and a tileset's `tileoffset` moves its tiles, as in Tiled and on every other map. Drawn tile by tile, such a tile hung down from the top of its cell and the offset was ignored, so the map sat lower than its objects (and than a tile redrawn in a baked layer); tiles of a large tileset also went missing along the edges of the view +- Tiled, hexagonal maps: a side whose length leaves an odd number of pixels of a cell is laid out on whole pixels, as Tiled lays it out; every other column or row drifted half a pixel further. A staggered or hexagonal map of odd tile size, pre-rendered, no longer loses a row and a column of pixels +- Tiled: a tile layer taken out of its container and added again is drawn; it threw. A tile its tileset's image does not hold (an image cropped since the map was saved) is not drawn; drawing it threw every frame +- Tiled, isometric maps drawn tile by tile: tiles narrower than half a cell are no longer left out at the right edge of the view - Tiled, isometric maps drawn tile by tile: tiles of a tileset larger than the map's first one, or moved by a `tileoffset`, went missing along the edges of the view - Tiled, orthogonal maps drawn tile by tile: tiles more than two cells tall went missing along the bottom of the view - `NineSliceSprite` blinks with `flicker()`, as a `Sprite` does; it ignored it diff --git a/packages/melonjs/skills/melonjs-tilemaps/SKILL.md b/packages/melonjs/skills/melonjs-tilemaps/SKILL.md index da107d41cc..3ea557a668 100644 --- a/packages/melonjs/skills/melonjs-tilemaps/SKILL.md +++ b/packages/melonjs/skills/melonjs-tilemaps/SKILL.md @@ -469,14 +469,17 @@ per-pixel GID lookup in the fragment shader. It requires **all** of: - no collection-of-image tileset, and a tileset tile render size of "Tile size" (`tilerendersize`, not "Map grid size"), - on an orthogonal map: no non-zero `tileoffset`, and tiles that reach at most - 4 cells past their own, + 4 cells past their own; a layer of tiles **larger than a cell** takes them + from one tileset, on a map in the default render order ("right-down"), - on an isometric, staggered or hexagonal map: every tile of the layer from **one tileset** (tiles overlap, and their order is kept within a tileset, not across two: split floor and walls into a layer each, which a map usually does already), tiles at most 4 cells wide and 8 tall, a grid of even size (64 × 32, not 65 × 33; a hexagon side of even length), and the normal blend mode. A `tileoffset` is fine, -- no tile that is not square flipped diagonally (rotated, in Tiled). +- no tile that is not square flipped diagonally (rotated, in Tiled), and no + more columns or rows than the largest texture has pixels (4096 on the + smallest devices). A tile set at run time with `setTile` that breaks one of these moves the layer back to `"perTile"`, with a warning. The two paths draw the same picture, with @@ -541,6 +544,7 @@ layer into the offscreen-bake path instead. | after updating to 20.17 a tile layer sits away from where it did | it has an offset in Tiled that was ignored until then, and now moves it as in the editor: clear the layer's Offset in Tiled if it was not meant | | a map smaller than the screen (centred by `level.load`) had the tops of its tall tiles cut on WebGL or WebGPU | before 20.17 a layer read the camera's view as if it stood at the world's origin, and so did a layer in a container the game had moved: update | | the tops of tall tiles vanish when the map itself scrolls out of view | before 20.17 a layer was skipped once its grid left the view, though tiles larger than a cell (or moved by a `tileoffset`) still reached in: update | +| an orthogonal layer warns `tiles larger than the grid, of more than one tileset in a layer` | tiles larger than a cell overlap, and the shader path keeps their order within one tileset: put each tileset's large tiles on a layer of their own (trees apart from the ground they stand on) | | tilemap slow on some machines | layer fell back off the shader path — check the console warning for the reason | | a staggered or hexagonal map sits higher than before 20.17, by how much its tiles are taller than a cell (or by its tileset's `tileoffset`) | it is drawn as Tiled draws it: until then a tile hung from the top of its cell and the offset was ignored, away from the map's objects | | an isometric (staggered, hexagonal) layer warns `tiles of more than one tileset in an isometric layer` and is slow | the shader path draws such a layer with one tileset: put the tiles of each tileset on a layer of their own, or pack them into one tileset | diff --git a/packages/melonjs/src/level/tiled/TMXLayer.js b/packages/melonjs/src/level/tiled/TMXLayer.js index 996375c0fe..5fb50e7b7c 100644 --- a/packages/melonjs/src/level/tiled/TMXLayer.js +++ b/packages/melonjs/src/level/tiled/TMXLayer.js @@ -276,8 +276,13 @@ export default class TMXLayer extends Renderable { // A tile larger than its cell reaches past it: up and to the right, // as a tile stands on the bottom left of its cell (any way on an // oblique map) - const overX = Math.max(this.maxTileSize.width - this.tilewidth, 0); - const overY = Math.max(this.maxTileSize.height - this.tileheight, 0); + // (a staggered or a hexagonal map is laid on the even size below its + // tiles': a tile of odd size stands a pixel past its cell) + const evenGrid = orientation === "staggered" || orientation === "hexagonal"; + const cellWidth = evenGrid ? this.tilewidth & ~1 : this.tilewidth; + const cellHeight = evenGrid ? this.tileheight & ~1 : this.tileheight; + const overX = Math.max(this.maxTileSize.width - cellWidth, 0); + const overY = Math.max(this.maxTileSize.height - cellHeight, 0); const upRight = orientation !== "oblique"; /** * How far past the layer's grid its tiles can be drawn, on each @@ -541,6 +546,32 @@ export default class TMXLayer extends Renderable { // called when the layer is added to the game world or a container onActivateEvent() { + // afresh: a layer taken out of a container and put back is + // activated again + this.animatedTilesets = []; + // A container with a size of its own (one a level was loaded into) + // is in view by its bounds, before its children are asked: it is + // told how far past them this layer's tiles reach + const reach = this._reach; + if ( + reach.left > 0 || + reach.top > 0 || + reach.right > 0 || + reach.bottom > 0 + ) { + for (let node = this.ancestor; node; node = node.ancestor) { + const kept = (node._drawReach ??= { + left: 0, + top: 0, + right: 0, + bottom: 0, + }); + kept.left = Math.max(kept.left, reach.left); + kept.top = Math.max(kept.top, reach.top); + kept.right = Math.max(kept.right, reach.right); + kept.bottom = Math.max(kept.bottom, reach.bottom); + } + } if (this.tilesets) { const tileset = this.tilesets.tilesets; for (let i = 0; i < tileset.length; i++) { @@ -586,8 +617,14 @@ export default class TMXLayer extends Renderable { // an oversized tile reaches past its cell (which way depends on the // orientation: up and to the right, any way on an oblique map), and // a tileset's offset moves all of its tiles - const overX = Math.max(this.maxTileSize.width - this.tilewidth, 0); - const overY = Math.max(this.maxTileSize.height - this.tileheight, 0); + const overX = Math.max( + this.maxTileSize.width - (renderer.tilewidth ?? this.tilewidth), + 0, + ); + const overY = Math.max( + this.maxTileSize.height - (renderer.tileheight ?? this.tileheight), + 0, + ); let left = bounds.left - overX; let top = bounds.top - overY; let right = bounds.right + overX; @@ -798,8 +835,17 @@ export default class TMXLayer extends Renderable { if (!this.tilesets || this.tilesets.tilesets.length === 0) { return { ok: false, reason: "no tilesets" }; } + // the path keeps a texel per cell: no more of them, either way, than + // the largest texture has + const maxTexture = renderer.maxTextureSize ?? Infinity; + if (this.cols > maxTexture || this.rows > maxTexture) { + return { + ok: false, + reason: `more columns or rows than the largest texture has pixels (${maxTexture})`, + }; + } if (this.orientation !== "orthogonal") { - return this._checkOverlappingShaderEligibility(); + return this._checkOverlappingShaderEligibility(renderer); } // the shader iterates a fixed-size loop over candidate cells to // support oversized tiles (tile dim > cell dim) drawn bottom-aligned. @@ -807,14 +853,9 @@ export default class TMXLayer extends Renderable { // so refuse the shader path for layers with extreme oversampling. const MAX_OVERFLOW_CELLS = ORTHOGONAL_MAX_OVERFLOW; for (const ts of this.tilesets.tilesets) { - if (ts.isCollection) { - return { ok: false, reason: "collection-of-image tileset" }; - } - if (ts.tilerendersize !== "tile") { - return { - ok: false, - reason: `tilerendersize "${ts.tilerendersize}" not supported`, - }; + const reason = this._tilesetReason(ts, renderer); + if (reason !== undefined) { + return { ok: false, reason }; } if (ts.tileoffset.x !== 0 || ts.tileoffset.y !== 0) { return { ok: false, reason: "non-zero tileoffset" }; @@ -828,6 +869,10 @@ export default class TMXLayer extends Renderable { }; } } + const overlap = this._checkOrthogonalOverlap(); + if (overlap !== undefined) { + return { ok: false, reason: overlap }; + } if (this._hasDiagonalFlipNotSquare()) { return { ok: false, reason: DIAGONAL_NOT_SQUARE }; } @@ -863,6 +908,116 @@ export default class TMXLayer extends Renderable { return false; } + /** + * Why the shader path cannot draw the tiles of a tileset, on any map, if + * it cannot. + * @param {TMXTileset} tileset - the tileset + * @param {object} [renderer] - the renderer, for its largest texture + * @returns {string|undefined} the reason, or undefined when it can + * @ignore + * @internal + */ + _tilesetReason(tileset, renderer) { + if (tileset.isCollection) { + return "collection-of-image tileset"; + } + if (tileset.tilerendersize !== "tile") { + return `tilerendersize "${tileset.tilerendersize}" not supported`; + } + // an animated tileset has a texel per tile, in one row + const maxTexture = renderer?.maxTextureSize ?? Infinity; + if ( + tileset.isAnimated && + tileset.lastgid - tileset.firstgid + 1 > maxTexture + ) { + return `an animated tileset of more tiles than the largest texture has pixels (${maxTexture})`; + } + return undefined; + } + + /** + * Whether a tileset's tiles are larger than a cell: they overlap the + * cells around theirs. + * @param {TMXTileset} tileset - the tileset + * @returns {boolean} + * @ignore + * @internal + */ + _largerThanCell(tileset) { + return ( + tileset.tilewidth > this.tilewidth || tileset.tileheight > this.tileheight + ); + } + + /** + * Check an orthogonal layer for tiles that overlap in a way the shader + * path does not keep, and note the tilesets its tiles come from. + * + * Tiles the size of a cell never overlap, and any layer of them is fine. + * Tiles larger than a cell lie over their neighbours, the one drawn last + * on top: the shader keeps that order among the tiles of ONE tileset (it + * draws a pass per tileset, each whole pass over the last), and for the + * map's default order alone, from the top down and the left across + * ("right-down"). + * @returns {string|undefined} why the path cannot draw the layer, or + * undefined when it can + * @ignore + * @internal + */ + _checkOrthogonalOverlap() { + /** + * the tilesets an orthogonal layer's tiles come from, when some of + * the map's are larger than a cell (`setTile` adds to them): + * undefined when none is, and nothing can overlap + * @ignore + */ + this._usedTilesets = undefined; + if ( + !this.tilesets.tilesets.some((tileset) => { + return this._largerThanCell(tileset); + }) + ) { + return undefined; + } + const used = new Set(); + const data = this.layerData; + let tileset = null; + for (let i = 0; i < data.length; i += 2) { + const gid = data[i]; + if (gid !== 0 && (tileset === null || !tileset.contains(gid))) { + tileset = this.tilesets.getTilesetByGid(gid); + used.add(tileset); + } + } + this._usedTilesets = used; + return this._orthogonalOverlapReason(); + } + + /** + * Why the tiles of the tilesets an orthogonal layer uses overlap in a way + * the shader path does not keep, if they do (see + * {@link TMXLayer#_checkOrthogonalOverlap}). + * @returns {string|undefined} the reason, or undefined + * @ignore + * @internal + */ + _orthogonalOverlapReason() { + let larger = false; + for (const tileset of this._usedTilesets) { + larger = larger || this._largerThanCell(tileset); + } + if (!larger) { + return undefined; + } + if (this._usedTilesets.size > 1) { + return "tiles larger than the grid, of more than one tileset in a layer"; + } + if (this.renderorder !== "right-down") { + return `render order "${this.renderorder}" with tiles larger than the grid`; + } + return undefined; + } + /** * Check whether this layer of an isometric, staggered or hexagonal map * can take the shader path, and note the tileset it would draw with. @@ -877,7 +1032,7 @@ export default class TMXLayer extends Renderable { * @ignore * @internal */ - _checkOverlappingShaderEligibility() { + _checkOverlappingShaderEligibility(renderer) { this._shaderTileset = null; const kind = kindOf(this.orientation); // Half the cells of a grid of odd size stand half a pixel off (on a @@ -914,7 +1069,7 @@ export default class TMXLayer extends Renderable { } } if (tileset !== null) { - const reason = this._overlappingTilesetReason(tileset); + const reason = this._overlappingTilesetReason(tileset, renderer); if (reason !== undefined) { return { ok: false, reason }; } @@ -935,12 +1090,10 @@ export default class TMXLayer extends Renderable { * @ignore * @internal */ - _overlappingTilesetReason(tileset) { - if (tileset.isCollection) { - return "collection-of-image tileset"; - } - if (tileset.tilerendersize !== "tile") { - return `tilerendersize "${tileset.tilerendersize}" not supported`; + _overlappingTilesetReason(tileset, renderer) { + const unfit = this._tilesetReason(tileset, renderer); + if (unfit !== undefined) { + return unfit; } // The shader looks, for each pixel, at every cell whose tile image // could cover it: a bounded number, of lines of cells and of cells @@ -974,16 +1127,25 @@ export default class TMXLayer extends Renderable { */ _keepShaderEligible(gid, flipMask) { let reason; - if ( - this.orientation !== "orthogonal" && - this._shaderTileset?.contains(gid) !== true - ) { + if (this.orientation === "orthogonal") { + // a tileset the layer did not use yet, where tiles can overlap + if (this._usedTilesets !== undefined) { + const tileset = this.tilesets.getTilesetByGid(gid); + if (!this._usedTilesets.has(tileset)) { + this._usedTilesets.add(tileset); + reason = this._orthogonalOverlapReason(); + } + } + } else if (this._shaderTileset?.contains(gid) !== true) { if (this._shaderTileset) { reason = oneTilesetReason(this.orientation); } else { // an empty layer takes its tileset from its first tile const tileset = this.tilesets.getTilesetByGid(gid); - reason = this._overlappingTilesetReason(tileset); + reason = this._overlappingTilesetReason( + tileset, + this.parentApp?.renderer, + ); if (reason === undefined) { this._shaderTileset = tileset; } @@ -1013,7 +1175,7 @@ export default class TMXLayer extends Renderable { // renderer-side caches keyed by this layer (e.g. the WebGL2 shader // path's per-layer GID index texture) are cleared from the renderer's // own `reset()` path — tile layers only come and go on game reset. - this.animatedTilesets = undefined; + this.animatedTilesets = []; // keep canvasRenderer for reuse — dropping the reference would leak // event listeners registered by CanvasRenderer's constructor } @@ -1264,6 +1426,10 @@ export default class TMXLayer extends Renderable { this.canvasRenderer.renderTarget.invalidate(renderer); renderer.cache.tinted?.delete(this.canvasRenderer.getCanvas()); } + // whether the view is the default camera's, which draws the world + // where its `pos` puts it (any other draws it unmoved): asked of the + // camera, before a plain rectangle stands in for it below + const unmovedWorld = rect.isDefault === false; // the camera's offset (a look ahead, a shake) shifts what it shows // without moving it: the tiles to draw are the ones in that view const offset = rect.offset; @@ -1284,7 +1450,7 @@ export default class TMXLayer extends Renderable { const at = parent.getAbsolutePosition(); shiftX = at.x; shiftY = at.y; - if (rect.isDefault === false) { + if (unmovedWorld) { let root = parent; while (root.ancestor) { root = root.ancestor; diff --git a/packages/melonjs/src/level/tiled/TMXTileset.js b/packages/melonjs/src/level/tiled/TMXTileset.js index 23f32c8cf3..05c390af59 100644 --- a/packages/melonjs/src/level/tiled/TMXTileset.js +++ b/packages/melonjs/src/level/tiled/TMXTileset.js @@ -860,6 +860,16 @@ export default class TMXTileset { dy += this._renderDyCenter; } + // a tile the tileset's image does not hold (an image cropped since the + // map was saved, a `tilecount` larger than the image has): nothing + // to draw, as in Tiled + const frame = this.isCollection + ? undefined + : this.atlas[this.getViewTileId(gid)]; + if (!this.isCollection && frame === undefined) { + return; + } + // check if any transformation is required if (flipMask !== 0) { renderer.save(); @@ -892,7 +902,7 @@ export default class TMXTileset { dh, ); } else { - const offset = this.atlas[this.getViewTileId(gid)].offset; + const offset = frame.offset; renderer.drawImage( this.image, offset.x, diff --git a/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js b/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js index 63d1bf2935..93f84d5abc 100644 --- a/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js +++ b/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js @@ -46,8 +46,12 @@ export default class TMXHexagonalRenderer extends TMXRenderer { } } - this.sideoffsetx = (this.tilewidth - this.sidelengthx) / 2; - this.sideoffsety = (this.tileheight - this.sidelengthy) / 2; + // in whole numbers, as Tiled's own (libtiled's `RenderParams`): a + // side of odd length leaves an odd number of pixels to halve, and + // the half pixel is dropped, not kept: columns and rows stay on + // whole pixels, where Tiled has them + this.sideoffsetx = Math.trunc((this.tilewidth - this.sidelengthx) / 2); + this.sideoffsety = Math.trunc((this.tileheight - this.sidelengthy) / 2); this.columnwidth = this.sideoffsetx + this.sidelengthx; this.rowheight = this.sideoffsety + this.sidelengthy; @@ -498,7 +502,10 @@ export default class TMXHexagonalRenderer extends TMXRenderer { ); } } - rowPos.x += this.tilewidth + this.sidelengthx; + // the next cell of the line, two columns on: where + // `tileToPixelCoords` puts it (a side of odd length + // leaves the tile's width a pixel more than that) + rowPos.x += this.columnwidth * 2; } if (staggeredRow) { @@ -559,7 +566,10 @@ export default class TMXHexagonalRenderer extends TMXRenderer { ); } } - rowPos.x += this.tilewidth + this.sidelengthx; + // the next cell of the line, two columns on: where + // `tileToPixelCoords` puts it (a side of odd length + // leaves the tile's width a pixel more than that) + rowPos.x += this.columnwidth * 2; } startPos.y += this.rowheight; } diff --git a/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js b/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js index f81f5de6e0..2f9e2d0d64 100644 --- a/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js +++ b/packages/melonjs/src/level/tiled/renderer/TMXIsometricRenderer.js @@ -166,8 +166,15 @@ export default class TMXIsometricRenderer extends TMXRenderer { // missing along the bottom of the view, where their cell is below it // and their image still reaches in, and so did tiles moved into view // by an offset larger than a tile - const reachX = layer.maxTileSize.width + (layer.maxTileOffset?.x ?? 0); - const reachY = layer.maxTileSize.height + (layer.maxTileOffset?.y ?? 0); + // (by a cell at least: the corners below are rounded to whole + // cells, half a cell either way, which a tile narrower than that + // does not make up for) + const reachX = + Math.max(layer.maxTileSize.width, this.tilewidth) + + (layer.maxTileOffset?.x ?? 0); + const reachY = + Math.max(layer.maxTileSize.height, this.tileheight) + + (layer.maxTileOffset?.y ?? 0); const rowItr = this.pixelToTileCoords( rect.pos.x - reachX, rect.pos.y - reachY, diff --git a/packages/melonjs/src/renderable/container.js b/packages/melonjs/src/renderable/container.js index c7673d4712..ad02df323e 100644 --- a/packages/melonjs/src/renderable/container.js +++ b/packages/melonjs/src/renderable/container.js @@ -1,5 +1,6 @@ import { colorPool } from "../math/color.ts"; import { Matrix3d } from "../math/matrix3d.ts"; +import { boundsPool } from "../physics/bounds.ts"; import Body from "../physics/builtin/body.js"; import { needsShapeResolution, @@ -1130,6 +1131,36 @@ export default class Container extends Renderable { return obj.length > 0 ? obj[0] : null; } + /** + * The area this container draws in, when something in it draws past its + * own bounds and has said so (a tile layer whose tiles stand above its + * grid, see `TMXLayer#getDrawBounds`): the container's bounds, grown by + * as much. A camera has the container in view by this area, as it would + * the layer (see `Camera2d#isVisible`). + * @returns {Bounds|undefined} that area, or undefined when it is the + * container's bounds (or the container has no size of its own) + * @ignore + */ + getDrawBounds() { + const reach = this._drawReach; + if (reach === undefined) { + return undefined; + } + const bounds = this.getBounds(); + if (!bounds.isFinite()) { + return undefined; + } + /** @ignore */ + this._drawBounds ??= boundsPool.get(); + this._drawBounds.setMinMax( + bounds.left - reach.left, + bounds.top - reach.top, + bounds.right + reach.right, + bounds.bottom + reach.bottom, + ); + return this._drawBounds; + } + /** * return all child in this container * @returns {Renderable[]} an array of renderable object diff --git a/packages/melonjs/src/video/renderer.js b/packages/melonjs/src/video/renderer.js index 28a65333bc..c4a1b8f5a9 100644 --- a/packages/melonjs/src/video/renderer.js +++ b/packages/melonjs/src/video/renderer.js @@ -211,6 +211,16 @@ export default class Renderer { */ this.supportsShaderTileLayers = false; + /** + * The largest texture this renderer can make, in pixels a side: no + * limit on Canvas. A tile layer drawn by the GPU keeps a texel per + * cell, so a layer with more columns or rows than this is drawn + * tile by tile. + * @type {number} + * @ignore + */ + this.maxTextureSize = Infinity; + /** * Whether this backend can render into a half-float color buffer. * Renderability alone; {@link Renderer#supportsHDR} is the one to diff --git a/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag b/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag index 214b7a5e84..d6fc5fa175 100644 --- a/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag +++ b/packages/melonjs/src/video/webgl/shaders/tmxlayer-common.frag @@ -160,6 +160,11 @@ bool tryRenderCell(int cx, int cy, vec2 worldPx, Footprint fp, out vec4 outColor // may land a hair under the row it should int cols = int(uTilesetCols.x); int row = localId / cols; + // a tile the tileset's image does not hold (an image cropped since the + // map was saved): nothing to draw + if (row >= int(uTilesetCols.y)) { + return false; + } vec2 tileOriginPx = uTilesetMargin.xy + vec2(float(localId - row * cols), float(row)) * (uTileSize + uTilesetMargin.zw); vec2 texelPx = tileOriginPx + inTile * uTileSize; diff --git a/packages/melonjs/src/video/webgl/webgl_renderer.js b/packages/melonjs/src/video/webgl/webgl_renderer.js index 0aaf2676c2..6afeb19b87 100644 --- a/packages/melonjs/src/video/webgl/webgl_renderer.js +++ b/packages/melonjs/src/video/webgl/webgl_renderer.js @@ -521,6 +521,7 @@ export default class WebGLRenderer extends Renderer { // this backend renders TMX tile layers on the GPU (see TMXLayer) this.supportsShaderTileLayers = true; + this.maxTextureSize = this.gl.getParameter(this.gl.MAX_TEXTURE_SIZE); // this backend keeps mesh geometry resident on the GPU, so meshes can // hand it their model matrix instead of pre-transformed vertices diff --git a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl index a5b208c24b..4e7f1f6151 100644 --- a/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl +++ b/packages/melonjs/src/video/webgpu/shaders/tmxlayer-common.wgsl @@ -163,6 +163,11 @@ fn tryRenderCell(cx : i32, cy : i32, worldPx : vec2f, fp : Footprint) -> CellSam // may land a hair under the row it should let cols = i32(tmx.uTilesetCols.x); let row = localId / cols; + // a tile the tileset's image does not hold (an image cropped since the + // map was saved): nothing to draw + if (row >= i32(tmx.uTilesetCols.y)) { + return miss; + } let tileOriginPx = tmx.uTilesetMargin.xy + vec2f(f32(localId - row * cols), f32(row)) * (tmx.uTileSize + tmx.uTilesetMargin.zw); var texelPx = tileOriginPx + inTile * tmx.uTileSize; diff --git a/packages/melonjs/src/video/webgpu/webgpu_renderer.js b/packages/melonjs/src/video/webgpu/webgpu_renderer.js index dfadff3173..d2c25cdb55 100644 --- a/packages/melonjs/src/video/webgpu/webgpu_renderer.js +++ b/packages/melonjs/src/video/webgpu/webgpu_renderer.js @@ -199,6 +199,7 @@ export default class WebGPURenderer extends Renderer { this.supportsDepthBuffer = true; this.supportsRetainedMesh = true; this.supportsInstancing = true; + // (`maxTextureSize` is the device's, once there is one: see `init`) // `rgba16float` is renderable, blendable and filterable in core // WebGPU, with no optional feature to request and nothing to probe — // unlike WebGL2, where it takes an extension that some drivers do @@ -555,6 +556,8 @@ export default class WebGPURenderer extends Renderer { return adapter.features.has(feature); }), }); + this.maxTextureSize = + this.device.limits?.maxTextureDimension2D ?? this.maxTextureSize; // identify the driver in the console header, same as the WebGL // backend does through its debug-renderer-info strings. The diff --git a/packages/melonjs/tests/helpers/tile-layer-compare.js b/packages/melonjs/tests/helpers/tile-layer-compare.js index 64e59d8da7..5f925c086b 100644 --- a/packages/melonjs/tests/helpers/tile-layer-compare.js +++ b/packages/melonjs/tests/helpers/tile-layer-compare.js @@ -311,17 +311,22 @@ export function reference(scene, viewX, viewY, width, height) { const side = orientation === "hexagonal" ? hexsidelength : 0; const sideX = staggerX ? side : 0; const sideY = staggerX ? 0 : side; - const columnWidth = (tw - sideX) / 2 + sideX; - const rowHeight = (th - sideY) / 2 + sideY; + // Tiled's own numbers (libtiled's hexagonal `RenderParams`), all + // whole: the grid is the even size below the tiles', and half + // of what a side leaves of a cell is rounded down + const cw = tw & ~1; + const ch = th & ~1; + const columnWidth = Math.trunc((cw - sideX) / 2) + sideX; + const rowHeight = Math.trunc((ch - sideY) / 2) + sideY; const place = ({ col, row }) => { // `& 1` and not `% 2`: an infinite map has negative cells return staggerX ? { x: col * columnWidth, - y: row * (th + sideY) + ((col & 1) ^ even) * rowHeight, + y: row * (ch + sideY) + ((col & 1) ^ even) * rowHeight, } : { - x: col * (tw + sideX) + ((row & 1) ^ even) * columnWidth, + x: col * (cw + sideX) + ((row & 1) ^ even) * columnWidth, y: row * rowHeight, }; }; @@ -332,11 +337,16 @@ export function reference(scene, viewX, viewY, width, height) { return a.y - b.y || a.x - b.x; }); for (const { gid, x, y } of placed) { - drawTile(gid, ox + x, oy + y + th); + drawTile(gid, ox + x, oy + y + ch); } } else { + // the map's render order: rows from the top or the bottom, each + // from the left or the right ("right-down" unless said) + const order = scene.extra?.renderorder ?? "right-down"; + const down = order.endsWith("down") ? 1 : -1; + const right = order.startsWith("right") ? 1 : -1; ordered.sort((a, b) => { - return a.row - b.row || a.col - b.col; + return down * (a.row - b.row) || right * (a.col - b.col); }); for (const { col, row, gid } of ordered) { drawTile(gid, ox + col * tw, oy + (row + 1) * th); diff --git a/packages/melonjs/tests/helpers/tile-layer-edge-cases.js b/packages/melonjs/tests/helpers/tile-layer-edge-cases.js new file mode 100644 index 0000000000..1990d88a1d --- /dev/null +++ b/packages/melonjs/tests/helpers/tile-layer-edge-cases.js @@ -0,0 +1,501 @@ +/** + * Tile layers at their edges: the cases a set of adversarial probes broke, + * each held to the picture painted with no engine (`tile-layer-compare.js`). + * A layer the GPU path cannot draw right is left to the CPU renderer, with a + * word in the console, and is still drawn as Tiled draws it. + * + * One spec file per GPU backend calls this. + */ +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; +import { Container } from "../../src/index.js"; +import { + comparePaths, + diff, + drawIn, + drawn, + fill, + makeApp, + makeMap, + makeTileset, + reference, + show, +} from "./tile-layer-compare.js"; + +const W = 320; +const H = 240; + +const all = (cols, rows) => { + return fill(cols, rows, (c, r) => { + return 1 + ((c + r * 3) % 8); + }); +}; + +/** + * Declare the cases for one GPU backend. + * @param {number} renderer - `video.WEBGL` or `video.WEBGPU` + * @param {string} type - the `renderer.type` expected ("WebGL", "WebGPU") + */ +export function edgeCases(renderer, type) { + describe(`tile layers at their edges (${type})`, () => { + let app; + beforeAll(async () => { + app = await makeApp(renderer, type, W, H); + }); + afterAll(() => { + app?.world.pos.set(0, 0, 0); + app?.destroy(); + vi.restoreAllMocks(); + }); + const need = (ctx) => { + if (app === null || app === undefined) { + ctx.skip(`${type} not available in this environment`); + } + }; + + /** + * Put a scene in the world, and say how its first layer is drawn and + * what was warned. + */ + const shown = (scene) => { + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + const layers = show(app, makeMap(scene)); + return { + layers, + mode: layers[0].renderMode, + said: warn.mock.calls.flat().join(" "), + }; + } finally { + warn.mockRestore(); + } + }; + + /** + * A layer left to the CPU renderer for `reason`: drawn as the + * reference, as it stands and when asked for the GPU path by hand. + */ + const fallsBack = async (scene, reason, views = [[-40, -60]]) => { + const { layers, mode, said } = shown(scene); + expect(mode).toBe("perTile"); + expect(said).toMatch(reason); + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + const ref = reference(scene, x, y, W, H); + expect(drawn(ref)).toBeGreaterThan(0); + const cpu = await drawIn(app, layers, "perTile"); + const c = diff(ref, cpu.frame); + expect(c.different, `tile by tile: ${c.first}`).toBe(0); + // by hand: still tile by tile, and the same picture + const forced = await drawIn(app, layers, "shader"); + expect(forced.cpuDraws).toBe(layers.length); + const f = diff(ref, forced.frame); + expect(f.different, `asked for by hand: ${f.first}`).toBe(0); + } + }; + + describe("what is in view", () => { + it("isometric, tile by tile: a tile narrower than half a cell, at the right edge of the view", async (ctx) => { + need(ctx); + // the view's corners are rounded to whole cells, half a cell + // either way: a tile 8 pixels wide does not make up for it + const scene = { + orientation: "isometric", + cols: 1, + rows: 1, + tw: 64, + th: 32, + tilesets: [makeTileset({ tw: 8, th: 8 })], + layers: [{ data: [1] }], + }; + const layers = show(app, makeMap(scene)); + // the one tile is drawn at x 0 to 7: these views end 1 to 8 + // pixels right of its left edge + for (let right = 1; right <= 8; right++) { + const x = right - W; + app.viewport.moveTo(x, -120); + const ref = reference(scene, x, -120, W, H); + expect(drawn(ref), "the tile is in view").toBe(right * 8); + for (const mode of ["shader", "perTile"]) { + const { frame } = await drawIn(app, layers, mode); + const d = diff(ref, frame); + expect(d.different, `${mode}, ${right} px in view`).toBe(0); + } + } + }); + + it("a layer in a container with a size: the tops of tall tiles, the grid out of view", async (ctx) => { + need(ctx); + // a level loaded into a container gives it the map's size: + // the camera has the container in view by its bounds before + // it asks the layer, whose tiles stand 64 pixels above + const scene = { + orientation: "orthogonal", + cols: 4, + rows: 3, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 96 })], + layers: [{ data: all(4, 3) }], + }; + // the view ends 10 pixels above the grid's first row + const view = [-40, -H - 10]; + const ref = reference(scene, ...view, W, H); + expect(drawn(ref)).toBeGreaterThan(0); + for (const boxed of [false, true]) { + app.world.reset(); + app.world.pos.set(0, 0, app.world.pos.z); + app.renderer.reset?.(); + app.renderer.backgroundColor.setColor(16, 24, 48); + const map = makeMap(scene); + const box = boxed ? new Container() : app.world; + if (boxed) { + app.world.addChild(box); + } + map.addTo(box, true, false); + app.world.sortNow(true); + app.viewport.setBounds(-100000, -100000, 200000, 200000); + app.viewport.moveTo(...view); + for (const mode of ["perTile", "shader"]) { + const { frame } = await drawIn(app, map.getLayers(), mode); + const d = diff(ref, frame); + expect( + d.different, + `${boxed ? "in a container" : "in the world"}, ${mode}: ${d.first}`, + ).toBe(0); + } + } + }); + + for (const mode of ["perTile", "shader", "prerender"]) { + it(`a zoomed camera with an offset, over a moved world, drawn "${mode}"`, async (ctx) => { + need(ctx); + // the camera at `pos + offset` with no offset shows the + // same thing, by definition: zoomed, it is not the + // default camera, and draws the world unmoved + const scene = { + orientation: "orthogonal", + cols: 12, + rows: 12, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [{ data: all(12, 12) }], + }; + app.world.reset(); + app.renderer.reset?.(); + app.renderer.backgroundColor.setColor(16, 24, 48); + // the world moved first, as a centred map leaves it + app.world.pos.set(100, 60, app.world.pos.z); + const map = makeMap(scene); + const layers = map.getLayers(); + if (mode === "prerender") { + layers[0].renderMode = mode; + } + map.addTo(app.world, true, false); + app.world.sortNow(true); + app.viewport.setBounds(-100000, -100000, 200000, 200000); + try { + for (const zoom of [1, 2]) { + app.viewport.zoom = zoom; + app.viewport.offset.set(0, 0); + app.viewport.moveTo(60 + 26, 40 - 18); + const moved = await drawIn(app, layers, mode); + expect(drawn(moved.frame)).toBeGreaterThan(0); + app.viewport.offset.set(26, -18); + app.viewport.moveTo(60, 40); + const shaken = await drawIn(app, layers, mode); + const d = diff(moved.frame, shaken.frame); + expect(d.different, `zoom ${zoom}: ${d.first}`).toBe(0); + } + } finally { + app.viewport.zoom = 1; + app.viewport.offset.set(0, 0); + app.world.pos.set(0, 0, app.world.pos.z); + } + }); + } + }); + + describe("grids of odd sizes, laid out as Tiled lays them out", () => { + it("a staggered map of odd tile size: on the even grid below, tile by tile and baked", async (ctx) => { + need(ctx); + // 63 x 31 tiles stand on 62 x 30 cells: one pixel above and + // one to the right of each + const scene = { + orientation: "staggered", + cols: 4, + rows: 4, + tw: 63, + th: 31, + tilesets: [makeTileset({ tw: 63, th: 31 })], + layers: [{ data: all(4, 4) }], + extra: { staggeraxis: "y", staggerindex: "odd" }, + }; + await fallsBack(scene, /odd size/, [ + [-20, -20], + [40, 10], + ]); + const baked = show(app, makeMap(scene), "prerender"); + for (const [x, y] of [ + [-20, -20], + [40, 10], + ]) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, baked, "prerender"); + const d = diff(reference(scene, x, y, W, H), frame); + expect(d.different, `baked: ${d.first}`).toBe(0); + } + }); + + for (const [axis, tw, th, side] of [ + ["x", 28, 24, 11], + ["y", 28, 24, 11], + ["x", 30, 24, 7], + // longer than the tile: the half left of a cell is negative + ["x", 28, 24, 33], + ]) { + it(`a hexagon side of ${side} on ${tw} x ${th} cells, axis ${axis}: columns and rows on whole pixels`, async (ctx) => { + need(ctx); + // Tiled halves what a side leaves of a cell in whole + // numbers: 28 wide with a side of 11 is columns 19 + // pixels apart, not 19.5 + const scene = { + orientation: "hexagonal", + cols: 6, + rows: 5, + tw, + th, + tilesets: [makeTileset({ tw, th })], + layers: [{ data: all(6, 5) }], + extra: { + staggeraxis: axis, + staggerindex: "odd", + hexsidelength: side, + }, + }; + const views = [ + [-20, -20], + [35, 12], + ]; + expect(await comparePaths(app, scene, { views })).toBeGreaterThan(0); + }); + } + }); + + describe("orthogonal layers whose tiles overlap", () => { + const big = (options = {}) => { + return makeTileset({ tw: 48, th: 48, ...options }); + }; + + it("tiles of two tilesets, larger than the grid, in one layer", async (ctx) => { + need(ctx); + // a later cell lies over an earlier one, whichever tileset + // its tile is from: a pass per tileset cannot keep that + const scene = { + orientation: "orthogonal", + cols: 4, + rows: 3, + tw: 32, + th: 32, + tilesets: [big(), big()], + layers: [ + { + // a checkerboard of the two tilesets + data: fill(4, 3, (c, r) => { + return 1 + ((c + r) % 2) * 8 + ((c + r * 3) % 8); + }), + }, + ], + }; + await fallsBack(scene, /more than one tileset/); + }); + + it("tiles of two tilesets, the size of the grid: nothing overlaps, and the GPU draws them", async (ctx) => { + need(ctx); + const scene = { + orientation: "orthogonal", + cols: 6, + rows: 5, + tw: 32, + th: 32, + tilesets: [ + makeTileset({ tw: 32, th: 32 }), + makeTileset({ tw: 32, th: 32 }), + ], + layers: [ + { + data: fill(6, 5, (c, r) => { + return 1 + ((c + r) % 2) * 8 + ((c + r * 3) % 8); + }), + }, + ], + }; + expect(await comparePaths(app, scene)).toBeGreaterThan(0); + }); + + it("a large tileset in the map that the layer does not use", async (ctx) => { + need(ctx); + // the layer's own tiles are the size of the grid + const scene = { + orientation: "orthogonal", + cols: 6, + rows: 5, + tw: 32, + th: 32, + tilesets: [ + makeTileset({ tw: 32, th: 32 }), + makeTileset({ tw: 32, th: 32 }), + big(), + ], + layers: [ + { + data: fill(6, 5, (c, r) => { + return 1 + ((c + r) % 2) * 8 + ((c + r * 3) % 8); + }), + }, + ], + }; + const { layers, mode } = shown(scene); + expect(mode).toBe("shader"); + expect(await comparePaths(app, scene, { layers })).toBeGreaterThan(0); + // until a tile of the large one is set: three tilesets in + // use, one of them overlapping + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + scene.layers[0].data[2 * 6 + 2] = 19; + layers[0].setTile(layers[0].getTileById(19, 2, 2), 2, 2); + expect(layers[0].renderMode).toBe("perTile"); + expect(warn.mock.calls.flat().join(" ")).toMatch( + /more than one tileset/, + ); + } finally { + warn.mockRestore(); + } + app.viewport.moveTo(0, 0); + const { frame } = await drawIn(app, layers, "perTile"); + const d = diff(reference(scene, 0, 0, W, H), frame); + expect(d.different, d.first).toBe(0); + }); + + for (const order of ["right-up", "left-down", "left-up"]) { + it(`render order "${order}", tiles larger than the grid`, async (ctx) => { + need(ctx); + // the map walks its rows and columns the way it says: + // with "left-up" the first cell's tile is in front + const scene = { + orientation: "orthogonal", + cols: 4, + rows: 3, + tw: 32, + th: 32, + tilesets: [big()], + layers: [{ data: all(4, 3) }], + extra: { renderorder: order }, + }; + await fallsBack(scene, /render order/); + }); + + it(`render order "${order}", tiles the size of the grid: on the GPU`, async (ctx) => { + need(ctx); + const scene = { + orientation: "orthogonal", + cols: 6, + rows: 5, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [{ data: all(6, 5) }], + extra: { renderorder: order }, + }; + expect(await comparePaths(app, scene)).toBeGreaterThan(0); + }); + } + }); + + describe("layers past what the GPU holds, and tiles the image does not", () => { + for (const orientation of ["orthogonal", "isometric"]) { + it(`${orientation}: a layer with more columns than the largest texture has pixels`, async (ctx) => { + need(ctx); + // the GPU path keeps a texel per cell + const cols = app.renderer.maxTextureSize + 1; + expect(Number.isFinite(cols)).toBe(true); + const iso = orientation === "isometric"; + const scene = { + orientation, + cols, + rows: 1, + tw: iso ? 64 : 32, + th: 32, + tilesets: [makeTileset({ tw: iso ? 64 : 32, th: 32 })], + layers: [ + { + data: fill(cols, 1, (c) => { + return 1 + (c % 8); + }), + }, + ], + }; + await fallsBack(scene, /largest texture/, [[0, iso ? -100 : 0]]); + }); + } + + it("a tile the tileset's image does not hold is not drawn, either way", async (ctx) => { + need(ctx); + // an image cropped since the map was saved: 4 tiles of the 8 + // the tileset says it has + const scene = { + orientation: "orthogonal", + cols: 6, + rows: 5, + tw: 32, + th: 32, + tilesets: [ + makeTileset({ tw: 32, th: 32, count: 4, extra: { tilecount: 8 } }), + ], + layers: [{ data: all(6, 5) }], + }; + const { layers } = shown(scene); + app.viewport.moveTo(0, 0); + const cpu = await drawIn(app, layers, "perTile"); + const gpu = await drawIn(app, layers, "shader"); + // the tiles the image holds, and none of the others + const held = { + ...scene, + layers: [ + { + data: scene.layers[0].data.map((gid) => { + return gid <= 4 ? gid : 0; + }), + }, + ], + }; + const ref = reference(held, 0, 0, W, H); + expect(drawn(ref)).toBeGreaterThan(0); + expect(diff(ref, cpu.frame).different, "tile by tile").toBe(0); + expect(diff(ref, gpu.frame).different, "on the GPU").toBe(0); + }); + + it("a layer taken out of the world and put back is drawn again", async (ctx) => { + need(ctx); + const scene = { + orientation: "orthogonal", + cols: 6, + rows: 5, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [{ data: all(6, 5) }], + }; + const layers = show(app, makeMap(scene)); + const layer = layers[0]; + app.world.removeChildNow(layer, true); + expect(() => { + app.world.addChild(layer); + }).not.toThrow(); + app.world.sortNow(true); + expect(layer.renderMode).toBe("shader"); + expect(await comparePaths(app, scene, { layers })).toBeGreaterThan(0); + }); + }); + }); +} diff --git a/packages/melonjs/tests/helpers/tile-layer-staggered-scenarios.js b/packages/melonjs/tests/helpers/tile-layer-staggered-scenarios.js index a78cb54f47..58b15352e1 100644 --- a/packages/melonjs/tests/helpers/tile-layer-staggered-scenarios.js +++ b/packages/melonjs/tests/helpers/tile-layer-staggered-scenarios.js @@ -461,18 +461,12 @@ export function staggeredScenarios(renderer, type) { expect(said).toMatch(/larger than the shader's limit/); }); - it("a grid whose cells stand half a pixel off", (ctx) => { + it("tiles of odd size, which stand a pixel past their cell", (ctx) => { need(ctx); - // a hexagon side of odd length: every other column is half a - // pixel off - const grid = { - orientation: "hexagonal", - tw: 28, - th: 24, - hexsidelength: 11, - }; + // the map is laid on the even grid below: 62 x 30 cells + const grid = { orientation: "staggered", tw: 63, th: 31 }; const [mode, said] = modeOf( - staggeredScene(grid, "x", "odd", makeTileset({ tw: 28, th: 24 })), + staggeredScene(grid, "y", "odd", makeTileset({ tw: 63, th: 31 })), ); expect(mode).toBe("perTile"); expect(said).toMatch(/odd size/); diff --git a/packages/melonjs/tests/tmxlayer-edge-cases.spec.js b/packages/melonjs/tests/tmxlayer-edge-cases.spec.js new file mode 100644 index 0000000000..70835be045 --- /dev/null +++ b/packages/melonjs/tests/tmxlayer-edge-cases.spec.js @@ -0,0 +1,8 @@ +/** + * Tile layers at their edges, on WebGL: see + * `helpers/tile-layer-edge-cases.js`. + */ +import { video } from "../src/index.js"; +import { edgeCases } from "./helpers/tile-layer-edge-cases.js"; + +edgeCases(video.WEBGL, "WebGL"); diff --git a/packages/melonjs/tests/webgpu_tmxlayer_edge_cases.spec.js b/packages/melonjs/tests/webgpu_tmxlayer_edge_cases.spec.js new file mode 100644 index 0000000000..4f5f3b8e28 --- /dev/null +++ b/packages/melonjs/tests/webgpu_tmxlayer_edge_cases.spec.js @@ -0,0 +1,9 @@ +/** + * Tile layers at their edges, on WebGPU: see + * `helpers/tile-layer-edge-cases.js`. Skipped where the browser has no + * WebGPU adapter. + */ +import { video } from "../src/index.js"; +import { edgeCases } from "./helpers/tile-layer-edge-cases.js"; + +edgeCases(video.WEBGPU, "WebGPU"); From 4ada8ff291202f4b2038422baa794ccaa6dc5c60 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 04:40:29 +0000 Subject: [PATCH 09/11] Tile layers: the rest of the adversarial audit (#1897) Staggered and hexagonal maps are laid out with Tiled 1.10's own numbers: what a side leaves of a tile is halved in whole numbers, and the cell a tile stands on is made of those halves, a pixel smaller than a tile of odd size. A map of odd sizes is on whole pixels, so the GPU path takes it; the shader's pixels are measured from as much lower as the cell is shorter. A painter written apart from the tests' reference, from Tiled's source, agrees with both paths. A container holds the tile layers that can draw outside its bounds, and is in view as far as they do: their tiles' reach, a layer's offset, an infinite map's first chunks. (A level loaded into a container gives it the map's size, and the camera asks the container before its layers.) A tile its tileset's image does not hold is left undrawn by `drawTile` too: setting one on a baked layer threw. Tests, from the gaps the audit listed: pictures for the grids of odd sizes, where only the render mode was looked at; flips on hexagons; an infinite map with chunks at odd coordinates; a layer offset and an infinite map in a container; a sliver of 1, 2 and 5 pixels of each orientation at each edge of the view, and a pixel out of sight; `getTile` at the middle of every cell of every kind of map, the layer moved; and two assertions that could not fail made to. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01JnzwA4a7ZyjLBpbeEpFLu6 --- packages/melonjs/CHANGELOG.md | 3 +- packages/melonjs/src/level/tiled/TMXLayer.js | 58 ++--- .../melonjs/src/level/tiled/TMXTileset.js | 11 +- .../tiled/renderer/TMXHexagonalRenderer.js | 20 +- packages/melonjs/src/renderable/container.js | 27 ++- .../src/video/gpu/tmxlayer_orientation.js | 13 +- .../tests/helpers/tile-layer-compare.js | 21 +- .../tests/helpers/tile-layer-edge-cases.js | 228 +++++++++++++++++- .../helpers/tile-layer-staggered-scenarios.js | 80 +++++- .../melonjs/tests/tmxlayer-offset.spec.js | 78 ++++++ 10 files changed, 455 insertions(+), 84 deletions(-) diff --git a/packages/melonjs/CHANGELOG.md b/packages/melonjs/CHANGELOG.md index 56cb535b29..ac85585d49 100644 --- a/packages/melonjs/CHANGELOG.md +++ b/packages/melonjs/CHANGELOG.md @@ -24,7 +24,8 @@ - Tiled: tiles that reach past their layer's grid (taller or wider than a cell, or moved by a `tileoffset`) are drawn when they are in view and the grid is not; the whole layer was skipped, so the tops of tall tiles vanished once the map scrolled out of view. The layer's bounds are still its grid's - Tiled: a map centred in the view because it is smaller (as `level.load` centres it), and a tile layer in a container that was moved, are drawn whole. The layer read the camera's view as if it stood at the world's origin: on WebGL, WebGPU and a pre-rendered layer the tiles reaching above or left of the grid were cut, and tile by tile a layer far from the origin lost tiles - Tiled, staggered and hexagonal maps: a tile larger than its cell stands on the cell, and a tileset's `tileoffset` moves its tiles, as in Tiled and on every other map. Drawn tile by tile, such a tile hung down from the top of its cell and the offset was ignored, so the map sat lower than its objects (and than a tile redrawn in a baked layer); tiles of a large tileset also went missing along the edges of the view -- Tiled, hexagonal maps: a side whose length leaves an odd number of pixels of a cell is laid out on whole pixels, as Tiled lays it out; every other column or row drifted half a pixel further. A staggered or hexagonal map of odd tile size, pre-rendered, no longer loses a row and a column of pixels +- Tiled, staggered and hexagonal maps of odd sizes are laid out in whole numbers, as Tiled 1.10 lays them out: a tile of odd size stands on a cell a pixel smaller, and a hexagon side that leaves an odd number of pixels of a tile drops the half pixel. Every other column or row drifted half a pixel further, and a pre-rendered layer lost a row and a column of pixels +- Tiled: a level loaded into a container of its own is in view as far as its tile layers draw: their tiles' reach, a layer's offset, and an infinite map's chunks left of and above its origin were cut off once the map's own rectangle left the view - Tiled: a tile layer taken out of its container and added again is drawn; it threw. A tile its tileset's image does not hold (an image cropped since the map was saved) is not drawn; drawing it threw every frame - Tiled, isometric maps drawn tile by tile: tiles narrower than half a cell are no longer left out at the right edge of the view - Tiled, isometric maps drawn tile by tile: tiles of a tileset larger than the map's first one, or moved by a `tileoffset`, went missing along the edges of the view diff --git a/packages/melonjs/src/level/tiled/TMXLayer.js b/packages/melonjs/src/level/tiled/TMXLayer.js index 5fb50e7b7c..c1d62d3396 100644 --- a/packages/melonjs/src/level/tiled/TMXLayer.js +++ b/packages/melonjs/src/level/tiled/TMXLayer.js @@ -276,11 +276,11 @@ export default class TMXLayer extends Renderable { // A tile larger than its cell reaches past it: up and to the right, // as a tile stands on the bottom left of its cell (any way on an // oblique map) - // (a staggered or a hexagonal map is laid on the even size below its - // tiles': a tile of odd size stands a pixel past its cell) - const evenGrid = orientation === "staggered" || orientation === "hexagonal"; - const cellWidth = evenGrid ? this.tilewidth & ~1 : this.tilewidth; - const cellHeight = evenGrid ? this.tileheight & ~1 : this.tileheight; + // (the cell is the map renderer's: on a staggered or a hexagonal map + // it can be a pixel less than the map's tile size) + const grid = map.getRenderer?.(); + const cellWidth = grid?.tilewidth ?? this.tilewidth; + const cellHeight = grid?.tileheight ?? this.tileheight; const overX = Math.max(this.maxTileSize.width - cellWidth, 0); const overY = Math.max(this.maxTileSize.height - cellHeight, 0); const upRight = orientation !== "oblique"; @@ -550,27 +550,14 @@ export default class TMXLayer extends Renderable { // activated again this.animatedTilesets = []; // A container with a size of its own (one a level was loaded into) - // is in view by its bounds, before its children are asked: it is - // told how far past them this layer's tiles reach - const reach = this._reach; - if ( - reach.left > 0 || - reach.top > 0 || - reach.right > 0 || - reach.bottom > 0 - ) { - for (let node = this.ancestor; node; node = node.ancestor) { - const kept = (node._drawReach ??= { - left: 0, - top: 0, - right: 0, - bottom: 0, - }); - kept.left = Math.max(kept.left, reach.left); - kept.top = Math.max(kept.top, reach.top); - kept.right = Math.max(kept.right, reach.right); - kept.bottom = Math.max(kept.bottom, reach.bottom); - } + // is in view by its bounds, before its children are asked: each one + // the layer is in is told that the layer can draw outside them (its + // tiles' reach, its offset, an infinite map's first chunks) + /** @ignore */ + this._toldAncestors = []; + for (let node = this.ancestor; node; node = node.ancestor) { + (node._drawPast ??= new Set()).add(this); + this._toldAncestors.push(node); } if (this.tilesets) { const tileset = this.tilesets.tilesets; @@ -1035,16 +1022,13 @@ export default class TMXLayer extends Renderable { _checkOverlappingShaderEligibility(renderer) { this._shaderTileset = null; const kind = kindOf(this.orientation); - // Half the cells of a grid of odd size stand half a pixel off (on a - // hexagonal map, of a side of odd length). Tile by tile, each tile - // is moved to a whole pixel; the shader has one quad for all of - // them, and would draw those a pixel away. - const map = this.getRenderer(); + // Half the tiles of an isometric grid of odd size stand half a pixel + // off. Tile by tile, each is moved to a whole pixel; the shader has + // one quad for all of them, and would draw those a pixel away. (A + // staggered or a hexagonal map is laid out in whole numbers.) if ( - this.tilewidth % 2 !== 0 || - this.tileheight % 2 !== 0 || - (this.orientation !== "isometric" && - !(Number.isInteger(map.columnwidth) && Number.isInteger(map.rowheight))) + this.orientation === "isometric" && + (this.tilewidth % 2 !== 0 || this.tileheight % 2 !== 0) ) { return { ok: false, reason: `${kind} grid of odd size` }; } @@ -1176,6 +1160,10 @@ export default class TMXLayer extends Renderable { // path's per-layer GID index texture) are cleared from the renderer's // own `reset()` path — tile layers only come and go on game reset. this.animatedTilesets = []; + for (const node of this._toldAncestors ?? []) { + node._drawPast?.delete(this); + } + this._toldAncestors = undefined; // keep canvasRenderer for reuse — dropping the reference would leak // event listeners registered by CanvasRenderer's constructor } diff --git a/packages/melonjs/src/level/tiled/TMXTileset.js b/packages/melonjs/src/level/tiled/TMXTileset.js index 05c390af59..408ae04e76 100644 --- a/packages/melonjs/src/level/tiled/TMXTileset.js +++ b/packages/melonjs/src/level/tiled/TMXTileset.js @@ -753,6 +753,15 @@ export default class TMXTileset { dy += this._renderDyCenter; } + // a tile the tileset's image does not hold: nothing to draw (see + // `drawTileRaw`) + const frame = this.isCollection + ? undefined + : this.atlas[this.getViewTileId(tmxTile.tileId)]; + if (!this.isCollection && frame === undefined) { + return; + } + // check if any transformation is required if (tmxTile.flipped) { renderer.save(); @@ -779,7 +788,7 @@ export default class TMXTileset { ); } else { // use the tileset texture atlas - const offset = this.atlas[this.getViewTileId(tmxTile.tileId)].offset; + const offset = frame.offset; renderer.drawImage( this.image, offset.x, diff --git a/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js b/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js index 93f84d5abc..7ca646e3b3 100644 --- a/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js +++ b/packages/melonjs/src/level/tiled/renderer/TMXHexagonalRenderer.js @@ -29,7 +29,7 @@ export default class TMXHexagonalRenderer extends TMXRenderer { * @param {TMXTileMap} map - the TMX map */ constructor(map) { - super(map.cols, map.rows, map.tilewidth & ~1, map.tileheight & ~1); + super(map.cols, map.rows, map.tilewidth, map.tileheight); this.hexsidelength = map.hexsidelength || 0; this.staggerX = map.staggeraxis === "x"; @@ -46,16 +46,22 @@ export default class TMXHexagonalRenderer extends TMXRenderer { } } - // in whole numbers, as Tiled's own (libtiled's `RenderParams`): a - // side of odd length leaves an odd number of pixels to halve, and - // the half pixel is dropped, not kept: columns and rows stay on - // whole pixels, where Tiled has them - this.sideoffsetx = Math.trunc((this.tilewidth - this.sidelengthx) / 2); - this.sideoffsety = Math.trunc((this.tileheight - this.sidelengthy) / 2); + // In whole numbers, as Tiled's own (libtiled's hexagonal + // `RenderParams`): what a side leaves of a tile is halved, and when + // that is an odd number of pixels the half pixel is dropped. + // Columns and rows stay on whole pixels, where Tiled has them + this.sideoffsetx = Math.trunc((map.tilewidth - this.sidelengthx) / 2); + this.sideoffsety = Math.trunc((map.tileheight - this.sidelengthy) / 2); this.columnwidth = this.sideoffsetx + this.sidelengthx; this.rowheight = this.sideoffsety + this.sidelengthy; + // and the cell a tile stands on is made of those: the map's tile + // size, less the pixel dropped (63 x 31 tiles of a staggered map + // stand on 62 x 30 cells) + this.tilewidth = this.columnwidth + this.sideoffsetx; + this.tileheight = this.rowheight + this.sideoffsety; + this.centers = [ new Vector2d(), new Vector2d(), diff --git a/packages/melonjs/src/renderable/container.js b/packages/melonjs/src/renderable/container.js index ad02df323e..c313297482 100644 --- a/packages/melonjs/src/renderable/container.js +++ b/packages/melonjs/src/renderable/container.js @@ -1132,18 +1132,20 @@ export default class Container extends Renderable { } /** - * The area this container draws in, when something in it draws past its - * own bounds and has said so (a tile layer whose tiles stand above its - * grid, see `TMXLayer#getDrawBounds`): the container's bounds, grown by - * as much. A camera has the container in view by this area, as it would - * the layer (see `Camera2d#isVisible`). + * The area this container draws in, when something in it draws outside + * the container's own bounds and has said so: a tile layer does (see + * `TMXLayer#getDrawBounds`), whose tiles stand above its grid, which an + * offset can move, and which on an infinite map starts left of and above + * the map's origin. A container with a size of its own (one a level was + * loaded into) is in view by this area, as the layer would be (see + * `Camera2d#isVisible`). * @returns {Bounds|undefined} that area, or undefined when it is the * container's bounds (or the container has no size of its own) * @ignore */ getDrawBounds() { - const reach = this._drawReach; - if (reach === undefined) { + const layers = this._drawPast; + if (layers === undefined || layers.size === 0) { return undefined; } const bounds = this.getBounds(); @@ -1153,11 +1155,14 @@ export default class Container extends Renderable { /** @ignore */ this._drawBounds ??= boundsPool.get(); this._drawBounds.setMinMax( - bounds.left - reach.left, - bounds.top - reach.top, - bounds.right + reach.right, - bounds.bottom + reach.bottom, + bounds.left, + bounds.top, + bounds.right, + bounds.bottom, ); + for (const layer of layers) { + this._drawBounds.addBounds(layer.getDrawBounds()); + } return this._drawBounds; } diff --git a/packages/melonjs/src/video/gpu/tmxlayer_orientation.js b/packages/melonjs/src/video/gpu/tmxlayer_orientation.js index c3fdf54e96..b3fe304f8a 100644 --- a/packages/melonjs/src/video/gpu/tmxlayer_orientation.js +++ b/packages/melonjs/src/video/gpu/tmxlayer_orientation.js @@ -226,10 +226,17 @@ export function staggeredRegion(layer, rect, tileset, out) { // bottom of the last line's boxes const left = offsetX; const right = lastBox * columnWidth + tileset.tilewidth + offsetX; - const top = layer.tileheight - tileset.tileheight + offsetY; - const bottom = (lines - 1) * rowHeight + layer.tileheight + offsetY; + // (a tile stands on the cell the map lays out, which can be a pixel + // less than the map's tile size: see `TMXHexagonalRenderer`) + const top = map.tileheight - tileset.tileheight + offsetY; + const bottom = (lines - 1) * rowHeight + map.tileheight + offsetY; - if (!viewIn(rect, left, top, right, bottom, offsetX, offsetY, out)) { + // the shader stands a tile on `uCellSize`, the map's tile size: where + // the cell is shorter, its pixels are measured from as much lower + const shorter = layer.tileheight - map.tileheight; + if ( + !viewIn(rect, left, top, right, bottom, offsetX, offsetY - shorter, out) + ) { return false; } // how many cells' images reach a pixel: as many in a line as the image diff --git a/packages/melonjs/tests/helpers/tile-layer-compare.js b/packages/melonjs/tests/helpers/tile-layer-compare.js index 5f925c086b..e45feb4236 100644 --- a/packages/melonjs/tests/helpers/tile-layer-compare.js +++ b/packages/melonjs/tests/helpers/tile-layer-compare.js @@ -311,13 +311,16 @@ export function reference(scene, viewX, viewY, width, height) { const side = orientation === "hexagonal" ? hexsidelength : 0; const sideX = staggerX ? side : 0; const sideY = staggerX ? 0 : side; - // Tiled's own numbers (libtiled's hexagonal `RenderParams`), all - // whole: the grid is the even size below the tiles', and half - // of what a side leaves of a cell is rounded down - const cw = tw & ~1; - const ch = th & ~1; - const columnWidth = Math.trunc((cw - sideX) / 2) + sideX; - const rowHeight = Math.trunc((ch - sideY) / 2) + sideY; + // Tiled's own numbers (libtiled's hexagonal `RenderParams`, as + // of Tiled 1.10), all whole: half of what a side leaves of a + // tile, rounded down; a column and a row; and the cell a tile + // stands on, the tile's size less the pixel dropped + const offsetX = Math.trunc((tw - sideX) / 2); + const offsetY = Math.trunc((th - sideY) / 2); + const columnWidth = offsetX + sideX; + const rowHeight = offsetY + sideY; + const cw = columnWidth + offsetX; + const ch = rowHeight + offsetY; const place = ({ col, row }) => { // `& 1` and not `% 2`: an infinite map has negative cells return staggerX @@ -593,6 +596,10 @@ export async function comparePaths(app, scene, options = {}) { } const c = diff(ref, cpu.frame, tolerance); expect(c.different, `${at}, CPU path: ${c.first}`).toBe(0); + // and where something is to be seen, the layer was asked for it + if (drawn(ref) > 0) { + expect(cpu.cpuDraws, `${at}, CPU draws`).toBeGreaterThan(0); + } const g = diff(ref, gpu.frame, tolerance); expect(g.different, `${at}, GPU path: ${g.first}`).toBe(0); } diff --git a/packages/melonjs/tests/helpers/tile-layer-edge-cases.js b/packages/melonjs/tests/helpers/tile-layer-edge-cases.js index 1990d88a1d..15bdf446e6 100644 --- a/packages/melonjs/tests/helpers/tile-layer-edge-cases.js +++ b/packages/melonjs/tests/helpers/tile-layer-edge-cases.js @@ -123,6 +123,95 @@ export function edgeCases(renderer, type) { } }); + for (const [orientation, tw, th, extra] of [ + ["orthogonal", 32, 32, {}], + ["isometric", 64, 32, {}], + ["staggered", 64, 32, { staggeraxis: "y", staggerindex: "odd" }], + [ + "hexagonal", + 28, + 24, + { staggeraxis: "x", staggerindex: "even", hexsidelength: 12 }, + ], + ]) { + it(`${orientation}: a sliver of the map, a pixel or a few, at each edge of the view`, async (ctx) => { + need(ctx); + // tiles larger than a cell, moved by a tileoffset where + // the GPU takes one: how far the map reaches is measured + // on the reference itself + const scene = { + orientation, + cols: 5, + rows: 5, + tw, + th, + tilesets: [ + makeTileset({ + tw: tw + 10, + th: th * 2, + // (an orthogonal tileset with an offset is left + // to the CPU renderer) + extra: + orientation === "orthogonal" + ? {} + : { tileoffset: { x: -7, y: 5 } }, + }), + ], + layers: [{ data: all(5, 5) }], + extra, + }; + const wide = reference(scene, -400, -400, 1200, 1000); + let left = Infinity; + let top = Infinity; + let right = -Infinity; + let bottom = -Infinity; + for (let y = 0; y < wide.height; y++) { + for (let x = 0; x < wide.width; x++) { + const i = (y * wide.width + x) * 4; + if ( + wide.data[i] !== 16 || + wide.data[i + 1] !== 24 || + wide.data[i + 2] !== 48 + ) { + left = Math.min(left, x - 400); + right = Math.max(right, x - 400 + 1); + top = Math.min(top, y - 400); + bottom = Math.max(bottom, y - 400 + 1); + } + } + } + expect(right - left).toBeGreaterThan(100); + const middleX = Math.round((left + right - W) / 2); + const middleY = Math.round((top + bottom - H) / 2); + const views = []; + for (const k of [1, 2, 5]) { + // the map's left edge at the view's right, and so on + views.push([left + k - W, middleY]); + views.push([right - k, middleY]); + views.push([middleX, top + k - H]); + views.push([middleX, bottom - k]); + } + const layers = show(app, makeMap(scene)); + expect(layers[0].renderMode).toBe("shader"); + for (const view of views) { + expect( + await comparePaths(app, scene, { views: [view], layers }), + `view (${view})`, + ).toBeGreaterThan(0); + } + // and a pixel out of sight: nothing, either way + const out = [ + [left - W, middleY], + [right, middleY], + [middleX, top - H], + [middleX, bottom], + ]; + expect(await comparePaths(app, scene, { views: out, layers })).toBe( + 0, + ); + }); + } + it("a layer in a container with a size: the tops of tall tiles, the grid out of view", async (ctx) => { need(ctx); // a level loaded into a container gives it the map's size: @@ -166,6 +255,83 @@ export function edgeCases(renderer, type) { } }); + /** a scene put in a container of its own, as a level loaded into one */ + const boxed = (scene) => { + app.world.reset(); + app.world.pos.set(0, 0, app.world.pos.z); + app.renderer.reset?.(); + app.renderer.backgroundColor.setColor(16, 24, 48); + const map = makeMap(scene); + const box = new Container(); + app.world.addChild(box); + map.addTo(box, true, false); + app.world.sortNow(true); + app.viewport.setBounds(-100000, -100000, 200000, 200000); + return map.getLayers(); + }; + + it("a layer offset in Tiled, in a container with a size: drawn where it is, the map's rectangle out of view", async (ctx) => { + need(ctx); + // the container has the map's size: 160 wide, and the layer + // is 200 to its right + const scene = { + orientation: "orthogonal", + cols: 5, + rows: 4, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [{ data: all(5, 4), offsetx: 200, offsety: 0 }], + }; + const view = [170, -40]; + const ref = reference(scene, ...view, W, H); + expect(drawn(ref)).toBeGreaterThan(0); + const layers = boxed(scene); + app.viewport.moveTo(...view); + for (const mode of ["perTile", "shader"]) { + const { frame } = await drawIn(app, layers, mode); + const d = diff(ref, frame); + expect(d.different, `${mode}: ${d.first}`).toBe(0); + } + }); + + it("an infinite map in a container: the tiles left of and above its origin", async (ctx) => { + need(ctx); + const chunk = (x, y) => { + return { x, y, width: 4, height: 4, data: all(4, 4) }; + }; + const scene = { + orientation: "orthogonal", + cols: 4, + rows: 4, + tw: 32, + th: 32, + tilesets: [makeTileset({ tw: 32, th: 32 })], + layers: [ + { + startx: -4, + starty: -8, + width: 8, + height: 12, + data: undefined, + chunks: [chunk(-3, -5), chunk(0, 0)], + }, + ], + extra: { infinite: true }, + }; + // left of and above the map's own rectangle + const view = [-W - 4, -H - 4]; + const ref = reference(scene, ...view, W, H); + expect(drawn(ref)).toBeGreaterThan(0); + const layers = boxed(scene); + app.viewport.moveTo(...view); + for (const mode of ["perTile", "shader"]) { + const { frame } = await drawIn(app, layers, mode); + const d = diff(ref, frame); + expect(d.different, `${mode}: ${d.first}`).toBe(0); + } + }); + for (const mode of ["perTile", "shader", "prerender"]) { it(`a zoomed camera with an offset, over a moved world, drawn "${mode}"`, async (ctx) => { need(ctx); @@ -217,7 +383,7 @@ export function edgeCases(renderer, type) { }); describe("grids of odd sizes, laid out as Tiled lays them out", () => { - it("a staggered map of odd tile size: on the even grid below, tile by tile and baked", async (ctx) => { + it("a staggered map of odd tile size: on cells a pixel smaller, every way it is drawn", async (ctx) => { need(ctx); // 63 x 31 tiles stand on 62 x 30 cells: one pixel above and // one to the right of each @@ -231,15 +397,13 @@ export function edgeCases(renderer, type) { layers: [{ data: all(4, 4) }], extra: { staggeraxis: "y", staggerindex: "odd" }, }; - await fallsBack(scene, /odd size/, [ + const views = [ [-20, -20], [40, 10], - ]); + ]; + expect(await comparePaths(app, scene, { views })).toBeGreaterThan(0); const baked = show(app, makeMap(scene), "prerender"); - for (const [x, y] of [ - [-20, -20], - [40, 10], - ]) { + for (const [x, y] of views) { app.viewport.moveTo(x, y); const { frame } = await drawIn(app, baked, "prerender"); const d = diff(reference(scene, x, y, W, H), frame); @@ -249,7 +413,10 @@ export function edgeCases(renderer, type) { for (const [axis, tw, th, side] of [ ["x", 28, 24, 11], + // on this axis the cell is a pixel shorter than the tile ["y", 28, 24, 11], + // an odd height: 7 a side, rows 18 apart + ["y", 28, 25, 11], ["x", 30, 24, 7], // longer than the tile: the half left of a cell is negative ["x", 28, 24, 33], @@ -278,6 +445,14 @@ export function edgeCases(renderer, type) { [35, 12], ]; expect(await comparePaths(app, scene, { views })).toBeGreaterThan(0); + // and baked: the canvas holds every tile, to its last pixel + const baked = show(app, makeMap(scene), "prerender"); + for (const [x, y] of views) { + app.viewport.moveTo(x, y); + const { frame } = await drawIn(app, baked, "prerender"); + const d = diff(reference(scene, x, y, W, H), frame); + expect(d.different, `baked: ${d.first}`).toBe(0); + } }); } }); @@ -475,6 +650,45 @@ export function edgeCases(renderer, type) { expect(diff(ref, gpu.frame).different, "on the GPU").toBe(0); }); + it("a tile the image does not hold, set on a baked layer: left undrawn", async (ctx) => { + need(ctx); + const scene = { + orientation: "orthogonal", + cols: 6, + rows: 5, + tw: 32, + th: 32, + tilesets: [ + makeTileset({ tw: 32, th: 32, count: 4, extra: { tilecount: 8 } }), + ], + layers: [ + { + data: fill(6, 5, (c, r) => { + return 1 + ((c + r) % 4); + }), + }, + ], + }; + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); + try { + const layers = show(app, makeMap(scene), "prerender"); + const layer = layers[0]; + // one the image holds, and one it does not + scene.layers[0].data[1 * 6 + 1] = 3; + layer.setTile(layer.getTileById(3, 1, 1), 1, 1); + scene.layers[0].data[2 * 6 + 2] = 0; + expect(() => { + layer.setTile(layer.getTileById(7, 2, 2), 2, 2); + }).not.toThrow(); + app.viewport.moveTo(0, 0); + const { frame } = await drawIn(app, layers, "prerender"); + const d = diff(reference(scene, 0, 0, W, H), frame); + expect(d.different, d.first).toBe(0); + } finally { + warn.mockRestore(); + } + }); + it("a layer taken out of the world and put back is drawn again", async (ctx) => { need(ctx); const scene = { diff --git a/packages/melonjs/tests/helpers/tile-layer-staggered-scenarios.js b/packages/melonjs/tests/helpers/tile-layer-staggered-scenarios.js index 58b15352e1..18d3d12399 100644 --- a/packages/melonjs/tests/helpers/tile-layer-staggered-scenarios.js +++ b/packages/melonjs/tests/helpers/tile-layer-staggered-scenarios.js @@ -221,6 +221,67 @@ export function staggeredScenarios(renderer, type) { } }); + it("tiles of odd size: on the cell Tiled lays out, a pixel smaller", async (ctx) => { + need(ctx); + // 63 x 31 tiles of a staggered map stand on 62 x 30 cells; + // a hexagon side of 11 on 28 x 25 leaves 7 a side, rows 18 + // apart + for (const [grid, tw, th] of [ + [{ orientation: "staggered", tw: 63, th: 31 }, 63, 31], + [{ orientation: "staggered", tw: 64, th: 33 }, 64, 66], + [ + { orientation: "hexagonal", tw: 28, th: 25, hexsidelength: 11 }, + 28, + 25, + ], + [ + { orientation: "hexagonal", tw: 27, th: 24, hexsidelength: 12 }, + 27, + 40, + ], + ]) { + for (const axis of ["y", "x"]) { + expect( + await same( + staggeredScene(grid, axis, "odd", makeTileset({ tw, th })), + ), + `${grid.orientation} ${grid.tw} x ${grid.th}, axis ${axis}`, + ).toBeGreaterThan(0); + } + } + }); + + it("tiles flipped across and down, on hexagons", async (ctx) => { + need(ctx); + // (a hexagonal map's third flag is a turn of 60 degrees in + // Tiled, which the engine does not draw: left out here) + const flips = [0, FLIP_H, FLIP_V, FLIP_H + FLIP_V]; + for (const axis of ["y", "x"]) { + const scene = staggeredScene( + HEXAGONAL, + axis, + "even", + makeTileset({ + tw: 28, + th: 40, + opaque: (x, y) => { + return x + y > 12; + }, + }), + { + layers: [ + { + data: fill(COLS, ROWS, (c, r) => { + return 1 + ((c + r) % 8) + flips[(c * 3 + r) % 4]; + }), + }, + ], + }, + ); + expect(await same(scene), `axis ${axis}`).toBeGreaterThan(0); + } + }); + it("tiles flipped, every way", async (ctx) => { need(ctx); const flips = [ @@ -354,6 +415,10 @@ export function staggeredScenarios(renderer, type) { chunk(0, -4), chunk(4, 0), chunk(0, 0), + // odd coordinates: the lines that are shifted + // are the map's, not the chunk's + chunk(-3, 5), + chunk(5, -9), ], }, ], @@ -461,17 +526,6 @@ export function staggeredScenarios(renderer, type) { expect(said).toMatch(/larger than the shader's limit/); }); - it("tiles of odd size, which stand a pixel past their cell", (ctx) => { - need(ctx); - // the map is laid on the even grid below: 62 x 30 cells - const grid = { orientation: "staggered", tw: 63, th: 31 }; - const [mode, said] = modeOf( - staggeredScene(grid, "y", "odd", makeTileset({ tw: 63, th: 31 })), - ); - expect(mode).toBe("perTile"); - expect(said).toMatch(/odd size/); - }); - it("a tile of another tileset set while the game runs", (ctx) => { need(ctx); const scene = staggeredScene(HEXAGONAL, "y", "odd", blocks(HEXAGONAL), { @@ -515,6 +569,7 @@ export function staggeredScenarios(renderer, type) { })) { const scene = build(); const layers = show(app, makeMap(scene), "prerender"); + let total = 0; for (const [x, y] of STAGGERED_VIEWS.slice(0, 4)) { app.viewport.moveTo(x, y); const { frame } = await drawIn(app, layers, "prerender"); @@ -522,8 +577,9 @@ export function staggeredScenarios(renderer, type) { expect(d.different, `${label}, view (${x}, ${y}): ${d.first}`).toBe( 0, ); - expect(drawn(frame)).toBeGreaterThanOrEqual(0); + total += drawn(frame); } + expect(total, label).toBeGreaterThan(0); } }); }); diff --git a/packages/melonjs/tests/tmxlayer-offset.spec.js b/packages/melonjs/tests/tmxlayer-offset.spec.js index 82e6e1c737..aba505bccf 100644 --- a/packages/melonjs/tests/tmxlayer-offset.spec.js +++ b/packages/melonjs/tests/tmxlayer-offset.spec.js @@ -227,6 +227,84 @@ describe("where a tile layer is (Canvas renderer)", () => { }); }); + describe("getTile, on every kind of map, with the layer moved", () => { + const grids = [ + ["orthogonal", 32, 32, {}], + ["isometric", 64, 32, {}], + ["staggered", 64, 32, { staggeraxis: "y", staggerindex: "odd" }], + ["staggered", 64, 32, { staggeraxis: "x", staggerindex: "even" }], + [ + "hexagonal", + 28, + 24, + { staggeraxis: "y", staggerindex: "odd", hexsidelength: 12 }, + ], + [ + "hexagonal", + 28, + 24, + { staggeraxis: "x", staggerindex: "even", hexsidelength: 11 }, + ], + ]; + for (const [orientation, tw, th, extra] of grids) { + it(`${orientation}${extra.staggeraxis ? `, axis ${extra.staggeraxis}` : ""}: the middle of every cell is that cell`, () => { + const cols = 7; + const rows = 6; + const scene = { + orientation, + cols, + rows, + tw, + th, + tilesets: [makeTileset({ tw, th })], + layers: [{ data: filled(cols, rows), offsetx: 37, offsety: -21 }], + extra, + }; + const [layer] = show(app, makeMap(scene)); + const map = layer.getRenderer(); + let found = 0; + for (const [dx, dy] of [ + [0, 0], + [55, -140], + ]) { + layer.pos.x += dx; + layer.pos.y += dy; + const movedX = 37 + dx; + const movedY = -21 + dy; + for (let row = 0; row < rows; row++) { + for (let col = 0; col < cols; col++) { + // the middle of the cell, where the map's renderer + // puts it, moved as the layer is + const at = + orientation === "isometric" + ? map.tileToPixelCoords(col + 0.5, row + 0.5) + : map.tileToPixelCoords(col, row); + if (orientation !== "isometric") { + at.x += map.tilewidth / 2; + at.y += map.tileheight / 2; + } + const tile = layer.getTile(at.x + movedX, at.y + movedY); + const gid = scene.layers[0].data[row * cols + col]; + if (gid === 0) { + expect(tile, `empty cell (${col}, ${row})`).toBeNull(); + } else { + expect( + [tile?.col, tile?.row], + `cell (${col}, ${row}), layer moved by (${movedX}, ${movedY})`, + ).toEqual([col, row]); + expect(tile.tileId).toBe(gid); + found++; + } + } + } + } + expect(found).toBeGreaterThan(40); + // and far from the layer, nothing + expect(layer.getTile(-5000, -5000)).toBeNull(); + }); + } + }); + describe("moved in the game, by its pos", () => { for (const make of [orthogonal, isometric]) { it(`${make.name}: drawn, and found by getTile, where it was moved to`, async () => { From 11e7899c81597f3681f04905243418fc297492d3 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 05:07:03 +0000 Subject: [PATCH 10/11] Changelog: 20.17.0 rewritten for readers GPU rendering of isometric, staggered and hexagonal maps under Added; its gain in percent under Performance, with the orthogonal figures for reference; the opacity change under Fixed; the tile layer fixes merged into six entries, those a game cannot see left out. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01JnzwA4a7ZyjLBpbeEpFLu6 --- packages/melonjs/CHANGELOG.md | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/packages/melonjs/CHANGELOG.md b/packages/melonjs/CHANGELOG.md index ac85585d49..eb1795ce54 100644 --- a/packages/melonjs/CHANGELOG.md +++ b/packages/melonjs/CHANGELOG.md @@ -4,32 +4,19 @@ ### Added - Transitions: a mask transition can be shaped like an image or a `Sprite`, close on the player with `center`, and fill the shape with `invert`, on `state.transition`, `MaskEffect` and `Trigger` ([#1399](https://github.com/melonjs/melonJS/issues/1399)) - -### Changed -- Tiled: an isometric, staggered or hexagonal tile layer with an `opacity` below 1 fades as one flat picture on WebGL and WebGPU, where each tile was faded over the ones behind it and showed through the one in front. Set the layer's `renderMode` to `"perTile"` for the look it had +- Tiled: isometric maps are rendered by the GPU on WebGL and WebGPU, as orthogonal ones are, and so are staggered and hexagonal maps. Nothing to enable: a tile layer whose tiles come from one tileset takes it by itself, and any other is drawn as before, the console saying why ([#1897](https://github.com/melonjs/melonJS/issues/1897)) ### Performance -- Tiled: isometric, staggered and hexagonal tile layers are drawn by the GPU on WebGL and WebGPU, as orthogonal ones are: one quad for the layer, its tiles found by the fragment shader, in place of a quad per tile in view found and placed in JavaScript every frame. On a 100 × 100 isometric map with about 4000 tiles in view, drawing the layer takes under **0.1 ms** of main-thread time a frame, down from about **2 ms**, and no longer grows with the number of tiles shown. A layer takes this path by itself when all its tiles come from one tileset, up to 4 cells wide and 8 tall (a `tileoffset`, flipped and animated tiles, and `setTile` are all fine); any other is drawn as before, and the console says why ([#1897](https://github.com/melonjs/melonJS/issues/1897)) -- Tiled: a reminder that orthogonal tile layers have been drawn this way since 19.4, with the same gain (about 1.9 ms down to under 0.1 ms for 3600 tiles in view). A layer that is not says why in the console when it is activated: most often a tileset with a `tileoffset`, a collection of images, or the Canvas renderer. - -### Fixed -- Tiled, GPU tile layers: the soft edge of a tile larger than the grid shows the tile behind it; it was drawn over the background instead, a thin outline round such tiles -- Tiled, GPU tile layers: tiles larger than the grid are drawn whole above the map's first row and right of its last column, where they were cut off -- Tiled, GPU tile layers: a tileset with a `margin` whose image stops at its last tiles, with no margin after them, draws the right tiles; they were counted a column or a row short -- Tiled, GPU tile layers: an orthogonal layer whose tiles are larger than the grid and come from more than one tileset, or whose map has a render order other than "right-down", is drawn tile by tile; on the GPU such tiles lay over each other in the wrong order -- Tiled, GPU tile layers: a layer with more columns or rows than the largest texture the GPU takes is drawn tile by tile; it drew nothing -- Tiled, GPU tile layers: a layer with a tile that is not square, flipped diagonally, is drawn tile by tile; the shader drew such a tile wrong -- Tiled, GPU tile layers: the shader runs at full precision whatever `highPrecisionShader` says; at a lower one a phone could misplace the tiles of a large map -- Tiled: a tile layer follows its offset in Tiled (`offsetx`, `offsety`), and its own `pos` when the game moves it: its tiles are drawn there and `getTile()` finds them there, on every renderer and map orientation. Both were ignored, and the layer drawn at the map's origin; only image layers followed their offset. A map whose tile layers have an offset now looks as it does in Tiled -- Tiled: tiles that reach past their layer's grid (taller or wider than a cell, or moved by a `tileoffset`) are drawn when they are in view and the grid is not; the whole layer was skipped, so the tops of tall tiles vanished once the map scrolled out of view. The layer's bounds are still its grid's -- Tiled: a map centred in the view because it is smaller (as `level.load` centres it), and a tile layer in a container that was moved, are drawn whole. The layer read the camera's view as if it stood at the world's origin: on WebGL, WebGPU and a pre-rendered layer the tiles reaching above or left of the grid were cut, and tile by tile a layer far from the origin lost tiles -- Tiled, staggered and hexagonal maps: a tile larger than its cell stands on the cell, and a tileset's `tileoffset` moves its tiles, as in Tiled and on every other map. Drawn tile by tile, such a tile hung down from the top of its cell and the offset was ignored, so the map sat lower than its objects (and than a tile redrawn in a baked layer); tiles of a large tileset also went missing along the edges of the view -- Tiled, staggered and hexagonal maps of odd sizes are laid out in whole numbers, as Tiled 1.10 lays them out: a tile of odd size stands on a cell a pixel smaller, and a hexagon side that leaves an odd number of pixels of a tile drops the half pixel. Every other column or row drifted half a pixel further, and a pre-rendered layer lost a row and a column of pixels -- Tiled: a level loaded into a container of its own is in view as far as its tile layers draw: their tiles' reach, a layer's offset, and an infinite map's chunks left of and above its origin were cut off once the map's own rectangle left the view -- Tiled: a tile layer taken out of its container and added again is drawn; it threw. A tile its tileset's image does not hold (an image cropped since the map was saved) is not drawn; drawing it threw every frame -- Tiled, isometric maps drawn tile by tile: tiles narrower than half a cell are no longer left out at the right edge of the view -- Tiled, isometric maps drawn tile by tile: tiles of a tileset larger than the map's first one, or moved by a `tileoffset`, went missing along the edges of the view -- Tiled, orthogonal maps drawn tile by tile: tiles more than two cells tall went missing along the bottom of the view +- Tiled: thanks to the GPU renderer, drawing an isometric tile layer takes about **90%** less of the main thread's time with 1200 tiles in view (0.68 to 0.07 ms a frame) and about **98%** less with 4000 (2.0 to 0.04 ms), and no longer grows with the number of tiles shown. Staggered and hexagonal maps gain the same +- Tiled: for reference, orthogonal tile layers, on the GPU since 19.4, measure about **85%** less with 1000 tiles in view (0.62 to 0.09 ms) and about **97%** less with 3600 (1.9 to 0.06 ms) + +### Fixed +- Tiled: a tile layer follows its offset in Tiled, and its own `pos` when the game moves it: its tiles are drawn there, and `getTile()` finds them there. Both were ignored; a map whose tile layers have an offset now looks as it does in Tiled +- Tiled: tiles larger than a cell are drawn wherever they are in view. They were cut off or left out along the edges of the view, above and right of the map, on a map centred in the screen, and once the map itself had scrolled out of view +- Tiled: staggered and hexagonal maps are drawn where Tiled draws them: a tile larger than its cell stands on it, a tileset's `tileoffset` moves its tiles, and maps of odd sizes stay on whole pixels +- Tiled: orthogonal layers drawn by the GPU show the soft edges of large tiles over the tiles behind them, lay large tiles over each other in the map's order, and read a tileset with a `margin` right +- Tiled: an isometric, staggered or hexagonal layer with an `opacity` below 1 fades as one picture on WebGL and WebGPU; each tile showed through the one in front of it +- Tiled: a tile layer removed from its container and added again, and a tile missing from its tileset's image, no longer throw - `NineSliceSprite` blinks with `flicker()`, as a `Sprite` does; it ignored it - glTF: a model compressed with Draco or meshopt, which the loader cannot decode, says so in the console, naming the extension, instead of failing on an accessor error that did not mention it From 33c1d20f1f9eca13376c9873e37186d51430e922 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Nov 2026 05:25:14 +0000 Subject: [PATCH 11/11] README: GPU tile rendering covers isometric, staggered and hexagonal maps The Tiled feature line said orthogonal maps only, and that the others fell back to the CPU renderer. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_01JnzwA4a7ZyjLBpbeEpFLu6 --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5734408cc2..07a8f3da58 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ melonJS is designed so you can **focus on making games, not on graphics plumbing - **Complete engine, minimal footprint** — Physics, tilemaps, audio, input, cameras, tweens, particles, UI — a full game stack in a single tree-shakeable ES module. No dependency sprawl, no library stitching. -- **Scenes, loaded in one call** — `level.load(name)` brings an authored scene straight into your world. [Tiled](https://www.mapeditor.org) is a first-class citizen for **2D** — orthogonal, isometric, hexagonal & staggered maps, animated tilesets, collision shapes, object properties, compressed formats, with GPU-accelerated tile rendering on the GPU backends — and **glTF / GLB** is the equivalent for **3D scenes**: author in Blender (or any DCC tool), export a `.glb`, and the whole scene — meshes, materials, cameras, lights, and node animation — loads under a `Camera3d`, no per-mesh wiring. Animated models play back through the same animation API as a 2D `Sprite`. +- **Scenes, loaded in one call** — `level.load(name)` brings an authored scene straight into your world. [Tiled](https://www.mapeditor.org) is a first-class citizen for **2D** — orthogonal, isometric, hexagonal & staggered maps, all four rendered on the GPU (a whole tile layer in a single draw, on WebGPU and WebGL 2), animated tilesets, collision shapes, object properties, compressed formats — and **glTF / GLB** is the equivalent for **3D scenes**: author in Blender (or any DCC tool), export a `.glb`, and the whole scene — meshes, materials, cameras, lights, and node animation — loads under a `Camera3d`, no per-mesh wiring. Animated models play back through the same animation API as a 2D `Sprite`. - **Batteries included, hackable by design** — Get started in minutes with minimal setup. When you need to go deeper: modern class-based architecture, a plugin system for engine extensions, and a clean architecture that's easy to extend without fighting the framework. @@ -117,7 +117,7 @@ UI Scenes - Load a scene in one call with `level.load(name)` — 2D Tiled maps and 3D glTF scenes alike, auto-registered on preload - [Tiled](https://www.mapeditor.org) map format [up to 1.12](https://doc.mapeditor.org/en/stable/reference/tmx-changelog/) built-in support for easy level design - - **GPU-accelerated tile rendering** for orthogonal maps on the GPU backends (WebGL 2 and WebGPU) — each layer draws as a single quad with no per-tile loop, ~5–8× faster than the legacy CPU renderer on dense maps. Honors animated tiles, flip bits, per-layer opacity/tint/blend, and oversized bottom-aligned tiles; falls back transparently to the CPU renderer on isometric/staggered/hexagonal layers or under the Canvas renderer + - **GPU-accelerated tile rendering** for orthogonal, isometric, staggered and hexagonal maps on the GPU backends (WebGL 2 and WebGPU) — each layer draws as a single quad with no per-tile loop, so its cost on the main thread stays flat however many tiles are in view: 85% to 98% less than the CPU renderer on a dense map. Honors animated and flipped tiles, per-layer opacity and tint, tiles larger than the grid and tileset offsets, and `setTile` at run time; falls back transparently to the CPU renderer for a layer it cannot draw (the console says why), on oblique maps and under the Canvas renderer - Uncompressed and [compressed](https://github.com/melonjs/melonJS/tree/master/packages/tiled-inflate-plugin) Plain, Base64, CSV and JSON encoded XML tilemap loading - Orthogonal, Isometric, Hexagonal (both normal and staggered) and Oblique maps - Multiple layers with per-layer alpha, tinting and blend modes (multiple background/foreground, collision and Image layers)