Skip to content

Commit

Permalink
optimize spine attach node logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Canvasfull committed Oct 9, 2023
1 parent 785ee19 commit f93cab8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 47 deletions.
73 changes: 28 additions & 45 deletions cocos/spine/attach-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,67 +35,50 @@ const tempMat4 = new Mat4();
* @class sp.AttachUtil
*/
export class AttachUtil {
protected _inited = false;
protected skeletonBones: spine.Bone[] | null = [];
protected isInitialized = false;
protected skeletonBones: spine.Bone[] | FrameBoneInfo[] | undefined;

Check failure on line 39 in cocos/spine/attach-util.ts

View workflow job for this annotation

GitHub Actions / npm_test

Cannot find name 'FrameBoneInfo'.
protected socketNodes: Map<number, Node> | undefined;
private keysToDelete:number[] = [];

Check warning on line 41 in cocos/spine/attach-util.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected a space after the ':'

constructor () {
this._inited = false;
this.isInitialized = false;
}

init (skeletonComp: Skeleton): void {
this._inited = true;
this.skeletonBones = skeletonComp._skeleton.bones;
this.isInitialized = false;
if (!skeletonComp || skeletonComp.socketNodes.size === 0) return;
const isCached = skeletonComp.isAnimationCached();
this.skeletonBones = isCached && skeletonComp._curFrame ? skeletonComp._curFrame.boneInfos : skeletonComp._skeleton.bones;
if (!this.skeletonBones || this.skeletonBones.length < 1) return;
this.socketNodes = skeletonComp.socketNodes;
if (this.socketNodes.size <= 0) return;
this.isInitialized = true;
this._syncAttachedNode();
}

reset (): void {
this._inited = false;
this.skeletonBones = null;
this.isInitialized = false;
this.skeletonBones = undefined;
this.socketNodes = undefined;
this.keysToDelete.length = 0;
}

_syncAttachedNode (skeletonComp: Skeleton): void {
if (!this._inited || skeletonComp!.socketNodes.size === 0) {
return;
}
const isCached = skeletonComp!.isAnimationCached();
const boneInfos = isCached && skeletonComp!._curFrame ? skeletonComp!._curFrame.boneInfos : this.skeletonBones;

if (!boneInfos || boneInfos.length < 1) {
return;
}

const sockets = skeletonComp!.sockets;
const socketNodes = skeletonComp!.socketNodes;
//const keysToDelete = [];


for (let l = sockets.length - 1; l >= 0; l--) {
const sock = sockets[l];
const boneNode = sock.target;
if (!boneNode) continue;
const bone = boneInfos[sock.boneIndex];
if (bone) this.matrixHandle(boneNode, bone);
}

/*
_syncAttachedNode (): void {
if(!this.isInitialized) return;

Check warning on line 67 in cocos/spine/attach-util.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Expected space(s) after "if"
const socketNodes = this.socketNodes!;
for (const [boneIdx, boneNode] of socketNodes) {
/*
if (!boneNode || !boneNode.isValid) {
keysToDelete.push(boneIdx);
this.keysToDelete.push(boneIdx);
continue;
}
*/

//const bone = boneInfos[boneIdx];
//if (bone) this.matrixHandle(boneNode, bone);
//}
*/
/*
for (const boneIdx of keysToDelete) {
const bone = this.skeletonBones![boneIdx];
if (bone) this.matrixHandle(boneNode, bone);
}
if (this.keysToDelete.length <= 0) return;
for (const boneIdx of this.keysToDelete) {
socketNodes.delete(boneIdx);
}
*/
this.keysToDelete.length = 0;
}

matrixHandle (node: Node, bone: any): void {
Expand Down
5 changes: 3 additions & 2 deletions cocos/spine/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ export class Skeleton extends UIRenderer {
}
this._sockets = val;
this._updateSocketBindings();
this.syncAttachedNode();
this.attachUtil.init(this);

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

View workflow job for this annotation

GitHub Actions / Run ESLint

Trailing spaces not allowed
}

/**
Expand Down Expand Up @@ -1268,7 +1268,7 @@ export class Skeleton extends UIRenderer {
*/
public syncAttachedNode (): void {
// sync attached node matrix
this.attachUtil._syncAttachedNode(this);
this.attachUtil._syncAttachedNode();
}

/**
Expand All @@ -1295,6 +1295,7 @@ export class Skeleton extends UIRenderer {
this._cacheMode = cacheMode;
//this.setSkin(this.defaultSkin);
this._instance.isCache = this.isAnimationCached();
this.attachUtil.init(this);
this._updateSkeletonData();
this.setSkin(this.defaultSkin);
this._updateUseTint();
Expand Down

0 comments on commit f93cab8

Please sign in to comment.