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.

feat: add spine skeleton info query and improve editor spine integration

esengine078a881daff8
diff --git a/editor/src/Editor.ts b/editor/src/Editor.ts
index f599a3b73..d7a9f3400 100644
--- a/editor/src/Editor.ts
+++ b/editor/src/Editor.ts
@@ -24,6 +24,7 @@ import {
isAssetNavigable,
isOutputAppendable,
isSpineControllerAware,
+ isSpineInfoProvider,
lockBuiltinPanels,
clearExtensionPanels,
isBuiltinPanel,
@@ -204,6 +205,15 @@ export class Editor {
return null;
}
+ getSpineSkeletonInfo(entityId: number): { animations: string[]; skins: string[] } | null {
+ for (const panel of this.panelInstances_.values()) {
+ if (isSpineInfoProvider(panel)) {
+ return panel.getSpineSkeletonInfo(entityId);
+ }
+ }
+ return null;
+ }
+
// =========================================================================
// Panel Operations
// =========================================================================
diff --git a/editor/src/asset/AssetLoader.ts b/editor/src/asset/AssetLoader.ts
index 4b1c97a4a..24c7af231 100644
--- a/editor/src/asset/AssetLoader.ts
+++ b/editor/src/asset/AssetLoader.ts
@@ -208,12 +208,16 @@ export class AssetLoader {
const rm = this.module_.getResourceManager();
const pageCount = controller.getAtlasPageCount(skeletonHandle);
+ console.log(`[AssetLoader] Spine atlas: ${pageCount} pages, ${textureResults.length} textures loaded`);
for (let i = 0; i < pageCount; i++) {
const pageName = controller.getAtlasPageTextureName(skeletonHandle, i);
const tex = textureResults.find(t => t.path.endsWith(pageName));
if (tex) {
const glTexId = rm.getTextureGLId(tex.handle);
+ console.log(`[AssetLoader] page[${i}] "${pageName}" → handle=${tex.handle} glTex=${glTexId} ${tex.width}x${tex.height}`);
controller.setAtlasPageTexture(skeletonHandle, i, glTexId, tex.width, tex.height);
+ } else {
+ console.warn(`[AssetLoader] Atlas page ${i} texture not found: "${pageName}". Available: ${textureResults.map(t => t.path).join(', ')}`);
}
}
diff --git a/editor/src/panels/PanelRegistry.ts b/editor/src/panels/PanelRegistry.ts
index d4c86bc3d..d05a3d58e 100644
--- a/editor/src/panels/PanelRegistry.ts
+++ b/editor/src/panels/PanelRegistry.ts
@@ -52,6 +52,10 @@ export interface SpineControllerAware {
setSpineController(controller: SpineModuleController | null): void;
}
+export interface SpineInfoProvider {
+ getSpineSkeletonInfo(entityId: number): { animations: string[]; skins: string[] } | null;
+}
+
export function isResizable(p: PanelInstance): p is PanelInstance & Resizable {
return typeof (p as any).resize === 'function';
}
@@ -80,6 +84,10 @@ export function isOutputAppendable(p: PanelInstance): p is PanelInstance & Outpu
return typeof (p as any).appendOutput === 'function';
}
+export function isSpineInfoProvider(p: PanelInstance): p is PanelInstance & SpineInfoProvider {
+ return typeof (p as any).getSpineSkeletonInfo === 'function';
+}
+
const panels = new Map<string, PanelDescriptor>();
const builtinPanelIds = new Set<string>();
diff --git a/editor/src/panels/SceneViewPanel.ts b/editor/src/panels/SceneViewPanel.ts
index 188fa206c..33208fed9 100644
--- a/editor/src/panels/SceneViewPanel.ts
+++ b/editor/src/panels/SceneViewPanel.ts
@@ -244,6 +244,10 @@ export class SceneViewPanel {
this.sceneRenderer_?.setSpineController(controller);
}
+ getSpineSkeletonInfo(entityId: number): { animations: string[]; skins: string[] } | null {
+ return this.sceneRenderer_?.getSpineSkeletonInfo(entityId) ?? null;
+ }
+
private async initWebGLRenderer(): Promise<void> {
if (this.webglInitPending_ || this.webglInitialized_) return;
if (!this.app_?.wasmModule || !this.webglCanvas_) return;
@@ -281,9 +285,15 @@ export class SceneViewPanel {
await this.sceneRenderer_.syncScene(this.store_.scene);
}
+ private lastSceneEntityIds_: string = '';
+
private async onSceneChanged(): Promise<void> {
if (this.sceneRenderer_ && this.useWebGL_) {
- await this.syncSceneToRenderer();
+ const ids = this.store_.scene.entities.map(e => e.id).join(',');
+ if (ids !== this.lastSceneEntityIds_) {
+ this.lastSceneEntityIds_ = ids;
+ await this.syncSceneToRenderer();
+ }
}
this.requestRender();
}
diff --git a/editor/src/property/editors.ts b/editor/src/property/editors.ts
index 66791667a..377258cdb 100644
--- a/editor/src/property/editors.ts
+++ b/editor/src/property/editors.ts
@@ -13,6 +13,7 @@ import { getPlatformAdapter } from '../platform/PlatformAdapter';
import { getAssetLibrary, isUUID } from '../asset/AssetLibrary';
import type { NativeFS } from '../types/NativeFS';
+
// =============================================================================
// Drag Helper
// =============================================================================
@@ -1065,26 +1066,44 @@ interface SpineSkeletonData {
skins?: Array<{ name: string }> | Record<string, unknown>;
}
-async function loadSpineSkeletonData(skeletonPath: string): Promise<SpineSkeletonData | null> {
+async function loadSpineSkeletonData(skeletonPath: string, atlasPath?: string): Promise<SpineSkeletonData | null> {
if (!skeletonPath) return null;
+ const editor = getEditorInstance();
+ if (editor) {
+ const entityId = editor.store.selectedEntity as number | null;
+ if (entityId !== null) {
+ const info = editor.getSpineSkeletonInfo(entityId);
+ if (info) {
+ const animRecord: Record<string, unknown> = {};
+ for (const name of info.animations) {
+ animRecord[name] = {};
+ }
+ return {
+ animations: animRecord,
+ skins: info.skins.map(name => ({ name })),
+ };
+ }
+ }
+ }
+
const projectDir = getProjectDir();
const fs = getNativeFS();
if (!projectDir || !fs) return null;
- let jsonPath = `${projectDir}/${skeletonPath}`;
- if (jsonPath.endsWith('.skel')) {
- jsonPath = jsonPath.replace(/\.skel$/, '.json');
+ if (!skeletonPath.endsWith('.skel')) {
+ let jsonPath = `${projectDir}/${skeletonPath}`;
+ try {
+ const content = await fs.readFile(jsonPath);
+ if (!content) return null;
+ return JSON.parse(content) as SpineSkeletonData;
+ } catch (err) {
+ console.warn('Failed to load spine skeleton:', err);
+ return null;
+ }
}
- try {
- const content = await fs.readFile(jsonPath);
- if (!content) return null;
- return JSON.parse(content) as SpineSkeletonData;
- } catch (err) {
- console.warn('Failed to load spine skeleton:', err);
- return null;
- }
+ return null;
}
function getAnimationNames(data: SpineSkeletonData): string[] {
@@ -1118,6 +1137,7 @@ function createSpineAnimationEditor(
refreshBtn.innerHTML = `<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 2v6h-6M3 12a9 9 0 0 1 15-6.7L21 8M3 22v-6h6M21 12a9 9 0 0 1-15 6.7L3 16"></path></svg>`;
let currentAnimations: string[] = [];
+ let retryTimer: ReturnType<typeof setTimeout> | null = null;
const updateOptions = (animations: string[], currentValue: string) => {
select.innerHTML = '';
@@ -1140,6 +1160,8 @@ function createSpineAnimationEditor(
currentAnimations = animations;
};
+ let retryCount = 0;
+
const loadAnimations = async () => {
const skeletonRef = getComponentValue?.('skeletonPath') as string;
const skeletonPath = skeletonRef ? resolveToPath(skeletonRef) : '';
@@ -1148,12 +1170,19 @@ function createSpineAnimationEditor(
return;
}
- const data = await loadSpineSkeletonData(skeletonPath);
+ const atlasRef = getComponentValue?.('atlasPath') as string;
+ const atlasPath = atlasRef ? resolveToPath(atlasRef) : undefined;
+ const data = await loadSpineSkeletonData(skeletonPath, atlasPath);
if (data) {
const animations = getAnimationNames(data);
updateOptions(animations, String(value ?? ''));
+ retryCount = 0;
} else {
updateOptions([], String(value ?? ''));
+ if (skeletonPath.endsWith('.skel') && retryCount < 3) {
+ retryCount++;
+ retryTimer = setTimeout(loadAnimations, 500 * retryCount);
+ }
}
};
@@ -1181,6 +1210,7 @@ function createSpineAnimationEditor(
}
},
dispose() {
+ if (retryTimer) clearTimeout(retryTimer);
wrapper.remove();
},
};
@@ -1208,6 +1238,8 @@ function createSpineSkinEditor(
refreshBtn.innerHTML = `<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 2v6h-6M3 12a9 9 0 0 1 15-6.7L21 8M3 22v-6h6M21 12a9 9 0 0 1-15 6.7L3 16"></path></svg>`;
let currentSkins: string[] = [];
+ let skinRetryTimer: ReturnType<typeof setTimeout> | null = null;
+ let skinRetryCount = 0;
const updateOptions = (skins: string[], currentValue: string) => {
select.innerHTML = '';
@@ -1240,12 +1272,19 @@ function createSpineSkinEditor(
return;
}
- const data = await loadSpineSkeletonData(skeletonPath);
+ const atlasRef = getComponentValue?.('atlasPath') as string;
+ const atlasPath = atlasRef ? resolveToPath(atlasRef) : undefined;
+ const data = await loadSpineSkeletonData(skeletonPath, atlasPath);
if (data) {
const skins = getSkinNames(data);
updateOptions(skins.length > 0 ? skins : ['default'], String(value ?? 'default'));
+ skinRetryCount = 0;
} else {
updateOptions(['default'], String(value ?? 'default'));
+ if (skeletonPath.endsWith('.skel') && skinRetryCount < 3) {
+ skinRetryCount++;
+ skinRetryTimer = setTimeout(loadSkins, 500 * skinRetryCount);
+ }
}
};
@@ -1273,6 +1312,7 @@ function createSpineSkinEditor(
}
},
dispose() {
+ if (skinRetryTimer) clearTimeout(skinRetryTimer);
wrapper.remove();
},
};
diff --git a/editor/src/renderer/EditorSceneRenderer.ts b/editor/src/renderer/EditorSceneRenderer.ts
index 5adcaee71..62c5f2859 100644
--- a/editor/src/renderer/EditorSceneRenderer.ts
+++ b/editor/src/renderer/EditorSceneRenderer.ts
@@ -158,6 +158,10 @@ export class EditorSceneRenderer {
return { x: bounds.x, y: bounds.y, width: bounds.width, height: bounds.height };
}
+ getSpineSkeletonInfo(entityId: number): { animations: string[]; skins: string[] } | null {
+ return this.sceneManager_?.getSpineSkeletonInfo(entityId) ?? null;
+ }
+
// =========================================================================
// Rendering
// =========================================================================
diff --git a/editor/src/scene/EditorSceneManager.ts b/editor/src/scene/EditorSceneManager.ts
index 852cbf93f..150657c19 100644
--- a/editor/src/scene/EditorSceneManager.ts
+++ b/editor/src/scene/EditorSceneManager.ts
@@ -53,6 +53,8 @@ export class EditorSceneManager {
private spineController_: SpineModuleController | null = null;
private spineInstances_ = new Map<number, number>();
private spineSkeletons_ = new Map<string, number>();
+ private spineInstanceKeys_ = new Map<number, string>();
+ private spineCurrentState_ = new Map<number, { animation: string; skin: string; loop: boolean }>();
constructor(module: ESEngineModule, pathResolver: AssetPathResolver) {
this.module_ = module;
@@ -286,15 +288,27 @@ export class EditorSceneManager {
if (!this.spineController_ || this.spineInstances_.size === 0) return;
for (const [entityId, instanceId] of this.spineInstances_) {
- this.spineController_.update(instanceId, dt);
-
const entityData = this.entityDataMap_.get(entityId);
const spineComp = entityData?.components.find(c => c.type === 'SpineAnimation');
- const skeletonScale = (spineComp?.data as Record<string, number> | undefined)?.skeletonScale ?? 1;
+ const spineData = spineComp?.data as Record<string, unknown> | undefined;
+
+ const skeletonScale = (spineData?.skeletonScale as number) ?? 1;
+ const flipX = (spineData?.flipX as boolean) ?? false;
+ const flipY = (spineData?.flipY as boolean) ?? false;
+ const playing = spineData?.playing !== false;
+ const timeScale = (spineData?.timeScale as number) ?? 1;
+ const color = spineData?.color as { x: number; y: number; z: number; w: number } | undefined;
+
+ if (playing) {
+ this.spineController_.update(instanceId, dt * timeScale);
+ }
const entity = this.entityMap_.get(entityId);
if (!entity) continue;
+ const scaleX = skeletonScale * (flipX ? -1 : 1);
+ const scaleY = skeletonScale * (flipY ? -1 : 1);
+
const registry = this.registry;
let transform: Float32Array | undefined;
@@ -303,26 +317,18 @@ export class EditorSceneManager {
transform = transformToMatrix4x4({
position: wt.position,
rotation: wt.rotation,
- scale: {
- x: wt.scale.x * skeletonScale,
- y: wt.scale.y * skeletonScale,
- z: wt.scale.z,
- },
+ scale: { x: wt.scale.x * scaleX, y: wt.scale.y * scaleY, z: wt.scale.z },
});
} else if (registry.hasLocalTransform(entity)) {
const lt = registry.getLocalTransform(entity) as LocalTransformData;
transform = transformToMatrix4x4({
position: lt.position,
rotation: lt.rotation,
- scale: {
- x: lt.scale.x * skeletonScale,
- y: lt.scale.y * skeletonScale,
- z: lt.scale.z,
- },
+ scale: { x: lt.scale.x * scaleX, y: lt.scale.y * scaleY, z: lt.scale.z },
});
}
- submitSpineMeshesToCore(coreModule, this.spineController_, instanceId, transform);
+ submitSpineMeshesToCore(coreModule, this.spineController_, instanceId, transform, color);
}
}
@@ -337,6 +343,16 @@ export class EditorSceneManager {
return this.spineInstances_.has(entityId);
}
+ getSpineSkeletonInfo(entityId: number): { animations: string[]; skins: string[] } | null {
+ if (!this.spineController_) return null;
+ const instanceId = this.spineInstances_.get(entityId);
+ if (instanceId === undefined) return null;
+ return {
+ animations: this.spineController_.getAnimations(instanceId),
+ skins: this.spineController_.getSkins(instanceId),
+ };
+ }
+
// =========================================================================
// Spine Instance Lifecycle
// =========================================================================
@@ -347,6 +363,8 @@ export class EditorSceneManager {
this.spineController_.destroyInstance(instanceId);
}
this.spineInstances_.delete(entityId);
+ this.spineInstanceKeys_.delete(entityId);
+ this.spineCurrentState_.delete(entityId);
}
private clearSpineInstances(): void {
@@ -357,6 +375,8 @@ export class EditorSceneManager {
}
this.spineInstances_.clear();
this.spineSkeletons_.clear();
+ this.spineInstanceKeys_.clear();
+ this.spineCurrentState_.clear();
}
// =========================================================================
@@ -585,33 +605,46 @@ export class EditorSceneManager {
const controller = this.spineController_!;
const cacheKey = `${skeletonPath}:${atlasPath}`;
- this.destroySpineInstance(entityId);
+ const existingKey = this.spineInstanceKeys_.get(entityId);
+ let instanceId = this.spineInstances_.get(entityId);
- let skeletonHandle = this.spineSkeletons_.get(cacheKey);
- if (skeletonHandle === undefined) {
- const result = await this.assetServer_.assetLoader.loadSpineToModule(
- controller, skeletonPath, atlasPath
- );
- if (!result.success || result.skeletonHandle === undefined) {
- console.warn(`[EditorSceneManager] Failed to load Spine to module: ${result.error}`);
- return;
+ if (instanceId === undefined || existingKey !== cacheKey) {
+ this.destroySpineInstance(entityId);
+
+ let skeletonHandle = this.spineSkeletons_.get(cacheKey);
+ if (skeletonHandle === undefined) {
+ const result = await this.assetServer_.assetLoader.loadSpineToModule(
+ controller, skeletonPath, atlasPath
+ );
+ if (!result.success || result.skeletonHandle === undefined) {
+ console.warn(`[EditorSceneManager] Failed to load Spine to module: ${result.error}`);
+ return;
+ }
+ skeletonHandle = result.skeletonHandle;
+ this.spineSkeletons_.set(cacheKey, skeletonHandle);
}
- skeletonHandle = result.skeletonHandle;
- this.spineSkeletons_.set(cacheKey, skeletonHandle);
- }
- const instanceId = controller.createInstance(skeletonHandle);
- this.spineInstances_.set(entityId, instanceId);
+ instanceId = controller.createInstance(skeletonHandle);
+ this.spineInstances_.set(entityId, instanceId);
+ this.spineInstanceKeys_.set(entityId, cacheKey);
+ this.spineCurrentState_.delete(entityId);
+ }
const skin = data.skin ?? 'default';
- if (skin) {
+ const animation = data.animation ?? '';
+ const loop = data.loop ?? true;
+ const prev = this.spineCurrentState_.get(entityId);
+
+ if (!prev || prev.skin !== skin) {
controller.setSkin(instanceId, skin);
}
-
- const animation = data.animation ?? '';
- if (animation) {
- controller.play(instanceId, animation, data.loop ?? true);
+ if (!prev || prev.animation !== animation || prev.loop !== loop) {
+ if (animation) {
+ controller.play(instanceId, animation, loop);
+ }
}
+
+ this.spineCurrentState_.set(entityId, { animation, skin, loop });
}
private async syncSpineViaCore(