Skip to content

Commit

Permalink
fix setSkin invalidation on cached mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
Canvasfull committed Jul 13, 2023
1 parent b225804 commit d9b2781
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 31 deletions.
59 changes: 36 additions & 23 deletions cocos/spine/skeleton-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ export class AnimationCache {
boneInfo.worldY = bone.worldY;
boneInfosArray.push(boneInfo);
});

this.frames[index] = {
model: modelData,
boneInfos: boneInfosArray,
Expand All @@ -235,14 +234,9 @@ export class AnimationCache {
// If pre animation not finished, play it to the end.
preAnimationCache.updateToFrame(0);
}
}

const skeleton = skeletonInfo?.skeleton;
}

Check failure on line 237 in cocos/spine/skeleton-cache.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Trailing spaces not allowed
const listener = skeletonInfo?.listener;
const state = skeletonInfo?.state;

const animation = skeleton?.data.findAnimation(this._animationName!);
state?.setAnimationWith(0, animation!, false);
this._instance.setAnimation(0, this._animationName!, false);
this.bind(listener!);

// record cur animation cache
Expand Down Expand Up @@ -336,6 +330,18 @@ class SkeletonCache {
this._skeletonCache = {};
}

public invalidAnimationCache (uuid: string) {
const skeletonInfo = this._skeletonCache[uuid];
const skeleton = skeletonInfo && skeletonInfo.skeleton;
if (!skeleton) return;

const animationsCache = skeletonInfo.animationsCache;
for (const aniKey in animationsCache) {
const animationCache = animationsCache[aniKey];
animationCache.invalidAllFrame();
}
}

public removeSkeleton (uuid: string) {
const skeletonInfo = this._skeletonCache[uuid];
if (!skeletonInfo) return;
Expand Down Expand Up @@ -381,23 +387,30 @@ class SkeletonCache {
return animCache;
}

public initAnimationCache (data: SkeletonData, animationName: string) {
const uuid = data.uuid;
const poolKey = `${uuid}#${animationName}`;
public initAnimationCache (uuid: string, data: SkeletonData, animationName: string) {
const spData = data.getRuntimeData();

let animCache = this._animationPool[poolKey];
if (animCache) {
delete this._animationPool[poolKey];
} else {
animCache = new AnimationCache(spData!);
this._animationPool[poolKey] = animCache;
}

if(!spData) return null;

Check warning on line 392 in cocos/spine/skeleton-cache.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected space(s) after "if"
const skeletonInfo = this._skeletonCache[uuid];
animCache.init(skeletonInfo, animationName);
animCache.setAnimation(animationName);
return animCache;
const skeleton = skeletonInfo && skeletonInfo.skeleton;
if (!skeleton) return null;
const animationsCache = skeletonInfo.animationsCache;
let animationCache = animationsCache[animationName];
if (!animationCache) {
// If cache exist in pool, then just use it.
const poolKey = `${uuid}#${animationName}`;
animationCache = this._animationPool[poolKey];
if (animationCache) {
delete this._animationPool[poolKey];
} else {
animationCache = new AnimationCache(spData);
animationCache._privateMode = this._privateMode;
}
animationCache.init(skeletonInfo, animationName);
animationsCache[animationName] = animationCache;
}
animationCache.init(skeletonInfo, animationName);
animationCache.setAnimation(animationName);
return animationCache;
}

public destroyCachedAnimations (uuid?: string) {
Expand Down
29 changes: 21 additions & 8 deletions cocos/spine/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,14 +716,15 @@ export class Skeleton extends UIRenderer {
this._skeletonCache = SkeletonCache.sharedCache;
} else if (this._cacheMode === AnimationCacheMode.PRIVATE_CACHE) {
this._skeletonCache = new SkeletonCache();
this._skeletonCache.enablePrivateMode();
}
}

if (this.isAnimationCached()) {
if (this.debugBones || this.debugSlots) {
warn('Debug bones or slots is invalid in cached mode');
}
const skeletonInfo = this._skeletonCache!.getSkeletonCache((this.skeletonData as any)._uuid, skeletonData);
const skeletonInfo = this._skeletonCache!.getSkeletonCache((this.skeletonData as any).uuid, skeletonData);
this._skeleton = skeletonInfo.skeleton;
} else {
this._skeleton = this._instance.initSkeleton(skeletonData);
Expand Down Expand Up @@ -761,7 +762,7 @@ export class Skeleton extends UIRenderer {
if (!this._skeletonCache) return null;
let cache = this._skeletonCache.getAnimationCache(this._skeletonData!.uuid, name);
if (!cache) {
cache = this._skeletonCache.initAnimationCache(this._skeletonData!, name);
cache = this._skeletonCache.initAnimationCache(this.skeletonData!.uuid, this._skeletonData!, name);

Check failure on line 765 in cocos/spine/skeleton.ts

View workflow job for this annotation

GitHub Actions / npm_test

Type 'AnimationCache | null' is not assignable to type 'AnimationCache'.
}
if (cache) {
this._animationName = name;
Expand Down Expand Up @@ -854,15 +855,14 @@ export class Skeleton extends UIRenderer {
*
* @param skinName @en The name of skin. @zh 皮肤名称。
*/
public setSkin (name: string) {
this._skinName = name;
public setSkin (name: string) {

Check failure on line 858 in cocos/spine/skeleton.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Trailing spaces not allowed
if (this.isAnimationCached()) {
if (this._animCache) {
this._animCache.setSkin(name);
this.invalidAnimationCache();

Check failure on line 862 in cocos/spine/skeleton.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Trailing spaces not allowed
}
} else {
this._instance.setSkin(name);
}
this._instance.setSkin(name);
}

/**
Expand All @@ -882,7 +882,7 @@ export class Skeleton extends UIRenderer {
if (frameCache && frameCache.isInvalid()) {
frameCache.updateToFrame(0);
const frames = frameCache.frames;
this._curFrame = frames[frames.length - 1];
this._curFrame = frames[1];
}
return;
}
Expand Down Expand Up @@ -933,7 +933,6 @@ export class Skeleton extends UIRenderer {
this._playCount = 0;
this._isAniComplete = true;
this._emitCacheCompleteEvent();
return;
}
this._accTime = 0;
frameIdx = 0;
Expand Down Expand Up @@ -1223,6 +1222,20 @@ export class Skeleton extends UIRenderer {
}
}

/**
* @en
* Invalidates the animation cache, which is then recomputed on each frame.
* @zh
* 使动画缓存失效,之后会在每帧重新计算。
* @method invalidAnimationCache
*/
public invalidAnimationCache () {

Check warning on line 1232 in cocos/spine/skeleton.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected indentation of 4 spaces but found 8
if (!this.isAnimationCached()) return;

Check warning on line 1233 in cocos/spine/skeleton.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected indentation of 8 spaces but found 12
if (this._skeletonCache) {

Check warning on line 1234 in cocos/spine/skeleton.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected indentation of 8 spaces but found 12
this._skeletonCache.invalidAnimationCache(this._skeletonData!.uuid);

Check warning on line 1235 in cocos/spine/skeleton.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected indentation of 12 spaces but found 16
}

Check warning on line 1236 in cocos/spine/skeleton.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected indentation of 8 spaces but found 12
}

Check warning on line 1237 in cocos/spine/skeleton.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected indentation of 4 spaces but found 8

/**
* @en
* Finds a bone by name.
Expand Down

0 comments on commit d9b2781

Please sign in to comment.