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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(editor): a world length is measured where it stands, not along one axis
esengine038a0955239f
diff --git a/desktop/scripts/editor-checks/gizmo-scale.mjs b/desktop/scripts/editor-checks/gizmo-scale.mjsnew file mode 100644index 000000000..980f68fe7--- /dev/null+++ b/desktop/scripts/editor-checks/gizmo-scale.mjs@@ -0,0 +1,109 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file gizmo-scale — a world-sized gizmo is drawn at the size it is.+ *+ * A light's reach, a collider circle, a particle spawn disk: each is a LENGTH in the+ * world, and how long that is on screen depends only on where it stands. Turning it+ * into pixels by projecting an offset along one world axis instead measures that+ * axis's foreshortening — the same length shrinks as the eye turns to look down it,+ * and vanishes when it looks straight down it.+ *+ * A point light at the view's focus, measured head-on and again from the eye the 3D+ * toggle parks: the reach it is drawn at must not have moved, because neither the+ * light nor the zoom did.+ */+import { cp } from 'node:fs/promises';+import path from 'node:path';+import { makeProject, checker, originIn, viewportRegion, DESKTOP } from '../lib/editorDriver.mjs';++const EXAMPLE = path.resolve(DESKTOP, '..', 'examples', 'sprite-rendering');++/** One point light at the origin, with a reach big enough to measure. */+const SCENE = JSON.stringify({+ version: '1.0', name: 'Main',+ entities: [+ {+ id: 0, name: 'Lamp', parent: null, children: [], visible: true,+ components: [+ { type: 'Transform', data: { position: { x: 0, y: 0, z: 0 } } },+ {+ type: 'Light2D',+ data: { type: 0, radius: 220, intensity: 1, enabled: true, color: { r: 1, g: 1, b: 1, a: 1 } },+ },+ ],+ },+ ],+}, null, 1);++export const name = 'gizmo-scale';+export const describes = 'a world-sized gizmo keeps its size when the eye turns';++/** How far from `c` the gizmo toggle painted anything. */+function reach(a, b, c, limit, tol = 25) {+ let far = 0;+ const x1 = Math.min(Math.min(a.w, b.w), c.x + limit);+ const y1 = Math.min(Math.min(a.h, b.h), c.y + limit);+ for (let y = Math.max(0, c.y - limit); y < y1; y++) {+ for (let x = Math.max(0, c.x - limit); x < x1; x++) {+ const p = a.px(x, y);+ const q = b.px(x, y);+ if (Math.abs(p[0] - q[0]) > tol || Math.abs(p[1] - q[1]) > tol || Math.abs(p[2] - q[2]) > tol) {+ far = Math.max(far, Math.hypot(x - c.x, y - c.y));+ }+ }+ }+ return far;+}++export async function run(ed) {+ const root = await makeProject({ 'assets/scenes/main.esscene': SCENE });+ await cp(path.join(EXAMPLE, 'project.esproject'), path.join(root, 'project.esproject'));+ await cp(path.join(EXAMPLE, 'src'), path.join(root, 'src'), { recursive: true });+ const check = checker();+ await ed.open(root, 'assets/scenes/main.esscene');+ // The design overlay belongs to UI mode, and the editor remembers its mode.+ await ed.call('run_editor_command', { id: 'mode.scene' }, 30000);+ await ed.call('select', { id: null }, 30000);+ await ed.sleep(800);++ const measure = async (label, at) => {+ await ed.call('run_editor_command', { id: 'view.toggleGizmos' }, 30000);+ await ed.sleep(700);+ const off = await ed.screenshot(`${label}-off`);+ await ed.call('run_editor_command', { id: 'view.toggleGizmos' }, 30000);+ await ed.sleep(700);+ const on = await ed.screenshot(`${label}-on`);+ return reach(off, on, at, 260);+ };++ const first = await ed.screenshot('gizmo-scale-locate');+ // The light is at the origin, which is the view's focus, and the eye turns ABOUT+ // the focus — so one point serves both eyes, and only the head-on grid has the+ // straight axis lines it is read from.+ const origin = originIn(first, viewportRegion(first));+ if (!check(origin != null, "the grid's axis lines were not found — the light is at the origin")) {+ return check.failures;+ }++ const flat = await measure('gizmo-scale-flat', origin);+ if (!check(flat > 60, `head-on the light's reach is drawn ${flat.toFixed(0)}px across — it is not `+ + 'being drawn at all, so there is no size to compare')) {+ return check.failures;+ }++ await ed.call('run_editor_command', { id: 'view.toggleViewPerspective' }, 30000);+ await ed.sleep(1200);+ const turned = await measure('gizmo-scale-turned', origin);++ // Neither the light nor the zoom moved, so neither did the length.+ const drift = Math.abs(turned - flat) / flat;+ check(+ drift < 0.03,+ `the same reach measures ${flat.toFixed(0)}px head-on and ${turned.toFixed(0)}px from the `+ + `turned eye (${(drift * 100).toFixed(1)}% apart) — the length is being measured through one `+ + 'world axis, so it carries that axis\'s foreshortening',+ );++ return check.failures;+}diff --git a/desktop/src/engine/ViewportController.ts b/desktop/src/engine/ViewportController.tsindex 9b2252dd8..1ab9918e8 100644--- a/desktop/src/engine/ViewportController.ts+++ b/desktop/src/engine/ViewportController.ts@@ -412,6 +412,17 @@ export const ViewportController = {return view && heightPx > 0 ? editorViewWorldPerPixel(view, heightPx, at) : 0;},+ /**+ * A world length as it measures on screen where it stands — what any world-sized+ * circle, reach or arm is drawn at. Projecting an offset along ONE world axis+ * answers this only while the eye is square to that axis, and shrinks the length+ * to nothing as the eye turns to look down it.+ */+ screenLengthAt(at: Vec3, world: number): number {+ const perPixel = this.worldPerPixelAt(at);+ return perPixel > 0 ? world / perPixel : 0;+ },+/** The world half-size an icon covers where it stands, for the zoom it is drawn* at — what a click has to hit. */iconPickHalf(): (at: Vec3) => number {@@ -1022,8 +1033,7 @@ export const ViewportController = {// Point (0) / Spot (3) have a falloff radius. What a world length measures on// screen where the light stands — projecting an offset along ONE world axis// would shrink the reach whenever the eye foreshortens that axis.- const perPixel = this.worldPerPixelAt(at);- const radiusPx = (l.type === 0 || l.type === 3) && perPixel > 0 ? l.radius / perPixel : 0;+ const radiusPx = (l.type === 0 || l.type === 3) ? this.screenLengthAt(at, l.radius) : 0;// Directional (1) / Spot (3) aim along the entity's forward, projected through the// view like every other arrow the editor draws. An aim pointing at the eye projects// to nothing and draws no arrow, which is what it looks like.@@ -1233,6 +1243,9 @@ export const ViewportController = {const sin = Math.sin(rot);const wp = { x: t.worldPosition.x, y: t.worldPosition.y };const toClient = this.projectorFor(id);+ const planeZ = this.entityPlaneZ(id);+ const lengthPx = (world: number): number =>+ this.screenLengthAt({ x: wp.x, y: wp.y, z: planeZ }, world);// readColliderShapes only reads (has/get); the editor world is the readonly view.const instances = readColliderShapes(world as unknown as Parameters<typeof readColliderShapes>[0], id);@@ -1253,9 +1266,8 @@ export const ViewportController = {}for (const c of o.circles) {const ctr = toClient(c.c.x, c.c.y);- const edge = toClient(c.c.x + c.r, c.c.y);- if (!ctr || !edge) continue;- const r = Math.hypot(edge.x - ctr.x, edge.y - ctr.y);+ if (!ctr) continue;+ const r = lengthPx(c.r);d += `M${ctr.x - r},${ctr.y}a${r},${r} 0 1,0 ${2 * r},0a${r},${r} 0 1,0 ${-2 * r},0 `;}return d;@@ -1528,9 +1540,8 @@ export const ViewportController = {}for (const c of piece.circles) {const ctr = toClient(c.c.x, c.c.y);- const edge = toClient(c.c.x + c.r, c.c.y);- if (!ctr || !edge) continue;- const r = Math.hypot(edge.x - ctr.x, edge.y - ctr.y);+ if (!ctr) continue;+ const r = this.screenLengthAt({ x: c.c.x, y: c.c.y, z: piece.planeZ ?? 0 }, c.r);// A full circle as two half-arcs (SVG has no closed-circle path primitive).const arc = `M${ctr.x - r},${ctr.y}a${r},${r} 0 1,0 ${2 * r},0a${r},${r} 0 1,0 ${-2 * r},0 `;if (piece.sensor) sensor += arc; else solid += arc;@@ -1820,7 +1831,8 @@ export const ViewportController = {angleSpreadMin: number; angleSpreadMax: number; enabled: boolean;};const toClient = this.projectorFor(id);- const center = toClient(t.worldPosition.x, t.worldPosition.y);+ const at = { x: t.worldPosition.x, y: t.worldPosition.y, z: t.worldPosition.z };+ const center = toClient(at.x, at.y);if (!center) return null;const on = p.enabled !== false;const rot = quatAngleZ(t.worldRotation as { w: number; x: number; y: number; z: number });@@ -1853,11 +1865,10 @@ export const ViewportController = {switch (p.shape) {case 1: { // Circle — spawn disk of shapeRadius- const edge = toClient(t.worldPosition.x + p.shapeRadius, t.worldPosition.y);- const r = edge ? Math.hypot(edge.x - center.x, edge.y - center.y) : 0;- // Radius handle at the top of the ring (drag to resize shapeRadius).- const handle = toClient(t.worldPosition.x, t.worldPosition.y + p.shapeRadius);- return { cx: center.x, cy: center.y, kind: 'circle', r, pts: [], spread: null, on, handle: handle ?? null, sizeHandle: null, angleHandle: null };+ const r = this.screenLengthAt(at, p.shapeRadius);+ // Radius handle at the top of the ring as DRAWN (drag to resize shapeRadius).+ const handle = { x: center.x, y: center.y - r };+ return { cx: center.x, cy: center.y, kind: 'circle', r, pts: [], spread: null, on, handle, sizeHandle: null, angleHandle: null };}case 2: { // Rectangle — oriented spawn box of shapeSizeconst corners = obbCorners({