Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions docs/docs/graphics-3d/advanced.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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).
15 changes: 11 additions & 4 deletions modules/graphics-3d/src/graphics-3d.library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
});
}
Expand Down Expand Up @@ -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 {
Expand Down
27 changes: 25 additions & 2 deletions modules/graphics-3d/test/graphics-3d.library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand Down
Loading