A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(spine): effects that flip through atlas pages stop sampling empty space
yhhb78be8327973
diff --git a/src/esengine/bindings/modules/spine/SpineRuntime.hpp b/src/esengine/bindings/modules/spine/SpineRuntime.hppindex 5830aed53..cd8687835 100644--- a/src/esengine/bindings/modules/spine/SpineRuntime.hpp+++ b/src/esengine/bindings/modules/spine/SpineRuntime.hpp@@ -119,6 +119,45 @@ using EventSink = void (*)(const Event&);/// backends below name this type, so it stays spelled `es::spine::TriangleSink`.using TriangleSink = es::skeletal::TriangleSink;+/**+ * How PREMULTIPLIED artwork changes what a slot emits — one rule, because both+ * backends below face the same question and used to answer it separately.+ *+ * An atlas page declares `pma: true` when its pixels are stored premultiplied.+ * Two things follow, and Spine's own runtimes (spine-libgdx SkeletonRenderer,+ * spine-glfw) settle both:+ *+ * 1. THE BLEND FUNCTION, but only for two of the four modes. Normal and Additive+ * have premultiplied twins — their source factor becomes ONE, which is what+ * blend codes 4 and 5 are. Multiply and Screen use the SAME function either+ * way (DST_COLOR / ONE already ignore the source alpha), so they pass through+ * unchanged; that is Spine's table, not a simplification of it.+ *+ * 2. THE TINT, for EVERY mode. The shader multiplies a premultiplied texel by+ * the emitted colour, and that product is only itself premultiplied when the+ * colour is — so the tint's rgb must carry its alpha. Gating this on the+ * blend code instead (the two modes that got a twin) leaves a Multiply or+ * Screen slot with a straight tint over premultiplied pixels, and it stops+ * fading correctly as its alpha animates.+ */+inline bool blendHasPremultipliedTwin(int slotBlend) {+ return slotBlend == 0 || slotBlend == 1; // normal, additive+}++/// The blend code to emit for a slot's mode on a page of the given kind (see above).+inline int blendForPage(int slotBlend, bool pagePremultiplied) {+ return pagePremultiplied && blendHasPremultipliedTwin(slotBlend) ? slotBlend + 4 : slotBlend;+}++/// Fold the tint's alpha into its rgb — required for every slot on a premultiplied+/// page, and a no-op on any other. Call with the SAME flag `blendForPage` got.+inline void premultiplyTint(float rgba[4], bool pagePremultiplied) {+ if (!pagePremultiplied) return;+ rgba[0] *= rgba[3];+ rgba[1] *= rgba[3];+ rgba[2] *= rgba[3];+}+// --- resources -------------------------------------------------------------/** Returns null and fills `error` when the atlas or the skeleton data is unreadable. */diff --git a/src/esengine/bindings/modules/spine/SpineRuntimeC.cpp b/src/esengine/bindings/modules/spine/SpineRuntimeC.cppindex 4669233bf..d747af7a7 100644--- a/src/esengine/bindings/modules/spine/SpineRuntimeC.cpp+++ b/src/esengine/bindings/modules/spine/SpineRuntimeC.cpp@@ -138,12 +138,11 @@ uint32_t meshTexture(spMeshAttachment* attachment) {return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(attachment->region->rendererObject));}-/// Straight-alpha blend modes get their premultiplied twin when the page is PMA.-int premultiply(int blendMode, spAtlasRegion* region) {- if (!region || !region->page || !region->page->pma) return blendMode;- if (blendMode == 0) return 4;- if (blendMode == 1) return 5;- return blendMode;+/// Whether an attachment's page stores premultiplied artwork — the one fact that+/// decides both its blend code and whether its tint carries its alpha+/// (SpineRuntime.hpp). Atlases before 4.0 have no `pma` flag at all.+bool pagePremultiplied(spAtlasRegion* region) {+ return region && region->page && region->page->pma;}#endif@@ -624,19 +623,25 @@ void render(Instance* instance, TriangleSink& sink, bool clipping) {if (attachment->type == SP_ATTACHMENT_REGION) {auto* region = reinterpret_cast<spRegionAttachment*>(attachment);- const uint32_t texture = regionTexture(region);- if (texture) {- g_worldVertices.resize(8);+ g_worldVertices.resize(8);#if ES_SPINE_VERSION < 40- spRegionAttachment_computeWorldVertices(region, slot->bone,- g_worldVertices.data(), 0, 2);- const int effectiveBlend = blendMode;+ spRegionAttachment_computeWorldVertices(region, slot->bone,+ g_worldVertices.data(), 0, 2);+ const bool pma = false; // no page carries a pma flag before 4.0#else- spRegionAttachment_computeWorldVertices(region, slot,- g_worldVertices.data(), 0, 2);- const int effectiveBlend = premultiply(- blendMode, reinterpret_cast<spAtlasRegion*>(region->region));+ // Applies the attachment's SEQUENCE, if it has one — which swaps the+ // region this attachment points at for the frame the slot is on, and+ // with it the page, the uvs and the renderer object. So everything+ // that identifies the artwork must be read AFTER this call: a+ // sequence whose frames sit on different atlas pages (an effect+ // flipbook usually does) otherwise draws the new frame's uvs against+ // the previous frame's texture, and the effect samples empty space.+ spRegionAttachment_computeWorldVertices(region, slot,+ g_worldVertices.data(), 0, 2);+ const bool pma = pagePremultiplied(reinterpret_cast<spAtlasRegion*>(region->region));#endif+ const uint32_t texture = regionTexture(region);+ if (texture) {const spColor& attachmentColor = region->color;float rgba[4] = {skeletonColor.r * slotColor.r * attachmentColor.r,@@ -644,30 +649,28 @@ void render(Instance* instance, TriangleSink& sink, bool clipping) {skeletonColor.b * slotColor.b * attachmentColor.b,skeletonColor.a * slotColor.a * attachmentColor.a,};- if (effectiveBlend >= 4) {- rgba[0] *= rgba[3];- rgba[1] *= rgba[3];- rgba[2] *= rgba[3];- }+ premultiplyTint(rgba, pma);static unsigned short quad[6] = {0, 1, 2, 2, 3, 0};emit(sink, clipping, g_worldVertices.data(), 4, region->uvs, quad, 6,- texture, effectiveBlend, rgba);+ texture, blendForPage(blendMode, pma), rgba);}} else if (attachment->type == SP_ATTACHMENT_MESH) {auto* mesh = reinterpret_cast<spMeshAttachment*>(attachment);- const uint32_t texture = meshTexture(mesh);- if (texture) {- const int length = SUPER(mesh)->worldVerticesLength;- g_worldVertices.resize(length);- spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0, length,- g_worldVertices.data(), 0, 2);+ const int length = SUPER(mesh)->worldVerticesLength;+ g_worldVertices.resize(length);+ // Applies this attachment's SEQUENCE — see the region branch above:+ // the texture, the region and the uvs are only settled once this has+ // run, so none of them may be read before it.+ spVertexAttachment_computeWorldVertices(SUPER(mesh), slot, 0, length,+ g_worldVertices.data(), 0, 2);#if ES_SPINE_VERSION < 40- const int effectiveBlend = blendMode;+ const bool pma = false; // no page carries a pma flag before 4.0#else- const int effectiveBlend = premultiply(- blendMode, reinterpret_cast<spAtlasRegion*>(mesh->region));+ const bool pma = pagePremultiplied(reinterpret_cast<spAtlasRegion*>(mesh->region));#endif+ const uint32_t texture = meshTexture(mesh);+ if (texture) {const spColor& attachmentColor = mesh->color;float rgba[4] = {skeletonColor.r * slotColor.r * attachmentColor.r,@@ -675,15 +678,11 @@ void render(Instance* instance, TriangleSink& sink, bool clipping) {skeletonColor.b * slotColor.b * attachmentColor.b,skeletonColor.a * slotColor.a * attachmentColor.a,};- if (effectiveBlend >= 4) {- rgba[0] *= rgba[3];- rgba[1] *= rgba[3];- rgba[2] *= rgba[3];- }+ premultiplyTint(rgba, pma);emit(sink, clipping, g_worldVertices.data(), length / 2, mesh->uvs,mesh->triangles, mesh->trianglesCount,- texture, effectiveBlend, rgba);+ texture, blendForPage(blendMode, pma), rgba);}}}diff --git a/src/esengine/bindings/modules/spine/SpineRuntimeCpp.cpp b/src/esengine/bindings/modules/spine/SpineRuntimeCpp.cppindex 52c0d7d47..81366c0de 100644--- a/src/esengine/bindings/modules/spine/SpineRuntimeCpp.cpp+++ b/src/esengine/bindings/modules/spine/SpineRuntimeCpp.cpp@@ -514,20 +514,16 @@ void render(Instance* instance, TriangleSink& sink, bool clipping) {static_cast<float>((packed >> 24) & 0xFF) / 255.0f,};- int blendMode = blendModeOf(command->blendMode);- if (instance->owner->isPremultiplied(command->texture)) {- if (blendMode == 0 || blendMode == 1) {- blendMode += 4;- rgba[0] *= rgba[3];- rgba[1] *= rgba[3];- rgba[2] *= rgba[3];- }- }+ // Both consequences of premultiplied artwork, from the one rule the C+ // backend also reads (SpineRuntime.hpp): the blend code gains a twin for+ // two of the four modes, and the tint carries its alpha for ALL of them.+ const bool pma = instance->owner->isPremultiplied(command->texture);+ premultiplyTint(rgba, pma);sink.emit(command->positions, command->uvs, command->numVertices,command->indices, command->numIndices,static_cast<uint32_t>(reinterpret_cast<uintptr_t>(command->texture)),- blendMode, rgba);+ blendForPage(blendModeOf(command->blendMode), pma), rgba);}}