Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
chore: perf increase (#20)
Browse files Browse the repository at this point in the history
Co-authored-by: Zyie <[email protected]>
  • Loading branch information
GoodBoyDigital and Zyie authored Jun 14, 2024
1 parent be521f6 commit 45c4db6
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/Spine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class Spine extends Container implements View
private _lastAttachments: Attachment[];

private _stateChanged: boolean;
private attachmentCacheData: Record<string, AttachmentCacheData> = {};
private attachmentCacheData: Record<string, AttachmentCacheData>[] = [];

public get debug(): ISpineDebugRenderer | undefined
{
Expand Down Expand Up @@ -209,6 +209,13 @@ export class Spine extends Container implements View
this.state = new AnimationState(new AnimationStateData(skeletonData));
this.autoUpdate = options?.autoUpdate ?? true;

const slots = this.skeleton.slots;

for (let i = 0; i < slots.length; i++)
{
this.attachmentCacheData[i] = Object.create(null);
}

this._updateState(0);
}

Expand Down Expand Up @@ -546,23 +553,19 @@ export class Spine extends Container implements View
/** @internal */
_getCachedData(slot: Slot, attachment: RegionAttachment | MeshAttachment): AttachmentCacheData
{
const key = `${slot.data.index}-${attachment.name}`;

return this.attachmentCacheData[key] || this.initCachedData(slot, attachment);
return this.attachmentCacheData[slot.data.index][attachment.name] || this.initCachedData(slot, attachment);
}

private initCachedData(slot: Slot, attachment: RegionAttachment | MeshAttachment): AttachmentCacheData
{
const key = `${slot.data.index}-${attachment.name}`;

let vertices: Float32Array;

if (attachment instanceof RegionAttachment)
{
vertices = new Float32Array(8);

this.attachmentCacheData[key] = {
id: key,
this.attachmentCacheData[slot.data.index][attachment.name] = {
id: `${slot.data.index}-${attachment.name}`,
vertices,
clipped: false,
indices: [0, 1, 2, 0, 2, 3],
Expand All @@ -574,8 +577,8 @@ export class Spine extends Container implements View
{
vertices = new Float32Array(attachment.worldVerticesLength);

this.attachmentCacheData[key] = {
id: key,
this.attachmentCacheData[slot.data.index][attachment.name] = {
id: `${slot.data.index}-${attachment.name}`,
vertices,
clipped: false,
indices: attachment.triangles,
Expand All @@ -584,7 +587,7 @@ export class Spine extends Container implements View
};
}

return this.attachmentCacheData[key];
return this.attachmentCacheData[slot.data.index][attachment.name];
}

protected onViewUpdate()
Expand Down

0 comments on commit 45c4db6

Please sign in to comment.