From 960460a87c4177a8fd64629cc411352cfc361f61 Mon Sep 17 00:00:00 2001 From: Leo Date: Thu, 3 Dec 2026 14:17:47 +0100 Subject: [PATCH] fix(graphics-3d): skip render on zero-size canvas --- docs/docs/graphics-3d/advanced.mdx | 14 +++++----- .../graphics-3d/src/graphics-3d.library.ts | 15 ++++++++--- .../test/graphics-3d.library.spec.ts | 27 +++++++++++++++++-- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/docs/docs/graphics-3d/advanced.mdx b/docs/docs/graphics-3d/advanced.mdx index 98fdc1bf..4a8b9be1 100644 --- a/docs/docs/graphics-3d/advanced.mdx +++ b/docs/docs/graphics-3d/advanced.mdx @@ -71,7 +71,7 @@ canvas.addEventListener("pointerdown", (event) => { ## Render loop -Never call `renderer.setAnimationLoop` or render from `requestAnimationFrame`. The engine renders once per tick, and the editor relies on this: pause freezes the image, and step draws exactly one frame. To draw something else than `scene` through `camera`, such as a post-processing pipeline, replace the library's render step. The library still calls it once per tick, when the viewport or the container changes size, and for editor screenshots: +Never call `renderer.setAnimationLoop` or render from `requestAnimationFrame`. The engine renders once per tick, and the editor relies on this: pause freezes the image, and step draws exactly one frame. To draw something else than `scene` through `camera`, such as a post-processing pipeline, replace the library's render step. The library still calls it once per tick, when the viewport or the container changes size, and for editor screenshots, but never while the canvas has no area (a hidden game, such as another editor tab): ```ts import { RenderPipeline } from "@nanoforge-dev/graphics-3d/three"; @@ -91,11 +91,11 @@ Assign `undefined` to go back to the default render. The custom render is reset You don't need this section to write a game: it is for editor hosts. When the editor starts the game (with `EditorLibrary` registered), the library adds to the [editor protocol](../editor/protocol): -| Name | Direction | Payload | Purpose | -| ------------------ | --------------- | ---------------------- | --------------------------------------------------------------------------------------------------------------------------- | -| `graphicsCamera` | feature | `boolean` | Asked for in the editor's `welcome`. | -| `graphics-camera` | engine → editor | `{ view, projection }` | The active camera's matrices (16 numbers, column-major), on start and on every change. | -| `graphics-capture` | editor → engine | none | Renders a frame now, even while paused. | -| `graphics-capture` | engine → editor | PNG data URL | That frame, or an empty string when there is no renderer. WebGL and WebGPU canvases are only readable right after a render. | +| Name | Direction | Payload | Purpose | +| ------------------ | --------------- | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | +| `graphicsCamera` | feature | `boolean` | Asked for in the editor's `welcome`. | +| `graphics-camera` | engine → editor | `{ view, projection }` | The active camera's matrices (16 numbers, column-major), on start and on every change. | +| `graphics-capture` | editor → engine | none | Renders a frame now, even while paused. | +| `graphics-capture` | engine → editor | PNG data URL | That frame, or an empty string when there is no renderer or the canvas has no area. WebGL and WebGPU canvases are only readable right after a render. | The projection includes the viewport crop, so its normalized device coordinates span the canvas. The canvas is drawn `visibleWidth` × `visibleHeight` CSS px at (`contentLeft`, `contentTop`) of the container (see the `viewport` event). diff --git a/modules/graphics-3d/src/graphics-3d.library.ts b/modules/graphics-3d/src/graphics-3d.library.ts index f7fd5d7c..a789460e 100644 --- a/modules/graphics-3d/src/graphics-3d.library.ts +++ b/modules/graphics-3d/src/graphics-3d.library.ts @@ -123,9 +123,9 @@ export class Graphics3DLibrary extends Library implements EditorAwareLibrary { this._editor = ctx.editor; ctx.editor.on("graphics-capture", () => { // No renderer between a clear and an init: answer empty so the editor does not wait. - if (!this._renderer) return ctx.editor.emit("graphics-capture", ""); + // Same for a hidden game: a 0×0 canvas has nothing to capture. // A WebGL/WebGPU canvas is only readable right after a render. - this._draw(); + if (!this._renderer || !this._draw()) return ctx.editor.emit("graphics-capture", ""); ctx.editor.emit("graphics-capture", this._renderer.domElement.toDataURL("image/png")); }); } @@ -273,10 +273,17 @@ export class Graphics3DLibrary extends Library implements EditorAwareLibrary { }; } - /** Draws one frame with the custom render function, or the scene through the camera. */ - private _draw(): void { + /** + * Draws one frame with the custom render function, or the scene through the + * camera. Returns false without drawing when the canvas has no area. + */ + private _draw(): boolean { + const { width, height } = this.renderer.domElement; + // Hidden game (another editor tab, collapsed container): WebGPU rejects a 0-size swapchain. + if (!width || !height) return false; if (this._customRender) this._customRender(); else this._drawDefault(); + return true; } private _drawDefault(): void { diff --git a/modules/graphics-3d/test/graphics-3d.library.spec.ts b/modules/graphics-3d/test/graphics-3d.library.spec.ts index b2be12a3..6151919c 100644 --- a/modules/graphics-3d/test/graphics-3d.library.spec.ts +++ b/modules/graphics-3d/test/graphics-3d.library.spec.ts @@ -22,8 +22,13 @@ const fake = vi.hoisted(() => { this.info = { autoReset: true, render: { drawCalls: 1 }, reset: vi.fn() }; this.init = vi.fn(async () => this); this.render = vi.fn(); - this.setSize = vi.fn(); - this.setPixelRatio = vi.fn(); + this.pixelRatio = 1; + // Like three: the drawing buffer follows the CSS size times the pixel ratio. + this.setSize = vi.fn((width: number, height: number) => { + this.domElement.width = Math.floor(width * this.pixelRatio); + this.domElement.height = Math.floor(height * this.pixelRatio); + }); + this.setPixelRatio = vi.fn((ratio: number) => (this.pixelRatio = ratio)); this.setAnimationLoop = vi.fn(); this.dispose = vi.fn(async () => {}); renderers.push(this); @@ -304,6 +309,16 @@ describe("Graphics3DLibrary", () => { expect(renderer.render).toHaveBeenCalledOnce(); }); + it("skips rendering while the game has no area, and resumes once it has one", async () => { + const { library, viewport, renderer } = await setup(state("contain", 0, 0)); + renderer.render.mockClear(); + await run(library); + expect(renderer.render).not.toHaveBeenCalled(); + viewport.emit(state("contain", 1920, 1080)); + await run(library); + expect(renderer.render).toHaveBeenCalledTimes(2); + }); + it("follows viewport changes and applies them to a camera assigned later", async () => { const { library, viewport, renderer, canvas } = await setup(state("contain", 1000, 1000)); viewport.emit(state("contain", 1920, 1080)); @@ -475,6 +490,14 @@ describe("Graphics3DLibrary", () => { expect(editor.emit).toHaveBeenCalledWith("graphics-capture", ""); }); + it("answers graphics-capture with an empty string while the canvas has no area", async () => { + const { library, editor, send } = await setup({}); + library.renderer.domElement.width = 0; + send("graphics-capture"); + expect(lastRenderer().render).not.toHaveBeenCalled(); + expect(editor.emit).toHaveBeenCalledWith("graphics-capture", ""); + }); + it("sends the camera's world position, parents included", async () => { const { library, editor } = await setup({ graphicsCamera: true }); const parent = new Object3D();