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(trail): a motion trail keeps the depth it was drawn through
esengineeb6f0dd18a7e
diff --git a/fixtures/scenes/trail-depth.esscene b/fixtures/scenes/trail-depth.esscenenew file mode 100644index 000000000..56dbc8402--- /dev/null+++ b/fixtures/scenes/trail-depth.esscene@@ -0,0 +1,43 @@+{+ "version": 3,+ "name": "Trail through depth",+ "entities": [+ {+ "id": 0,+ "name": "Camera",+ "parent": null,+ "children": [],+ "components": [+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 520 } } },+ { "type": "Camera", "data": { "projectionType": 0, "fov": 60, "nearPlane": 0.1, "farPlane": 2000, "isActive": true, "priority": 0 } }+ ],+ "visible": true+ },+ {+ "id": 1,+ "name": "Trailer",+ "parent": null,+ "children": [],+ "components": [+ { "type": "Transform", "data": { "position": { "x": -200, "y": 0, "z": 0 } } },+ {+ "type": "TrailRenderer",+ "data": {+ "time": 5.0,+ "minVertexDistance": 5,+ "emitting": true,+ "startWidth": 40,+ "endWidth": 40,+ "startColor": { "r": 0, "g": 1, "b": 0, "a": 1 },+ "endColor": { "r": 0, "g": 1, "b": 0, "a": 1 },+ "blendMode": 0,+ "layer": 0,+ "material": 0,+ "enabled": true+ }+ }+ ],+ "visible": true+ }+ ]+}diff --git a/fixtures/scenes/trail-depth.textures.json b/fixtures/scenes/trail-depth.textures.jsonnew file mode 100644index 000000000..0967ef424--- /dev/null+++ b/fixtures/scenes/trail-depth.textures.json@@ -0,0 +1 @@+{}diff --git a/src/esengine/renderer/RenderTypePlugin.hpp b/src/esengine/renderer/RenderTypePlugin.hppindex fc6cee349..5aa98a4c9 100644--- a/src/esengine/renderer/RenderTypePlugin.hpp+++ b/src/esengine/renderer/RenderTypePlugin.hpp@@ -108,6 +108,29 @@ struct RenderCollectContext {/** @brief Decomposes @p transform and returns its world position shifted toward the* camera by (1 - parallax); factor 1 = no shift, 0 = screen-pinned. Applied* before the frustum cull so a parallaxed renderable is culled where drawn. */+/**+ * @brief The direction the viewer lies in from @p worldPos, in CameraView::eye's+ * convention — the eye's own direction orthographically, where every point+ * shares it, and the vector to the eye under perspective.+ */+inline glm::vec3 towardViewer(const glm::vec3& worldPos, const CameraView& camera) {+ return camera.eye.w < 0.5f ? glm::vec3(camera.eye) : glm::vec3(camera.eye) - worldPos;+}++/**+ * @brief The side vector a ribbon widens along: perpendicular to both its path and+ * the viewer, so the strip faces the eye instead of lying in one plane.+ * @details @p lastSide carries a segment aimed straight at the viewer — no width it+ * could see, so no side of its own. Flat scenes are the degenerate case:+ * the eye direction is +Z and this reduces to the XY perpendicular.+ */+inline glm::vec3 ribbonSide(const glm::vec3& tangent, const glm::vec3& worldPos,+ const CameraView& camera, const glm::vec3& lastSide) {+ const glm::vec3 side = glm::cross(towardViewer(worldPos, camera), tangent);+ const f32 len2 = glm::dot(side, side);+ return len2 > 1e-12f ? side / std::sqrt(len2) : lastSide;+}+inline glm::vec3 parallaxedWorldPosition(ecs::Transform& transform, const glm::vec2& parallax,const CameraView& camera) {transform.ensureDecomposed();diff --git a/src/esengine/renderer/plugins/ParticlePlugin.cpp b/src/esengine/renderer/plugins/ParticlePlugin.cppindex 42916831e..b4c6df881 100644--- a/src/esengine/renderer/plugins/ParticlePlugin.cpp+++ b/src/esengine/renderer/plugins/ParticlePlugin.cpp@@ -241,19 +241,18 @@ void ParticlePlugin::collect(RenderCollectContext& collect_ctx) {// 2 verts per point: t runs 0 at the faded zero-width tail (oldest) → 1// at the full-width head carrying the particle's current colour.- // Widened in the scene's own plane, not toward the eye: the- // centreline carries depth, but a billboarded ribbon is a- // per-segment turn this vertex path has no room for.+ // Widened along ribbonSide, so a trail whose centreline leaves the+ // XY plane still presents its width to the viewer.trail_verts_.clear();- glm::vec2 lastTangent(1.0f, 0.0f);+ glm::vec3 lastTangent(1.0f, 0.0f, 0.0f);+ glm::vec3 lastSide(0.0f, 1.0f, 0.0f);for (int j = 0; j < n; ++j) {const glm::vec3& a = trail_center_[j == 0 ? 0 : j - 1];const glm::vec3& b = trail_center_[j + 1 == n ? j : j + 1];- glm::vec2 seg = glm::vec2(b) - glm::vec2(a);- f32 len = std::sqrt(seg.x * seg.x + seg.y * seg.y);- glm::vec2 tangent = len > 1e-6f ? seg / len : lastTangent;+ glm::vec3 seg = b - a;+ f32 len = glm::length(seg);+ glm::vec3 tangent = len > 1e-6f ? seg / len : lastTangent;lastTangent = tangent;- glm::vec2 perp(-tangent.y, tangent.x);f32 t = static_cast<f32>(j) / static_cast<f32>(n - 1);f32 halfW = 0.5f * emitter.trailWidth * t;@@ -262,8 +261,10 @@ void ParticlePlugin::collect(RenderCollectContext& collect_ctx) {u32 packed = packColor(col);const glm::vec3& c = trail_center_[j];- trail_verts_.push_back({{c.x + perp.x * halfW, c.y + perp.y * halfW, c.z}, packed, glm::vec2(1.0f - t, 0.0f)});- trail_verts_.push_back({{c.x - perp.x * halfW, c.y - perp.y * halfW, c.z}, packed, glm::vec2(1.0f - t, 1.0f)});+ const glm::vec3 side = ribbonSide(tangent, c, collect_ctx.camera, lastSide);+ lastSide = side;+ trail_verts_.push_back({c + side * halfW, packed, glm::vec2(1.0f - t, 0.0f)});+ trail_verts_.push_back({c - side * halfW, packed, glm::vec2(1.0f - t, 1.0f)});}trail_indices_.clear();diff --git a/src/esengine/renderer/plugins/TrailPlugin.cpp b/src/esengine/renderer/plugins/TrailPlugin.cppindex 0f77d57a4..269108e67 100644--- a/src/esengine/renderer/plugins/TrailPlugin.cpp+++ b/src/esengine/renderer/plugins/TrailPlugin.cpp@@ -42,12 +42,9 @@ void TrailPlugin::collect(RenderCollectContext& collect_ctx) {// --- Build the centerline: anchors (oldest→newest), then the live head. -----const f32 invTime = trail.time > 1e-6f ? 1.0f / trail.time : 0.0f;scratch_center_.clear();- f32 minX = std::numeric_limits<f32>::max(), minY = minX;- f32 maxX = std::numeric_limits<f32>::lowest(), maxY = maxX;- auto extend = [&](const glm::vec2& p) {- minX = std::min(minX, p.x); minY = std::min(minY, p.y);- maxX = std::max(maxX, p.x); maxY = std::max(maxY, p.y);- };+ glm::vec3 lo(std::numeric_limits<f32>::max());+ glm::vec3 hi(std::numeric_limits<f32>::lowest());+ auto extend = [&](const glm::vec3& p) { lo = glm::min(lo, p); hi = glm::max(hi, p); };for (const auto& pt : state->points) {f32 age01 = std::min(std::max((now - pt.birth_time) * invTime, 0.0f), 1.0f);@@ -58,11 +55,10 @@ void TrailPlugin::collect(RenderCollectContext& collect_ctx) {// anchors. Skip it when frozen (the streak fades where it was left) or when it// coincides with the newest anchor (avoids a degenerate zero-length segment).if (trail.emitting) {- glm::vec2 head(headWorld.x, headWorld.y);- glm::vec2 d = head - scratch_center_.back().pos;+ glm::vec3 d = headWorld - scratch_center_.back().pos;if (glm::dot(d, d) > 1e-4f) {- scratch_center_.push_back({head, 0.0f});- extend(head);+ scratch_center_.push_back({headWorld, 0.0f});+ extend(headWorld);} else {scratch_center_.back().age01 = 0.0f;}@@ -73,9 +69,8 @@ void TrailPlugin::collect(RenderCollectContext& collect_ctx) {// --- Cull against the frustum (centerline AABB padded by half the max width). -const f32 maxHalf = 0.5f * std::max(trail.startWidth, trail.endWidth);- glm::vec3 aabbCenter((minX + maxX) * 0.5f, (minY + maxY) * 0.5f, headWorld.z);- glm::vec3 halfExtents((maxX - minX) * 0.5f + maxHalf,- (maxY - minY) * 0.5f + maxHalf, 0.0f);+ const glm::vec3 aabbCenter = (lo + hi) * 0.5f;+ const glm::vec3 halfExtents = (hi - lo) * 0.5f + maxHalf;if (!frustum.intersectsAABB(aabbCenter, halfExtents)) { ++collect_ctx.culled; continue; }// --- Draw key (mirrors MeshPlugin: material owns shading when present). ------@@ -111,26 +106,26 @@ void TrailPlugin::collect(RenderCollectContext& collect_ctx) {// --- Emit the ribbon: 2 verts per centerline point, 6 indices per segment. ---scratch_verts_.clear();scratch_verts_.reserve(n * 2);- glm::vec2 lastTangent(1.0f, 0.0f);+ glm::vec3 lastTangent(1.0f, 0.0f, 0.0f);+ glm::vec3 lastSide(0.0f, 1.0f, 0.0f);for (usize i = 0; i < n; ++i) {const TrailCenter& c = scratch_center_[i];// Central-difference tangent (forward/backward at the ends).- const glm::vec2& a = scratch_center_[i == 0 ? 0 : i - 1].pos;- const glm::vec2& b = scratch_center_[i + 1 == n ? i : i + 1].pos;- glm::vec2 seg = b - a;- f32 len = std::sqrt(seg.x * seg.x + seg.y * seg.y);- glm::vec2 tangent = len > 1e-6f ? seg / len : lastTangent;+ const glm::vec3& a = scratch_center_[i == 0 ? 0 : i - 1].pos;+ const glm::vec3& b = scratch_center_[i + 1 == n ? i : i + 1].pos;+ glm::vec3 seg = b - a;+ f32 len = glm::length(seg);+ glm::vec3 tangent = len > 1e-6f ? seg / len : lastTangent;lastTangent = tangent;- glm::vec2 perp(-tangent.y, tangent.x);+ const glm::vec3 side = ribbonSide(tangent, c.pos, collect_ctx.camera, lastSide);+ lastSide = side;f32 halfW = 0.5f * (trail.startWidth + (trail.endWidth - trail.startWidth) * c.age01);u32 color = packColor(glm::mix(trail.startColor, trail.endColor, c.age01));f32 u = c.age01; // U runs head(0)→tail(1) along the ribbon- glm::vec2 left = c.pos + perp * halfW;- glm::vec2 right = c.pos - perp * halfW;- scratch_verts_.push_back({{left, headWorld.z}, color, glm::vec2(u, 0.0f)});- scratch_verts_.push_back({{right, headWorld.z}, color, glm::vec2(u, 1.0f)});+ scratch_verts_.push_back({c.pos + side * halfW, color, glm::vec2(u, 0.0f)});+ scratch_verts_.push_back({c.pos - side * halfW, color, glm::vec2(u, 1.0f)});}scratch_indices_.clear();diff --git a/src/esengine/renderer/plugins/TrailPlugin.hpp b/src/esengine/renderer/plugins/TrailPlugin.hppindex 279e082b8..1497d21d7 100644--- a/src/esengine/renderer/plugins/TrailPlugin.hpp+++ b/src/esengine/renderer/plugins/TrailPlugin.hpp@@ -13,7 +13,7 @@ namespace trail { class TrailSystem; }/** @brief One centerline sample: world position + normalized age (0 head, 1 tail). */struct TrailCenter {- glm::vec2 pos{0.0f, 0.0f};+ glm::vec3 pos{0.0f, 0.0f, 0.0f};f32 age01 = 0.0f;};diff --git a/src/esengine/trail/TrailSystem.cpp b/src/esengine/trail/TrailSystem.cppindex 22149dc11..ff05f5830 100644--- a/src/esengine/trail/TrailSystem.cpp+++ b/src/esengine/trail/TrailSystem.cpp@@ -22,7 +22,7 @@ void TrailSystem::update(ecs::Registry& registry, f32 dt) {auto& transform = view.get<ecs::Transform>(entity);transform.ensureDecomposed();- glm::vec2 pos(transform.worldPosition.x, transform.worldPosition.y);+ const glm::vec3& pos = transform.worldPosition;auto& state = states_[entity];@@ -32,7 +32,7 @@ void TrailSystem::update(ecs::Registry& registry, f32 dt) {if (trail.emitting) {bool record = state.points.empty();if (!record) {- glm::vec2 d = pos - state.points.back().position;+ glm::vec3 d = pos - state.points.back().position;f32 md = trail.minVertexDistance;record = glm::dot(d, d) >= md * md;}diff --git a/src/esengine/trail/TrailSystem.hpp b/src/esengine/trail/TrailSystem.hppindex 81a60349c..eb444139a 100644--- a/src/esengine/trail/TrailSystem.hpp+++ b/src/esengine/trail/TrailSystem.hpp@@ -24,7 +24,7 @@ namespace esengine::trail {/** @brief One recorded trail sample: a world position stamped with the sim clock. */struct TrailPoint {- glm::vec2 position{0.0f, 0.0f};+ glm::vec3 position{0.0f, 0.0f, 0.0f};f32 birth_time = 0.0f; ///< Value of TrailSystem::now() when recorded.};diff --git a/tools/render-host/host.ts b/tools/render-host/host.tsindex bb080a320..344398c3b 100644--- a/tools/render-host/host.ts+++ b/tools/render-host/host.ts@@ -433,12 +433,16 @@ const api = {Renderer.setYSortLayers(mask >>> 0);},- /** Moves an entity — the 2.5D and parallax gates drive the camera this way. */- setEntityXY(id: number, x: number, y: number): void {+ /**+ * Moves an entity — the 2.5D and parallax gates drive the camera this way.+ * @p z is optional so a flat caller keeps whatever depth the scene gave it.+ */+ setEntityXY(id: number, x: number, y: number, z?: number): void {const entity = requireEntity(id);const t = app!.world.get(entity, Transform);t.position.x = x;t.position.y = y;+ if (z !== undefined) t.position.z = z;app!.world.set(entity, Transform, t);},diff --git a/tools/render-host/run.mjs b/tools/render-host/run.mjsindex 2d06e5c37..9d1f4f443 100644--- a/tools/render-host/run.mjs+++ b/tools/render-host/run.mjs@@ -225,9 +225,9 @@ app.whenReady().then(async () => {if (process.env.ESTELLA_VERIFY_YSORT) {await exec(`window.__estellaHeadless.api.setYSortLayers(${Number(process.env.ESTELLA_VERIFY_YSORT)})`);}- // ESTELLA_VERIFY_TRAIL = {"from":[x,y],"to":[x,y],"steps":N}: move the entity- // carrying a TrailRenderer along a world-space path, one frame per sample, so- // the trail system records points — motion a static scene can't express.+ // ESTELLA_VERIFY_TRAIL = {"from":[x,y[,z]],"to":[x,y[,z]],"steps":N}: move the+ // entity carrying a TrailRenderer along a world-space path, one frame per+ // sample, so the trail system records points. A third component drives depth.// Requires play mode (ESTELLA_VERIFY_PLAY=1) for the trail update to run.// ESTELLA_VERIFY_MOVE = {"component":"TilemapLayer","to":[x,y],"steps":N}:// after the scene settles, teleport the first entity carrying the named@@ -269,10 +269,11 @@ app.whenReady().then(async () => {if (e && e.components && e.components.some((c) => c.replace(/\\s+/g, '') === 'TrailRenderer')) { target = id; break; }}if (target == null) throw new Error('no TrailRenderer entity in scene');- const [fx, fy] = cfg.from, [tx, ty] = cfg.to, N = cfg.steps;+ const [fx, fy, fz] = cfg.from, [tx, ty, tz] = cfg.to, N = cfg.steps;for (let i = 0; i <= N; i++) {const t = N === 0 ? 1 : i / N;- api.setEntityXY(target, fx + (tx - fx) * t, fy + (ty - fy) * t);+ const z = fz === undefined ? undefined : fz + ((tz ?? fz) - fz) * t;+ api.setEntityXY(target, fx + (tx - fx) * t, fy + (ty - fy) * t, z);await api.step(1, 1 / 60);}})()`);diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjsindex 4e862e36d..caeb83b40 100644--- a/tools/renderScenes.mjs+++ b/tools/renderScenes.mjs@@ -220,6 +220,10 @@ export const SCENES = [{ id: "depth-off", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/depth-layers.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/depth-layers.textures.json", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.573,\"y\":0.5,\"rgb\":[0,0,255],\"tol\":60}]" } },// Motion trail: move a TrailRenderer entity across the frame in play mode, asserting the ribbon rasterizes as a green band (head/mid/tail) with black above it — the CPU-ribbon-through-the-batch-face path.{ id: "trail", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PLAY: "1", ESTELLA_VERIFY_SCENE: "/scenes/trail.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/trail.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_TRAIL: "{\"from\":[-200,0],\"to\":[200,0],\"steps\":20}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.80,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":50},{\"x\":0.5,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":50},{\"x\":0.25,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":60},{\"x\":0.5,\"y\":0.15,\"rgb\":[0,0,0],\"tol\":30}]" } },+ // A ribbon whose path runs through depth. Measured both ways: per-point z spans+ // x 0.20-0.65, flattened onto the head's plane only 0.40-0.60 — hence two left+ // probes, and none at the head, the plane a flat ribbon collapses ONTO.+ { id: "trail-depth", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_PLAY: "1", ESTELLA_VERIFY_SCENE: "/scenes/trail-depth.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/trail-depth.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_TRAIL: "{\"from\":[-200,0,0],\"to\":[200,0,-400],\"steps\":20}", ESTELLA_VERIFY_EXPECT: "[{\"x\": 0.22, \"y\": 0.5, \"rgb\": [0, 255, 0], \"tol\": 50}, {\"x\": 0.3, \"y\": 0.5, \"rgb\": [0, 255, 0], \"tol\": 50}, {\"x\": 0.5, \"y\": 0.5, \"rgb\": [0, 255, 0], \"tol\": 50}, {\"x\": 0.8, \"y\": 0.5, \"rgb\": [0, 0, 0], \"tol\": 30}, {\"x\": 0.5, \"y\": 0.3, \"rgb\": [0, 0, 0], \"tol\": 30}]" } },// Linear-light pipeline (colorSpace: 'linear'). The lit falloff moves 52 to 125 (perceptually-uniform light); the mid-gray texture must ROUND-TRIP exactly (sRGB store → hw decode → blit encode = identity) — a missing sRGB upload variant would read ~187, so tol 6 is a hard discriminator.{ id: "linear-lit", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_COLORSPACE: "linear", ESTELLA_VERIFY_SCENE: "/scenes/mat-lit-point.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mat-lit-point.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[0,254,0],\"tol\":25},{\"x\":0.75,\"y\":0.5,\"rgb\":[0,125,0],\"tol\":25}]" } },{ id: "linear-midtone", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_COLORSPACE: "linear", ESTELLA_VERIFY_SCENE: "/scenes/srgb-midtone.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/srgb-midtone.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[128,128,128],\"tol\":6}]" } },