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.

fix(scene): a streaming cell is a sphere, so a tower is not all one place

esengine1405b28ec205
diff --git a/docs/astro/src/data/apiStability.generated.json b/docs/astro/src/data/apiStability.generated.json
index 7cb017d7b..bd353533c 100644
--- a/docs/astro/src/data/apiStability.generated.json
+++ b/docs/astro/src/data/apiStability.generated.json
@@ -2,8 +2,8 @@
"note": "Generated by tools/api-stability.mjs from sdk/etc/*.api.md — do not edit.",
"counts": {
"public": 113,
- "beta": 65,
- "experimental": 1654,
+ "beta": 71,
+ "experimental": 1648,
"deprecated": 3
},
"total": 1832,
@@ -1738,6 +1738,18 @@
"minigame"
]
},
+ {
+ "name": "AnimatorData",
+ "kind": "interface",
+ "deprecated": false,
+ "entries": [
+ "index",
+ "index.node",
+ "index.native",
+ "wechat",
+ "minigame"
+ ]
+ },
{
"name": "Assets",
"kind": "const",
@@ -1835,6 +1847,19 @@
"minigame"
]
},
+ {
+ "name": "CharacterController2DData",
+ "kind": "interface",
+ "deprecated": false,
+ "entries": [
+ "index",
+ "index.node",
+ "index.native",
+ "physics",
+ "wechat",
+ "minigame"
+ ]
+ },
{
"name": "GraphNodeType",
"kind": "type",
@@ -2025,6 +2050,19 @@
"minigame"
]
},
+ {
+ "name": "Physics2DEventsData",
+ "kind": "interface",
+ "deprecated": false,
+ "entries": [
+ "index",
+ "index.node",
+ "index.native",
+ "physics",
+ "wechat",
+ "minigame"
+ ]
+ },
{
"name": "PrefabOverride",
"kind": "interface",
@@ -2170,6 +2208,18 @@
"minigame"
]
},
+ {
+ "name": "SpineAnimationData",
+ "kind": "interface",
+ "deprecated": false,
+ "entries": [
+ "index",
+ "index.node",
+ "index.native",
+ "wechat",
+ "minigame"
+ ]
+ },
{
"name": "SpriteAnimator",
"kind": "const",
@@ -2182,6 +2232,18 @@
"minigame"
]
},
+ {
+ "name": "SpriteAnimatorData",
+ "kind": "interface",
+ "deprecated": false,
+ "entries": [
+ "index",
+ "index.node",
+ "index.native",
+ "wechat",
+ "minigame"
+ ]
+ },
{
"name": "ThemeColors",
"kind": "interface",
@@ -2278,6 +2340,18 @@
"minigame"
]
},
+ {
+ "name": "UIVisualData",
+ "kind": "interface",
+ "deprecated": false,
+ "entries": [
+ "index",
+ "index.node",
+ "index.native",
+ "wechat",
+ "minigame"
+ ]
+ },
{
"name": "acquireWebGPUDevice",
"kind": "function",
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index 4162abc96..f84a85beb 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -6895,7 +6895,7 @@ configure: (config: SceneStreamingConfig) => void
getActive: () => string[]
getFocusEntity: () => Entity | null
register: (cell: StreamCell) => void
-setFocus: (x: number, y: number) => void
+setFocus: (x: number, y: number, z?: number) => void
setFocusEntity: (entity: Entity | null) => void
unregister: (scene: string) => void
update: () => void
@@ -7531,6 +7531,7 @@ radius: number
scene: string
x: number
y: number
+z: number | undefined
```
## StreamDecision — interface @experimental
@@ -9868,7 +9869,7 @@ BtPlugin
## computeStreaming — function @experimental
```
-(cells: readonly StreamCell[], focusX: number, focusY: number, loadRadius: number, unloadRadius: number, active: ReadonlySet<string>): StreamDecision
+(cells: readonly StreamCell[], focusX: number, focusY: number, loadRadius: number, unloadRadius: number, active: ReadonlySet<string>, focusZ?: number): StreamDecision
```
## connect — function @beta
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 11cfea4d3..ef08ae20b 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -7088,7 +7088,7 @@ configure: (config: SceneStreamingConfig) => void
getActive: () => string[]
getFocusEntity: () => Entity | null
register: (cell: StreamCell) => void
-setFocus: (x: number, y: number) => void
+setFocus: (x: number, y: number, z?: number) => void
setFocusEntity: (entity: Entity | null) => void
unregister: (scene: string) => void
update: () => void
@@ -7724,6 +7724,7 @@ radius: number
scene: string
x: number
y: number
+z: number | undefined
```
## StreamDecision — interface @experimental
@@ -10076,7 +10077,7 @@ BtPlugin
## computeStreaming — function @experimental
```
-(cells: readonly StreamCell[], focusX: number, focusY: number, loadRadius: number, unloadRadius: number, active: ReadonlySet<string>): StreamDecision
+(cells: readonly StreamCell[], focusX: number, focusY: number, loadRadius: number, unloadRadius: number, active: ReadonlySet<string>, focusZ?: number): StreamDecision
```
## connect — function @beta
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index dd96cae8d..224be51ac 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -6890,7 +6890,7 @@ configure: (config: SceneStreamingConfig) => void
getActive: () => string[]
getFocusEntity: () => Entity | null
register: (cell: StreamCell) => void
-setFocus: (x: number, y: number) => void
+setFocus: (x: number, y: number, z?: number) => void
setFocusEntity: (entity: Entity | null) => void
unregister: (scene: string) => void
update: () => void
@@ -7526,6 +7526,7 @@ radius: number
scene: string
x: number
y: number
+z: number | undefined
```
## StreamDecision — interface @experimental
@@ -9863,7 +9864,7 @@ BtPlugin
## computeStreaming — function @experimental
```
-(cells: readonly StreamCell[], focusX: number, focusY: number, loadRadius: number, unloadRadius: number, active: ReadonlySet<string>): StreamDecision
+(cells: readonly StreamCell[], focusX: number, focusY: number, loadRadius: number, unloadRadius: number, active: ReadonlySet<string>, focusZ?: number): StreamDecision
```
## connect — function @beta
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index ce168dfc7..a8cce24a5 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -7135,7 +7135,7 @@ configure: (config: SceneStreamingConfig) => void
getActive: () => string[]
getFocusEntity: () => Entity | null
register: (cell: StreamCell) => void
-setFocus: (x: number, y: number) => void
+setFocus: (x: number, y: number, z?: number) => void
setFocusEntity: (entity: Entity | null) => void
unregister: (scene: string) => void
update: () => void
@@ -7771,6 +7771,7 @@ radius: number
scene: string
x: number
y: number
+z: number | undefined
```
## StreamDecision — interface @experimental
@@ -10114,7 +10115,7 @@ BtPlugin
## computeStreaming — function @experimental
```
-(cells: readonly StreamCell[], focusX: number, focusY: number, loadRadius: number, unloadRadius: number, active: ReadonlySet<string>): StreamDecision
+(cells: readonly StreamCell[], focusX: number, focusY: number, loadRadius: number, unloadRadius: number, active: ReadonlySet<string>, focusZ?: number): StreamDecision
```
## connect — function @beta
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index 5549b12a4..1d55db31e 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -6897,7 +6897,7 @@ configure: (config: SceneStreamingConfig) => void
getActive: () => string[]
getFocusEntity: () => Entity | null
register: (cell: StreamCell) => void
-setFocus: (x: number, y: number) => void
+setFocus: (x: number, y: number, z?: number) => void
setFocusEntity: (entity: Entity | null) => void
unregister: (scene: string) => void
update: () => void
@@ -7533,6 +7533,7 @@ radius: number
scene: string
x: number
y: number
+z: number | undefined
```
## StreamDecision — interface @experimental
@@ -9890,7 +9891,7 @@ BtPlugin
## computeStreaming — function @experimental
```
-(cells: readonly StreamCell[], focusX: number, focusY: number, loadRadius: number, unloadRadius: number, active: ReadonlySet<string>): StreamDecision
+(cells: readonly StreamCell[], focusX: number, focusY: number, loadRadius: number, unloadRadius: number, active: ReadonlySet<string>, focusZ?: number): StreamDecision
```
## connect — function @beta
diff --git a/sdk/src/scene/scenePlugin.ts b/sdk/src/scene/scenePlugin.ts
index 4b1773ba7..374e8ee44 100644
--- a/sdk/src/scene/scenePlugin.ts
+++ b/sdk/src/scene/scenePlugin.ts
@@ -27,13 +27,15 @@ const sceneTransitionSystem = defineSystem(
// Drives proximity streaming: pulls the focus from a follow-entity's Transform (if
// one is set), then reconciles resident cells. A no-op until cells are registered.
-const sceneStreamingSystem = defineSystem(
+// Exported for the criterion that holds the follow to all three axes — the plugin
+// is the only place that reads a Transform into a focus.
+export const sceneStreamingSystem = defineSystem(
[Res(SceneStreaming), GetWorld()],
(streaming: SceneStreamingController, world: World) => {
const focus = streaming.getFocusEntity();
if (focus != null && world.valid(focus) && world.has(focus, Transform)) {
const t = world.get(focus, Transform) as TransformData;
- streaming.setFocus(t.position.x, t.position.y);
+ streaming.setFocus(t.position.x, t.position.y, t.position.z);
}
streaming.update();
},
diff --git a/sdk/src/scene/sceneStreaming.ts b/sdk/src/scene/sceneStreaming.ts
index 9024f633f..375179e15 100644
--- a/sdk/src/scene/sceneStreaming.ts
+++ b/sdk/src/scene/sceneStreaming.ts
@@ -20,6 +20,8 @@ export interface StreamCell {
scene: string;
x: number;
y: number;
+ /** Where the cell sits in depth. Absent = the plane a flat scene draws on. */
+ z?: number;
/** Cell radius in world units; distances are measured to the cell edge. */
radius: number;
}
@@ -34,6 +36,11 @@ export interface StreamDecision {
* active when its edge is within `loadRadius`; an active cell deactivates only
* once its edge passes `unloadRadius` (hysteresis). Cells inside the band keep
* their current state. Pure.
+ *
+ * A cell is a sphere, not a circle: measuring in two axes makes every floor of a
+ * tower the same place, so a world stacked in depth holds all of it resident at
+ * once. Both z default to 0, which is where a flat game's cells and focus are —
+ * the third term vanishes and the answer is the plane one.
*/
export function computeStreaming(
cells: readonly StreamCell[],
@@ -42,11 +49,13 @@ export function computeStreaming(
loadRadius: number,
unloadRadius: number,
active: ReadonlySet<string>,
+ focusZ = 0,
): StreamDecision {
const toActivate: string[] = [];
const toDeactivate: string[] = [];
for (const c of cells) {
- const edge = Math.max(0, Math.hypot(focusX - c.x, focusY - c.y) - c.radius);
+ const edge = Math.max(
+ 0, Math.hypot(focusX - c.x, focusY - c.y, focusZ - (c.z ?? 0)) - c.radius);
const isActive = active.has(c.scene);
if (!isActive && edge <= loadRadius) toActivate.push(c.scene);
else if (isActive && edge > unloadRadius) toDeactivate.push(c.scene);
@@ -86,6 +95,7 @@ export class SceneStreamingController {
private readonly inFlight_ = new Set<string>();
private focusX_ = 0;
private focusY_ = 0;
+ private focusZ_ = 0;
private loadRadius_ = 0;
private unloadRadius_ = 0;
private policy_: StreamPolicy = 'unload';
@@ -118,10 +128,12 @@ export class SceneStreamingController {
this.focusEntity_ = null;
}
- /** Set the focus directly (or let the streaming system read a focus entity). */
- setFocus(x: number, y: number): void {
+ /** Set the focus directly (or let the streaming system read a focus entity).
+ * @p z defaults to the plane a flat game's cells sit on. */
+ setFocus(x: number, y: number, z = 0): void {
this.focusX_ = x;
this.focusY_ = y;
+ this.focusZ_ = z;
}
/** Follow an entity; the streaming system reads its Transform each tick. */
@@ -146,6 +158,7 @@ export class SceneStreamingController {
this.focusX_, this.focusY_,
this.loadRadius_, this.unloadRadius_,
this.active_,
+ this.focusZ_,
);
for (const scene of decision.toActivate) {
this.active_.add(scene);
diff --git a/sdk/tests/scene-streaming.test.ts b/sdk/tests/scene-streaming.test.ts
index ecfeedd99..c29f10d6b 100644
--- a/sdk/tests/scene-streaming.test.ts
+++ b/sdk/tests/scene-streaming.test.ts
@@ -7,6 +7,7 @@ import {
type StreamCell,
type SceneStreamHost,
} from '../src/scene/sceneStreaming';
+import { sceneStreamingSystem } from '../src/scene/scenePlugin';
const flush = async () => { await Promise.resolve(); await Promise.resolve(); };
@@ -56,6 +57,46 @@ describe('computeStreaming', () => {
it('deactivates an active cell past unloadRadius', () => {
expect(computeStreaming(cells(a), 25, 0, 10, 20, new Set(['a'])).toDeactivate).toEqual(['a']);
});
+
+ it('separates cells stacked in depth, which is what a tower is', () => {
+ const ground: StreamCell = { scene: 'ground', x: 0, y: 0, z: 0, radius: 0 };
+ const upstairs: StreamCell = { scene: 'upstairs', x: 0, y: 0, z: 300, radius: 0 };
+ // Standing on the ground floor: same x/y as both, so measuring in two axes
+ // would bring the floor above in with it.
+ const d = computeStreaming(cells(ground, upstairs), 0, 0, 10, 20, new Set(), 0);
+ expect(d.toActivate).toEqual(['ground']);
+
+ // Climb, and they swap — neither is a fixed answer.
+ expect(computeStreaming(cells(ground, upstairs), 0, 0, 10, 20, new Set(), 300).toActivate)
+ .toEqual(['upstairs']);
+ });
+
+ it('reads an absent z as the plane a flat game is on', () => {
+ // `a` declares no z at all, and a flat focus declares none either: the
+ // depth term has to vanish rather than read as a distance.
+ expect(computeStreaming(cells(a), 5, 0, 10, 20, new Set()).toActivate).toEqual(['a']);
+ expect(computeStreaming(cells(a), 5, 0, 10, 20, new Set(), 300).toActivate).toEqual([]);
+ });
+});
+
+describe('sceneStreamingSystem', () => {
+ it('follows the focus entity in all three axes', () => {
+ const seen: number[][] = [];
+ const streaming = {
+ getFocusEntity: () => 7 as never,
+ setFocus: (x: number, y: number, z?: number) => { seen.push([x, y, z ?? 0]); },
+ update: () => {},
+ };
+ const world = {
+ valid: () => true,
+ has: () => true,
+ get: () => ({ position: { x: 1, y: 2, z: 3 } }),
+ };
+ (sceneStreamingSystem as { _fn: (...a: unknown[]) => void })._fn(streaming, world);
+ // z is the one a flat reader drops, and dropping it puts every floor of a
+ // tower at the focus — so the whole position has to arrive, not two of it.
+ expect(seen).toEqual([[1, 2, 3]]);
+ });
});
describe('SceneStreamingController', () => {
@@ -76,6 +117,19 @@ describe('SceneStreamingController', () => {
expect(c.getActive()).toEqual(['near']);
});
+ it('carries the focus depth into the decision', async () => {
+ const { host, calls } = mockHost();
+ const c = new SceneStreamingController(host, { loadRadius: 10, unloadRadius: 20 });
+ c.register(near);
+ // Directly above the cell: in range by x/y alone, out of range in space.
+ c.setFocus(0, 0, 300);
+ c.update(); await flush();
+ expect(calls).toEqual([]);
+ c.setFocus(0, 0, 5);
+ c.update(); await flush();
+ expect(calls).toEqual(['load:near']);
+ });
+
it('unloads a cell when the focus leaves (past unloadRadius)', async () => {
const { host, calls, loaded } = mockHost();
const c = new SceneStreamingController(host, { loadRadius: 10, unloadRadius: 20 });