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

fix up the texture swapping #46

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/BatchableSpineSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ export class BatchableSpineSlot implements DefaultBatchableMeshElement
setData(
renderable:Spine,
data:AttachmentCacheData,
texture:Texture,
blendMode:BLEND_MODES,
roundPixels: 0 | 1)
{
Expand All @@ -136,7 +135,7 @@ export class BatchableSpineSlot implements DefaultBatchableMeshElement
this.uvs = data.uvs;
}

this.texture = texture;
this.texture = data.texture;
this.roundPixels = roundPixels;

this.blendMode = blendMode;
Expand Down
18 changes: 16 additions & 2 deletions src/Spine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
DEG_TO_RAD,
DestroyOptions,
PointData,
Texture,
Ticker,
ViewContainer,
} from 'pixi.js';
Expand Down Expand Up @@ -104,6 +105,7 @@ export interface AttachmentCacheData
darkColor: Color | null;
darkTint: boolean;
skipRender: boolean;
texture: Texture;
clippedData?: {
vertices: Float32Array;
uvs: Float32Array;
Expand Down Expand Up @@ -145,10 +147,12 @@ export class Spine extends ViewContainer
return slot;
}

public spineAttachmentsDirty: boolean;
public spineAttachmentsDirty = true;
public spineTexturesDirty = true;

private _lastAttachments: Attachment[];

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

public get debug(): ISpineDebugRenderer | undefined
Expand Down Expand Up @@ -443,6 +447,14 @@ export class Spine extends ViewContainer

cacheData.skipRender = cacheData.clipped = false;

const texture = attachment.region?.texture.texture || Texture.EMPTY;

if (cacheData.texture !== texture)
{
cacheData.texture = texture;
this.spineTexturesDirty = true;
}

if (clipper.isClipping())
{
this.updateClippingData(cacheData);
Expand Down Expand Up @@ -595,6 +607,7 @@ export class Spine extends ViewContainer
darkColor: new Color(0, 0, 0, 0),
darkTint: false,
skipRender: false,
texture: attachment.region?.texture.texture,
};
}
else
Expand All @@ -611,6 +624,7 @@ export class Spine extends ViewContainer
darkColor: new Color(0, 0, 0, 0),
darkTint: false,
skipRender: false,
texture: attachment.region?.texture.texture,
};
}

Expand Down
40 changes: 36 additions & 4 deletions src/SpinePipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
InstructionSet,
type Renderer,
type RenderPipe,
Texture
} from 'pixi.js';
import { BatchableSpineSlot } from './BatchableSpineSlot';
import { Spine } from './Spine';
Expand Down Expand Up @@ -73,9 +72,43 @@ export class SpinePipe implements RenderPipe<Spine>
validateRenderable(spine: Spine): boolean
{
spine._applyState();
// loop through and see if the mesh lengths have changed..

return spine.spineAttachmentsDirty;
// if pine attachments have changed, 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..
const drawOrder = spine.skeleton.drawOrder;
const gpuSpine = this.gpuSpineData[spine.uid];

for (let i = 0, n = drawOrder.length; i < n; i++)
{
const slot = drawOrder[i];
const attachment = slot.getAttachment();

if (attachment instanceof RegionAttachment || attachment instanceof MeshAttachment)
{
const cacheData = spine._getCachedData(slot, attachment);
const batchableSpineSlot = gpuSpine.slotBatches[cacheData.id];

const texture = cacheData.texture;

if (texture !== batchableSpineSlot.texture)
{
if (!batchableSpineSlot._batcher.checkAndUpdateTexture(batchableSpineSlot, texture))
{
return true;
}
}
}
}
}

return false;
}

addRenderable(spine: Spine, instructionSet:InstructionSet)
Expand Down Expand Up @@ -104,7 +137,6 @@ export class SpinePipe implements RenderPipe<Spine>
batchableSpineSlot.setData(
spine,
cacheData,
(attachment.region?.texture.texture) || Texture.EMPTY,
blendMode,
roundPixels
);
Expand Down
Loading