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 near plane follows how far back it stands
esengine4f03c83a8efa
diff --git a/desktop/scripts/editor-checks/depth-precision.mjs b/desktop/scripts/editor-checks/depth-precision.mjsnew file mode 100644index 000000000..1735358d4--- /dev/null+++ b/desktop/scripts/editor-checks/depth-precision.mjs@@ -0,0 +1,110 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file depth-precision — a nearer surface hides a further one, at any zoom.+ *+ * Depth resolution goes as the distance squared over the near plane, so a near+ * plane fixed close to the eye spends the whole buffer on the first few units and+ * leaves the far half unable to tell surfaces apart. The editor's eye stands off+ * what it looks at, and that stand-off is the distance — so how close the near+ * plane may sit is a question about the zoom, not a constant.+ *+ * Two opaque quads in the same place, the further one submitted LAST so it wins+ * whenever depth cannot separate them, framed on a large scene and 200 units apart.+ * Both directions are asked, so a viewport that simply always drew the first one+ * would fail the control.+ */+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 tens of thousands of units back. */+const PROJECT = JSON.stringify({+ formatVersion: '1',+ name: 'Depth Precision',+ defaultScene: 'assets/scenes/main.esscene',+ designResolution: { width: 120000, height: 90000 },+ spineVersion: 'none',+}, null, 2);++const quad = (id, name, z, colour) => ({+ id, name, parent: null, children: [], visible: true,+ components: [+ { type: 'Transform', data: { position: { x: 0, y: 0, z }, scale: { x: 300, y: 300, z: 1 } } },+ {+ type: 'Mesh2D',+ data: { mesh: 'builtin:quad', lit: false, opaque: true, cullBackfaces: false, color: colour },+ },+ ],+});++const SCENE = JSON.stringify({+ version: '1.0', name: 'Main',+ entities: [+ quad(1, 'Front', 0, { r: 1, g: 0, b: 0, a: 1 }),+ quad(2, 'Mover', -200, { r: 0, g: 0, b: 1, a: 1 }),+ ],+}, null, 1);++export const name = 'depth-precision';+export const describes = 'a nearer surface hides a further one even when the view is far out';++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 RED = [255, 0, 0];+const BLUE = [0, 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.call('run_editor_command', { id: 'view.toggleViewPerspective' }, 30000);+ await ed.call('run_editor_command', { id: 'view.resetOrbit' }, 30000);+ await ed.sleep(1500);++ const behind = await ed.capture('depth-precision-behind');+ const front = count(behind, RED);+ const back = count(behind, BLUE);+ // Whether the pair is on screen at all, asked of BOTH: which one covers the other+ // is the question, and a guard on one of them answers it by accident.+ if (!check(front + back > 200, 'neither quad is on screen — the scene is not framed the way this check needs')) {+ return check.failures;+ }+ check(+ back === 0,+ `${back}px of a quad 200 units BEHIND another came through it, leaving ${front}px of the `+ + 'front one — at this stand-off the depth buffer cannot tell them apart',+ );++ // The control: move it in front and it must win, or the claim above would hold+ // for a viewport that simply always drew the first quad.+ await ed.call('set_field', {+ entity: 2, component: 'Transform', key: 'position', type: 'vec3', value: [0, 0, 200],+ }, 30000);+ await ed.sleep(1200);+ const ahead = await ed.capture('depth-precision-ahead');+ check(+ count(ahead, BLUE) > 200 && count(ahead, RED) === 0,+ `moved 200 units IN FRONT, the same quad covers ${count(ahead, BLUE)}px and leaves `+ + `${count(ahead, RED)}px of the other — depth is not what decided the first claim`,+ );++ return check.failures;+}diff --git a/desktop/tests/viewport-navigation.test.ts b/desktop/tests/viewport-navigation.test.tsindex f5387269f..48a40c885 100644--- a/desktop/tests/viewport-navigation.test.ts+++ b/desktop/tests/viewport-navigation.test.ts@@ -11,7 +11,8 @@*/import { describe, it, expect, vi, beforeEach } from 'vitest';import {- DEFAULT_EDITOR_VIEW, editorViewClipFar, editorViewHalfExtent, editorViewWorldPerPixel,+ DEFAULT_EDITOR_VIEW, editorViewClipFar, editorViewClipNear, editorViewHalfExtent,+ editorViewWorldPerPixel,type EditorViewData,} from 'esengine';@@ -296,4 +297,25 @@ describe("the editor eye's clip volume", () => {expect(editorViewClipFar(far)).toBeGreaterThan(editorViewClipFar(near));expect(editorViewClipFar(far) - far.distance).toBeCloseTo(100000, 6);});++ it('an orthographic eye has no near plane to place', () => {+ expect(editorViewClipNear(view({}))).toBe(0);+ expect(editorViewClipNear(view({ yaw: 30, pitch: 20 }))).toBe(0);+ });++ // Depth resolution goes as distance² / near, and the stand-off IS the distance:+ // a near plane fixed close to the eye spends the buffer before the scene starts.+ it('the near plane follows the stand-off, so the far half keeps its precision', () => {+ const close = perspectiveView({ distance: 500 });+ const wide = perspectiveView({ distance: 500000 });+ expect(editorViewClipNear(wide)).toBeGreaterThan(editorViewClipNear(close) * 100);+ expect(editorViewClipFar(wide) / editorViewClipNear(wide))+ .toBeLessThan(editorViewClipFar(close) / editorViewClipNear(close));+ });++ // The floor answers for a close-up eye, so it never sits further out than that.+ it('never sits further out than the value a close-up eye always had', () => {+ expect(editorViewClipNear(perspectiveView({ distance: 14 }))).toBeCloseTo(0.1, 9);+ expect(editorViewClipNear(perspectiveView({ distance: 1 }))).toBeCloseTo(0.1, 9);+ });});diff --git a/docs/astro/src/data/apiStability.generated.json b/docs/astro/src/data/apiStability.generated.jsonindex 511d550c4..65dce59f2 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": 1594,+ "experimental": 1595,"deprecated": 3},- "total": 1780,+ "total": 1781,"subsystems": [{"id": "ecs",diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.mdindex 874dab68e..c23c4b8ba 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 · 1480 experimental · 3 deprecated+Symbols: 113 public · 66 beta · 1481 experimental · 3 deprecated## ABI_LAYOUT_HASH — const @experimental```@@ -9946,6 +9946,11 @@ DragPlugin(view: EditorViewData): number```+## editorViewClipNear — 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 36844350a..14d72068e 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 · 1499 experimental · 3 deprecated+Symbols: 113 public · 71 beta · 1500 experimental · 3 deprecated## ABI_LAYOUT_HASH — const @experimental```@@ -10179,6 +10179,11 @@ DragPlugin(view: EditorViewData): number```+## editorViewClipNear — 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 319e7277d..043956bd3 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 · 1479 experimental · 3 deprecated+Symbols: 113 public · 68 beta · 1480 experimental · 3 deprecated## ANCHOR_AXES — const @experimental```@@ -9941,6 +9941,11 @@ DragPlugin(view: EditorViewData): number```+## editorViewClipNear — function @experimental+```+(view: EditorViewData): number+```+## editorViewEye — function @experimental```(view: EditorViewData): Vec3diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.mdindex 0fcaa3ad4..2c17dfa23 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 · 1500 experimental · 3 deprecated+Symbols: 113 public · 66 beta · 1501 experimental · 3 deprecated## ANCHOR_AXES — const @experimental```@@ -10191,6 +10191,11 @@ DragPlugin(view: EditorViewData): number```+## editorViewClipNear — function @experimental+```+(view: EditorViewData): number+```+## editorViewEye — function @experimental```(view: EditorViewData): Vec3diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.mdindex fd84faf51..b3a9b4da9 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 · 1490 experimental · 3 deprecated+Symbols: 113 public · 66 beta · 1491 experimental · 3 deprecated## ANCHOR_AXES — const @experimental```@@ -9968,6 +9968,11 @@ DragPlugin(view: EditorViewData): number```+## editorViewClipNear — function @experimental+```+(view: EditorViewData): number+```+## editorViewEye — function @experimental```(view: EditorViewData): Vec3diff --git a/sdk/src/camera/CameraPlugin.ts b/sdk/src/camera/CameraPlugin.tsindex c727c5d10..c2945462e 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, editorViewClipFar, type EditorViewData } from './EditorView';+import { EditorView, DEFAULT_EDITOR_VIEW, editorViewStandoff, editorViewClipFar, editorViewClipNear, 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';@@ -438,9 +438,7 @@ export function editorCameraInfo(projection: view.perspective ? ProjectionType.Perspective : ProjectionType.Orthographic,orthoSize: view.orthoSize,fov: view.fov,- // 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,+ near: editorViewClipNear(view),far: editorViewClipFar(view),viewport: { x: 0, y: 0, z: 1, w: 1 },clearFlags: ClearFlags.ColorAndDepth,diff --git a/sdk/src/camera/EditorView.ts b/sdk/src/camera/EditorView.tsindex b083124a6..4293b59f6 100644--- a/sdk/src/camera/EditorView.ts+++ b/sdk/src/camera/EditorView.ts@@ -212,8 +212,23 @@ export function editorViewClipFar(view: EditorViewData): number {return editorViewStandoff(view) + EDITOR_VIEW_REACH;}+/**+ * Where the volume begins, from the eye. Zero orthographically — that projection+ * builds a symmetric box and ignores it. Depth resolution goes as distance² over+ * near, and the stand-off IS the distance, so how close the near plane may sit is+ * a question about the zoom. The floor is what answers at a close-up eye.+ */+export function editorViewClipNear(view: EditorViewData): number {+ return view.perspective+ ? Math.max(EDITOR_VIEW_NEAR_FLOOR, editorViewStandoff(view) * EDITOR_VIEW_NEAR_FRACTION)+ : 0;+}+/** How far past what the view is looking at its volume extends. */const EDITOR_VIEW_REACH = 100000;+/** The near plane as a fraction of the stand-off, and the closest it ever sits. */+const EDITOR_VIEW_NEAR_FRACTION = 1e-3;+const EDITOR_VIEW_NEAR_FLOOR = 0.1;/** Where the eye stands in world space. */export function editorViewEye(view: EditorViewData): Vec3 {diff --git a/sdk/src/core-render.ts b/sdk/src/core-render.tsindex 6f2210a9f..dff1a9991 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, editorViewClipFar, editorViewWorkPlane, screenWorkAxes, worldAxisVector, moveEditorViewFocus, editorViewBoxExtent } from './camera/EditorView';+export { EditorView, DEFAULT_EDITOR_VIEW, editorViewHalfHeight, editorViewHalfExtent, editorViewWorldPerPixel, setEditorViewHalfHeight, editorViewIsOrbited, editorViewAxes, editorViewAxisAngles, editorViewBasis, editorViewEye, editorViewStandoff, editorViewClipFar, editorViewClipNear, 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';