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
24 changes: 24 additions & 0 deletions agent-notes/perf-visual-render.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Goal

Optimize the live Three.js room view and open a pull request against master.

## State

Implemented the shared animation frame after Q approved. Joint motion advances before drawing, the last drawn room's resources survive until its replacement draws, and each visible label uses one keyed lookup.

## Decisions and why

- `RoomLayout3D.tsx:241` draws synchronously on OrbitControls change. Wheel zoom updates controls then draws explicitly at lines 544–545. Coalesce visual changes into one pending animation frame. Keep input state updates immediate and use the latest camera, room, labels and selection when drawing.
- Audit first paint, resize, orbit, wheel, touch, keyboard, camera modes, layout, bodies, selection and bake completion as frame producers. Advance joint animation before drawing so the final pose appears and animation does not trail input by a frame. Cancel pending work on teardown.
- `RoomLayout3D.tsx:421` depends on drawing replacement materials before disposing the prior room. Preserve that lifetime across deferred and superseded frames and unmount.
- `placeVisibleLabels` searches the entire request list for each placed label. Reuse the per-id lookup already needed for projected anchors to avoid repeated scans while preserving label selection, priority and hit regions.

## Surprises

The lighting bake uses its own frame budget and signals completion. The preview has its own explicit rendering contract. A five-event wheel burst in the live component with the sample layout at 480 by 320 in headless Edge produced ten composer draws on master and one after this change. Synchronous composer draws fell from ten to zero. The regression test independently reproduced ten before the change.

## Next step

The regression covers the wheel burst, idle, bake completion, teardown and replacement before painting. Publish the branch and open the PR, then follow CI.

Reference: https://threejs.org/manual/pages/rendering-on-demand.html
76 changes: 76 additions & 0 deletions src/components/RoomLayout3D.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { fireEvent, render } from "@testing-library/react";
import { afterEach, expect, it, vi } from "vitest";
import { sampleLayout } from "../map/roomLayout.fixture";
import * as roomScene from "../map/roomScene";
import RoomLayout3D from "./RoomLayout3D";

const draw = vi.hoisted(() => vi.fn());
let finishBake: () => void;
vi.mock("../map/roomRenderer", () => ({
roomRenderer: () => ({
renderer: { domElement: document.createElement("canvas"), setSize: vi.fn(), shadowMap: {} },
dispose: vi.fn(),
}),
}));
vi.mock("../map/roomPost", () => ({
roomPost: () => ({ setRoom: vi.fn(), setSize: vi.fn(), render: draw, dispose: vi.fn() }),
}));
vi.mock("../map/roomBounce", () => ({
lightRoom: () => ({ baked: new Promise<void>((resolve) => { finishBake = resolve; }), dispose: vi.fn() }),
}));

afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
});

it("combines wheel events into one room draw and cancels the last request on unmount", async () => {
vi.spyOn(HTMLElement.prototype, "clientWidth", "get").mockReturnValue(480);
vi.spyOn(HTMLElement.prototype, "clientHeight", "get").mockReturnValue(320);
vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockReturnValue({} as WebGL2RenderingContext);
const pending = new Map<number, FrameRequestCallback>();
let id = 0;
vi.stubGlobal("requestAnimationFrame", (step: FrameRequestCallback) => {
pending.set(++id, step);
return id;
});
vi.stubGlobal("cancelAnimationFrame", (handle: number) => pending.delete(handle));
const paint = () => {
const callbacks = [...pending.values()];
pending.clear();
for (const callback of callbacks) callback(100);
};
const layout = { ...sampleLayout(), things: [] };
const picture = (size: typeof layout.size) => <RoomLayout3D layout={{ ...layout, size }} placed={[]} youId="" label="Room" mode="dollhouse"
selectedId={null} onSelect={vi.fn()} onExit={vi.fn()} onFitting={vi.fn()} onInspect={vi.fn()} />;
const dispose = vi.spyOn(roomScene, "disposeRoomScene");
const view = render(picture(layout.size));
paint();
expect(draw).toHaveBeenCalledTimes(1);
draw.mockClear();
const room = view.getByRole("img");
for (let i = 0; i < 5; i++) fireEvent.wheel(room, { deltaY: -20 });
expect(draw).not.toHaveBeenCalled();
expect(pending.size).toBe(1);
paint();
expect(draw).toHaveBeenCalledTimes(1);
expect(pending.size).toBe(0);
finishBake();
await Promise.resolve();
expect(pending.size).toBe(1);
paint();
expect(draw).toHaveBeenCalledTimes(2);
view.rerender(picture([8, 8, 3]));
expect(dispose).not.toHaveBeenCalled();
view.rerender(picture([9, 8, 3]));
expect(dispose).toHaveBeenCalledTimes(1);
paint();
expect(dispose).toHaveBeenCalledTimes(2);
fireEvent.keyDown(room, { key: "ArrowLeft" });
expect(pending.size).toBe(1);
view.unmount();
expect(dispose).toHaveBeenCalledTimes(3);
finishBake();
await Promise.resolve();
expect(pending.size).toBe(0);
});
Loading