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(sdk): a mesh loads on the core the realm actually has
esenginea8b9a14ac17a
diff --git a/sdk/src/asset/Assets.ts b/sdk/src/asset/Assets.tsindex a35836b41..480ee2c6b 100644--- a/sdk/src/asset/Assets.ts+++ b/sdk/src/asset/Assets.ts@@ -22,6 +22,7 @@ import { AssetRefLedger } from './AssetRefLedger';import { SpineAssetLoader } from './loaders/SpineAssetLoader';import { MaterialAssetLoader } from './loaders/MaterialAssetLoader';import { MeshAssetLoader } from './loaders/MeshAssetLoader';+import { nativeEngineApi, type EngineApi } from '../ecs/bridge/engineApi';import { isBuiltinMeshRef } from './builtinMeshes';import { EnvironmentAssetLoader } from './loaders/EnvironmentAssetLoader';import { FontAssetLoader } from './loaders/FontAssetLoader';@@ -1707,8 +1708,12 @@ export class Assets {this.materialLoader_ = new MaterialAssetLoader();this.register(this.materialLoader_);this.register(new FontAssetLoader());- this.register(new MeshAssetLoader(() => this.module_));- this.register(new EnvironmentAssetLoader(() => this.module_));+ // The engine core however it is embedded: the module on the web, the host's+ // bindings on a device. Taking only the module made these two web-only by+ // construction — a packaged game passes `module: null`.+ const core = (): EngineApi | null => this.module_ ?? nativeEngineApi();+ this.register(new MeshAssetLoader(core));+ this.register(new EnvironmentAssetLoader(core));// The audio loader needs the AudioAPI outside load() too (unload /// invalidate have no LoadContext), so it shares Assets' lazy accessor.this.register(new AudioAssetLoader(() => this.getAudio_()));diff --git a/sdk/src/asset/loaders/EnvironmentAssetLoader.ts b/sdk/src/asset/loaders/EnvironmentAssetLoader.tsindex 87a0c39c9..2a400c399 100644--- a/sdk/src/asset/loaders/EnvironmentAssetLoader.ts+++ b/sdk/src/asset/loaders/EnvironmentAssetLoader.ts@@ -2,7 +2,8 @@// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Teamimport type { AssetLoader, LoadContext } from '../AssetLoader';import { resolveDocumentRef } from '../documentRef';-import type { ESEngineModule } from '../../wasm';+import type { EngineApi } from '../../ecs/bridge/engineApi';+import { marshallingCore } from './engineCore';import { withScratch } from '../../wasm/wasmScratch';import { log } from '../../util/logger';@@ -36,7 +37,7 @@ export class EnvironmentAssetLoader implements AssetLoader<EnvironmentResult> {readonly extensions = ['.esenv'];/** Lazy like the mesh loader's: the module arrives after construction. */- constructor(private readonly module_: () => ESEngineModule | null) {}+ constructor(private readonly core_: () => EngineApi | null) {}async load(path: string, ctx: LoadContext): Promise<EnvironmentResult> {const text = await ctx.loadText(ctx.catalog.getBuildPath(path));@@ -45,7 +46,7 @@ export class EnvironmentAssetLoader implements AssetLoader<EnvironmentResult> {throw new Error(`${path}: an environment is nine RGB coefficients, got `+ `${data.irradiance?.length ?? 0} numbers`);}- const m = this.module_();+ const m = marshallingCore(this.core_());if (!m?.environment_create) {throw new Error('this engine build carries no environment_create');}@@ -83,7 +84,7 @@ export class EnvironmentAssetLoader implements AssetLoader<EnvironmentResult> {}unload(asset: EnvironmentResult, ctx: LoadContext): void {- this.module_()?.environment_release?.(asset.handle);+ this.core_()?.environment_release?.(asset.handle);if (asset.specularPath) ctx.releaseTexture(asset.specularPath);}}diff --git a/sdk/src/asset/loaders/MeshAssetLoader.ts b/sdk/src/asset/loaders/MeshAssetLoader.tsindex ee1e090a9..108fdaed4 100644--- a/sdk/src/asset/loaders/MeshAssetLoader.ts+++ b/sdk/src/asset/loaders/MeshAssetLoader.ts@@ -1,7 +1,8 @@// SPDX-License-Identifier: Apache-2.0// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Teamimport type { AssetLoader, LoadContext } from '../AssetLoader';-import type { ESEngineModule } from '../../wasm';+import type { EngineApi } from '../../ecs/bridge/engineApi';+import { marshallingCore } from './engineCore';import { withScratch } from '../../wasm/wasmScratch';import { decodeMesh, encodeChannelTable } from '../meshFormat';import { builtinMeshTemplate, isBuiltinMeshRef } from '../builtinMeshes';@@ -27,7 +28,7 @@ export class MeshAssetLoader implements AssetLoader<MeshResult> {readonly extensions = ['.esmesh'];/** Lazy like the audio loader's: unload/invalidate have no LoadContext. */- constructor(private readonly module_: () => ESEngineModule | null) {}+ constructor(private readonly core_: () => EngineApi | null) {}async load(path: string, ctx: LoadContext): Promise<MeshResult> {const builtin = isBuiltinMeshRef(path) ? builtinMeshTemplate(path) : undefined;@@ -35,7 +36,7 @@ export class MeshAssetLoader implements AssetLoader<MeshResult> {? builtin.build(): decodeMesh(new Uint8Array(await ctx.loadBinary(ctx.catalog.getBuildPath(path))));const table = encodeChannelTable(mesh.channels);- const m = this.module_();+ const m = marshallingCore(this.core_());if (!m?.mesh_createFromChannels) {throw new Error('this engine build carries no mesh_createFromChannels');}@@ -76,6 +77,6 @@ export class MeshAssetLoader implements AssetLoader<MeshResult> {unload(asset: MeshResult): void {releaseMeshCollision(asset.handle);- this.module_()?.mesh_release?.(asset.handle);+ this.core_()?.mesh_release?.(asset.handle);}}diff --git a/sdk/src/asset/loaders/engineCore.ts b/sdk/src/asset/loaders/engineCore.tsnew file mode 100644index 000000000..169811693--- /dev/null+++ b/sdk/src/asset/loaders/engineCore.ts@@ -0,0 +1,22 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file The engine core a loader can marshal bulk data through.+ *+ * A wasm module carries its heap by construction; a native host answers one from+ * its arena. Both, or neither — a core that has the entry point but no heap has+ * no way to be handed vertices, and saying so here keeps the two loaders that+ * marshal from each open-coding the same four checks.+ */+import type { EngineApi } from '../../ecs/bridge/engineApi';+import type { NativeHeap } from '../../ecs/bridge/nativeHeap';++/** An engine core with the heap its bulk entry points read from. */+export type MarshallingCore = EngineApi & NativeHeap;++/** `api` when it can marshal, else null. */+export function marshallingCore(api: EngineApi | null): MarshallingCore | null {+ return api?._malloc && api._free && api.HEAPU8 && api.HEAPF32+ ? (api as MarshallingCore)+ : null;+}diff --git a/sdk/tests/native-assets.test.ts b/sdk/tests/native-assets.test.tsindex 772b190aa..cf89272b0 100644--- a/sdk/tests/native-assets.test.ts+++ b/sdk/tests/native-assets.test.ts@@ -12,6 +12,8 @@*/import { afterEach, describe, expect, it, vi } from 'vitest';import { createNativeApp } from '../src/ecs/bridge/nativeRuntime';+import { setNativeEngineApi } from '../src/ecs/bridge/engineApi';+import { builtinMeshTemplate } from '../src/asset/builtinMeshes';import { createNativeResourceManager } from '../src/ecs/bridge/nativeResourceManager';import { Assets } from '../src/asset/AssetPlugin';import { shutdownResourceManager } from '../src/wasm/resourceManager';@@ -144,3 +146,63 @@ describe('native Assets.loadTexture', () => {expect(releaseSpy).toHaveBeenCalledWith(1);});});++// A device's core is the host's bindings, not a wasm module. The two loaders that+// marshal bulk data took only the module, so no native package ever loaded a mesh.+describe('native Assets mesh loading', () => {+ it('marshals built-in geometry through the native core, with no wasm module', async () => {+ const { scope } = makeTextureScope();+ const heap = new Uint8Array(1 << 20);+ let brk = 16;+ const created: Array<{ channels: number; stride: number; indices: number }> = [];+ Object.assign(scope, {+ es_heap: () => heap,+ es_malloc: (n: number) => { const p = brk; brk += n + (8 - (n % 8)); return p; },+ es_free: () => {},+ es_mesh_createFromChannels: (+ _table: number, channelCount: number, stride: number,+ _verts: number, _vertBytes: number, _idx: number, indexCount: number,+ ) => {+ created.push({ channels: channelCount, stride, indices: indexCount });+ return 7;+ },+ });+ setNativeEngineApi({+ HEAPU8: heap,+ HEAPU16: new Uint16Array(heap.buffer),+ HEAPU32: new Uint32Array(heap.buffer),+ HEAPI32: new Int32Array(heap.buffer),+ HEAPF32: new Float32Array(heap.buffer),+ HEAPF64: new Float64Array(heap.buffer),+ _malloc: (n: number) => (scope.es_malloc as (n: number) => number)(n),+ _free: () => {},+ mesh_createFromChannels: scope.es_mesh_createFromChannels as never,+ });+ try {+ const app = createNativeApp(makeBridge({}), scope);+ await app.tick(0);+ const assets = app.getResource(Assets);++ const scene = {+ version: '1.0', name: 'Main',+ entities: [{+ id: 0, name: 'Block', parent: null, children: [], visible: true,+ components: [+ { type: 'Transform', data: {} },+ { type: 'Mesh2D', data: { mesh: 'builtin:cube' } },+ ],+ }],+ };+ const result = await assets.preloadSceneAssets(scene as never);++ expect(result.missing).toEqual([]);+ expect(created).toHaveLength(1);+ // The geometry that crossed is the cube's own, not an empty upload.+ const cube = builtinMeshTemplate('builtin:cube')!.build();+ expect(created[0]!.indices).toBe(cube.indices.length);+ expect(created[0]!.channels).toBe(cube.channels.length);+ } finally {+ setNativeEngineApi(null);+ }+ });+});