Skip to content

Commit

Permalink
[ts][pixi-v8] Clean up SpinePipe after Spine object is destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
davidetan committed Nov 12, 2024
1 parent 6ab5ddf commit d0a93b3
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions spine-ts/spine-pixi-v8/src/SpinePipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ import {
collectAllRenderables,
extensions, ExtensionType,
InstructionSet,
type BLEND_MODES,
type Container,
type Renderer,
type RenderPipe,
} from 'pixi.js';
import { BatchableSpineSlot } from './BatchableSpineSlot';
import { Spine } from './Spine';
import { MeshAttachment, RegionAttachment, SkeletonClipping } from '@esotericsoftware/spine-core';
import { MeshAttachment, RegionAttachment } from '@esotericsoftware/spine-core';

const spineBlendModeMap = {
const spineBlendModeMap : Record<number, BLEND_MODES> = {
0: 'normal',
1: 'add',
2: 'multiply',
Expand All @@ -59,7 +61,8 @@ export class SpinePipe implements RenderPipe<Spine> {

renderer: Renderer;

private gpuSpineData: Record<string, any> = {};
private gpuSpineData: Record<string, { slotBatches: Record<string, BatchableSpineSlot> }> = {};
private readonly _destroyRenderableBound = this.destroyRenderable.bind(this) as (renderable: Container) => void;

constructor (renderer: Renderer) {
this.renderer = renderer;
Expand All @@ -68,10 +71,11 @@ export class SpinePipe implements RenderPipe<Spine> {
validateRenderable (spine: Spine): boolean {
spine._applyState();

// if pine attachments have changed, we need to rebuild the batch!
// if spine attachments have changed or destroyed, we need to rebuild the batch!
if (spine.spineAttachmentsDirty) {
return true;
}

// if the textures have changed, we need to rebuild the batch, but only if the texture is not already in the batch
else if (spine.spineTexturesDirty) {
// loop through and see if the textures have changed..
Expand Down Expand Up @@ -102,6 +106,7 @@ export class SpinePipe implements RenderPipe<Spine> {

addRenderable (spine: Spine, instructionSet: InstructionSet) {
const gpuSpine = this.gpuSpineData[spine.uid] ||= { slotBatches: { } };
const gpuSpine = this._getSpineData(spine);

const batcher = this.renderer.renderPipes.batch;

Expand Down Expand Up @@ -169,14 +174,24 @@ export class SpinePipe implements RenderPipe<Spine> {
}

destroyRenderable (spine: Spine) {
// TODO remove the renderable from the batcher
this.gpuSpineData[spine.uid] = null as any;
spine.off('destroyed', this._destroyRenderableBound);
}

destroy () {
this.gpuSpineData = null as any;
this.renderer = null as any;
}

private _getSpineData(spine: Spine): { slotBatches: Record<string, BatchableSpineSlot> } {
return this.gpuSpineData[spine.uid] || this._initMeshData(spine);
}

private _initMeshData(spine: Spine): { slotBatches: Record<string, BatchableSpineSlot> } {
this.gpuSpineData[spine.uid] = { slotBatches: { } };
spine.on('destroyed', this._destroyRenderableBound);
return this.gpuSpineData[spine.uid];
}
}

extensions.add(SpinePipe);

0 comments on commit d0a93b3

Please sign in to comment.