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(text): transform native bitmap glyphs and ink bounds

yhh2ac2d47a678b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f84d33d3a..4e4ea5a4e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -26,6 +26,9 @@ published separately; it ships inside the editor.
- **Playable exports honor authored image and audio optimization.** Automatic asset optimization applies PNG Max Size (including playable overrides) without loading a GPU texture encoder, and converts eligible WAV clips to MP3 at their authored bitrate. Per-clip opt-outs and the build's Skip setting preserve originals; failed asset cooking prevents a successful export.
+- **Native bitmap text follows its complete world transform.** Glyph vertices retain rotation, independent axis scales, mirrors and parent-induced shear while preserving parallax and batching. Cached ink bounds include alignment and glyph offsets, preventing visible edge text from being culled; layout no longer repeats a separate full-text measurement.
+
+
- **Playable preview records identify the delivered archive.** Full export reports verify the adjacent ZIP hash before previewing, catching config changes even when HTML stays identical. Downloaded logs retain HTML/delivery hashes and explicitly disclose omitted events; HTML-only previews remain labeled accordingly.
diff --git a/src/esengine/renderer/plugins/TextPlugin.cpp b/src/esengine/renderer/plugins/TextPlugin.cpp
index 91be0331e..75ceccc6c 100644
--- a/src/esengine/renderer/plugins/TextPlugin.cpp
+++ b/src/esengine/renderer/plugins/TextPlugin.cpp
@@ -6,10 +6,12 @@
#include "../frame/RenderFrame.hpp"
#include "../rhi/Texture.hpp"
#include "../../ecs/components/Transform.hpp"
+#include "../../ecs/components/Hierarchy.hpp"
#include "../../ecs/components/BitmapText.hpp"
#include "../../text/BitmapFont.hpp"
#include <cmath>
+#include <limits>
namespace esengine {
@@ -47,9 +49,8 @@ void TextPlugin::rebuildLayout(const text::BitmapFont& font, const ecs::BitmapTe
cache.vertices.clear();
cache.indices.clear();
- auto metrics = font.measureText(bt.text, bt.fontSize, bt.spacing);
- cache.width = metrics.width;
- cache.height = metrics.height;
+ cache.min = glm::vec3(std::numeric_limits<f32>::max());
+ cache.max = glm::vec3(std::numeric_limits<f32>::lowest());
const f32 fs = bt.fontSize;
const f32 spacing = bt.spacing;
@@ -94,6 +95,8 @@ void TextPlugin::rebuildLayout(const text::BitmapFont& font, const ecs::BitmapTe
f32 halfH = glyph->height * fs * 0.5f;
f32 cx = cursorX + (glyph->xOffset + glyph->width * 0.5f) * fs;
f32 cy = (fontBase - glyph->yOffset - glyph->height * 0.5f) * fs;
+ cache.min = glm::min(cache.min, glm::vec3(cx - halfW, cy - halfH, 0.0f));
+ cache.max = glm::max(cache.max, glm::vec3(cx + halfW, cy + halfH, 0.0f));
// Glyph rows are top-down in the atlas, so v is flipped (top at vMin).
f32 uMin = glyph->x / texW;
@@ -118,7 +121,6 @@ void TextPlugin::rebuildLayout(const text::BitmapFont& font, const ecs::BitmapTe
void TextPlugin::collect(RenderCollectContext& collect_ctx) {
auto& registry = collect_ctx.registry;
- auto& frustum = collect_ctx.frustum;
auto& clips = collect_ctx.clip_state;
auto& buffers = collect_ctx.buffer_pool;
auto& draw_list = collect_ctx.draw_list;
@@ -129,6 +131,7 @@ void TextPlugin::collect(RenderCollectContext& collect_ctx) {
const auto& bt = textView.get<ecs::BitmapText>(entity);
if (!bt.enabled) continue;
if (bt.text.empty() || !bt.font.isValid()) continue;
+ if (!batchEntityVisible(draw_list, clips, entity, bt.layer, 0)) continue;
auto* font = ctx.resources.getBitmapFont(bt.font);
if (!font) continue;
@@ -155,14 +158,20 @@ void TextPlugin::collect(RenderCollectContext& collect_ctx) {
auto& transform = textView.get<ecs::Transform>(entity);
glm::vec3 position = parallaxedWorldPosition(transform, bt.parallax, collect_ctx.camera);
- const f32 s = transform.worldScale.x;
-
- glm::vec3 halfExtents = glm::abs(glm::vec3(
- cache.width * s * 0.5f,
- cache.height * transform.worldScale.y * 0.5f,
- 0.0f
- ));
- if (!frustum.intersectsAABB(position, halfExtents)) {
+ glm::vec3 axisX, axisY;
+ if (registry.has<ecs::Parent>(entity)) {
+ axisX = glm::vec3(transform.cachedMatrix_[0]);
+ axisY = glm::vec3(transform.cachedMatrix_[1]);
+ } else {
+ const auto rotation = glm::mat3_cast(transform.worldRotation);
+ axisX = rotation[0] * transform.worldScale.x;
+ axisY = rotation[1] * transform.worldScale.y;
+ }
+ const glm::vec3 localCenter = (cache.min + cache.max) * 0.5f;
+ const glm::vec3 localHalf = (cache.max - cache.min) * 0.5f;
+ const glm::vec3 center = position + axisX * localCenter.x + axisY * localCenter.y;
+ const glm::vec3 halfExtents = glm::abs(axisX) * localHalf.x + glm::abs(axisY) * localHalf.y;
+ if (!collect_ctx.visible(center, halfExtents, false)) {
++collect_ctx.culled;
continue;
}
@@ -172,7 +181,7 @@ void TextPlugin::collect(RenderCollectContext& collect_ctx) {
scratch_.reserve(cache.vertices.size());
for (const BatchVertex& v : cache.vertices) {
scratch_.push_back({
- { position.x + v.position.x * s, position.y + v.position.y * s, position.z },
+ position + axisX * v.position.x + axisY * v.position.y,
packedColor, v.texCoord });
}
diff --git a/src/esengine/renderer/plugins/TextPlugin.hpp b/src/esengine/renderer/plugins/TextPlugin.hpp
index bcc75cd10..c39a16116 100644
--- a/src/esengine/renderer/plugins/TextPlugin.hpp
+++ b/src/esengine/renderer/plugins/TextPlugin.hpp
@@ -19,16 +19,14 @@ public:
const char* collectScope() const override { return "render.collect.text"; }
private:
- // Glyph quads cached in fontSize-applied, scale-free units relative to the
- // text origin, untinted. Transform scale, world position and color are
- // per-frame linear factors applied at collect, so only a content change
- // ({text, font, fontSize, spacing, align}) re-runs layout — and the whole
- // text is one DrawCommand instead of one per glyph.
+ // Local glyph quads and ink bounds are cached per content signature.
+ // World transforms, parallax and tint apply at collect; the whole text
+ // remains one command rather than one per glyph.
struct TextLayoutCache {
std::vector<BatchVertex> vertices;
std::vector<u32> indices;
- f32 width = 0.0f; // measureText extents at the cached fontSize
- f32 height = 0.0f;
+ glm::vec3 min{0.0f};
+ glm::vec3 max{0.0f};
std::string text;
u32 font_id = 0;
f32 font_size = 0.0f;
diff --git a/tests/renderer/test_batch_builder.cpp b/tests/renderer/test_batch_builder.cpp
index cdbea135f..247afe793 100644
--- a/tests/renderer/test_batch_builder.cpp
+++ b/tests/renderer/test_batch_builder.cpp
@@ -18,6 +18,8 @@
#include "esengine/renderer/frame/RenderContext.hpp"
#include "esengine/resource/ResourceManager.hpp"
#include "esengine/renderer/plugins/UIElementPlugin.hpp"
+#include "esengine/renderer/plugins/TextPlugin.hpp"
+#include "esengine/ecs/components/BitmapText.hpp"
#include "esengine/ecs/components/UINode.hpp"
#include "esengine/ecs/components/UIVisual.hpp"
#include "esengine/ecs/components/UIMask.hpp"
@@ -279,6 +281,77 @@ TEST_CASE("camera mask: hidden text and skeletal submissions upload no geometry"
CHECK(frame.stats().triangles == 4);
}
+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;
+ 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);
+ REQUIRE(texture.isValid());
+ const auto font = resources.createBitmapFont(
+ "common lineHeight=16 base=12 scaleW=16 scaleH=16 pages=1\n"
+ "char id=65 x=0 y=0 width=8 height=16 xoffset=2 yoffset=1 xadvance=10 page=0 chnl=15\n",
+ texture, 16, 16);
+ REQUIRE(font.isValid());
+ ecs::Registry registry;
+ const auto entity = registry.create();
+ auto& transform = registry.emplace<ecs::Transform>(entity);
+ auto& text = registry.emplace<ecs::BitmapText>(entity);
+ text.font = font;
+ text.text = "A";
+ glm::mat4 projection;
+ if (alignedEdge) {
+ text.align = ecs::TextAlign::Right;
+ transform.cachedMatrix_ = glm::translate(glm::mat4(1), glm::vec3(7, 0, 0));
+ projection = glm::ortho(-1.0f, 1.0f, -20.0f, 20.0f);
+ } else if (rootWithoutMatrix) {
+ transform.worldPosition = {4, -7, 0};
+ transform.worldRotation = glm::angleAxis(.7f, glm::vec3(0, 0, 1));
+ transform.worldScale = {-2, 3, 1};
+ text.parallax = {.5f, .25f};
+ projection = glm::ortho(-100.0f, 100.0f, -100.0f, 100.0f);
+ } else {
+ const auto parent = registry.create();
+ registry.emplace<ecs::Parent>(entity).entity = parent;
+ registry.emplace<ecs::Children>(parent).entities.push_back(entity);
+ transform.cachedMatrix_ = glm::scale(glm::mat4(1), glm::vec3(2, 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(-2, 3, 1));
+ text.parallax = {.5f, .25f};
+ projection = glm::ortho(-100.0f, 100.0f, -100.0f, 100.0f);
+ }
+ transform.decomposed_ = rootWithoutMatrix;
+ Frustum frustum;
+ frustum.extractFromMatrix(projection);
+ RenderFrameContext frameContext{context, resources};
+ RenderCollectContext collect{registry, frustum, h.clips, h.pool, h.list, frameContext, {}};
+ collect.camera.center = {10, 20};
+ TextPlugin plugin;
+ plugin.init(frameContext);
+ plugin.collect(collect);
+ REQUIRE(h.pool.vertexBytesUsed(LayoutId::Batch) == 4 * sizeof(BatchVertex));
+ CHECK(h.pool.indicesUsed(LayoutId::Batch) == 6);
+ const auto* vertices = reinterpret_cast<const BatchVertex*>(h.pool.vertexData(LayoutId::Batch));
+ const glm::vec2 local[] = {{2, -5}, {10, -5}, {10, 11}, {2, 11}};
+ for (u32 i = 0; i < 4; ++i) {
+ const auto matrix = rootWithoutMatrix
+ ? glm::translate(glm::mat4(1), transform.worldPosition) * glm::mat4_cast(transform.worldRotation)
+ * glm::scale(glm::mat4(1), transform.worldScale)
+ : transform.cachedMatrix_;
+ const glm::vec3 origin = glm::vec3(matrix * glm::vec4(local[i].x - (alignedEdge ? 10 : 0), local[i].y, 0, 1));
+ const auto expected = origin + glm::vec3(collect.camera.center * (glm::vec2(1) - text.parallax), 0);
+ CHECK(vertices[i].position.x == doctest::Approx(expected.x));
+ CHECK(vertices[i].position.y == doctest::Approx(expected.y));
+ }
+ resources.shutdown();
+ }
+}
+
TEST_CASE("UI masks: deep grouping hierarchies reach their leaf without call-stack recursion") {
for (auto mode : {ecs::MaskMode::Scissor, ecs::MaskMode::Stencil}) {
MockGfxDevice device;
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index aa8e2e36d..7b92d59ab 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -33,6 +33,9 @@ export const NOTED = {
'feat(editor): show playable asset optimization decisions':
{ note: 'Playable asset quality adapts to the measured delivery budget.' },
+
+ 'fix(text): transform native bitmap glyphs and ink bounds':
+ { note: 'Native bitmap text follows its complete world transform.' },
'fix(playable): honor authored texture and audio optimization':
{ note: 'Playable exports honor authored image and audio optimization.' },