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.

feat(pick): the box of an entity can take a drawn extent from the caller, so a skeleton is picked by what it draws

yhha2099e907c56
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 365c570a9..93a7189fc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -40,6 +40,7 @@ published separately; it ships inside the editor.
### Fixed
+- **A Spine skeleton is selected by the character it draws.** A skeleton could be picked only on the small icon at its origin, so clicking the character selected nothing. The editor hands the pick box the skeleton's own bounds through the new `extentOf` option of `entityWorldBox` and `pickEntitiesByRay`, and the selection outline follows the same box.
- **Nudging a selection moves each selected root once, so a selected child no longer travels twice.** With a parent and its child both selected, an arrow key moved the child by the nudge and again with its parent; each selected root now moves once and what is under it follows.
- **A click selects a shape by its drawn size, and a bare Canvas no longer takes the click.** A ShapeRenderer could be selected only on the small icon at its origin, and any scene with a Canvas answered every click in the design screen with the Canvas, so the sprites beneath could not be clicked at all. A shape is picked by its size and its own sorting layer, and a UI node that draws nothing is picked only where nothing else is.
- **An unset texture array reads its default on WebGL.** A `textureArray` param a material left unset was given the plain white texture, which an array sampler cannot read, so on WebGL it read black (WebGPU already drew white). An unset array param now binds layers of its declared default: white, black or a flat normal.
diff --git a/sdk/src/ecs/entityBox.ts b/sdk/src/ecs/entityBox.ts
index c9680219a..ac036ca77 100644
--- a/sdk/src/ecs/entityBox.ts
+++ b/sdk/src/ecs/entityBox.ts
@@ -55,6 +55,12 @@ export interface EntityBoxOptions {
* caller is handed the entity's world position to answer for it.
*/
iconHalf?: number | ((at: Vec3) => number);
+ /**
+ * The drawn extent of an entity whose size is not in a component the box reads — a skeleton, a
+ * tilemap: a rectangle in the entity's own units, `x`/`y` its lower-left corner from the origin.
+ * Scaled and turned by the world transform like a sprite's.
+ */
+ extentOf?: (entity: Entity) => { x: number; y: number; width: number; height: number } | null;
}
/** The rotation an entity is drawn with, defaulted for a partial write. */
@@ -100,6 +106,13 @@ export function entityWorldBox(world: ReadableWorld, entity: Entity, opts?: Enti
w = Math.max(shape.size.x * t.worldScale.x, iconHalf * 2);
h = Math.max(shape.size.y * t.worldScale.y, iconHalf * 2);
d = 0;
+ } else if (opts?.extentOf?.(entity)) {
+ const ext = opts.extentOf(entity)!;
+ w = Math.max(ext.width * t.worldScale.x, iconHalf * 2);
+ h = Math.max(ext.height * t.worldScale.y, iconHalf * 2);
+ d = 0;
+ px = 0.5 - ((ext.x + ext.width / 2) * t.worldScale.x) / (w || 1);
+ py = 0.5 - ((ext.y + ext.height / 2) * t.worldScale.y) / (h || 1);
} else if (iconHalf === 0) {
return null;
}
diff --git a/sdk/src/ecs/entityPick.ts b/sdk/src/ecs/entityPick.ts
index 9b762663d..7f0dd8a53 100644
--- a/sdk/src/ecs/entityPick.ts
+++ b/sdk/src/ecs/entityPick.ts
@@ -16,7 +16,7 @@ import {
layerOrderOf, rankPickCandidates, sortingIdentity, type PickCandidate,
} from '../render/layerOrder';
import {
- entityWorldBox, meshWorldBox, entityBoxRayHit, type LayoutWorld,
+ entityWorldBox, meshWorldBox, entityBoxRayHit, type LayoutWorld, type EntityBoxOptions,
} from './entityBox';
/** Sorting layer for an entity that has none of its own, where a realm draws a
@@ -62,6 +62,8 @@ export interface EntityPickOptions {
* is a screen size (see {@link EntityBoxOptions.iconHalf}).
*/
iconHalf?: number | ((at: Vec3) => number);
+ /** See {@link EntityBoxOptions.extentOf}. */
+ extentOf?: EntityBoxOptions['extentOf'];
/** Entities this realm refuses to select. Everything is selectable by default. */
pickable?(entity: Entity): boolean;
/** The project's sorting-layer masks, so hits rank the way the frame stacked them. */
@@ -91,7 +93,7 @@ export function pickEntitiesByRay(
if (opts.pickable && !opts.pickable(entity)) continue;
// A mesh's extent is in its vertices, so it is asked for before the icon
// fallback would answer with the size of a gizmo.
- const box = meshWorldBox(world, entity) ?? entityWorldBox(world, entity, { iconHalf });
+ const box = meshWorldBox(world, entity) ?? entityWorldBox(world, entity, { iconHalf, extentOf: opts.extentOf });
if (!box || entityBoxRayHit(box, ray.origin, ray.dir) === null) continue;
const sprite = world.has(entity, Sprite) ? world.get(entity, Sprite)
diff --git a/sdk/tests/entity-box.test.ts b/sdk/tests/entity-box.test.ts
index 324035b75..0c656ee4b 100644
--- a/sdk/tests/entity-box.test.ts
+++ b/sdk/tests/entity-box.test.ts
@@ -242,3 +242,17 @@ describe('a shape\'s box', () => {
expect(box).toMatchObject({ hw: 11, hh: 11 });
});
});
+
+describe('a drawn extent the caller supplies', () => {
+ it('is scaled by the world scale and centred where the skeleton stands, not on the origin', () => {
+ const world = fakeWorld({ 1: { Transform: T(100, 50, 2, 2) } });
+ const box = entityWorldBox(world, ent(1), { extentOf: () => ({ x: -10, y: 0, width: 40, height: 120 }) });
+ expect(box).toMatchObject({ cx: 100 + 2 * 10, cy: 50 + 2 * 60, hw: 40, hh: 120 });
+ });
+
+ it('leaves an entity with no extent where it was, a box the size of its icon', () => {
+ const world = fakeWorld({ 1: { Transform: T(0, 0) } });
+ expect(entityWorldBox(world, ent(1), { extentOf: () => null })).toBeNull();
+ expect(entityWorldBox(world, ent(1), { iconHalf: 11, extentOf: () => null })).toMatchObject({ hw: 11, hh: 11 });
+ });
+});