Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

perf(editor): resolve collider pixelsPerUnit once per gizmo pass

esengine25d738e2b5ba
diff --git a/desktop/src/engine/ViewportController.ts b/desktop/src/engine/ViewportController.ts
index a3cbc74fb..4aed10b6f 100644
--- a/desktop/src/engine/ViewportController.ts
+++ b/desktop/src/engine/ViewportController.ts
@@ -540,11 +540,14 @@ export const ViewportController = {
*/
getColliderGizmo(
id: EntityId,
+ ppuHint?: number,
): { kind: 'box'; pts: Array<{ x: number; y: number }>; handle: { x: number; y: number } | null; sizeHandle: { x: number; y: number } | null } | { kind: 'circle'; cx: number; cy: number; r: number; handle: { x: number; y: number } | null; sizeHandle: { x: number; y: number } | null } | null {
const world = EngineHost.world;
if (!world || !world.valid(id) || !world.has(id, Transform)) return null;
const t = world.get(id, Transform);
- const ppu = this.colliderPixelsPerUnit();
+ // pixelsPerUnit is a scene-wide constant; callers looping many colliders pass
+ // it in to avoid re-scanning all entities for the Canvas once per collider.
+ const ppu = ppuHint ?? this.colliderPixelsPerUnit();
const rot = quatAngleZ(t.worldRotation as { w: number; x: number; y: number; z: number });
const cos = Math.cos(rot);
const sin = Math.sin(rot);
diff --git a/desktop/src/panels/Viewport.tsx b/desktop/src/panels/Viewport.tsx
index 07e2ea77b..3b56e9316 100644
--- a/desktop/src/panels/Viewport.tsx
+++ b/desktop/src/panels/Viewport.tsx
@@ -979,9 +979,11 @@ export function Viewport() {
}
// Collider gizmos — box polygon / circle outline at the collider's shape.
+ // Resolve the scene-wide pixelsPerUnit once, not once per collider.
+ const colliderPpu = camsOn ? ViewportController.colliderPixelsPerUnit() : 0;
for (const [cid, svg] of colliderRefs.current) {
if (!svg) continue;
- const cg = camsOn ? ViewportController.getColliderGizmo(cid) : null;
+ const cg = camsOn ? ViewportController.getColliderGizmo(cid, colliderPpu) : null;
const poly = svg.querySelector('.cl-box') as SVGPolygonElement | null;
const circ = svg.querySelector('.cl-circle') as SVGCircleElement | null;
if (cg && cg.kind === 'box' && poly) {