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): the view works on a plane it can see
esengine2b1c2e7d2772
diff --git a/desktop/tests/viewport-navigation.test.ts b/desktop/tests/viewport-navigation.test.tsindex a3ace70e6..2dbe6be23 100644--- a/desktop/tests/viewport-navigation.test.ts+++ b/desktop/tests/viewport-navigation.test.ts@@ -131,7 +131,7 @@ describe('the plane the view authors on', () => {// A 3D scene stands on the ground, and that is the surface a dropped thing// belongs on — not the wall at z = 0 the cursor happens to cross.it('perspective is the ground, and says so in the axes it names', () => {- host.view = perspectiveView({});+ host.view = perspectiveView({ pitch: 40 });host.ray = downAndBack;const p = ViewportController.canvasToWorkPlane(400, 300);expect(p).not.toBeNull();@@ -144,12 +144,16 @@ describe('the plane the view authors on', () => {// A perspective eye held head-on looks ALONG the ground: every ray meets it// where nobody pointed. The plane through the focus facing the eye is what a// screen point means there — and head-on that is the 2D plane, unchanged.- it('falls back to the focus plane when the work plane is edge-on', () => {- host.view = perspectiveView({ z: -25 });- host.ray = { origin: { x: 3, y: 4, z: 900 }, dir: { x: 0, y: 0.001, z: -1 } };+ // A ray can lie along a plane the VIEW is square enough on: half a wide fov at+ // the frame's edge is a long way off the view axis.+ it('falls back to the focus plane when the ray itself lies in the plane', () => {+ host.view = perspectiveView({ pitch: 8, z: -25 });+ host.ray = { origin: { x: 3, y: 4, z: 900 }, dir: { x: 0, y: 0.0005, z: -1 } };const p = ViewportController.canvasToWorkPlane(400, 300);expect(p).not.toBeNull();- expect(p!.z).toBeCloseTo(-25, 4);+ // Near the focus, on the plane facing the eye — not the thousands of units+ // away the ground would have answered.+ expect(Math.abs(p!.z + 25)).toBeLessThan(5);expect(p!.x).toBeCloseTo(3, 4);});});@@ -173,6 +177,16 @@ describe('the arrow-key nudge', () => {expect(d.z).toBeCloseTo(-4, 6);});+ // A perspective eye that has not been turned yet looks ALONG the ground, whose+ // second axis then points into the picture. The 2D pair is what the keys name.+ it('is the 2D nudge again when the ground is edge-on', () => {+ host.view = perspectiveView({}); // head-on: the eye looks ALONG the ground+ const d = ViewportController.nudgeVector(10, 4);+ expect(d.x).toBeCloseTo(10, 6);+ expect(d.y).toBeCloseTo(4, 6);+ expect(d.z).toBeCloseTo(0, 6);+ });+// Turned a quarter turn about Y, screen-right is world Z and the ground's other// axis takes the vertical key — never the same axis twice.it('follows the eye round, and never names one axis twice', () => {@@ -200,7 +214,7 @@ describe('the minimap plots the plane the view works on', () => {// Looking down at the ground, +Z runs AWAY from the eye — down the map, the way// a plan drawn from above reads. Plotting it as +y up would mirror the scene.it('is the ground under a 3D eye, drawn as looking down reads', () => {- const v = perspectiveView({ x: 5, y: 1, z: 7 });+ const v = perspectiveView({ x: 5, y: 1, z: 7, pitch: 40 });host.view = v;const r = ViewportController.editorViewRect()!;expect(r.cx).toBeCloseTo(5, 6);diff --git a/sdk/src/camera/EditorView.ts b/sdk/src/camera/EditorView.tsindex b48ba664f..e8422f4d2 100644--- a/sdk/src/camera/EditorView.ts+++ b/sdk/src/camera/EditorView.ts@@ -237,13 +237,27 @@ export interface EditorWorkPlane {* Which plane that is.** The perspective toggle is the editor's word for "this view is looking at a 3D- * scene", and a 3D scene stands on the ground (y = 0). Orthographic is the 2D- * editor, whose plane is the one 2D content lives on.+ * scene", and a 3D scene stands on the ground (y = 0); orthographic is the 2D+ * editor, whose plane is the one 2D content lives on. A plane the eye looks ALONG+ * offers nothing to work on, so there the plane the eye most faces answers instead+ * — which is how a perspective view held head-on is the 2D one again.*/export function editorViewWorkPlane(view: EditorViewData): EditorWorkPlane {- return view.perspective ? { u: 0, v: 2, normal: 1 } : { u: 0, v: 1, normal: 2 };+ const nominal: EditorWorkPlane = view.perspective+ ? { u: 0, v: 2, normal: 1 }+ : { u: 0, v: 1, normal: 2 };+ const screen = editorViewAxes(view);+ const depth = (a: WorldAxis): number =>+ Math.abs((a === 0 ? screen.x : a === 1 ? screen.y : screen.z).depth);+ if (depth(nominal.normal) >= PLANE_SEEN_MIN) return nominal;+ const normal = ([0, 1, 2] as WorldAxis[]).reduce((best, a) => (depth(a) > depth(best) ? a : best), 0);+ const [u, v] = ([0, 1, 2] as WorldAxis[]).filter((a) => a !== normal);+ return { u: u!, v: v!, normal };}+/** How square-on a plane must be to be the one worked on — about 6° off edge-on. */+const PLANE_SEEN_MIN = 0.1;+/*** The work-plane axis a screen direction names, with the sign pointing that way —* what an arrow key nudges along. `right` resolves first and `up` takes the plane's@@ -255,6 +269,7 @@ export function screenNudgeAxes(const plane = editorViewWorkPlane(view);const screen = editorViewAxes(view);const of = (a: WorldAxis): ScreenAxis => (a === 0 ? screen.x : a === 1 ? screen.y : screen.z);+ const candidates: WorldAxis[] = [plane.u, plane.v];// Screen y runs down, so pointing up the screen is a negative dy.const score = (a: WorldAxis, sign: number, dir: 'right' | 'up'): number =>sign * (dir === 'right' ? of(a).dx : -of(a).dy);@@ -269,8 +284,8 @@ export function screenNudgeAxes(}return out;};- const right = best([plane.u, plane.v], 'right');- const up = best([plane.u, plane.v].filter((a) => a !== right.axis), 'up');+ const right = best(candidates, 'right');+ const up = best(candidates.filter((a) => a !== right.axis), 'up');return { right, up };}