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(sprite): preserve affine geometry and mirrored tile grids
yhh47e11651f3ab
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ac9a42e65..8427d7fd6 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,6 +14,10 @@ published separately; it ships inside the editor.## [Unreleased]+### Fixed++- **Sprites retain parent-induced shear in every draw mode.** Ordinary, nine-slice and tiled geometry follows the complete parent transform, and camera bounds use the same basis. Mirrored tiled grids retain positive tile steps before applying signed axes, including the capped fallback. Zero-area sprites skip geometry; existing material, UV and batch state is preserved.+## [0.82.0] - 2026-10-14### Addeddiff --git a/src/esengine/renderer/plugins/SpritePlugin.cpp b/src/esengine/renderer/plugins/SpritePlugin.cppindex 2053e47fa..fcdc63f63 100644--- a/src/esengine/renderer/plugins/SpritePlugin.cpp+++ b/src/esengine/renderer/plugins/SpritePlugin.cpp@@ -6,6 +6,7 @@#include "../frame/RenderFrame.hpp"#include "../rhi/Texture.hpp"#include "../../ecs/components/Transform.hpp"+#include "../../ecs/components/Hierarchy.hpp"#include "../../ecs/components/Sprite.hpp"#include "../../ecs/components/SpriteMask.hpp"#include "../../ecs/components/UINode.hpp"@@ -36,18 +37,22 @@ void SpritePlugin::collect(RenderCollectContext& collect_ctx) {const auto& rotation = transform.worldRotation;const auto& scale = transform.worldScale;+ if (scale.x == 0.0f || scale.y == 0.0f || sprite.size.x == 0.0f || sprite.size.y == 0.0f) continue;+glm::vec2 finalSize = sprite.size * glm::vec2(scale);- glm::vec2 pivotOffset((0.5f - sprite.pivot.x) * finalSize.x,- (0.5f - sprite.pivot.y) * finalSize.y);// The quad turns about `position`, so its pivot offset turns with it and its// extents grow. Bounding the unturned box instead loses a turned sprite while// part of it is still on screen — at 45 degrees the box is 41% short.const glm::vec2 turn = flatTurnZ(rotation);- glm::vec3 aabbCenter = position + glm::vec3(pivotOffset.x * turn.x - pivotOffset.y * turn.y,- pivotOffset.x * turn.y + pivotOffset.y * turn.x,- 0.0f);- glm::vec3 halfExtents = flatHalfExtents(- turn, glm::vec3(std::abs(finalSize.x), std::abs(finalSize.y), 0.0f) * 0.5f);+ const bool parented = registry.has<ecs::Parent>(entity);+ const glm::vec3 axisX = parented ? glm::vec3(transform.cachedMatrix_[0])+ : glm::vec3(turn.x, turn.y, 0.0f) * scale.x;+ const glm::vec3 axisY = parented ? glm::vec3(transform.cachedMatrix_[1])+ : glm::vec3(-turn.y, turn.x, 0.0f) * scale.y;+ const glm::vec2 pivotOffset = (glm::vec2(.5f) - sprite.pivot) * sprite.size;+ const glm::vec3 aabbCenter = position + axisX * pivotOffset.x + axisY * pivotOffset.y;+ const glm::vec3 halfExtents = glm::abs(axisX) * std::abs(sprite.size.x) * .5f+ + glm::abs(axisY) * std::abs(sprite.size.y) * .5f;// Never depth-tested unless a material says so, and that is resolved// below — so a sprite is judged by the view alone, and an occluder in// front of it does not get to speak.@@ -97,6 +102,13 @@ void SpritePlugin::collect(RenderCollectContext& collect_ctx) {const bool hasTiling = draw.tiled;useNineSlice = draw.nineSlice;+ // Emit into the existing batch stream, then map just this sprite's new+ // vertices through its full basis. Parent matrices may contain shear;+ // tiled grids also need positive steps before applying signed axes.+ const bool mapBasis = parented || hasTiling;+ if (mapBasis) angle = 0.0f;+ const u32 vertexStart = buffers.vertexBytesUsed(LayoutId::Batch);+BatchDrawKey key{.stage = ctx.current_stage,.layer = sprite.layer,@@ -143,8 +155,8 @@ void SpritePlugin::collect(RenderCollectContext& collect_ctx) {emitTiledQuads(buffers, draw_list, clips,position, finalSize, sprite.pivot,angle, sprite.color, uvOff, uvSc,- sprite.tileSize * glm::vec2(scale),- sprite.tileSpacing * glm::vec2(scale),+ sprite.tileSize * glm::abs(glm::vec2(scale)),+ sprite.tileSpacing * glm::abs(glm::vec2(scale)),key);} else if (useNineSlice) {emitNineSlice(buffers, draw_list, clips,@@ -157,6 +169,19 @@ void SpritePlugin::collect(RenderCollectContext& collect_ctx) {position, finalSize, sprite.pivot,angle, uvOff, uvSc, sprite.color, key);}+ if (mapBasis) {+ const glm::vec2 divisor = hasTiling ? glm::abs(finalSize) / sprite.size+ : glm::vec2(scale);+ const glm::vec3 basisX = axisX / divisor.x;+ const glm::vec3 basisY = axisY / divisor.y;+ const u32 vertexEnd = buffers.vertexBytesUsed(LayoutId::Batch);+ // appendQuad can grow the pool, so retrieve its pointer after emission.+ auto* vertices = reinterpret_cast<BatchVertex*>(buffers.vertexData(LayoutId::Batch));+ for (u32 i = vertexStart / sizeof(BatchVertex); i < vertexEnd / sizeof(BatchVertex); ++i) {+ const glm::vec3 delta = vertices[i].position - position;+ vertices[i].position = position + basisX * delta.x + basisY * delta.y;+ }+ }}}diff --git a/tests/renderer/test_batch_builder.cpp b/tests/renderer/test_batch_builder.cppindex 0dffc7c46..e2dd68fda 100644--- a/tests/renderer/test_batch_builder.cpp+++ b/tests/renderer/test_batch_builder.cpp@@ -19,6 +19,8 @@#include "esengine/resource/ResourceManager.hpp"#include "esengine/renderer/plugins/UIElementPlugin.hpp"#include "esengine/renderer/plugins/TextPlugin.hpp"+#include "esengine/renderer/plugins/SpritePlugin.hpp"+#include "esengine/ecs/components/Sprite.hpp"#include "esengine/ecs/components/BitmapText.hpp"#include "esengine/ecs/components/UINode.hpp"#include "esengine/ecs/components/UIVisual.hpp"@@ -281,6 +283,90 @@ TEST_CASE("camera mask: hidden text and skeletal submissions upload no geometry"CHECK(frame.stats().triangles == 4);}+TEST_CASE("sprites: affine parents preserve every draw mode and mirrored tile grid") {+ for (auto mode : {ecs::SpriteDrawMode::Simple, ecs::SpriteDrawMode::NineSlice, ecs::SpriteDrawMode::Tiled}) {+ for (bool parented : {false, true}) for (bool mirror : {false, true}) for (bool capped : {false, true}) {+ if (capped && mode != ecs::SpriteDrawMode::Tiled) continue;+ Harness h;+ RenderContext context(h.device);+ resource::ResourceManager resources;+ resources.init(h.device);+ const u8 pixels[16 * 16 * 4] = {};+ const auto texture = resources.createTexture(resource::ResourceContent::Retained, 16, 16,+ pixels, TextureFormat::RGBA8);+ resource::TextureMetadata metadata;+ metadata.sliceBorder = {2, 2, 2, 2};+ resources.setTextureMetadata(texture, metadata);+ ecs::Registry registry;+ const auto parent = registry.create(), entity = registry.create();+ if (parented) registry.emplace<ecs::Parent>(entity).entity = parent;+ auto& transform = registry.emplace<ecs::Transform>(entity);+ const auto matrix = glm::scale(glm::mat4(1), glm::vec3(parented ? 2 : 1, 1, 1))+ * glm::translate(glm::mat4(1), glm::vec3(4, -7, 0))+ * glm::mat4_cast(glm::angleAxis(.7f, glm::vec3(0, 0, 1)))+ * glm::scale(glm::mat4(1), glm::vec3(mirror ? -3 : 3, 2, 1));+ if (parented) {+ transform.cachedMatrix_ = matrix;+ transform.decomposed_ = false;+ } else {+ // Childless roots intentionally leave cachedMatrix_ untouched.+ transform.worldPosition = {4, -7, 0};+ transform.worldRotation = glm::angleAxis(.7f, glm::vec3(0, 0, 1));+ transform.worldScale = {mirror ? -3 : 3, 2, 1};+ transform.decomposed_ = true;+ }+ auto& sprite = registry.emplace<ecs::Sprite>(entity);+ sprite.texture = texture;+ sprite.size = {20, 30};+ sprite.pivot = {.25f, .75f};+ sprite.tileSize = capped ? glm::vec2(.01f) : glm::vec2(4, 5);+ sprite.drawMode = mode;+ Frustum frustum;+ frustum.extractFromMatrix(glm::ortho(-500.0f, 500.0f, -500.0f, 500.0f));+ RenderFrameContext frameContext{context, resources};+ RenderCollectContext collect{registry, frustum, h.clips, h.pool, h.list, frameContext, {}};+ SpritePlugin plugin;+ plugin.init(frameContext);+ plugin.collect(collect);+ const auto* vertices = reinterpret_cast<const BatchVertex*>(h.pool.vertexData(LayoutId::Batch));+ const u32 count = h.pool.vertexBytesUsed(LayoutId::Batch) / sizeof(BatchVertex);+ CHECK(count > 0);+ const auto inverse = glm::inverse(matrix);+ f32 area = 0;+ for (u32 i = 0; i < count; ++i) {+ const auto local = glm::vec3(inverse * glm::vec4(vertices[i].position, 1));+ CHECK(local.x >= -5.001f);+ CHECK(local.x <= 15.001f);+ CHECK(local.y >= -22.501f);+ CHECK(local.y <= 7.501f);+ if (i % 4 == 0) {+ const auto a = vertices[i+1].position - vertices[i].position;+ const auto b = vertices[i+3].position - vertices[i].position;+ area += std::abs(a.x * b.y - a.y * b.x);+ }+ }+ CHECK(area == doctest::Approx(parented ? 7200 : 3600).epsilon(.0001));+ // A camera touching the actual transformed corner must not cull it.+ const auto corner = glm::vec3(matrix * glm::vec4(-5, -22.5f, 0, 1));+ frustum.extractFromMatrix(glm::ortho(corner.x - .1f, corner.x + .1f,+ corner.y - .1f, corner.y + .1f));+ h.pool.beginFrame();+ h.list.clear();+ plugin.collect(collect);+ CHECK(h.pool.vertexBytesUsed(LayoutId::Batch) > 0);+ for (auto zeroAxis : {0, 1}) {+ sprite.size[zeroAxis] = 0;+ h.pool.beginFrame();+ h.list.clear();+ plugin.collect(collect);+ CHECK(h.pool.vertexBytesUsed(LayoutId::Batch) == 0);+ sprite.size = {20, 30};+ }+ resources.shutdown();+ }+ }+}+TEST_CASE("bitmap text: world geometry follows affine transforms and parallax") {for (u32 kind : {0u, 1u, 2u}) {const bool alignedEdge = kind == 1, rootWithoutMatrix = kind == 2;diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 4aa7033e5..39b5c8135 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'fix(sprite): preserve affine geometry and mirrored tile grids':+ { note: 'Sprites retain parent-induced shear in every draw mode.' },'fix(text): preserve long bitmap strings and cull hidden glyphs':{ note: 'Long native bitmap text preserves UTF-8 content without uploading hidden glyphs.' },'feat(playable): adapt asset quality to measured delivery budgets':