Skip to content

Commit 0ac67cf

Browse files
committed
[ts][pixi] Added PMA support.
1 parent f4f22cd commit 0ac67cf

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

spine-ts/index.html

+4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ <h1>spine-ts Examples</h1>
3636
<li>
3737
<a href="/spine-pixi/example/mouse-following.html">Mouse following</a>
3838
</li>
39+
<li><a href="/spine-pixi/example/physics.html">Physics</a></li>
40+
<li><a href="/spine-pixi/example/physics2.html">Physics II</a></li>
41+
<li><a href="/spine-pixi/example/physics3.html">Physics III</a></li>
42+
<li><a href="/spine-pixi/example/physics4.html">Physics IV</a></li>
3943
</ul>
4044
<li>Phaser</li>
4145
<ul>

spine-ts/spine-pixi/src/assets/atlasLoader.ts

+22-6
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE
2727
* SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
*****************************************************************************/
29-
3029
import { TextureAtlas } from "@esotericsoftware/spine-core";
3130
import { SpineTexture } from "../SpineTexture.js";
3231
import type { AssetExtension, Loader } from "@pixi/assets";
32+
import { Assets } from "@pixi/assets";
3333
import { LoaderParserPriority, checkExtension } from "@pixi/assets";
3434
import type { Texture } from "@pixi/core";
35-
import { ExtensionType, settings, utils, BaseTexture, extensions } from "@pixi/core";
35+
import { ALPHA_MODES, ExtensionType, settings, utils, BaseTexture, extensions } from "@pixi/core";
3636

3737
type RawAtlas = string;
3838

@@ -77,7 +77,7 @@ const spineTextureAtlasLoader: AssetExtension<RawAtlas | TextureAtlas, ISpineAtl
7777
basePath += "/";
7878
}
7979

80-
// Retval is going to be a texture atlas. However we need to wait for it's callback to resolve this promise.
80+
// Retval is going to be a texture atlas. However, we need to wait for its callback to resolve this promise.
8181
const retval = new TextureAtlas(asset);
8282

8383
// If the user gave me only one texture, that one is assumed to be the "first" texture in the atlas
@@ -90,6 +90,17 @@ const spineTextureAtlasLoader: AssetExtension<RawAtlas | TextureAtlas, ISpineAtl
9090
// we will wait for all promises for the textures at the same time at the end.
9191
const textureLoadingPromises = [];
9292

93+
94+
// setting preferCreateImageBitmap to false for loadTextures loader to allow loading PMA images
95+
let oldPreferCreateImageBitmap = true;
96+
for (const parser of loader.parsers) {
97+
if (parser.name == "loadTextures") {
98+
oldPreferCreateImageBitmap = parser.config?.preferCreateImageBitmap;
99+
break;
100+
}
101+
}
102+
Assets.setPreferences({ preferCreateImageBitmap: false });
103+
93104
// fill the pages
94105
for (const page of retval.pages) {
95106
const pageName = page.name;
@@ -98,15 +109,20 @@ const spineTextureAtlasLoader: AssetExtension<RawAtlas | TextureAtlas, ISpineAtl
98109
page.setTexture(SpineTexture.from(providedPage));
99110
} else {
100111
const url: string = providedPage ?? utils.path.normalize([...basePath.split(utils.path.sep), pageName].join(utils.path.sep));
101-
const pixiPromise = loader.load<Texture>({ src: url, data: metadata.imageMetadata }).then((texture) => {
102-
page.setTexture(SpineTexture.from(texture.baseTexture));
103-
});
112+
const assetsToLoadIn = { src: url, data: { ...metadata.imageMetadata, ...{ alphaMode: page.pma ? ALPHA_MODES.PMA : ALPHA_MODES.UNPACK } } };
113+
const pixiPromise = loader.load<Texture>(assetsToLoadIn)
114+
.then((texture) => {
115+
page.setTexture(SpineTexture.from(texture.baseTexture));
116+
});
104117
textureLoadingPromises.push(pixiPromise);
105118
}
106119
}
107120

108121
await Promise.all(textureLoadingPromises);
109122

123+
// restoring preferCreateImageBitmap old value for loadTextures loader
124+
Assets.setPreferences({ preferCreateImageBitmap: oldPreferCreateImageBitmap });
125+
110126
return retval;
111127
},
112128
},

0 commit comments

Comments
 (0)