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
feat(assets): a project can declare what only its code names
esengine55101a164c97
diff --git a/desktop/electron/cookAssets.ts b/desktop/electron/cookAssets.tsindex 258cd0da7..4a95c27a7 100644--- a/desktop/electron/cookAssets.ts+++ b/desktop/electron/cookAssets.ts@@ -296,9 +296,15 @@ export async function cookAssets(// shared dep that lives outside subpackages/ resolves to group 'main' below, so// it lands in the always-present main package, not duplicated per subpackage.for (const e of index.entries) {+ const group = resolveAssetGroup(e.path, groupsConfig);// …and every non-local (subpackage / remote-CDN) asset: never scene-// referenced, but must be cooked + staged for its group's delivery.- if (resolveAssetGroup(e.path, groupsConfig).delivery !== 'local') seed(e.uuid);+ if (group.delivery !== 'local') seed(e.uuid);+ // …and every asset in a group the project declared always-include: what a+ // scene cannot reference because only code names it (a path built at run+ // time, a texture in rich-text markup). Reachability would cull it, and the+ // first anyone would hear of that is a missing image on a device.+ else if (group.alwaysInclude) seed(e.uuid);}// Force-include locale string tables: translations load by code / plugin// option (a scene never references them — Text carries KEYS, not paths), sodiff --git a/examples/audio-demo/.esengine/asset-groups.json b/examples/audio-demo/.esengine/asset-groups.jsonnew file mode 100644index 000000000..23471074a--- /dev/null+++ b/examples/audio-demo/.esengine/asset-groups.json@@ -0,0 +1,10 @@+{+ "version": "1.0",+ "groups": {+ "clips": {+ "folder": "assets/audio",+ "mode": "local",+ "alwaysInclude": true+ }+ }+}diff --git a/examples/rich-text/.esengine/asset-groups.json b/examples/rich-text/.esengine/asset-groups.jsonnew file mode 100644index 000000000..3398a2cd7--- /dev/null+++ b/examples/rich-text/.esengine/asset-groups.json@@ -0,0 +1,10 @@+{+ "version": "1.0",+ "groups": {+ "markup-images": {+ "folder": "assets/textures",+ "mode": "local",+ "alwaysInclude": true+ }+ }+}diff --git a/examples/space-shooter/.esengine/asset-groups.json b/examples/space-shooter/.esengine/asset-groups.jsonnew file mode 100644index 000000000..fe23c6836--- /dev/null+++ b/examples/space-shooter/.esengine/asset-groups.json@@ -0,0 +1,10 @@+{+ "version": "1.0",+ "groups": {+ "spawnables": {+ "folder": "assets/prefabs",+ "mode": "local",+ "alwaysInclude": true+ }+ }+}diff --git a/examples/sprite-animation/.esengine/asset-groups.json b/examples/sprite-animation/.esengine/asset-groups.jsonnew file mode 100644index 000000000..ba257143d--- /dev/null+++ b/examples/sprite-animation/.esengine/asset-groups.json@@ -0,0 +1,10 @@+{+ "version": "1.0",+ "groups": {+ "clips": {+ "folder": "assets/animations",+ "mode": "local",+ "alwaysInclude": true+ }+ }+}diff --git a/examples/video-playback/.esengine/asset-groups.json b/examples/video-playback/.esengine/asset-groups.jsonnew file mode 100644index 000000000..57f20624d--- /dev/null+++ b/examples/video-playback/.esengine/asset-groups.json@@ -0,0 +1,10 @@+{+ "version": "1.0",+ "groups": {+ "clips": {+ "folder": "assets/video",+ "mode": "local",+ "alwaysInclude": true+ }+ }+}diff --git a/native/host/Host.hpp b/native/host/Host.hppindex 6c60b560a..82103f74e 100644--- a/native/host/Host.hpp+++ b/native/host/Host.hpp@@ -71,6 +71,15 @@ struct FontFile {std::string path;std::vector<esengine::u8> bytes;int faceIndex = 0;+ /**+ * What the matched file does NOT provide of the style that was asked for, so+ * the rasterizer can synthesize it — which is what a browser does when a+ * family has no bold or no italic. Android's system font is one VARIABLE file+ * for every weight and stb_truetype renders its default instance, so a bold+ * request comes back regular and this is how anyone finds out.+ */+ bool syntheticBold = false;+ bool syntheticItalic = false;};/**diff --git a/native/host/media/glyph_raster.cpp b/native/host/media/glyph_raster.cppindex 8d6fe228a..956d679fd 100644--- a/native/host/media/glyph_raster.cpp+++ b/native/host/media/glyph_raster.cpp@@ -37,8 +37,11 @@ struct Font {* path we have not seen costs a read + parse. */std::unordered_map<std::string, std::unique_ptr<Font>> g_fonts;-Font* loadFont(Platform& platform, const std::string& family, uint32_t codepoint, int style) {+Font* loadFont(Platform& platform, const std::string& family, uint32_t codepoint, int style,+ bool& syntheticBold, bool& syntheticItalic) {FontFile file = platform.loadFont(family, codepoint, style);+ syntheticBold = file.syntheticBold;+ syntheticItalic = file.syntheticItalic;if (file.path.empty()) return nullptr;auto it = g_fonts.find(file.path);@@ -75,6 +78,45 @@ std::vector<uint8_t> downsample(const std::vector<uint8_t>& src, int width, intreturn out;}+// Faux styles, for a file that does not carry the one that was asked for. The+// oblique shear is FreeType's (12 degrees); the emboldening radius scales with+// the size so it reads the same at every font size. A browser does exactly this+// for a family with no bold or italic face, which is why text on the web looks+// styled even where the font has only one weight.+constexpr float OBLIQUE_SHEAR = 0.2126f;++/** Widen the ink: each pixel takes the max of itself and its left neighbours,+ * which is a horizontal dilation — the classic synthetic bold. */+void embolden(std::vector<uint8_t>& alpha, int width, int height, int radius) {+ if (radius <= 0) return;+ std::vector<uint8_t> src = alpha;+ for (int y = 0; y < height; y++) {+ const uint8_t* in = src.data() + (size_t)y * width;+ uint8_t* out = alpha.data() + (size_t)y * width;+ for (int x = 0; x < width; x++) {+ uint8_t v = in[x];+ for (int d = 1; d <= radius && x - d >= 0; d++) v = std::max(v, in[x - d]);+ out[x] = v;+ }+ }+}++/** Lean the ink to the right, about the baseline: row y shifts by+ * `shear * (baseline - y)` px, so the bottom stays put and the top leans. */+void oblique(std::vector<uint8_t>& alpha, int width, int height, int baselineRow) {+ std::vector<uint8_t> src = alpha;+ std::fill(alpha.begin(), alpha.end(), (uint8_t)0);+ for (int y = 0; y < height; y++) {+ const int shift = (int)std::lround(OBLIQUE_SHEAR * (float)(baselineRow - y));+ const uint8_t* in = src.data() + (size_t)y * width;+ uint8_t* out = alpha.data() + (size_t)y * width;+ for (int x = 0; x < width; x++) {+ const int sx = x - shift;+ if (sx >= 0 && sx < width) out[x] = in[sx];+ }+ }+}+/** Expand single-channel coverage into the atlas' RGBA tile (RGB = 255, A = it). */std::vector<uint8_t> toAtlasRgba(const std::vector<uint8_t>& coverage, int width, int height) {std::vector<uint8_t> out((size_t)width * height * 4);@@ -94,7 +136,8 @@ GlyphBitmap rasterizeGlyph(Platform& platform, uint32_t codepoint, const std::stGlyphBitmap out;if (pixelSize <= 0.0f) return out;- Font* font = loadFont(platform, family, codepoint, style);+ bool syntheticBold = false, syntheticItalic = false;+ Font* font = loadFont(platform, family, codepoint, style, syntheticBold, syntheticItalic);if (!font) return out;const int glyph = stbtt_FindGlyphIndex(&font->info, (int)codepoint);@@ -117,8 +160,16 @@ GlyphBitmap rasterizeGlyph(Platform& platform, uint32_t codepoint, const std::stconst int inkHss = y1 - y0;if (inkWss <= 0 || inkHss <= 0) return out; // whitespace: advance only, no cell+ // Room for what the file does not provide: emboldening widens the ink to the+ // right, the oblique shear leans its top right — both need the tile to grow,+ // or the synthesis is clipped by the very box it is drawn in.+ const int boldRadius = syntheticBold+ ? std::max(1, (int)std::lround((double)pixelSize * (double)ss / 24.0)) : 0;+ const int leanRoom = syntheticItalic+ ? (int)std::ceil(OBLIQUE_SHEAR * (float)(inkHss - y1)) + 1 : 0; // y1 = rows below the baseline+// Stored (post-downsample) tile, padded; the supersampled grid tiles it exactly.- const int inkW = (inkWss + ss - 1) / ss;+ const int inkW = (inkWss + boldRadius + leanRoom + ss - 1) / ss;const int inkH = (inkHss + ss - 1) / ss;const int w = inkW + pad * 2;const int h = inkH + pad * 2;@@ -129,6 +180,16 @@ GlyphBitmap rasterizeGlyph(Platform& platform, uint32_t codepoint, const std::ststbtt_MakeGlyphBitmap(&font->info, alpha.data() + (size_t)pad * ss * wss + (size_t)pad * ss,inkWss, inkHss, wss, scale, scale, glyph);+ if (boldRadius > 0) {+ embolden(alpha, wss, hss, boldRadius);+ out.advance += (float)boldRadius / (float)ss; // wider ink needs a wider step+ }+ if (leanRoom > 0) {+ // The baseline sits at ink row (-y0) inside the ink box, itself `pad*ss`+ // into the tile: rows below it must not move.+ oblique(alpha, wss, hss, pad * ss - y0);+ }+std::vector<uint8_t> coverage;if (sdf) {// The engine's own generator, so the field the shared SDF shader samples isdiff --git a/native/host/platform/android.cpp b/native/host/platform/android.cppindex 942565df4..6fd490a86 100644--- a/native/host/platform/android.cpp+++ b/native/host/platform/android.cpp@@ -204,6 +204,14 @@ struct AndroidPlatform final : eshost::Platform {AFontMatcher_destroy(matcher);if (!font) return out;+ // What the matcher actually gave us: the system font is a single variable+ // file, so a bold request comes back as the regular instance and the+ // rasterizer has to embolden it itself.+ const bool wantBold = (style & eshost::GLYPH_BOLD) != 0;+ const bool wantItalic = (style & eshost::GLYPH_ITALIC) != 0;+ out.syntheticBold = wantBold && AFont_getWeight(font) < AFONT_WEIGHT_SEMI_BOLD;+ out.syntheticItalic = wantItalic && !AFont_isItalic(font);+if (const char* path = AFont_getFontFilePath(font)) {out.path = path;out.faceIndex = (int)AFont_getCollectionIndex(font);diff --git a/native/host/platform/ios.mm b/native/host/platform/ios.mmindex 836b0fa4b..4e20bfd80 100644--- a/native/host/platform/ios.mm+++ b/native/host/platform/ios.mm@@ -126,6 +126,11 @@ struct IOSPlatform final : eshost::Platform {font = styled;}}+ // A family without a real bold or italic face leaves the trait unset; the+ // rasterizer then synthesizes it, as a browser does for the same family.+ const CTFontSymbolicTraits actual = CTFontGetSymbolicTraits(font);+ out.syntheticBold = (traits & kCTFontTraitBold) && !(actual & kCTFontTraitBold);+ out.syntheticItalic = (traits & kCTFontTraitItalic) && !(actual & kCTFontTraitItalic);// Does this face have the glyph? If not, let Core Text find one that does.if (codepoint) {diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.mdindex fc246203b..8a3492d50 100644--- a/sdk/etc/index.api.md+++ b/sdk/etc/index.api.md@@ -543,6 +543,7 @@ static prototype: ArrayDataSource<any>## AssetGroupDef — interface```+alwaysInclude: boolean | undefinedfolder: stringmode: "local" | "subpackage" | "remote"```@@ -5442,6 +5443,7 @@ static prototype: ResMutInstance<any>## ResolvedAssetGroup — interface```+alwaysInclude: booleandelivery: BundleModename: string```diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.mdindex 58e1e42a6..a8cd02212 100644--- a/sdk/etc/index.native.api.md+++ b/sdk/etc/index.native.api.md@@ -543,6 +543,7 @@ static prototype: ArrayDataSource<any>## AssetGroupDef — interface```+alwaysInclude: boolean | undefinedfolder: stringmode: "local" | "subpackage" | "remote"```@@ -5605,6 +5606,7 @@ static prototype: ResMutInstance<any>## ResolvedAssetGroup — interface```+alwaysInclude: booleandelivery: BundleModename: string```diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.mdindex 7ffbe6d62..3cd714a95 100644--- a/sdk/etc/index.node.api.md+++ b/sdk/etc/index.node.api.md@@ -538,6 +538,7 @@ static prototype: ArrayDataSource<any>## AssetGroupDef — interface```+alwaysInclude: boolean | undefinedfolder: stringmode: "local" | "subpackage" | "remote"```@@ -5437,6 +5438,7 @@ static prototype: ResMutInstance<any>## ResolvedAssetGroup — interface```+alwaysInclude: booleandelivery: BundleModename: string```diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.mdindex 7ffea13de..8608f6a61 100644--- a/sdk/etc/minigame.api.md+++ b/sdk/etc/minigame.api.md@@ -538,6 +538,7 @@ static prototype: ArrayDataSource<any>## AssetGroupDef — interface```+alwaysInclude: boolean | undefinedfolder: stringmode: "local" | "subpackage" | "remote"```@@ -5652,6 +5653,7 @@ static prototype: ResMutInstance<any>## ResolvedAssetGroup — interface```+alwaysInclude: booleandelivery: BundleModename: string```diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.mdindex a2d49e7d7..dbe6b448d 100644--- a/sdk/etc/wechat.api.md+++ b/sdk/etc/wechat.api.md@@ -538,6 +538,7 @@ static prototype: ArrayDataSource<any>## AssetGroupDef — interface```+alwaysInclude: boolean | undefinedfolder: stringmode: "local" | "subpackage" | "remote"```@@ -5444,6 +5445,7 @@ static prototype: ResMutInstance<any>## ResolvedAssetGroup — interface```+alwaysInclude: booleandelivery: BundleModename: string```diff --git a/sdk/src/asset/assetGroups.ts b/sdk/src/asset/assetGroups.tsindex 16565a880..6814e70e6 100644--- a/sdk/src/asset/assetGroups.ts+++ b/sdk/src/asset/assetGroups.ts@@ -29,6 +29,20 @@ export interface AssetGroupDef {/** Project-relative folder whose assets belong to this group. */folder: string;mode: AssetGroupMode;+ /**+ * Ship every asset in this folder whether or not a scene references it.+ *+ * A build cooks what it can REACH from the entry scenes, which is right for+ * anything a scene names — and blind to anything named only in code: a path+ * built at runtime, a texture in rich-text markup, a clip picked by id. This+ * is where a project says so, rather than discovering at run time on a device+ * that the file was culled. (Unity's Resources folder and Unreal's additional+ * cook directories are the same declaration.)+ *+ * Off by default: reachability is what keeps a build from shipping the whole+ * project.+ */+ alwaysInclude?: boolean;}/** A build environment's variables — currently just the CDN root remote groups@@ -52,6 +66,8 @@ export interface AssetGroupsConfig {export interface ResolvedAssetGroup {name: string;delivery: BundleMode;+ /** See {@link AssetGroupDef.alwaysInclude}. */+ alwaysInclude: boolean;}/** Map a user-facing mode to the manifest bundle mode (`subpackage` → `lazy`). */@@ -80,23 +96,29 @@ export function resolveAssetGroup(const path = projectRelPath.replace(/\\/g, '/');if (config?.groups) {- let best: { name: string; mode: AssetGroupMode; len: number } | null = null;+ let best: { name: string; def: AssetGroupDef; len: number } | null = null;for (const [name, def] of Object.entries(config.groups)) {const folder = def.folder.replace(/\\/g, '/').replace(/\/+$/, '');if (folder === '' ) continue;if (path === folder || path.startsWith(`${folder}/`)) {- if (!best || folder.length > best.len) best = { name, mode: def.mode, len: folder.length };+ if (!best || folder.length > best.len) best = { name, def, len: folder.length };}}- if (best) return { name: best.name, delivery: modeToDelivery(best.mode) };+ if (best) {+ return {+ name: best.name,+ delivery: modeToDelivery(best.def.mode),+ alwaysInclude: best.def.alwaysInclude === true,+ };+ }}const sub = SUBPACKAGE_RE.exec(path);- if (sub) return { name: sub[1], delivery: 'lazy' };+ if (sub) return { name: sub[1], delivery: 'lazy', alwaysInclude: false };const rem = REMOTE_RE.exec(path);- if (rem) return { name: rem[1], delivery: 'remote' };+ if (rem) return { name: rem[1], delivery: 'remote', alwaysInclude: false };- return { name: 'main', delivery: 'local' };+ return { name: 'main', delivery: 'local', alwaysInclude: false };}/** The CDN root of the active build profile (the value the export bakes into thediff --git a/sdk/tests/asset-groups.test.ts b/sdk/tests/asset-groups.test.tsindex 023e3990d..cf4a7b4de 100644--- a/sdk/tests/asset-groups.test.ts+++ b/sdk/tests/asset-groups.test.ts@@ -30,39 +30,39 @@ describe('resolveAssetGroup — explicit config decouples delivery from folder n};it('assigns an ordinarily-named folder to a remote group', () => {- expect(resolveAssetGroup('assets/dlc/boss.png', config)).toEqual({ name: 'dlc', delivery: 'remote' });+ expect(resolveAssetGroup('assets/dlc/boss.png', config)).toEqual({ name: 'dlc', delivery: 'remote', alwaysInclude: false });});it('maps a subpackage-mode group to lazy delivery', () => {- expect(resolveAssetGroup('assets/level2/map.png', config)).toEqual({ name: 'level2', delivery: 'lazy' });+ expect(resolveAssetGroup('assets/level2/map.png', config)).toEqual({ name: 'level2', delivery: 'lazy', alwaysInclude: false });});it('longest folder prefix wins for nested groups', () => {- expect(resolveAssetGroup('assets/dlc/hd/[email protected]', config)).toEqual({ name: 'hd', delivery: 'lazy' });- expect(resolveAssetGroup('assets/dlc/boss.png', config)).toEqual({ name: 'dlc', delivery: 'remote' });+ expect(resolveAssetGroup('assets/dlc/hd/[email protected]', config)).toEqual({ name: 'hd', delivery: 'lazy', alwaysInclude: false });+ expect(resolveAssetGroup('assets/dlc/boss.png', config)).toEqual({ name: 'dlc', delivery: 'remote', alwaysInclude: false });});it('unconfigured paths fall through to main/local', () => {- expect(resolveAssetGroup('assets/hero.png', config)).toEqual({ name: 'main', delivery: 'local' });+ expect(resolveAssetGroup('assets/hero.png', config)).toEqual({ name: 'main', delivery: 'local', alwaysInclude: false });});it('normalizes backslashes and a trailing folder slash', () => {const cfg: AssetGroupsConfig = { version: '1.0', groups: { g: { folder: 'assets/g/', mode: 'remote' } } };- expect(resolveAssetGroup('assets\\g\\a.png', cfg)).toEqual({ name: 'g', delivery: 'remote' });+ expect(resolveAssetGroup('assets\\g\\a.png', cfg)).toEqual({ name: 'g', delivery: 'remote', alwaysInclude: false });});});describe('resolveAssetGroup — legacy folder-name convention (fallback / back-compat)', () => {it('still honors subpackages/<name>/ and remote/<name>/ with no config', () => {- expect(resolveAssetGroup('subpackages/level1/a.png', null)).toEqual({ name: 'level1', delivery: 'lazy' });- expect(resolveAssetGroup('remote/cdn/a.png', null)).toEqual({ name: 'cdn', delivery: 'remote' });- expect(resolveAssetGroup('assets/a.png', null)).toEqual({ name: 'main', delivery: 'local' });+ expect(resolveAssetGroup('subpackages/level1/a.png', null)).toEqual({ name: 'level1', delivery: 'lazy', alwaysInclude: false });+ expect(resolveAssetGroup('remote/cdn/a.png', null)).toEqual({ name: 'cdn', delivery: 'remote', alwaysInclude: false });+ expect(resolveAssetGroup('assets/a.png', null)).toEqual({ name: 'main', delivery: 'local', alwaysInclude: false });});it('explicit config takes priority over the folder-name convention', () => {const cfg: AssetGroupsConfig = { version: '1.0', groups: { pack: { folder: 'remote/cdn', mode: 'subpackage' } } };// The path is under remote/cdn/, but the config re-declares it a subpackage.- expect(resolveAssetGroup('remote/cdn/a.png', cfg)).toEqual({ name: 'pack', delivery: 'lazy' });+ expect(resolveAssetGroup('remote/cdn/a.png', cfg)).toEqual({ name: 'pack', delivery: 'lazy', alwaysInclude: false });});});@@ -147,4 +147,18 @@ describe('activeRemoteRoot', () => {expect(activeRemoteRoot({ version: '1.0', activeProfile: 'x', profiles: { x: {} } })).toBeUndefined();expect(activeRemoteRoot({ version: '1.0', activeProfile: 'missing', profiles: { dev: { remoteRoot: 'u' } } })).toBeUndefined();});++ it('carries a group\'s always-include declaration through to the cook', () => {+ const cfg: AssetGroupsConfig = {+ version: '1.0',+ groups: {+ markup: { folder: 'assets/markup', mode: 'local', alwaysInclude: true },+ plain: { folder: 'assets/plain', mode: 'local' },+ },+ };+ // What only code names (a rich-text <img>, a path built at run time) ships+ // because the project said so — reachability from a scene would cull it.+ expect(resolveAssetGroup('assets/markup/heart.png', cfg).alwaysInclude).toBe(true);+ expect(resolveAssetGroup('assets/plain/hero.png', cfg).alwaysInclude).toBe(false);+ });});