Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding debug capabilities to every single spine! #453

Merged
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
44 changes: 42 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,49 @@ let spine = new PIXI.heaven.Spine(spineData);

### Debug

To show bones and bounds you can use [pixi-spine-debug](https://github.com/sbfkcel/pixi-spine-debug). If you want to write your own debug plugin, look at how this one [was created](https://github.com/pixijs/pixi-spine/issues/324)
To show debug graphics you can set `yourSpine.debug = new SpineDebugRenderer()`

Demo: https://sbfkcel.github.io/pixi-spine-debug/
Control what gets drawn with the following flags:

```js
// Master toggle
yourSpine.debug.drawDebug = true;

// Per feature toggle
yourSpine.debug.drawMeshHull = true;
yourSpine.debug.drawMeshTriangles = true;
yourSpine.debug.drawBones = true;
yourSpine.debug.drawPaths = true;
yourSpine.debug.drawBoundingBoxes = true;
yourSpine.debug.drawClipping = true;
yourSpine.debug.drawRegionAttachments = true;
```

To have even more control, you can customize the color and line thickness with
```js
yourSpine.debug.debugOptions.lineWidth = 1;
yourSpine.debug.debugOptions.regionAttachmentsColor = 0x0078ff;
yourSpine.debug.debugOptions.meshHullColor = 0x0078ff;
yourSpine.debug.debugOptions.meshTrianglesColor = 0xffcc00;
yourSpine.debug.debugOptions.clippingPolygonColor = 0xff00ff;
yourSpine.debug.debugOptions.boundingBoxesRectColor = 0x00ff00;
yourSpine.debug.debugOptions.boundingBoxesPolygonColor = 0x00ff00;
yourSpine.debug.debugOptions.boundingBoxesCircleColor = 0x00ff00;
yourSpine.debug.debugOptions.pathsCurveColor = 0xff0000;
yourSpine.debug.debugOptions.pathsLineColor = 0xff00ff;
yourSpine.debug.debugOptions.skeletonXYColor = 0xff0000;
yourSpine.debug.debugOptions.bonesColor = 0x00eecc;
```

You can reuse a single debug renderer and they will share the debug settings!
```js
const debugRenderer = new SpineDebugRenderer();

oneSpine.debug = debugRenderer;
anotherSpine.debug = debugRenderer;
```

If you want to create your own debugger you can extend `SpineDebugRenderer` or create a class from scratch that implements `ISpineDebugRenderer`!

## Build & Development

Expand Down
23 changes: 21 additions & 2 deletions packages/base/src/SpineBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {Rectangle, Polygon, Transform} from '@pixi/math';
import {hex2rgb, rgb2hex} from '@pixi/utils';
import type {Texture} from '@pixi/core';
import {settings} from "./settings";
import { ISpineDebugRenderer } from './SpineDebugRenderer';

let tempRgb = [0, 0, 0];

Expand Down Expand Up @@ -74,7 +75,6 @@ export abstract class SpineBase<Skeleton extends ISkeleton,
AnimationState extends IAnimationState,
AnimationStateData extends IAnimationStateData>
extends Container implements GlobalMixins.Spine {

tintRgb: ArrayLike<number>;
spineData: SkeletonData;
skeleton: Skeleton;
Expand All @@ -85,6 +85,19 @@ export abstract class SpineBase<Skeleton extends ISkeleton,
localDelayLimit: number;
private _autoUpdate: boolean;
protected _visible: boolean;
private _debug: ISpineDebugRenderer;
public get debug(): ISpineDebugRenderer {
return this._debug;
}
public set debug(value: ISpineDebugRenderer) {
if (value == this._debug) { // soft equality allows null == undefined
return;
}
this._debug?.unregisterSpine(this);
value?.registerSpine(this);
this._debug = value;
}


abstract createSkeleton(spineData: ISkeletonData);

Expand Down Expand Up @@ -266,7 +279,7 @@ export abstract class SpineBase<Skeleton extends ISkeleton,
let region = (attachment as IRegionAttachment).region;

let attColor = (attachment as any).color;
switch (attachment.type) {
switch (attachment != null && attachment.type) {
case AttachmentType.Region:
let transform = slotContainer.transform;
transform.setFromMatrix(slot.bone.matrix);
Expand Down Expand Up @@ -471,6 +484,9 @@ export abstract class SpineBase<Skeleton extends ISkeleton,
}
}
}

// if you can debug, then debug!
this._debug?.renderDebug(this);
};

private setSpriteRegion(attachment: IRegionAttachment, sprite: SpineSprite, region: TextureRegion) {
Expand Down Expand Up @@ -766,7 +782,10 @@ export abstract class SpineBase<Skeleton extends ISkeleton,
return [list_d, list_n];
};


destroy(options?: any): void {
this.debug = null; // setter will do the cleanup

for (let i = 0, n = this.skeleton.slots.length; i < n; i++) {
let slot = this.skeleton.slots[i];
for (let name in slot.meshes) {
Expand Down
Loading