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(spine): atlas page paths resolve through the manifest; KTX2 pages transcode via basis
yhh73d1731536a6
diff --git a/sdk/src/runtimeLoader.ts b/sdk/src/runtimeLoader.tsindex 1cc97160b..7baa6038f 100644--- a/sdk/src/runtimeLoader.ts+++ b/sdk/src/runtimeLoader.ts@@ -18,6 +18,7 @@ import type { App } from './app';import { Assets as AssetsClass } from './asset/Assets';import { Assets as AssetsResource } from './asset/AssetPlugin';import { initBuiltinAssetFields } from './asset/AssetFieldRegistry';+import { transcoderFromModule, type BasisWasmModule } from './asset/basisTranscoder';import type { TextureImportSettings } from './asset/loaders/TextureLoader';import { SceneManager, type SceneConfig } from './sceneManager';import { DEFAULT_GRAVITY, DEFAULT_FIXED_TIMESTEP } from './defaults';@@ -223,7 +224,14 @@ export async function loadRuntimeScene(options: LoadRuntimeSceneOptions): PromissceneAssets.resolveSceneAssetPaths(sceneData, assetResult);applyTextureMetadata(sceneData, assetResult.textureHandles, source.resolveRef ?? ((ref) => ref));- const spineAssetInfo = await loadSpineAssets(module, source, spineManager, discovered.spines);+ // KTX2 atlas pages transcode through the realm's basis module, acquired the+ // same lazy way the TextureLoader's provider does (AssetPlugin).+ const spineAssetInfo = await loadSpineAssets(module, source, spineManager, discovered.spines, async () => {+ const host = app.sideModules;+ if (!host) return null;+ const mod = await host.acquire('basis');+ return mod ? transcoderFromModule(mod as unknown as BasisWasmModule) : null;+ });// Self-gating: install physics when the project declares it OR the scene uses// it, so no runtime entry can forget to wire it. The module comes from thediff --git a/sdk/src/spine/loadSpineScene.ts b/sdk/src/spine/loadSpineScene.tsindex 3fd97c8b2..952556db6 100644--- a/sdk/src/spine/loadSpineScene.ts+++ b/sdk/src/spine/loadSpineScene.ts@@ -26,6 +26,12 @@ import { log } from '../logger';import { SpineManager, type SpineVersion } from './SpineManager';import { parseSpineAtlasPages } from './atlasPages';import { createTextureFromPixels, type RuntimeAssetSource } from '../runtimeAssets';+import type { BasisTranscoder } from '../asset/compressed';++/** Lazily yields the realm's Basis transcoder (KTX2 atlas pages), or null where+ * compressed textures can't occur (editor, uncooked dev). Same seam as the+ * TextureLoader's transcoder provider. */+export type TranscoderProvider = () => Promise<BasisTranscoder | null>;/** The opaque C++ registry handle SpineManager.loadEntity expects (app.world.getCppRegistry()). */type CppRegistry = Parameters<SpineManager['loadEntity']>[4];@@ -46,6 +52,7 @@ export async function loadSpineAssets(source: RuntimeAssetSource,spineManager: SpineManager | null | undefined,spinePairs: ReadonlyArray<{ skeleton: string; atlas: string }>,+ transcoderProvider?: TranscoderProvider,): Promise<Map<string, SpineAssetInfo>> {const assetInfoMap = new Map<string, SpineAssetInfo>();const resolveRef = source.resolveRef ?? ((r: string) => r);@@ -82,7 +89,21 @@ export async function loadSpineAssets(for (const texName of texNames) {const texPath = atlasDir + '/' + texName;try {- const result = await source.decodePixels(texPath, false);+ // The atlas names its pages by AUTHORED filename; a cook may+ // have re-encoded (.ktx2) or content-addressed the staged+ // file, so the derived logical path resolves through the+ // manifest/catalog like every other fetch.+ const staged = resolveRef(texPath);+ let result: { width: number; height: number; pixels: Uint8Array };+ if (staged.toLowerCase().endsWith('.ktx2')) {+ const transcoder = await transcoderProvider?.();+ if (!transcoder) throw new Error('KTX2 atlas page but no Basis transcoder in this realm');+ const rgba = transcoder.transcodeToRgba(new Uint8Array(await source.backend.fetchBinary(staged)));+ if (!rgba) throw new Error(`KTX2 transcode failed: ${staged}`);+ result = { width: rgba.width, height: rgba.height, pixels: rgba.data };+ } else {+ result = await source.decodePixels(staged, false);+ }const handle = createTextureFromPixels(module, result, false);rm.registerTextureWithPath(handle, texPath);textures.set(texName, {@@ -164,10 +185,11 @@ export async function loadSpineSceneEntities(opts: {sceneData: SceneData;entityMap: Map<number, Entity>;registry: CppRegistry;+ transcoderProvider?: TranscoderProvider;}): Promise<void> {const discovered = discoverSceneAssets(opts.sceneData);if (discovered.spines.length === 0) return;- const assetInfo = await loadSpineAssets(opts.module, opts.source, opts.spineManager, discovered.spines);+ const assetInfo = await loadSpineAssets(opts.module, opts.source, opts.spineManager, discovered.spines, opts.transcoderProvider);await applySpineEntities({spineManager: opts.spineManager,sceneData: opts.sceneData,diff --git a/sdk/tests/spine-atlas-page-resolve.test.ts b/sdk/tests/spine-atlas-page-resolve.test.tsnew file mode 100644index 000000000..77432c187--- /dev/null+++ b/sdk/tests/spine-atlas-page-resolve.test.ts@@ -0,0 +1,82 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+/**+ * @file spine-atlas-page-resolve.test.ts+ * @brief Regression: a spine atlas names its pages by AUTHORED filename+ * (`spineboy.png`), but a cook may re-encode the staged file (.ktx2)+ * or content-address it. The page path must resolve through+ * `source.resolveRef` like every other fetch, and a KTX2 page must+ * transcode through the realm's basis provider instead of the image+ * decoder (which cannot decode KTX2 on any platform).+ */+import { describe, it, expect, vi } from 'vitest';++const { rm, createTextureFromPixels } = vi.hoisted(() => ({+ rm: {+ registerTextureWithPath: vi.fn(),+ getTextureGLId: vi.fn(() => 7),+ },+ createTextureFromPixels: vi.fn(() => 11),+}));+vi.mock('../src/resourceManager', () => ({+ requireResourceManager: () => rm,+}));+vi.mock('../src/runtimeAssets', () => ({ createTextureFromPixels }));++import { loadSpineAssets } from '../src/spine/loadSpineScene';+import type { RuntimeAssetSource } from '../src/runtimeAssets';+import type { BasisTranscoder } from '../src/asset/compressed';++const ATLAS = 'spineboy.png\nsize: 4,4\nformat: RGBA8888\n';++function makeSource(resolved: Record<string, string>): RuntimeAssetSource & {+ decodePixels: ReturnType<typeof vi.fn>;+ fetchedBinaries: string[];+} {+ const fetchedBinaries: string[] = [];+ return {+ backend: {+ fetchText: vi.fn(async () => ATLAS),+ fetchBinary: vi.fn(async (p: string) => {+ fetchedBinaries.push(p);+ return new Uint8Array([1, 2, 3]).buffer;+ }),+ } as never,+ decodePixels: vi.fn(async () => ({ width: 4, height: 4, pixels: new Uint8Array(64) })),+ resolveRef: (r: string) => resolved[r] ?? r,+ fetchedBinaries,+ };+}++const PAIR = [{ skeleton: 'assets/spine/boy.skel', atlas: 'assets/spine/boy.atlas' }];++describe('loadSpineAssets resolves atlas page paths through the manifest', () => {+ it('transcodes a KTX2-staged page via the basis provider, keyed by logical path', async () => {+ const source = makeSource({ 'assets/spine/spineboy.png': 'assets/spine/spineboy.ktx2' });+ const transcoder: BasisTranscoder = {+ transcode: vi.fn(() => null),+ transcodeToRgba: vi.fn(() => ({ width: 2, height: 2, data: new Uint8Array(16) })),+ };+ const info = await loadSpineAssets({} as never, source, null, PAIR, async () => transcoder);++ expect(source.decodePixels).not.toHaveBeenCalled();+ expect(source.fetchedBinaries).toContain('assets/spine/spineboy.ktx2');+ expect(transcoder.transcodeToRgba).toHaveBeenCalledTimes(1);+ // Registered under the LOGICAL path — the atlas's stable identity.+ expect(rm.registerTextureWithPath).toHaveBeenCalledWith(11, 'assets/spine/spineboy.png');+ const tex = info.get('assets/spine/boy.skel:assets/spine/boy.atlas')!.textures.get('spineboy.png');+ expect(tex).toEqual({ glId: 7, w: 2, h: 2 });+ });++ it('decodes a content-addressed page from its STAGED path', async () => {+ const source = makeSource({ 'assets/spine/spineboy.png': 'assets/1234abcd.png' });+ await loadSpineAssets({} as never, source, null, PAIR);+ expect(source.decodePixels).toHaveBeenCalledWith('assets/1234abcd.png', false);+ });++ it('a KTX2 page in a realm without basis warns and skips, not throws', async () => {+ const source = makeSource({ 'assets/spine/spineboy.png': 'assets/spine/spineboy.ktx2' });+ const info = await loadSpineAssets({} as never, source, null, PAIR, async () => null);+ expect(info.get('assets/spine/boy.skel:assets/spine/boy.atlas')!.textures.size).toBe(0);+ });+});