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(spline): a terrain band's lip can be drawn with an image repeating along the surface; PathFollower is shown to answer a timeline track or graph node by property path

yhh78b80bb521c5
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index 0f0f1b039..07c601b01 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -10688,6 +10688,8 @@ ComponentDef<TerrainBand2DData>
collider: boolean
depth: number
edgeColor: { r: number; g: number; b: number; a: number; }
+edgeTexture: number
+edgeTile: number
edgeWidth: number
resolution: number
tile: number
@@ -10696,6 +10698,7 @@ tile: number
## TerrainBandBuild — interface @experimental
```
closed: boolean
+edge: MeshRendererGeometry | null
geometry: MeshRendererGeometry
outline: Vec2[]
```
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 299ea2f3e..5fc572aab 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -10901,6 +10901,8 @@ ComponentDef<TerrainBand2DData>
collider: boolean
depth: number
edgeColor: { r: number; g: number; b: number; a: number; }
+edgeTexture: number
+edgeTile: number
edgeWidth: number
resolution: number
tile: number
@@ -10909,6 +10911,7 @@ tile: number
## TerrainBandBuild — interface @experimental
```
closed: boolean
+edge: MeshRendererGeometry | null
geometry: MeshRendererGeometry
outline: Vec2[]
```
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index 00058cdba..80c4f3477 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -10683,6 +10683,8 @@ ComponentDef<TerrainBand2DData>
collider: boolean
depth: number
edgeColor: { r: number; g: number; b: number; a: number; }
+edgeTexture: number
+edgeTile: number
edgeWidth: number
resolution: number
tile: number
@@ -10691,6 +10693,7 @@ tile: number
## TerrainBandBuild — interface @experimental
```
closed: boolean
+edge: MeshRendererGeometry | null
geometry: MeshRendererGeometry
outline: Vec2[]
```
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index 9f8da8044..e643219dc 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -10978,6 +10978,8 @@ ComponentDef<TerrainBand2DData>
collider: boolean
depth: number
edgeColor: { r: number; g: number; b: number; a: number; }
+edgeTexture: number
+edgeTile: number
edgeWidth: number
resolution: number
tile: number
@@ -10986,6 +10988,7 @@ tile: number
## TerrainBandBuild — interface @experimental
```
closed: boolean
+edge: MeshRendererGeometry | null
geometry: MeshRendererGeometry
outline: Vec2[]
```
diff --git a/sdk/etc/spline.api.md b/sdk/etc/spline.api.md
index 3f15033db..f39fdd053 100644
--- a/sdk/etc/spline.api.md
+++ b/sdk/etc/spline.api.md
@@ -85,6 +85,8 @@ ComponentDef<TerrainBand2DData>
collider: boolean
depth: number
edgeColor: { r: number; g: number; b: number; a: number; }
+edgeTexture: number
+edgeTile: number
edgeWidth: number
resolution: number
tile: number
@@ -93,6 +95,7 @@ tile: number
## TerrainBandBuild — interface @experimental
```
closed: boolean
+edge: MeshRendererGeometry | null
geometry: MeshRendererGeometry
outline: Vec2[]
```
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index 96bbe7bed..f6ced4113 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -10690,6 +10690,8 @@ ComponentDef<TerrainBand2DData>
collider: boolean
depth: number
edgeColor: { r: number; g: number; b: number; a: number; }
+edgeTexture: number
+edgeTile: number
edgeWidth: number
resolution: number
tile: number
@@ -10698,6 +10700,7 @@ tile: number
## TerrainBandBuild — interface @experimental
```
closed: boolean
+edge: MeshRendererGeometry | null
geometry: MeshRendererGeometry
outline: Vec2[]
```
diff --git a/sdk/src/spline/terrainBand2D.ts b/sdk/src/spline/terrainBand2D.ts
index 48ed2a102..85124f787 100644
--- a/sdk/src/spline/terrainBand2D.ts
+++ b/sdk/src/spline/terrainBand2D.ts
@@ -21,6 +21,8 @@ import { triangulateRing } from '../render/vectorPath';
import { ChainCollider2D, type ChainCollider2DData } from '../physics/PhysicsComponents';
import { SplineCurve, type SplinePoint } from './curve';
import { Spline, type SplineData } from './splinePlugin';
+import { Parent } from '../ecs/component';
+import { Transform } from '../ecs/component';
/** The fields of the `TerrainBand2D` component. @experimental */
export interface TerrainBand2DData {
@@ -34,6 +36,10 @@ export interface TerrainBand2DData {
edgeWidth: number;
/** The lip's colour, multiplying the texture under it. */
edgeColor: { r: number; g: number; b: number; a: number };
+ /** An image the lip is drawn with, repeating along the surface (grass, snow, a rocky rim). None draws the lip as a flat colour in the fill's own mesh. */
+ edgeTexture: number;
+ /** The length of one repeat of the lip's image along the surface, in world units. */
+ edgeTile: number;
/** Whether the band gives its entity a chain collider along the surface, for a static body to carry. */
collider: boolean;
}
@@ -44,14 +50,17 @@ export interface TerrainBand2DData {
* @experimental
*/
export const TerrainBand2D: ComponentDef<TerrainBand2DData> = defineComponent<TerrainBand2DData>('TerrainBand2D', {
- resolution: 16, depth: 200, tile: 128, edgeWidth: 12, edgeColor: { r: 0.35, g: 0.65, b: 0.2, a: 1 }, collider: false,
+ resolution: 16, depth: 200, tile: 128, edgeWidth: 12, edgeColor: { r: 0.35, g: 0.65, b: 0.2, a: 1 }, edgeTexture: 0, edgeTile: 64, collider: false,
}, {
+ assetFields: [{ field: 'edgeTexture', type: 'texture' }],
fields: {
resolution: { min: 1, category: 'Shape', tooltip: 'How far apart, along the curve, the outline\'s points are. Smaller is smoother and costs more triangles.' },
depth: { min: 0, category: 'Shape', tooltip: 'How far below its lowest point an open spline\'s fill reaches. A closed spline ignores it.' },
tile: { min: 1, category: 'Look', tooltip: 'The length of one repeat of the fill\'s texture, in world units.' },
edgeWidth: { min: 0, category: 'Look', tooltip: 'How wide the lip along the surface is, drawn over the fill. 0 draws none.' },
edgeColor: { category: 'Look', tooltip: 'The lip\'s colour, multiplying the texture under it.' },
+ edgeTexture: { category: 'Look', tooltip: 'An image the lip is drawn with, repeating along the surface. None draws the lip as a flat colour.' },
+ edgeTile: { min: 1, category: 'Look', tooltip: 'The length of one repeat of the lip\'s image along the surface, in world units.' },
collider: { category: 'Collision', tooltip: 'Give this entity a chain collider along the surface. Add a static Rigidbody2D to make it solid.' },
},
});
@@ -59,6 +68,8 @@ export const TerrainBand2D: ComponentDef<TerrainBand2DData> = defineComponent<Te
/** What a band is built into: the mesh, and the outline its collider follows. */
export interface TerrainBandBuild {
geometry: MeshRendererGeometry;
+ /** The lip as a mesh of its own, with its image laid along the surface by arc length, when the band has an `edgeTexture`; the lip is then not in `geometry`. */
+ edge: MeshRendererGeometry | null;
/** The surface, in the entity's own space: the curve for an open spline, the ring for a closed one. */
outline: Vec2[];
closed: boolean;
@@ -112,10 +123,15 @@ export function buildTerrainBand(points: readonly SplinePoint[], closedCurve: bo
for (const p of fill) push(p.x, p.y, white);
for (const i of triangulateRing(fill)) indices.push(i);
- // The lip: a strip from each surface point a lip's width into the fill, along the outline's surface side.
+ // The lip: a strip from each surface point a lip's width into the fill, along the outline's surface side. With an image of its own it is a mesh apart.
+ let edgeGeometry: MeshRendererGeometry | null = null;
if (band.edgeWidth > 0) {
+ const separate = !!band.edgeTexture;
const n = outline.length;
const edge = lerpColor(band.edgeColor);
+ const ep: number[] = [], eu: number[] = [], ec: number[] = [], ei: number[] = [];
+ const edgeTile = Math.max(band.edgeTile, 1e-3);
+ let arc = 0;
const inward: Vec2[] = [];
for (let i = 0; i < n; i++) {
const a = outline[closed ? (i + n - 1) % n : Math.max(i - 1, 0)]!;
@@ -125,20 +141,29 @@ export function buildTerrainBand(points: readonly SplinePoint[], closedCurve: bo
inward.push(closed ? { x: -dy / len, y: dx / len } : { x: dy / len, y: -dx / len });
}
const top: number[] = [], under: number[] = [];
+ // A vertex of the lip: in the fill's mesh with the lip's colour, or in the lip's own with the image laid by distance along the surface (0 at the surface, 1 at the lip's inner edge).
+ const lipVertex = (x: number, y: number, v: number): number => {
+ if (!separate) return push(x, y, edge);
+ ep.push(x, y); eu.push(arc / edgeTile, v); ec.push(1, 1, 1, 1);
+ return ep.length / 2 - 1;
+ };
for (let i = 0; i < n; i++) {
const p = outline[i]!, d = inward[i]!;
- top.push(push(p.x, p.y, edge));
- under.push(push(p.x + d.x * band.edgeWidth, p.y + d.y * band.edgeWidth, edge));
+ if (i > 0) arc += Math.hypot(p.x - outline[i - 1]!.x, p.y - outline[i - 1]!.y);
+ top.push(lipVertex(p.x, p.y, 0));
+ under.push(lipVertex(p.x + d.x * band.edgeWidth, p.y + d.y * band.edgeWidth, 1));
}
+ const target = separate ? ei : indices;
const pieces = closed ? n : n - 1;
for (let i = 0; i < pieces; i++) {
const j = (i + 1) % n;
// Counter-clockwise, so it faces the same way as the fill.
- if (closed) indices.push(top[i]!, under[i]!, under[j]!, top[i]!, under[j]!, top[j]!);
- else indices.push(top[i]!, under[j]!, top[j]!, top[i]!, under[i]!, under[j]!);
+ if (closed) target.push(top[i]!, under[i]!, under[j]!, top[i]!, under[j]!, top[j]!);
+ else target.push(top[i]!, under[j]!, top[j]!, top[i]!, under[i]!, under[j]!);
}
+ if (separate) edgeGeometry = { positions: ep, uvs: eu, colors: ec, indices: ei };
}
- return { geometry: { positions, uvs, colors, indices }, outline, closed };
+ return { geometry: { positions, uvs, colors, indices }, edge: edgeGeometry, outline, closed };
}
/** The pixels-per-unit physics measures a Transform's units in. */
@@ -155,7 +180,20 @@ export function installTerrainBands(app: App): void {
/** What each entity's mesh was last built from, and whether that left it any (an empty one is not worth looking for). */
const built = new Map<Entity, { key: string; empty: boolean }>();
const wroteCollider = new Set<Entity>();
- app.world.onDespawn((entity: Entity) => { built.delete(entity); wroteCollider.delete(entity); });
+ /** The entity that draws each band's lip when the lip has an image of its own. */
+ const lips = new Map<Entity, Entity>();
+ app.world.onDespawn((entity: Entity) => {
+ built.delete(entity); wroteCollider.delete(entity);
+ const lip = lips.get(entity);
+ lips.delete(entity);
+ if (lip !== undefined && app.world.valid(lip)) app.world.despawn(lip);
+ });
+ const dropLip = (band: Entity): void => {
+ const lip = lips.get(band);
+ if (lip === undefined) return;
+ lips.delete(band);
+ if (app.world.valid(lip)) app.world.despawn(lip);
+ };
app.addSystemToSchedule(Schedule.Update, defineSystem([], () => {
const world = app.world;
@@ -165,6 +203,7 @@ export function installTerrainBands(app: App): void {
const band = world.get(entity, TerrainBand2D) as TerrainBand2DData;
const spline = world.get(entity, Spline) as SplineData;
const ppu = band.collider ? pixelsPerUnit(app) : 0;
+ const handle = band.edgeTexture;
const key = JSON.stringify([spline.points, spline.closed, band, ppu]);
const held = built.get(entity);
// The editor re-imports a MeshRenderer from its scene model, which carries no geometry, and that clears what was uploaded: a mesh
@@ -174,6 +213,19 @@ export function installTerrainBands(app: App): void {
built.set(entity, { key, empty: !result });
if (!world.has(entity, MeshRenderer)) world.insert(entity, MeshRenderer, { enabled: true });
if (result) api.setGeometry(entity, result.geometry); else api.clearGeometry(entity);
+ // A lip with an image of its own is an entity under the band, one layer above it, drawn with the image and the lip's colour.
+ if (result?.edge && handle) {
+ let lip = lips.get(entity);
+ if (lip === undefined || !world.valid(lip)) {
+ lip = world.spawn('Edge');
+ world.insert(lip, Transform, {});
+ world.setParent(lip, entity);
+ lips.set(entity, lip);
+ }
+ const own = world.get(entity, MeshRenderer) as { layer?: number };
+ world.insert(lip, MeshRenderer, { enabled: true, texture: handle, color: band.edgeColor, layer: (own.layer ?? 0) + 1 });
+ api.setGeometry(lip, result.edge);
+ } else dropLip(entity);
if (band.collider && result && result.outline.length >= 4) {
// Physics measures in units, the Transform in pixels.
const points = result.outline.map((p) => ({ x: p.x / ppu, y: p.y / ppu }));
diff --git a/sdk/tests/path-follower.test.ts b/sdk/tests/path-follower.test.ts
index bef3cbaea..e60b1b69c 100644
--- a/sdk/tests/path-follower.test.ts
+++ b/sdk/tests/path-follower.test.ts
@@ -4,6 +4,7 @@ import { describe, expect, it } from 'vitest';
import { afterEach, beforeAll, beforeEach } from 'vitest';
import { World } from '../src/ecs/world';
import { Transform } from '../src/ecs/component';
+import { setEntityProperty } from '../src/ecs/propertyPath';
import type { CppRegistry, ESEngineModule } from '../src/wasm';
import { HAS_WASM, loadWasmModule } from './helpers/loadWasm';
import {
@@ -87,6 +88,19 @@ describe.skipIf(!HAS_WASM)('PathFollower', () => {
expect(r.w).toBeCloseTo(1);
});
+ it("is moved by the generic property path, which is how a timeline track or a script graph node reaches any component", () => {
+ const { mover, step } = setup({ speed: 0 });
+ step(1);
+ expect(world.get(mover, Transform).position.x).toBeCloseTo(0);
+ // A timeline's property track, or a graph's property.set, writes by this path: distance drives the follower, speed sets its pace.
+ setEntityProperty(world as never, mover as never, "PathFollower.distance", 6);
+ step(0.0001);
+ expect(world.get(mover, Transform).position.x).toBeCloseTo(6, 1);
+ setEntityProperty(world as never, mover as never, "PathFollower.speed", 2);
+ step(1);
+ expect(world.get(mover, Transform).position.x).toBeCloseTo(8, 1);
+ });
+
it('does nothing while it is not playing, or when its spline is gone', () => {
const idle = setup({ playing: false });
idle.step(1);
diff --git a/sdk/tests/terrain-band-2d.test.ts b/sdk/tests/terrain-band-2d.test.ts
index a91b9ac0f..577f8f680 100644
--- a/sdk/tests/terrain-band-2d.test.ts
+++ b/sdk/tests/terrain-band-2d.test.ts
@@ -6,13 +6,14 @@ import { Transform, MeshRenderer } from '../src/ecs/component';
import type { CppRegistry, ESEngineModule } from '../src/wasm';
import { ChainCollider2D } from '../src/physics/PhysicsComponents';
import { MeshRenderers } from '../src/render/meshRenderer';
+import { Parent } from '../src/ecs/component';
import { Spline } from '../src/spline/splinePlugin';
import { TerrainBand2D, buildTerrainBand, installTerrainBands, type TerrainBand2DData } from '../src/spline/terrainBand2D';
import { triangulateRing } from '../src/render/vectorPath';
import { HAS_WASM, loadWasmModule } from './helpers/loadWasm';
const band = (over: Partial<TerrainBand2DData> = {}): TerrainBand2DData => ({
- resolution: 50, depth: 50, tile: 100, edgeWidth: 0, edgeColor: { r: 0, g: 1, b: 0, a: 1 }, collider: false, ...over,
+ resolution: 50, depth: 50, tile: 100, edgeWidth: 0, edgeColor: { r: 0, g: 1, b: 0, a: 1 }, edgeTexture: 0, edgeTile: 64, collider: false, ...over,
});
const pt = (x: number, y: number) => ({ x, y, z: 0, width: 1 });
@@ -89,6 +90,23 @@ describe('buildTerrainBand', () => {
expect(b.geometry.uvs![last * 2]).toBeCloseTo(2);
});
+ it('draws a lip with an image as a mesh of its own, the image laid by distance along the surface', () => {
+ const flat = buildTerrainBand([pt(0, 0), pt(200, 0)], false, band({ edgeWidth: 10 }))!;
+ const b = buildTerrainBand([pt(0, 0), pt(200, 0)], false, band({ edgeWidth: 10, edgeTexture: 7, edgeTile: 50 }))!;
+ // The fill is the same without the lip in it, and the lip is its own.
+ const none = buildTerrainBand([pt(0, 0), pt(200, 0)], false, band())!;
+ expect(b.geometry.indices.length).toBe(none.geometry.indices.length);
+ expect(flat.edge).toBeNull();
+ const e = b.edge!;
+ expect(coverage(e)).toBeCloseTo(2000, 0);
+ // v is 0 on the surface and 1 at the lip's inner edge; u runs 0 to 4 over 200 units at 50 a repeat.
+ const us = e.uvs!.filter((_, i) => i % 2 === 0), vs = e.uvs!.filter((_, i) => i % 2 === 1);
+ expect(new Set(vs)).toEqual(new Set([0, 1]));
+ expect(Math.min(...us)).toBeCloseTo(0);
+ expect(Math.max(...us)).toBeCloseTo(4, 1);
+ expect(e.colors!.every((c) => c === 1)).toBe(true);
+ });
+
it('builds nothing from one point', () => {
expect(buildTerrainBand([pt(0, 0)], false, band())).toBeNull();
});
@@ -147,6 +165,19 @@ describe.skipIf(!HAS_WASM)('TerrainBand2D system', () => {
expect(uploads).toEqual([e, e]);
});
+ it('draws a lip that has an image on an entity under the band, and takes it away with the image', () => {
+ const { run } = install();
+ const e = ground({ edgeWidth: 8, edgeTexture: 7 });
+ run();
+ const kids = () => world.getAllEntities().filter((c) => c !== e && world.has(c, Parent) && (world.get(c, Parent) as { entity: number }).entity === e);
+ const [lip] = kids();
+ expect(lip).toBeDefined();
+ expect((world.get(lip!, MeshRenderer) as { texture: number }).texture).toBe(7);
+ world.insert(e, TerrainBand2D, band({ edgeWidth: 8, edgeTexture: 0 }));
+ run();
+ expect(kids().length).toBe(0);
+ });
+
it('writes a chain collider along the surface in physics units, and takes it back', () => {
const { run } = install();
const e = ground({ collider: true });