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(render): the editor eye's volume reaches past what it is looking at
esengine42dd7f60c89e
diff --git a/desktop/scripts/editor-checks/far-plane.mjs b/desktop/scripts/editor-checks/far-plane.mjsnew file mode 100644index 000000000..f067fd9f5--- /dev/null+++ b/desktop/scripts/editor-checks/far-plane.mjs@@ -0,0 +1,111 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file far-plane — the editor's eye can see the scene it is framed on.+ *+ * The perspective far plane is measured from the EYE, and a perspective eye stands+ * off the thing it looks at. Framed on a large scene that stand-off is tens of+ * thousands of units, all of it spent before the scene begins — so a reach that+ * does not account for it cuts away the far half of what the view was framed on.+ *+ * The orthographic eye of the same framing is the control: it shows the same+ * content, so anything the perspective one loses it lost to its own volume rather+ * than to being off-screen or too small to see.+ */+import { cp } from 'node:fs/promises';+import path from 'node:path';+import { makeProject, checker, DESKTOP } from '../lib/editorDriver.mjs';++const EXAMPLE = path.resolve(DESKTOP, '..', 'examples', 'sprite-rendering');++/** Framed on this, the eye stands far enough back for the stand-off to matter. */+const PROJECT = JSON.stringify({+ formatVersion: '1',+ name: 'Far Plane',+ defaultScene: 'assets/scenes/main.esscene',+ designResolution: { width: 120000, height: 90000 },+ spineVersion: 'none',+}, null, 2);++const SCENE = JSON.stringify({+ version: '1.0', name: 'Main',+ entities: [+ {+ id: 1, name: 'OnPlane', parent: null, children: [], visible: true,+ components: [+ { type: 'Transform', data: { position: { x: -30000, y: 0, z: 0 } } },+ { type: 'Sprite', data: { size: { x: 30000, y: 30000 }, color: { r: 0, g: 1, b: 0, a: 1 } } },+ ],+ },+ {+ id: 2, name: 'FarBack', parent: null, children: [], visible: true,+ components: [+ { type: 'Transform', data: { position: { x: 30000, y: 0, z: -50000 } } },+ { type: 'Sprite', data: { size: { x: 60000, y: 60000 }, color: { r: 1, g: 0, b: 1, a: 1 } } },+ ],+ },+ ],+}, null, 1);++export const name = 'far-plane';+export const describes = "the 3D eye keeps the scene its framing put in front of it";++/** Pixels of a colour, sampled every other row and column. */+function count(png, want, tol = 60) {+ let n = 0;+ for (let y = 0; y < png.h; y += 2) {+ for (let x = 0; x < png.w; x += 2) {+ const p = png.px(x, y);+ if (Math.abs(p[0] - want[0]) <= tol && Math.abs(p[1] - want[1]) <= tol+ && Math.abs(p[2] - want[2]) <= tol) n += 1;+ }+ }+ return n;+}++const GREEN = [0, 255, 0];+const MAGENTA = [255, 0, 255];++export async function run(ed) {+ const root = await makeProject({+ 'assets/scenes/main.esscene': SCENE,+ 'project.esproject': PROJECT,+ });+ await cp(path.join(EXAMPLE, 'src'), path.join(root, 'src'), { recursive: true });+ const check = checker();+ await ed.open(root, 'assets/scenes/main.esscene');+ await ed.call('run_editor_command', { id: 'mode.scene' }, 30000);+ await ed.sleep(1000);++ const ortho = await ed.capture('far-plane-ortho');+ const orthoNear = count(ortho, GREEN);+ const orthoFar = count(ortho, MAGENTA);+ if (!check(orthoNear > 200 && orthoFar > 200,+ `the orthographic view shows ${orthoNear}px on the plane and ${orthoFar}px behind it — `+ + 'the scene is not framed the way this check needs')) {+ return check.failures;+ }++ // Held head-on, so the projection is the only thing that changed.+ await ed.call('run_editor_command', { id: 'view.toggleViewPerspective' }, 30000);+ await ed.call('run_editor_command', { id: 'view.resetOrbit' }, 30000);+ await ed.sleep(1200);+ const persp = await ed.capture('far-plane-persp');++ // On the plane the two projections agree, which is what says the framing held.+ const perspNear = count(persp, GREEN);+ check(+ Math.abs(perspNear - orthoNear) < orthoNear * 0.25,+ `the content ON the plane went from ${orthoNear}px to ${perspNear}px — the toggle changed `+ + 'the framing, so nothing below is about the far plane',+ );++ const perspFar = count(persp, MAGENTA);+ check(+ perspFar > orthoFar * 0.2,+ `the content 50000 units behind the plane went from ${orthoFar}px to ${perspFar}px — the far `+ + "plane is being measured from the eye, and the eye's stand-off has eaten it",+ );++ return check.failures;+}diff --git a/desktop/tests/viewport-navigation.test.ts b/desktop/tests/viewport-navigation.test.tsindex 938b3305a..f5387269f 100644--- a/desktop/tests/viewport-navigation.test.ts+++ b/desktop/tests/viewport-navigation.test.ts@@ -10,7 +10,10 @@* would say so.*/import { describe, it, expect, vi, beforeEach } from 'vitest';-import { DEFAULT_EDITOR_VIEW, editorViewHalfExtent, editorViewWorldPerPixel, type EditorViewData } from 'esengine';+import {+ DEFAULT_EDITOR_VIEW, editorViewClipFar, editorViewHalfExtent, editorViewWorldPerPixel,+ type EditorViewData,+} from 'esengine';const host = vi.hoisted(() => ({view: null as EditorViewData | null,@@ -274,3 +277,23 @@ describe('the plane a gesture is spelt on', () => {expect(ViewportController.planePointToWorld(p, uv.u, uv.v)).toEqual(p);});});++describe("the editor eye's clip volume", () => {+ it('an orthographic head-on eye keeps the reach it always had', () => {+ expect(editorViewClipFar(view({}))).toBeCloseTo(100000, 6);+ });++ // A perspective eye stands off what it looks at, and that stand-off is spent+ // before the scene begins — so the reach past the focus must survive it.+ it('a perspective eye adds its stand-off, so the reach past the focus is the same', () => {+ const v = perspectiveView({});+ expect(editorViewClipFar(v) - v.distance).toBeCloseTo(100000, 6);+ });++ it('grows with the zoom, which is what moves a perspective eye back', () => {+ const near = perspectiveView({});+ const far = perspectiveView({ distance: near.distance * 50 });+ expect(editorViewClipFar(far)).toBeGreaterThan(editorViewClipFar(near));+ expect(editorViewClipFar(far) - far.distance).toBeCloseTo(100000, 6);+ });+});diff --git a/docs/astro/src/data/apiStability.generated.json b/docs/astro/src/data/apiStability.generated.jsonindex 02526f3cb..511d550c4 100644--- a/docs/astro/src/data/apiStability.generated.json+++ b/docs/astro/src/data/apiStability.generated.json@@ -3,10 +3,10 @@"counts": {"public": 113,"beta": 73,- "experimental": 1593,+ "experimental": 1594,"deprecated": 3},- "total": 1779,+ "total": 1780,"subsystems": [{"id": "ecs",diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.mdindex 06cb510a3..874dab68e 100644--- a/sdk/etc/index.api.md+++ b/sdk/etc/index.api.md@@ -1,7 +1,7 @@# API surface — esengine/index<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->-Symbols: 113 public · 66 beta · 1479 experimental · 3 deprecated+Symbols: 113 public · 66 beta · 1480 experimental · 3 deprecated## ABI_LAYOUT_HASH — const @experimental```@@ -9941,6 +9941,11 @@ DragPlugin(view: EditorViewData, half: Vec3): { right: number; up: number; }```+## editorViewClipFar — function @experimental+```+(view: EditorViewData): number+```+## editorViewEye — function @experimental```(view: EditorViewData): Vec3diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.mdindex 188110e6c..36844350a 100644--- a/sdk/etc/index.native.api.md+++ b/sdk/etc/index.native.api.md@@ -1,7 +1,7 @@# API surface — esengine/index.native<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->-Symbols: 113 public · 71 beta · 1498 experimental · 3 deprecated+Symbols: 113 public · 71 beta · 1499 experimental · 3 deprecated## ABI_LAYOUT_HASH — const @experimental```@@ -10174,6 +10174,11 @@ DragPlugin(view: EditorViewData, half: Vec3): { right: number; up: number; }```+## editorViewClipFar — function @experimental+```+(view: EditorViewData): number+```+## editorViewEye — function @experimental```(view: EditorViewData): Vec3diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.mdindex 1240f97cd..319e7277d 100644--- a/sdk/etc/index.node.api.md+++ b/sdk/etc/index.node.api.md@@ -1,7 +1,7 @@# API surface — esengine/index.node<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->-Symbols: 113 public · 68 beta · 1478 experimental · 3 deprecated+Symbols: 113 public · 68 beta · 1479 experimental · 3 deprecated## ANCHOR_AXES — const @experimental```@@ -9936,6 +9936,11 @@ DragPlugin(view: EditorViewData, half: Vec3): { right: number; up: number; }```+## editorViewClipFar — function @experimental+```+(view: EditorViewData): number+```+## editorViewEye — function @experimental```(view: EditorViewData): Vec3diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.mdindex 46a11734f..0fcaa3ad4 100644--- a/sdk/etc/minigame.api.md+++ b/sdk/etc/minigame.api.md@@ -1,7 +1,7 @@# API surface — esengine/minigame<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->-Symbols: 113 public · 66 beta · 1499 experimental · 3 deprecated+Symbols: 113 public · 66 beta · 1500 experimental · 3 deprecated## ANCHOR_AXES — const @experimental```@@ -10186,6 +10186,11 @@ DragPlugin(view: EditorViewData, half: Vec3): { right: number; up: number; }```+## editorViewClipFar — function @experimental+```+(view: EditorViewData): number+```+## editorViewEye — function @experimental```(view: EditorViewData): Vec3diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.mdindex daaf87f2e..fd84faf51 100644--- a/sdk/etc/wechat.api.md+++ b/sdk/etc/wechat.api.md@@ -1,7 +1,7 @@# API surface — esengine/wechat<!-- Generated by tools/api-surface.mjs — do not edit. Run --update to accept changes. -->-Symbols: 113 public · 66 beta · 1489 experimental · 3 deprecated+Symbols: 113 public · 66 beta · 1490 experimental · 3 deprecated## ANCHOR_AXES — const @experimental```@@ -9963,6 +9963,11 @@ DragPlugin(view: EditorViewData, half: Vec3): { right: number; up: number; }```+## editorViewClipFar — function @experimental+```+(view: EditorViewData): number+```+## editorViewEye — function @experimental```(view: EditorViewData): Vec3diff --git a/sdk/src/camera/CameraPlugin.ts b/sdk/src/camera/CameraPlugin.tsindex 25e3d70cb..c727c5d10 100644--- a/sdk/src/camera/CameraPlugin.ts+++ b/sdk/src/camera/CameraPlugin.ts@@ -18,7 +18,7 @@ import type { Entity } from '../types';import { UICameraInfo } from '../ui/core/ui-camera-info';import { ProjectionType, SceneOwner, ClearFlags } from '../ecs/component';import { uiLayoutRect, computeEffectiveOrthoSize, EDITOR_VIEW_ENTITY, type CanvasScale } from './uiLayoutRect';-import { EditorView, DEFAULT_EDITOR_VIEW, editorViewStandoff, type EditorViewData } from './EditorView';+import { EditorView, DEFAULT_EDITOR_VIEW, editorViewStandoff, editorViewClipFar, type EditorViewData } from './EditorView';import { ScreenScaling, DEFAULT_SCREEN_SCALING, SCREEN_FIT_OFF } from './ScreenScaling';import { CameraDirector, createDirectorState, resolveMainPOV } from './CameraDirector';import { RenderPipeline } from '../render/renderPipeline';@@ -441,7 +441,7 @@ export function editorCameraInfo(// A perspective projection divides by z, so near must be > 0 — the// orthographic path builds a symmetric [-far, far] box and ignores it.near: view.perspective ? 0.1 : 0,- far: 100000,+ far: editorViewClipFar(view),viewport: { x: 0, y: 0, z: 1, w: 1 },clearFlags: ClearFlags.ColorAndDepth,priority: 0,diff --git a/sdk/src/camera/EditorView.ts b/sdk/src/camera/EditorView.tsindex b397c7575..b083124a6 100644--- a/sdk/src/camera/EditorView.ts+++ b/sdk/src/camera/EditorView.ts@@ -202,6 +202,19 @@ export function editorViewStandoff(view: EditorViewData): number {return view.perspective || editorViewIsOrbited(view) ? view.distance : 0;}+/**+ * How far the view's volume reaches, from the EYE — which stands off what it is+ * looking at, and spends that stand-off before the scene begins. So the reach past+ * the focus is constant and the stand-off is added to it; a head-on orthographic+ * eye, which has none, is left with exactly the reach it always had.+ */+export function editorViewClipFar(view: EditorViewData): number {+ return editorViewStandoff(view) + EDITOR_VIEW_REACH;+}++/** How far past what the view is looking at its volume extends. */+const EDITOR_VIEW_REACH = 100000;+/** Where the eye stands in world space. */export function editorViewEye(view: EditorViewData): Vec3 {const m = orbitMatrix(view);diff --git a/sdk/src/core-render.ts b/sdk/src/core-render.tsindex 212a2b629..6f2210a9f 100644--- a/sdk/src/core-render.ts+++ b/sdk/src/core-render.ts@@ -42,7 +42,7 @@ export { TextureFilter, TextureWrap, setTextureFilter, setTextureWrap, setTextur// =============================================================================export { CameraView, CameraViewAPI } from './camera/Camera';-export { EditorView, DEFAULT_EDITOR_VIEW, editorViewHalfHeight, editorViewHalfExtent, editorViewWorldPerPixel, setEditorViewHalfHeight, editorViewIsOrbited, editorViewAxes, editorViewAxisAngles, editorViewBasis, editorViewEye, editorViewStandoff, editorViewWorkPlane, screenWorkAxes, worldAxisVector, moveEditorViewFocus, editorViewBoxExtent } from './camera/EditorView';+export { EditorView, DEFAULT_EDITOR_VIEW, editorViewHalfHeight, editorViewHalfExtent, editorViewWorldPerPixel, setEditorViewHalfHeight, editorViewIsOrbited, editorViewAxes, editorViewAxisAngles, editorViewBasis, editorViewEye, editorViewStandoff, editorViewClipFar, editorViewWorkPlane, screenWorkAxes, worldAxisVector, moveEditorViewFocus, editorViewBoxExtent } from './camera/EditorView';export type { EditorViewData, ScreenAxis, EditorViewBasis, EditorWorkPlane, WorldAxis } from './camera/EditorView';export { ScreenScaling, DEFAULT_SCREEN_SCALING, SCREEN_FIT_OFF } from './camera/ScreenScaling';export type { ScreenScalingData } from './camera/ScreenScaling';