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(text): preserve long bitmap strings and cull hidden glyphs
yhh5818f8c807cc
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4e4ea5a4e..fc1141742 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -24,6 +24,8 @@ published separately; it ships inside the editor.### Fixed+- **Long native bitmap text preserves UTF-8 content without uploading hidden glyphs.** Byte lengths and decoder offsets no longer wrap at 64KiB. Complete layouts retain their glyph positions and alignment; cached block bounds reject camera-excluded runs, and partially visible runs submit only visible quads with rebased indices. Fully visible text keeps its existing batching path.+- **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.diff --git a/src/esengine/renderer/plugins/TextPlugin.cpp b/src/esengine/renderer/plugins/TextPlugin.cppindex 75ceccc6c..6f7a52a00 100644--- a/src/esengine/renderer/plugins/TextPlugin.cpp+++ b/src/esengine/renderer/plugins/TextPlugin.cpp@@ -15,7 +15,17 @@namespace esengine {-u32 TextPlugin::decodeUtf8(const char* data, u16 length, u16& pos) {+namespace {+bool containsBox(const Frustum& frustum, const glm::vec3& center, const glm::vec3& half) {+ for (const auto& plane : frustum.planes) {+ const f32 radius = glm::dot(glm::abs(plane.normal), half);+ if (plane.signedDistance(center) < radius) return false;+ }+ return true;+}+}++u32 TextPlugin::decodeUtf8(const char* data, usize length, usize& pos) {u8 b0 = static_cast<u8>(data[pos]);if (b0 < 0x80) {return b0;@@ -48,6 +58,7 @@ void TextPlugin::rebuildLayout(const text::BitmapFont& font, const ecs::BitmapTef32 texW, f32 texH, TextLayoutCache& cache) {cache.vertices.clear();cache.indices.clear();+ cache.blocks.clear();cache.min = glm::vec3(std::numeric_limits<f32>::max());cache.max = glm::vec3(std::numeric_limits<f32>::lowest());@@ -56,12 +67,12 @@ void TextPlugin::rebuildLayout(const text::BitmapFont& font, const ecs::BitmapTeconst f32 spacing = bt.spacing;const f32 fontBase = font.getBase();const char* textData = bt.text.c_str();- const u16 textLen = static_cast<u16>(bt.text.size());+ const usize textLen = bt.text.size();f32 totalWidth = 0;if (bt.align != ecs::TextAlign::Left) {u32 prevChar = 0;- for (u16 j = 0; j < textLen; ++j) {+ for (usize j = 0; j < textLen; ++j) {u32 charCode = decodeUtf8(textData, textLen, j);auto* glyph = font.getGlyph(charCode);if (!glyph) continue;@@ -81,7 +92,7 @@ void TextPlugin::rebuildLayout(const text::BitmapFont& font, const ecs::BitmapTe}u32 prevChar = 0;- for (u16 j = 0; j < textLen; ++j) {+ for (usize j = 0; j < textLen; ++j) {u32 charCode = decodeUtf8(textData, textLen, j);auto* glyph = font.getGlyph(charCode);if (!glyph) continue;@@ -97,6 +108,14 @@ void TextPlugin::rebuildLayout(const text::BitmapFont& font, const ecs::BitmapTef32 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));+ const glm::vec3 low{cx - halfW, cy - halfH, 0}, high{cx + halfW, cy + halfH, 0};+ if (cache.vertices.size() % (GLYPHS_PER_BLOCK * 4) == 0) {+ cache.blocks.push_back({low, high});+ } else {+ auto& block = cache.blocks.back();+ block.min = glm::min(block.min, low);+ block.max = glm::max(block.max, high);+ }// Glyph rows are top-down in the atlas, so v is flipped (top at vMin).f32 uMin = glyph->x / texW;@@ -178,16 +197,46 @@ void TextPlugin::collect(RenderCollectContext& collect_ctx) {u32 packedColor = packColor(bt.color);scratch_.clear();- scratch_.reserve(cache.vertices.size());- for (const BatchVertex& v : cache.vertices) {- scratch_.push_back({- position + axisX * v.position.x + axisY * v.position.y,- packedColor, v.texCoord });+ index_scratch_.clear();+ const bool allVisible = collect_ctx.isOverlay() || containsBox(collect_ctx.frustum, center, halfExtents);+ if (allVisible) {+ scratch_.reserve(cache.vertices.size());+ for (const BatchVertex& v : cache.vertices) {+ scratch_.push_back({position + axisX * v.position.x + axisY * v.position.y, packedColor, v.texCoord});+ }+ } else {+ const glm::vec3 absX = glm::abs(axisX), absY = glm::abs(axisY);+ const auto bounds = [&](const glm::vec3& low, const glm::vec3& high) {+ const auto mid = (low + high) * .5f, half = (high - low) * .5f;+ return std::pair{position + axisX * mid.x + axisY * mid.y, absX * half.x + absY * half.y};+ };+ for (u32 blockIndex = 0; blockIndex < cache.blocks.size(); ++blockIndex) {+ const auto& block = cache.blocks[blockIndex];+ const auto [blockCenter, blockHalf] = bounds(block.min, block.max);+ if (!collect_ctx.visible(blockCenter, blockHalf, false)) continue;+ const bool wholeBlock = containsBox(collect_ctx.frustum, blockCenter, blockHalf);+ const u32 first = blockIndex * GLYPHS_PER_BLOCK * 4;+ const u32 end = std::min(first + GLYPHS_PER_BLOCK * 4, static_cast<u32>(cache.vertices.size()));+ for (u32 i = first; i < end; i += 4) {+ if (!wholeBlock) {+ const auto [glyphCenter, glyphHalf] = bounds(cache.vertices[i].position, cache.vertices[i + 2].position);+ if (!collect_ctx.visible(glyphCenter, glyphHalf, false)) continue;+ }+ const u32 base = static_cast<u32>(scratch_.size());+ for (u32 j = 0; j < 4; ++j) {+ const auto& v = cache.vertices[i + j];+ scratch_.push_back({position + axisX * v.position.x + axisY * v.position.y, packedColor, v.texCoord});+ }+ for (u32 index : BATCH_QUAD_INDICES) index_scratch_.push_back(base + index);+ }+ }}+ if (scratch_.empty()) continue;+ const auto& indices = allVisible ? cache.indices : index_scratch_;appendIndexedBatch(buffers, draw_list, clips,scratch_.data(), static_cast<u32>(scratch_.size()),- cache.indices.data(), static_cast<u32>(cache.indices.size()),+ indices.data(), static_cast<u32>(indices.size()),BatchDrawKey{.stage = ctx.current_stage,.layer = bt.layer,diff --git a/src/esengine/renderer/plugins/TextPlugin.hpp b/src/esengine/renderer/plugins/TextPlugin.hppindex c39a16116..6c2eb4b89 100644--- a/src/esengine/renderer/plugins/TextPlugin.hpp+++ b/src/esengine/renderer/plugins/TextPlugin.hpp@@ -19,12 +19,15 @@ public:const char* collectScope() const override { return "render.collect.text"; }private:+ static constexpr u32 GLYPHS_PER_BLOCK = 64;+ struct GlyphBlock { glm::vec3 min, max; };// 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;+ std::vector<GlyphBlock> blocks;glm::vec3 min{0.0f};glm::vec3 max{0.0f};std::string text;@@ -34,12 +37,13 @@ private:u8 align = 0;};- static u32 decodeUtf8(const char* data, u16 length, u16& pos);+ static u32 decodeUtf8(const char* data, usize length, usize& pos);void rebuildLayout(const text::BitmapFont& font, const ecs::BitmapText& bt,f32 texW, f32 texH, TextLayoutCache& cache);std::unordered_map<Entity, TextLayoutCache> layout_cache_;std::vector<BatchVertex> scratch_;+ std::vector<u32> index_scratch_;};} // namespace esenginediff --git a/tests/renderer/test_batch_builder.cpp b/tests/renderer/test_batch_builder.cppindex 247afe793..0dffc7c46 100644--- a/tests/renderer/test_batch_builder.cpp+++ b/tests/renderer/test_batch_builder.cpp@@ -352,6 +352,70 @@ TEST_CASE("bitmap text: world geometry follows affine transforms and parallax")}}+TEST_CASE("bitmap text: UTF-8 strings retain every glyph beyond 64 KiB") {+ for (u32 length : {65535u, 65536u, 65537u, 65539u}) {+ 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);+ const auto font = resources.createBitmapFont(+ "common lineHeight=16 base=16 scaleW=16 scaleH=16 pages=1\n"+ "char id=65 x=0 y=0 width=8 height=16 xoffset=0 yoffset=0 xadvance=8 page=0 chnl=15\n"+ "char id=128512 x=8 y=0 width=8 height=16 xoffset=0 yoffset=0 xadvance=8 page=0 chnl=15\n",+ texture, 16, 16);+ REQUIRE(font.isValid());+ ecs::Registry registry;+ const auto entity = registry.create();+ registry.emplace<ecs::Transform>(entity);+ auto& text = registry.emplace<ecs::BitmapText>(entity);+ text.font = font;+ text.align = ecs::TextAlign::Right;+ text.text = length == 65539 ? std::string(65534, 'A') + "\xf0\x9f\x98\x80" + "A" : std::string(length, 'A');+ const u32 glyphs = length == 65539 ? 65536 : length;+ Frustum frustum;+ frustum.extractFromMatrix(glm::ortho(-1000000.0f, 1000000.0f, -32.0f, 32.0f));+ RenderFrameContext frameContext{context, resources};+ RenderCollectContext collect{registry, frustum, h.clips, h.pool, h.list, frameContext, {}};+ TextPlugin plugin;+ plugin.init(frameContext);+ plugin.collect(collect);+ CHECK(h.pool.vertexBytesUsed(LayoutId::Batch) == glyphs * 4 * sizeof(BatchVertex));+ CHECK(h.pool.indicesUsed(LayoutId::Batch) == glyphs * 6);+ if (h.pool.vertexBytesUsed(LayoutId::Batch) == glyphs * 4 * sizeof(BatchVertex)) {+ const auto* vertices = reinterpret_cast<const BatchVertex*>(h.pool.vertexData(LayoutId::Batch));+ CHECK(vertices[0].position.x == doctest::Approx(-8.0f * glyphs));+ CHECK(vertices[glyphs * 4 - 2].position.x == doctest::Approx(0));+ CHECK(h.pool.indexData(LayoutId::Batch)[glyphs * 6 - 2] == glyphs * 4 - 1);+ }+ h.pool.beginFrame();+ h.list.clear();+ frustum.extractFromMatrix(glm::ortho(-16.0f, 0.0f, -32.0f, 32.0f));+ plugin.collect(collect);+ CHECK(h.pool.vertexBytesUsed(LayoutId::Batch) == 3 * 4 * sizeof(BatchVertex));+ CHECK(h.pool.indicesUsed(LayoutId::Batch) == 18);+ if (h.pool.vertexBytesUsed(LayoutId::Batch) == 3 * 4 * sizeof(BatchVertex)) {+ const auto* vertices = reinterpret_cast<const BatchVertex*>(h.pool.vertexData(LayoutId::Batch));+ CHECK(vertices[10].position.x == doctest::Approx(0));+ CHECK(h.pool.indexData(LayoutId::Batch)[16] == 11);+ }+ h.pool.beginFrame();+ h.list.clear();+ frustum.extractFromMatrix(glm::ortho(-520.0f, -504.0f, -32.0f, 32.0f));+ plugin.collect(collect);+ CHECK(h.pool.vertexBytesUsed(LayoutId::Batch) == 4 * 4 * sizeof(BatchVertex));+ CHECK(h.pool.indicesUsed(LayoutId::Batch) == 24);+ if (h.pool.vertexBytesUsed(LayoutId::Batch) == 4 * 4 * sizeof(BatchVertex)) {+ const auto* vertices = reinterpret_cast<const BatchVertex*>(h.pool.vertexData(LayoutId::Batch));+ CHECK(vertices[0].position.x == doctest::Approx(-528));+ CHECK(vertices[14].position.x == doctest::Approx(-496));+ }+ 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.mjsindex 7b92d59ab..4aa7033e5 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(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':{ note: 'Playable asset quality adapts to the measured delivery budget.' },'feat(editor): show playable asset optimization decisions':