From 4fc4e752712882d03bc6bd106bec7348da78ad44 Mon Sep 17 00:00:00 2001 From: bofeng-song Date: Fri, 16 May 2025 11:21:20 +0800 Subject: [PATCH] In Spine 4.2, the attachment->computeWorldVertices() method may modify UV values, requiring updates every frame. (#18714) --- .../spine-creator-support/SkeletonCache.cpp | 58 ++++-- .../spine-creator-support/SkeletonDataMgr.cpp | 98 ++++++++-- .../spine-creator-support/SkeletonDataMgr.h | 6 +- .../SkeletonRenderer.cpp | 68 ++++--- .../spine-creator-support/SkeletonRenderer.h | 9 + .../spine-creator-support/spine-cocos2dx.cpp | 72 +------ .../AtlasAttachmentLoaderExtension.cpp | 77 -------- .../spine-wasm/spine-skeleton-instance.cpp | 178 +++++++++++++----- .../editor-support/spine-wasm/spine-wasm.cpp | 140 +++++++++----- native/external-config.json | 2 +- native/tools/swig-config/spine_3_8.i | 1 + native/tools/swig-config/spine_4_2.i | 1 + 12 files changed, 408 insertions(+), 302 deletions(-) diff --git a/native/cocos/editor-support/spine-creator-support/SkeletonCache.cpp b/native/cocos/editor-support/spine-creator-support/SkeletonCache.cpp index 446bdeb96b..3481af26f9 100644 --- a/native/cocos/editor-support/spine-creator-support/SkeletonCache.cpp +++ b/native/cocos/editor-support/spine-creator-support/SkeletonCache.cpp @@ -30,10 +30,14 @@ #include "SkeletonCache.h" #include "base/memory/Memory.h" #include "spine-creator-support/AttachmentVertices.h" +#include "SkeletonDataMgr.h" USING_NS_MW; // NOLINT(google-build-using-namespace) using namespace cc; // NOLINT(google-build-using-namespace) using namespace spine; + +extern "C" AttachmentVertices *generateAttachmentVertices(Attachment *attachment); + namespace cc { float SkeletonCache::FrameTime = 1.0F / 60.0F; @@ -311,6 +315,10 @@ void SkeletonCache::renderAnimationFrame(AnimationData *animationData) { matm[13] = bone->getWorldY(); } + + auto *verticesMap = SkeletonDataMgr::getInstance()->getSkeletonDataInfo(_uuid); + if (!verticesMap) return; + auto &attachmentVerticesMap = *verticesMap; auto &drawOrder = _skeleton->getDrawOrder(); for (size_t i = 0, n = drawOrder.size(); i < n; ++i) { slot = drawOrder[i]; @@ -319,20 +327,34 @@ void SkeletonCache::renderAnimationFrame(AnimationData *animationData) { continue; } - if (!slot->getAttachment()) { + auto *tmpAttachment = slot->getAttachment(); + if (!tmpAttachment) { _clipper->clipEnd(*slot); continue; } const spine::Color &slotColor = slot->getColor(); + auto iterAttachment = attachmentVerticesMap.find(tmpAttachment); + if (iterAttachment != attachmentVerticesMap.end()) { + attachmentVertices = iterAttachment->second; + } else { + attachmentVertices = nullptr; + } + TwoColorTriangles trianglesTwoColor; spine::Color attachmentColor; - if (slot->getAttachment()->getRTTI().isExactly(RegionAttachment::rtti)) { - auto *attachment = dynamic_cast(slot->getAttachment()); -#if CC_USE_SPINE_3_8 - attachmentVertices = static_cast(attachment->getRendererObject()); -#else - attachmentVertices = static_cast(attachment->getRegion()->rendererObject); + if (tmpAttachment->getRTTI().isExactly(RegionAttachment::rtti)) { + auto *attachment = dynamic_cast(tmpAttachment); +#if CC_USE_SPINE_4_2 + if (!attachment->getRegion()) { + attachment->getSequence()->apply(slot, attachment); + if (attachment->getRegion()) { + attachmentVertices = generateAttachmentVertices(attachment); + if (attachmentVertices) { + attachmentVerticesMap[attachment] = attachmentVertices; + } + } + } #endif // Early exit if attachment is invisible @@ -362,12 +384,18 @@ void SkeletonCache::renderAnimationFrame(AnimationData *animationData) { attachmentColor = attachment->getColor(); - } else if (slot->getAttachment()->getRTTI().isExactly(MeshAttachment::rtti)) { - auto *attachment = dynamic_cast(slot->getAttachment()); -#if CC_USE_SPINE_3_8 - attachmentVertices = static_cast(attachment->getRendererObject()); -#else - attachmentVertices = static_cast(attachment->getRegion()->rendererObject); + } else if (tmpAttachment->getRTTI().isExactly(MeshAttachment::rtti)) { + auto *attachment = dynamic_cast(tmpAttachment); +#if CC_USE_SPINE_4_2 + if (!attachment->getRegion()) { + attachment->getSequence()->apply(slot, attachment); + if (attachment->getRegion()) { + attachmentVertices = generateAttachmentVertices(attachment); + if (attachmentVertices) { + attachmentVerticesMap[attachment] = attachmentVertices; + } + } + } #endif // Early exit if attachment is invisible @@ -391,8 +419,8 @@ void SkeletonCache::renderAnimationFrame(AnimationData *animationData) { trianglesTwoColor.indices = reinterpret_cast(ib.getCurBuffer()); memcpy(trianglesTwoColor.indices, attachmentVertices->_triangles->indices, ibSize); attachmentColor = attachment->getColor(); - } else if (slot->getAttachment()->getRTTI().isExactly(ClippingAttachment::rtti)) { - auto *clip = dynamic_cast(slot->getAttachment()); + } else if (tmpAttachment->getRTTI().isExactly(ClippingAttachment::rtti)) { + auto *clip = dynamic_cast(tmpAttachment); _clipper->clipStart(*slot, clip); continue; } else { diff --git a/native/cocos/editor-support/spine-creator-support/SkeletonDataMgr.cpp b/native/cocos/editor-support/spine-creator-support/SkeletonDataMgr.cpp index 45bd512d4f..828f4115b7 100644 --- a/native/cocos/editor-support/spine-creator-support/SkeletonDataMgr.cpp +++ b/native/cocos/editor-support/spine-creator-support/SkeletonDataMgr.cpp @@ -35,9 +35,48 @@ using namespace spine; //NOLINT using namespace cc; //NOLINT + +static uint16_t quadTriangles[6] = {0, 1, 2, 2, 3, 0}; + +extern "C" AttachmentVertices *generateAttachmentVertices(Attachment *attachment) { + AttachmentVertices *attachmentVertices = nullptr; + if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) { + auto *regionAttachMent = static_cast(attachment); +#if CC_USE_SPINE_3_8 + auto *region = static_cast(regionAttachMent->getRendererObject()); + attachmentVertices = new AttachmentVertices(static_cast(region->page->getRendererObject()), 4, quadTriangles, 6); +#else + auto *region = static_cast(regionAttachMent->getRegion()); + if (!region) return nullptr; + attachmentVertices = new AttachmentVertices(static_cast(region->page->texture), 4, quadTriangles, 6); +#endif + auto *vertices = attachmentVertices->_triangles->verts; + for (int i = 0, ii = 0; i < 4; ++i, ii += 2) { + vertices[i].texCoord.u = regionAttachMent->getUVs()[ii]; + vertices[i].texCoord.v = regionAttachMent->getUVs()[ii + 1]; + } + } else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) { + auto *meshAttachMent = static_cast(attachment); +#if CC_USE_SPINE_3_8 + auto *region = static_cast(meshAttachMent->getRendererObject()); + attachmentVertices = new AttachmentVertices(static_cast(region->page->getRendererObject()), + static_cast(meshAttachMent->getWorldVerticesLength() >> 1), meshAttachMent->getTriangles().buffer(), static_cast(meshAttachMent->getTriangles().size())); +#else + auto *region = static_cast(meshAttachMent->getRegion()); + if (!region) return nullptr; + attachmentVertices = new AttachmentVertices(static_cast(region->page->texture), + static_cast(meshAttachMent->getWorldVerticesLength() >> 1), meshAttachMent->getTriangles().buffer(), static_cast(meshAttachMent->getTriangles().size())); +#endif + auto *vertices = attachmentVertices->_triangles->verts; + for (size_t i = 0, ii = 0, nn = meshAttachMent->getWorldVerticesLength(); ii < nn; ++i, ii += 2) { + vertices[i].texCoord.u = meshAttachMent->getUVs()[ii]; + vertices[i].texCoord.v = meshAttachMent->getUVs()[ii + 1]; + } + } + return attachmentVertices; +} + namespace cc { - - class SkeletonDataInfo { public: SkeletonDataInfo() = default; @@ -49,24 +88,6 @@ public: } if (atlas) { -#if CC_USE_SPINE_4_2 - auto ®ions = atlas->getRegions(); - auto size = regions.size(); - for (int i = 0; i < size; i++) { - auto *region = regions[i]; - if (region->rendererObject) { - auto *map = static_cast *>(region->rendererObject); - auto entries = map->getEntries(); - while (entries.hasNext()) { - auto entry = entries.next(); - auto *attachmentVertices = entry.value; - delete attachmentVertices; - } - delete map; - region->rendererObject = nullptr; - } - } -#endif delete atlas; atlas = nullptr; } @@ -75,14 +96,40 @@ public: delete attachmentLoader; attachmentLoader = nullptr; } + + for (const auto& pair : attachmentVerticesMap) { + delete pair.second; + } } SkeletonData *data = nullptr; Atlas *atlas = nullptr; AttachmentLoader *attachmentLoader = nullptr; std::vector texturesIndex; + std::unordered_map attachmentVerticesMap; }; + +void saveAttachmentVertices(SkeletonDataInfo *info) { + auto &attachmentVerticesMap = info->attachmentVerticesMap; + auto &skins = info->data->getSkins(); + auto skinSize = skins.size(); + for (int i = 0; i < skinSize; ++i) { + auto *skin = skins[i]; + auto entries = skin->getAttachments(); + while (entries.hasNext()) { + Skin::AttachmentMap::Entry &entry = entries.next(); + auto *attachment = entry._attachment; + if (attachmentVerticesMap.count(attachment) < 1) { + auto *attachmentVertices = generateAttachmentVertices(attachment); + if (attachmentVertices) { + attachmentVerticesMap[attachment] = attachmentVertices; + } + } + } + } +} + } // namespace cc SkeletonDataMgr *SkeletonDataMgr::instance = nullptr; @@ -111,6 +158,17 @@ void SkeletonDataMgr::setSkeletonData(const std::string &uuid, SkeletonData *dat info->attachmentLoader = attachmentLoader; info->texturesIndex = texturesIndex; _dataMap[uuid] = info; + + saveAttachmentVertices(info); +} + +std::unordered_map + *SkeletonDataMgr::getSkeletonDataInfo(const std::string &uuid) { + auto dataIt = _dataMap.find(uuid); + if (dataIt == _dataMap.end()) { + return nullptr; + } + return &dataIt->second->attachmentVerticesMap; } SkeletonData *SkeletonDataMgr::retainByUUID(const std::string &uuid) { diff --git a/native/cocos/editor-support/spine-creator-support/SkeletonDataMgr.h b/native/cocos/editor-support/spine-creator-support/SkeletonDataMgr.h index 8fdba3615a..e871486af1 100644 --- a/native/cocos/editor-support/spine-creator-support/SkeletonDataMgr.h +++ b/native/cocos/editor-support/spine-creator-support/SkeletonDataMgr.h @@ -33,6 +33,7 @@ #include #include #include +#include #include "base/RefCounted.h" #include "spine/SkeletonData.h" #include "spine/spine.h" @@ -41,6 +42,7 @@ using namespace spine; namespace cc { class SkeletonDataInfo; +class AttachmentVertices; /** * Cache skeleton data. @@ -71,6 +73,9 @@ public: // equal to 'deleteByUUID' void releaseByUUID(const std::string &uuid); + std::unordered_map + *getSkeletonDataInfo(const std::string &uuid); + using destroyCallback = std::function; void setDestroyCallback(destroyCallback callback) { _destroyCallback = std::move(callback); @@ -81,5 +86,4 @@ private: destroyCallback _destroyCallback = nullptr; std::map _dataMap; }; - } // namespace cc diff --git a/native/cocos/editor-support/spine-creator-support/SkeletonRenderer.cpp b/native/cocos/editor-support/spine-creator-support/SkeletonRenderer.cpp index 9c3a878cf7..b8628ce0f6 100644 --- a/native/cocos/editor-support/spine-creator-support/SkeletonRenderer.cpp +++ b/native/cocos/editor-support/spine-creator-support/SkeletonRenderer.cpp @@ -44,6 +44,7 @@ #include "spine-creator-support/AttachmentVertices.h" #include "spine-creator-support/spine-cocos2dx.h" + USING_NS_MW; // NOLINT(google-build-using-namespace) using namespace spine; // NOLINT(google-build-using-namespace) using namespace cc; // NOLINT(google-build-using-namespace) @@ -64,6 +65,7 @@ enum DebugType { BONES }; +extern "C" AttachmentVertices *generateAttachmentVertices(Attachment *attachment); namespace cc { SkeletonRenderer *SkeletonRenderer::create() { return new SkeletonRenderer(); @@ -410,6 +412,9 @@ void SkeletonRenderer::render(float /*deltaTime*/) { void *effect = nullptr; #endif + auto *verticesMap = SkeletonDataMgr::getInstance()->getSkeletonDataInfo(_uuid); + if (!verticesMap) return; + auto &attachmentVerticesMap = *verticesMap; auto &drawOrder = _skeleton->getDrawOrder(); for (size_t i = 0, n = drawOrder.size(); i < n; ++i) { isFull = 0; @@ -440,7 +445,12 @@ void SkeletonRenderer::render(float /*deltaTime*/) { cc::middleware::Triangles triangles; cc::middleware::TwoColorTriangles trianglesTwoColor; - attachmentVertices = nullptr; + auto iterAttachment = attachmentVerticesMap.find(tmpAttachment); + if (iterAttachment != attachmentVerticesMap.end()) { + attachmentVertices = iterAttachment->second; + } else { + attachmentVertices = nullptr; + } auto iter = _slotTextureSet.find(slot); if (iter != _slotTextureSet.end()) { @@ -449,14 +459,17 @@ void SkeletonRenderer::render(float /*deltaTime*/) { } if (tmpAttachment->getRTTI().isExactly(RegionAttachment::rtti)) { auto *attachment = dynamic_cast(tmpAttachment); - if (!attachmentVertices) { -#if CC_USE_SPINE_3_8 - attachmentVertices = reinterpret_cast(attachment->getRendererObject()); -#else - auto *tmpMap = static_cast *>(attachment->getRegion()->rendererObject); - attachmentVertices = (*tmpMap)[attachment]; -#endif +#if CC_USE_SPINE_4_2 + if (!attachment->getRegion()) { + attachment->getSequence()->apply(slot, attachment); + if (attachment->getRegion()) { + attachmentVertices = generateAttachmentVertices(attachment); + if (attachmentVertices) { + attachmentVerticesMap[attachment] = attachmentVertices; + } + } } +#endif // Early exit if attachment is invisible if (attachment->getColor().a == 0) { @@ -473,6 +486,7 @@ void SkeletonRenderer::render(float /*deltaTime*/) { #if CC_USE_SPINE_3_8 attachment->computeWorldVertices(slot->getBone(), reinterpret_cast(triangles.verts), 0, vs1); #else + loopUVCoords(triangles.verts, attachment->getUVs(), triangles.vertCount); attachment->computeWorldVertices(*slot, reinterpret_cast(triangles.verts), 0, vs1); #endif @@ -486,12 +500,13 @@ void SkeletonRenderer::render(float /*deltaTime*/) { vbSize = trianglesTwoColor.vertCount * sizeof(V3F_T2F_C4B_C4B); isFull |= vb.checkSpace(vbSize, true); trianglesTwoColor.verts = reinterpret_cast(vb.getCurBuffer()); +#if CC_USE_SPINE_3_8 for (int ii = 0; ii < trianglesTwoColor.vertCount; ii++) { trianglesTwoColor.verts[ii].texCoord = attachmentVertices->_triangles->verts[ii].texCoord; } -#if CC_USE_SPINE_3_8 attachment->computeWorldVertices(slot->getBone(), reinterpret_cast(trianglesTwoColor.verts), 0, vs2); #else + loopUVCoords(trianglesTwoColor.verts, attachment->getUVs(), trianglesTwoColor.vertCount); attachment->computeWorldVertices(*slot, reinterpret_cast(trianglesTwoColor.verts), 0, vs2); #endif @@ -521,14 +536,17 @@ void SkeletonRenderer::render(float /*deltaTime*/) { } } else if (tmpAttachment->getRTTI().isExactly(MeshAttachment::rtti)) { auto *attachment = dynamic_cast(tmpAttachment); - if (!attachmentVertices) { -#if CC_USE_SPINE_3_8 - attachmentVertices = static_cast(attachment->getRendererObject()); -#else - auto *tmpMap = static_cast *>(attachment->getRegion()->rendererObject); - attachmentVertices = (*tmpMap)[attachment]; -#endif +#if CC_USE_SPINE_4_2 + if (!attachment->getRegion()) { + attachment->getSequence()->apply(slot, attachment); + if (attachment->getRegion()) { + attachmentVertices = generateAttachmentVertices(attachment); + if (attachmentVertices) { + attachmentVerticesMap[attachment] = attachmentVertices; + } + } } +#endif // Early exit if attachment is invisible if (attachment->getColor().a == 0) { @@ -542,6 +560,9 @@ void SkeletonRenderer::render(float /*deltaTime*/) { isFull |= vb.checkSpace(vbSize, true); triangles.verts = reinterpret_cast(vb.getCurBuffer()); memcpy(static_cast(triangles.verts), static_cast(attachmentVertices->_triangles->verts), vbSize); +#ifdef CC_USE_SPINE_4_2 + loopUVCoords(triangles.verts, attachment->getUVs(), triangles.vertCount); +#endif attachment->computeWorldVertices(*slot, 0, attachment->getWorldVerticesLength(), reinterpret_cast(triangles.verts), 0, vs1); triangles.indexCount = attachmentVertices->_triangles->indexCount; @@ -554,9 +575,13 @@ void SkeletonRenderer::render(float /*deltaTime*/) { vbSize = trianglesTwoColor.vertCount * sizeof(V3F_T2F_C4B_C4B); isFull |= vb.checkSpace(vbSize, true); trianglesTwoColor.verts = reinterpret_cast(vb.getCurBuffer()); +#ifdef CC_USE_SPINE_3_8 for (int ii = 0; ii < trianglesTwoColor.vertCount; ii++) { trianglesTwoColor.verts[ii].texCoord = attachmentVertices->_triangles->verts[ii].texCoord; } +#else + loopUVCoords(trianglesTwoColor.verts, attachment->getUVs(), trianglesTwoColor.vertCount); +#endif attachment->computeWorldVertices(*slot, 0, attachment->getWorldVerticesLength(), reinterpret_cast(trianglesTwoColor.verts), 0, vs2); trianglesTwoColor.indexCount = attachmentVertices->_triangles->indexCount; @@ -1135,12 +1160,15 @@ void SkeletonRenderer::setSlotTexture(const std::string &slotName, cc::Texture2D if (!attachment) return; auto width = tex2d->getWidth(); auto height = tex2d->getHeight(); + auto *verticesMap = SkeletonDataMgr::getInstance()->getSkeletonDataInfo(_uuid); + if (!verticesMap) return; + auto &attachmentVerticesMap = *verticesMap; + auto *attachmentVertices = attachmentVerticesMap.at(attachment); if (createAttachment) { attachment = attachment->copy(); } SlotCacheInfo slotCacheInfo{createAttachment, attachment, nullptr}; - AttachmentVertices *attachmentVertices = nullptr; if (attachment->getRTTI().isExactly(spine::RegionAttachment::rtti)) { auto region = static_cast(attachment); #if CC_USE_SPINE_3_8 @@ -1152,7 +1180,6 @@ void SkeletonRenderer::setSlotTexture(const std::string &slotName, cc::Texture2D region->setHeight(height); region->setUVs(0, 0, 1.0f, 1.0f, false); region->updateOffset(); - attachmentVertices = static_cast(region->getRendererObject()); if (createAttachment) { attachmentVertices = attachmentVertices->copy(); slotCacheInfo.attachmentVertices = attachmentVertices; @@ -1182,8 +1209,6 @@ void SkeletonRenderer::setSlotTexture(const std::string &slotName, cc::Texture2D uvs[0] = 1; uvs[1] = 1; region->updateRegion(); - auto *tmpMap = static_cast *>(textureRegion->rendererObject); - attachmentVertices = (*tmpMap)[slot->getAttachment()]; if (createAttachment) { attachmentVertices = attachmentVertices->copy(); slotCacheInfo.attachmentVertices = attachmentVertices; @@ -1211,7 +1236,6 @@ void SkeletonRenderer::setSlotTexture(const std::string &slotName, cc::Texture2D mesh->setRegionRotate(true); mesh->setRegionDegrees(0); mesh->updateUVs(); - attachmentVertices = static_cast(mesh->getRendererObject()); if (createAttachment) { attachmentVertices = attachmentVertices->copy(); slotCacheInfo.attachmentVertices = attachmentVertices; @@ -1232,8 +1256,6 @@ void SkeletonRenderer::setSlotTexture(const std::string &slotName, cc::Texture2D mesh->setWidth(width); mesh->setHeight(height); mesh->updateRegion(); - auto *tmpMap = static_cast *>(mesh->getRegion()->rendererObject); - attachmentVertices = (*tmpMap)[slot->getAttachment()]; if (createAttachment) { attachmentVertices = attachmentVertices->copy(); slotCacheInfo.attachmentVertices = attachmentVertices; diff --git a/native/cocos/editor-support/spine-creator-support/SkeletonRenderer.h b/native/cocos/editor-support/spine-creator-support/SkeletonRenderer.h index 74768c3d3c..96c3129787 100644 --- a/native/cocos/editor-support/spine-creator-support/SkeletonRenderer.h +++ b/native/cocos/editor-support/spine-creator-support/SkeletonRenderer.h @@ -50,6 +50,15 @@ class Material; class AttachmentVertices; + +template +void loopUVCoords(VertexType *tmp, const UVArrayType &uvs, int count) { + for (int i = 0, ii = 0; i < count; ++i, ii += 2) { + tmp[i].texCoord.u = uvs[ii]; + tmp[i].texCoord.v = uvs[ii + 1]; + } +} + struct SlotCacheInfo { bool isOwner{false}; spine::Attachment *attachment{nullptr}; diff --git a/native/cocos/editor-support/spine-creator-support/spine-cocos2dx.cpp b/native/cocos/editor-support/spine-creator-support/spine-cocos2dx.cpp index fe9da47113..10a9d2aebd 100644 --- a/native/cocos/editor-support/spine-creator-support/spine-cocos2dx.cpp +++ b/native/cocos/editor-support/spine-creator-support/spine-cocos2dx.cpp @@ -49,83 +49,13 @@ USING_NS_MW; // NOLINT(google-build-using-namespace) using namespace cc; // NOLINT(google-build-using-namespace) using namespace spine; // NOLINT(google-build-using-namespace) -#if CC_USE_SPINE_3_8 -static void deleteAttachmentVertices(void *vertices) { - delete static_cast(vertices); -} -#endif - -static uint16_t quadTriangles[6] = {0, 1, 2, 2, 3, 0}; - -#if CC_USE_SPINE_4_2 -/** -* The relationship between ​​AtlasRegion​​ and ​​AttachmentVertices​​ is ​​one-to-many​​. -*/ -static void saveAttachmentVertices(Attachment *attachment, AtlasRegion *region, AttachmentVertices *attachmentVertices) { - spine::HashMap *map = nullptr; - if (region->rendererObject && region->rendererObject != region->page->texture) { - map = static_cast *>(region->rendererObject); - } else { - map = new spine::HashMap(); - region->rendererObject = map; - } - map->put(attachment, attachmentVertices); -} -#endif - -static void setAttachmentVertices(RegionAttachment *attachment) { -#if CC_USE_SPINE_3_8 - auto *region = static_cast(attachment->getRendererObject()); - auto *attachmentVertices = new AttachmentVertices(static_cast(region->page->getRendererObject()), 4, quadTriangles, 6); -#else - auto *region = static_cast(attachment->getRegion()); - auto *attachmentVertices = new AttachmentVertices(static_cast(region->page->texture), 4, quadTriangles, 6); -#endif - V3F_T2F_C4B *vertices = attachmentVertices->_triangles->verts; - for (int i = 0, ii = 0; i < 4; ++i, ii += 2) { - vertices[i].texCoord.u = attachment->getUVs()[ii]; - vertices[i].texCoord.v = attachment->getUVs()[ii + 1]; - } -#if CC_USE_SPINE_3_8 - attachment->setRendererObject(attachmentVertices, deleteAttachmentVertices); -#else - saveAttachmentVertices(attachment, region, attachmentVertices); -#endif -} - -static void setAttachmentVertices(MeshAttachment *attachment) { -#if CC_USE_SPINE_3_8 - auto *region = static_cast(attachment->getRendererObject()); - auto *attachmentVertices = new AttachmentVertices(static_cast(region->page->getRendererObject()), - static_cast(attachment->getWorldVerticesLength() >> 1), attachment->getTriangles().buffer(), static_cast(attachment->getTriangles().size())); -#else - auto *region = static_cast(attachment->getRegion()); - auto *attachmentVertices = new AttachmentVertices(static_cast(region->page->texture), - static_cast(attachment->getWorldVerticesLength() >> 1), attachment->getTriangles().buffer(), static_cast(attachment->getTriangles().size())); -#endif - V3F_T2F_C4B *vertices = attachmentVertices->_triangles->verts; - for (size_t i = 0, ii = 0, nn = attachment->getWorldVerticesLength(); ii < nn; ++i, ii += 2) { - vertices[i].texCoord.u = attachment->getUVs()[ii]; - vertices[i].texCoord.v = attachment->getUVs()[ii + 1]; - } -#if CC_USE_SPINE_3_8 - attachment->setRendererObject(attachmentVertices, deleteAttachmentVertices); -#else - saveAttachmentVertices(attachment, region, attachmentVertices); -#endif -} - Cocos2dAtlasAttachmentLoader::Cocos2dAtlasAttachmentLoader(Atlas *atlas) : AtlasAttachmentLoader(atlas) { } Cocos2dAtlasAttachmentLoader::~Cocos2dAtlasAttachmentLoader() = default; void Cocos2dAtlasAttachmentLoader::configureAttachment(Attachment *attachment) { - if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) { - setAttachmentVertices(dynamic_cast(attachment)); - } else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) { - setAttachmentVertices(dynamic_cast(attachment)); - } + // } uint32_t wrap(TextureWrap wrap) { diff --git a/native/cocos/editor-support/spine-wasm/AtlasAttachmentLoaderExtension.cpp b/native/cocos/editor-support/spine-wasm/AtlasAttachmentLoaderExtension.cpp index 4182b3a333..8fb5b806cb 100644 --- a/native/cocos/editor-support/spine-wasm/AtlasAttachmentLoaderExtension.cpp +++ b/native/cocos/editor-support/spine-wasm/AtlasAttachmentLoaderExtension.cpp @@ -3,19 +3,6 @@ //#include "LogUtil.h" using namespace spine; -#include -#include -using namespace spine; - -static void wasmLog(const char* message) { - EM_ASM({ - console.log(UTF8ToString($0)); - }, message); -} - - -static const uint16_t quadTriangles[6] = {0, 1, 2, 2, 3, 0}; - AttachmentVertices::AttachmentVertices(int verticesCount, uint16_t *triangles, int trianglesCount, const spine::String& textureName) { _triangles = new Triangles(); _triangles->verts = new V3F_T2F_C4B[verticesCount]; @@ -37,74 +24,10 @@ AttachmentVertices *AttachmentVertices::copy() { return atv; } -#ifdef CC_SPINE_VERSION_3_8 -static void deleteAttachmentVertices(void *vertices) { - delete static_cast(vertices); -} -#else -/** -* The relationship between ​​AtlasRegion​​ and ​​AttachmentVertices​​ is ​​one-to-many​​. -*/ -static void saveAttachmentVertices(Attachment *attachment, AtlasRegion *region, AttachmentVertices *attachmentVertices) { - spine::HashMap *map = nullptr; - if (region->rendererObject && region->rendererObject != region->page->texture) { - map = static_cast *>(region->rendererObject); - } else { - map = new spine::HashMap(); - region->rendererObject = map; - } - map->put(attachment, attachmentVertices); -} -#endif - AtlasAttachmentLoaderExtension::AtlasAttachmentLoaderExtension(Atlas *atlas) : AtlasAttachmentLoader(atlas), _atlasCache(atlas) { } AtlasAttachmentLoaderExtension::~AtlasAttachmentLoaderExtension() = default; void AtlasAttachmentLoaderExtension::configureAttachment(Attachment *attachment) { - if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) { - auto *regionAttachment = static_cast(attachment); - auto &pages = _atlasCache->getPages(); -#ifdef CC_SPINE_VERSION_3_8 - auto *region = static_cast(regionAttachment->getRendererObject()); -#else - auto *region = static_cast(regionAttachment->getRegion()); -#endif - auto *attachmentVertices = new AttachmentVertices(4, const_cast(quadTriangles), 6, region->page->name); - V3F_T2F_C4B *vertices = attachmentVertices->_triangles->verts; - const auto &uvs = regionAttachment->getUVs(); - for (int i = 0, ii = 0; i < 4; ++i, ii += 2) { - vertices[i].texCoord.u = uvs[ii]; - vertices[i].texCoord.v = uvs[ii + 1]; - } -#ifdef CC_SPINE_VERSION_3_8 - regionAttachment->setRendererObject(attachmentVertices, deleteAttachmentVertices); -#else - saveAttachmentVertices(attachment, region, attachmentVertices); -#endif - } else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) { - auto *meshAttachment = static_cast(attachment); - auto &pages = _atlasCache->getPages(); -#ifdef CC_SPINE_VERSION_3_8 - auto *region = static_cast(meshAttachment->getRendererObject()); -#else - auto *region = static_cast(meshAttachment->getRegion()); -#endif - auto *attachmentVertices = new AttachmentVertices( - static_cast(meshAttachment->getWorldVerticesLength() >> 1), meshAttachment->getTriangles().buffer(), static_cast(meshAttachment->getTriangles().size()), region->page->name); - V3F_T2F_C4B *vertices = attachmentVertices->_triangles->verts; - const auto &uvs = meshAttachment->getUVs(); - for (size_t i = 0, ii = 0, nn = meshAttachment->getWorldVerticesLength(); ii < nn; ++i, ii += 2) { - vertices[i].texCoord.u = uvs[ii]; - vertices[i].texCoord.v = uvs[ii + 1]; - } -#ifdef CC_SPINE_VERSION_3_8 - meshAttachment->setRendererObject(attachmentVertices, deleteAttachmentVertices); -#else - saveAttachmentVertices(attachment, region, attachmentVertices); -#endif - } else { - wasmLog(attachment->getName().buffer()); - } } \ No newline at end of file diff --git a/native/cocos/editor-support/spine-wasm/spine-skeleton-instance.cpp b/native/cocos/editor-support/spine-wasm/spine-skeleton-instance.cpp index 1ebe25ba07..b4abfaae7c 100644 --- a/native/cocos/editor-support/spine-wasm/spine-skeleton-instance.cpp +++ b/native/cocos/editor-support/spine-wasm/spine-skeleton-instance.cpp @@ -9,8 +9,81 @@ SlotMesh globalMesh(nullptr, nullptr, 0, 0); extern "C" { extern void spineListenerCallBackFromJS(); extern void spineTrackListenerCallback(); +AttachmentVertices* generateAttachmentVertices(spine::Attachment* attachment); } + using namespace spine; +extern HashMap*> spineAttachmentVerticesMap; +extern HashMap*> spineTexturesMap; + + +template +void loopUVCoords(VertexType* tmp, const UVArrayType& uvs, int count) { + for (int i = 0, ii = 0; i < count; ++i, ii += 2) { + tmp[i].texCoord.u = uvs[ii]; + tmp[i].texCoord.v = uvs[ii + 1]; + } +} + +#ifdef CC_SPINE_VERSION_4_2 +template +void setSpineTextureID(MeshT& currMesh, + AttachmentT* attachment, + VerticesT* vertices, + TexMapT* texMap) { + if (auto* region = static_cast(attachment->getRegion())) { + if (region->page && region->page->name != vertices->_textureName) { + currMesh.textureID = (*texMap)[region->page->name]; + return; + } + } + currMesh.textureID = vertices->_textureUUID; +} + +template +void initAttachmentVertices(VerticesT*& vertices, + AttachmentT* attachment, + SlotT* slot, + TexMapT* texMap, + VertMapT* vertMap) +{ + if (!vertices && !attachment->getRegion()) { + attachment->getSequence()->apply(slot, attachment); + if (attachment->getRegion()) { + vertices = generateAttachmentVertices(attachment); + if (texMap->containsKey(vertices->_textureName)) { + vertices->_textureUUID = (*texMap)[vertices->_textureName]; + } + vertMap->put(attachment, vertices); + } + } +} +#else +template +void setSpineTextureID(MeshT& currMesh, + AttachmentT*, + VerticesT* vertices, + TexMapT*) +{ + //do nothing + currMesh.textureID = vertices->_textureUUID; +} + +template +void initAttachmentVertices(VerticesT*&, AttachmentT*, SlotT*, TexMapT*, VertMapT*) +{ + //do nothing +} +#endif + static void animationCallback(AnimationState *state, EventType type, TrackEntry *entry, Event *event) { SpineSkeletonInstance *instance = (static_cast(state->getRendererObject())); @@ -177,6 +250,14 @@ void SpineSkeletonInstance::collectMeshData() { #else void* _effect = nullptr; #endif +#ifdef CC_SPINE_VERSION_4_2 + if (!spineTexturesMap.containsKey(_skeletonData)) return; + auto* texturesMap = spineTexturesMap[_skeletonData]; +#else + HashMap *texturesMap = nullptr; +#endif + if (!spineAttachmentVerticesMap.containsKey(_skeletonData)) return; + auto* attachmentVerticesMap = spineAttachmentVerticesMap[_skeletonData]; const Color& skeletonColor = _skeleton->getColor(); for (uint32_t drawIdx = 0; drawIdx < slotCount; ++drawIdx) { auto* slot = slotArray[drawIdx]; @@ -195,25 +276,24 @@ void SpineSkeletonInstance::collectMeshData() { color.a = _userData.color.a; spine::Attachment* attachmentSlot = slot->getAttachment(); AttachmentVertices *cacheSlotAttachmentVertices = nullptr; + AttachmentVertices *attachmentVertices = nullptr; + if (attachmentVerticesMap->containsKey(attachmentSlot)) { + attachmentVertices = (*attachmentVerticesMap)[attachmentSlot]; + } if (_userData.useSlotTexture && _slotTextureSet.containsKey(slot)) { auto info = _slotTextureSet[slot]; attachmentSlot = info.attachment; cacheSlotAttachmentVertices = info.attachmentVertices; } + if (cacheSlotAttachmentVertices) { + attachmentVertices = cacheSlotAttachmentVertices; + } const spine::RTTI& attachmentRTTI = attachmentSlot->getRTTI(); if (attachmentRTTI.isExactly(spine::RegionAttachment::rtti)) { debugShapeType = DEBUG_SHAPE_TYPE::DEBUG_REGION; auto *attachment = static_cast(attachmentSlot); - auto *attachmentVertices = cacheSlotAttachmentVertices; - if (!attachmentVertices) { -#ifdef CC_SPINE_VERSION_3_8 - attachmentVertices = reinterpret_cast(attachment->getRendererObject()); -#else - auto *tmpMap = static_cast *>(attachment->getRegion()->rendererObject); - attachmentVertices = (*tmpMap)[slot->getAttachment()]; -#endif - } - + initAttachmentVertices(attachmentVertices, attachment, slot, texturesMap, attachmentVerticesMap); + setSpineTextureID(currMesh, attachment, attachmentVertices, texturesMap); auto *triangles = attachmentVertices->_triangles; auto vertCount = triangles->vertCount; auto indexCount = triangles->indexCount; @@ -222,14 +302,23 @@ void SpineSkeletonInstance::collectMeshData() { auto vbSize = vertCount * byteStrideColor; auto *vertices = SpineMeshData::queryVBuffer(); auto *indices = SpineMeshData::queryIBuffer(); - if (!_userData.useTint) { memcpy(static_cast(vertices), static_cast(triangles->verts), vbSize); +#ifdef CC_SPINE_VERSION_4_2 + const auto& uvs = attachment->getUVs(); + V3F_T2F_C4B* tmp = (V3F_T2F_C4B *)(vertices); + loopUVCoords(tmp, uvs, 4); + #endif } else { V3F_T2F_C4B_C4B *verts = (V3F_T2F_C4B_C4B *)vertices; - for (int ii = 0; ii < vertCount; ii++) { - verts[ii].texCoord = triangles->verts[ii].texCoord; - } +#ifdef CC_SPINE_VERSION_4_2 + const auto& uvs = attachment->getUVs(); + loopUVCoords(verts, uvs, vertCount); +#else + for (int ii = 0; ii < vertCount; ii++) { + verts[ii].texCoord = triangles->verts[ii].texCoord; + } +#endif } memcpy(indices, triangles->indices, ibSize); #ifdef CC_SPINE_VERSION_3_8 @@ -243,20 +332,11 @@ void SpineSkeletonInstance::collectMeshData() { color.g *= attachmentColor.g; color.b *= attachmentColor.b; color.a *= attachmentColor.a; - currMesh.textureID = attachmentVertices->_textureUUID; } else if (attachmentRTTI.isExactly(spine::MeshAttachment::rtti)) { debugShapeType = DEBUG_SHAPE_TYPE::DEBUG_MESH; auto *attachment = static_cast(attachmentSlot); - auto *attachmentVertices = cacheSlotAttachmentVertices; - if (!attachmentVertices) { -#ifdef CC_SPINE_VERSION_3_8 - attachmentVertices = static_cast(attachment->getRendererObject()); -#else - auto *tmpMap = static_cast *>(attachment->getRegion()->rendererObject); - attachmentVertices = (*tmpMap)[slot->getAttachment()]; -#endif - } - + initAttachmentVertices(attachmentVertices, attachment, slot, texturesMap, attachmentVerticesMap); + setSpineTextureID(currMesh, attachment, attachmentVertices, texturesMap); auto *triangles = attachmentVertices->_triangles; auto vertCount = triangles->vertCount; auto indexCount = triangles->indexCount; @@ -265,13 +345,25 @@ void SpineSkeletonInstance::collectMeshData() { auto vbSize = vertCount * byteStrideColor; auto *vertices = SpineMeshData::queryVBuffer(); auto *indices = SpineMeshData::queryIBuffer(); + bool isRegionChanged = currMesh.textureID != attachmentVertices->_textureUUID; if (!_userData.useTint) { memcpy(static_cast(vertices), static_cast(triangles->verts), vbSize); +#ifdef CC_SPINE_VERSION_4_2 + // Calling 'attachment->computeWorldVertices()' can alter the UV coordinates. + const auto& uvs = attachment->getUVs(); + V3F_T2F_C4B* tmp = (V3F_T2F_C4B *)(vertices); + loopUVCoords(tmp, uvs, 4); +#endif } else { V3F_T2F_C4B_C4B *verts = (V3F_T2F_C4B_C4B *)vertices; +#ifdef CC_SPINE_VERSION_4_2 + const auto& uvs = attachment->getUVs(); + loopUVCoords(verts, uvs, vertCount); +#else for (int ii = 0; ii < vertCount; ii++) { verts[ii].texCoord = triangles->verts[ii].texCoord; } +#endif } memcpy(indices, triangles->indices, ibSize); attachment->computeWorldVertices(*slot, 0, attachment->getWorldVerticesLength(), (float *)vertices, 0, strideColor); @@ -281,7 +373,6 @@ void SpineSkeletonInstance::collectMeshData() { color.g *= attachmentColor.g; color.b *= attachmentColor.b; color.a *= attachmentColor.a; - currMesh.textureID = attachmentVertices->_textureUUID; } else if (attachmentRTTI.isExactly(spine::ClippingAttachment::rtti)) { auto *clip = static_cast(attachmentSlot); _clipper->clipStart(*slot, clip); @@ -530,6 +621,11 @@ void SpineSkeletonInstance::onAnimationStateEvent(TrackEntry *entry, EventType t void SpineSkeletonInstance::resizeSlotRegion(const spine::String &slotName, uint32_t width, uint32_t height, bool createNew) { if (!_skeleton) return; + HashMap *attachmentVerticesMap = nullptr; + if (spineAttachmentVerticesMap.containsKey(_skeletonData)) { + attachmentVerticesMap = spineAttachmentVerticesMap[_skeletonData]; + } + if (!attachmentVerticesMap) return; auto* slot = _skeleton->findSlot(slotName); if (!slot) return; auto *attachment = slot->getAttachment(); @@ -551,11 +647,6 @@ void SpineSkeletonInstance::resizeSlotRegion(const spine::String &slotName, uint region->setHeight(height); region->setUVs(0, 0, 1.0f, 1.0f, false); region->updateOffset(); - auto *attachmentVertices = static_cast(region->getRendererObject()); - if (createNew) { - attachmentVertices = attachmentVertices->copy(); - info.attachmentVertices = attachmentVertices; - } #else auto *textureRegion = region->getRegion(); if (textureRegion) { @@ -581,13 +672,17 @@ void SpineSkeletonInstance::resizeSlotRegion(const spine::String &slotName, uint uvs[0] = 1; uvs[1] = 1; region->updateRegion(); - auto *tmpMap = static_cast *>(textureRegion->rendererObject); - auto *attachmentVertices = (*tmpMap)[slot->getAttachment()]; +#endif + + AttachmentVertices *attachmentVertices = nullptr; + if (attachmentVerticesMap->containsKey(attachment)) { + attachmentVertices = (*attachmentVerticesMap)[attachment]; + } + if (!attachmentVertices) return; if (createNew) { attachmentVertices = attachmentVertices->copy(); info.attachmentVertices = attachmentVertices; } -#endif V3F_T2F_C4B *vertices = attachmentVertices->_triangles->verts; auto &UVs = region->getUVs(); for (int i = 0, ii = 0; i < 4; ++i, ii += 2) { @@ -610,11 +705,6 @@ void SpineSkeletonInstance::resizeSlotRegion(const spine::String &slotName, uint mesh->setRegionRotate(true); mesh->setRegionDegrees(0); mesh->updateUVs(); - auto *attachmentVertices = static_cast(mesh->getRendererObject()); - if (createNew) { - attachmentVertices = attachmentVertices->copy(); - info.attachmentVertices = attachmentVertices; - } #else auto *region = mesh->getRegion(); if (region) { @@ -631,13 +721,17 @@ void SpineSkeletonInstance::resizeSlotRegion(const spine::String &slotName, uint mesh->setWidth(width); mesh->setHeight(height); mesh->updateRegion(); - auto *tmpMap = static_cast *>(mesh->getRegion()->rendererObject); - auto *attachmentVertices = (*tmpMap)[slot->getAttachment()]; +#endif + + AttachmentVertices *attachmentVertices = nullptr; + if (attachmentVerticesMap->containsKey(attachment)) { + attachmentVertices = (*attachmentVerticesMap)[attachment]; + } + if (!attachmentVertices) return; if (createNew) { attachmentVertices = attachmentVertices->copy(); info.attachmentVertices = attachmentVertices; } -#endif V3F_T2F_C4B *vertices = attachmentVertices->_triangles->verts; const auto &UVs = mesh->getUVs(); for (size_t i = 0, ii = 0, nn = mesh->getWorldVerticesLength(); ii < nn; ++i, ii += 2) { diff --git a/native/cocos/editor-support/spine-wasm/spine-wasm.cpp b/native/cocos/editor-support/spine-wasm/spine-wasm.cpp index e5dd2b69ac..1d4daa3d22 100644 --- a/native/cocos/editor-support/spine-wasm/spine-wasm.cpp +++ b/native/cocos/editor-support/spine-wasm/spine-wasm.cpp @@ -11,6 +11,50 @@ using namespace spine; +HashMap*> spineAttachmentVerticesMap{}; +HashMap*> spineTexturesMap{}; + + +static const uint16_t quadTriangles[6] = {0, 1, 2, 2, 3, 0}; + +extern "C" AttachmentVertices* generateAttachmentVertices(Attachment* attachment) { + AttachmentVertices* attachmentVertices = nullptr; + if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) { + auto* regionAttachment = static_cast(attachment); +#ifdef CC_SPINE_VERSION_3_8 + auto* region = static_cast(regionAttachment->getRendererObject()); +#else + auto* region = static_cast(regionAttachment->getRegion()); +#endif + if (!region) return nullptr; + attachmentVertices = new AttachmentVertices(4, const_cast(quadTriangles), 6, region->page->name); + V3F_T2F_C4B* vertices = attachmentVertices->_triangles->verts; + const auto& uvs = regionAttachment->getUVs(); + for (int i = 0, ii = 0; i < 4; ++i, ii += 2) { + vertices[i].texCoord.u = uvs[ii]; + vertices[i].texCoord.v = uvs[ii + 1]; + } + + } else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) { + auto* meshAttachment = static_cast(attachment); +#ifdef CC_SPINE_VERSION_3_8 + auto* region = static_cast(meshAttachment->getRendererObject()); +#else + auto* region = static_cast(meshAttachment->getRegion()); +#endif + if (!region) return nullptr; + attachmentVertices = new AttachmentVertices( + static_cast(meshAttachment->getWorldVerticesLength() >> 1), meshAttachment->getTriangles().buffer(), static_cast(meshAttachment->getTriangles().size()), region->page->name); + V3F_T2F_C4B* vertices = attachmentVertices->_triangles->verts; + const auto& uvs = meshAttachment->getUVs(); + for (size_t i = 0, ii = 0, nn = meshAttachment->getWorldVerticesLength(); ii < nn; ++i, ii += 2) { + vertices[i].texCoord.u = uvs[ii]; + vertices[i].texCoord.v = uvs[ii + 1]; + } + } + return attachmentVertices; +} + namespace { const int LOG_LEVEL_ERROR = 3; const int LOG_LEVEL_WARN = 2; @@ -34,11 +78,31 @@ void logToConsole(const char* message, int logLevel = LOG_LEVEL_INFO) { HashMap skeletonDataMap{}; -void updateAttachmentVerticesTextureId(SkeletonData* skeletonData, const spine::Vector& textureNames, const spine::Vector& textureUUIDs) { +void saveAttachmentVertices(SkeletonData* skeletonData, const spine::Vector& textureNames, const spine::Vector& textureUUIDs) { spine::HashMap textureMap{}; + spine::HashMap* texturesMap = nullptr; +#ifdef CC_SPINE_VERSION_3_8 + // Attachment can not switch pages in 3.8 + texturesMap = &textureMap; +#else + if (!spineTexturesMap.containsKey(skeletonData)) { + texturesMap = new HashMap(); + spineTexturesMap.put(skeletonData, texturesMap); + } else { + texturesMap = spineTexturesMap[skeletonData]; + } +#endif + int textureSize = textureNames.size(); for (int i = 0; i < textureSize; ++i) { - textureMap.put(textureNames[i], textureUUIDs[i]); + texturesMap->put(textureNames[i], textureUUIDs[i]); + } + HashMap* attachmentVerticesMap = nullptr; + if (!spineAttachmentVerticesMap.containsKey(skeletonData)) { + attachmentVerticesMap = new HashMap(); + spineAttachmentVerticesMap.put(skeletonData, attachmentVerticesMap); + } else { + attachmentVerticesMap = spineAttachmentVerticesMap[skeletonData]; } auto& skins = skeletonData->getSkins(); @@ -50,28 +114,19 @@ void updateAttachmentVerticesTextureId(SkeletonData* skeletonData, const spine:: Skin::AttachmentMap::Entry& entry = entries.next(); AttachmentVertices* attachmentVertices = nullptr; auto* attachment = entry._attachment; - spine::HashMap *map = nullptr; - if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) { - auto* meshAttachment = static_cast(attachment); -#ifdef CC_SPINE_VERSION_3_8 - attachmentVertices = static_cast(meshAttachment->getRendererObject()); -#else - map = static_cast *>(meshAttachment->getRegion()->rendererObject); - attachmentVertices = (*map)[attachment]; -#endif - } else if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) { - auto* regionAttachment = static_cast(attachment); -#ifdef CC_SPINE_VERSION_3_8 - attachmentVertices = static_cast(regionAttachment->getRendererObject()); -#else - map = static_cast *>(regionAttachment->getRegion()->rendererObject); - attachmentVertices = (*map)[attachment]; -#endif + if (attachmentVerticesMap->containsKey(attachment)) { + attachmentVertices = (*attachmentVerticesMap)[attachment]; + } else { + attachmentVertices = generateAttachmentVertices(attachment); + if (attachmentVertices) { + attachmentVerticesMap->put(attachment, attachmentVertices); + } } + if (attachmentVertices) { auto& textureName = attachmentVertices->_textureName; - if (textureMap.containsKey(textureName)) { - attachmentVertices->_textureUUID = textureMap[textureName]; + if (texturesMap->containsKey(textureName)) { + attachmentVertices->_textureUUID = (*texturesMap)[textureName]; } else { spine::String logInfo(textureName); logInfo.append(" attachment's texture doesn`t exist "); @@ -134,7 +189,7 @@ SkeletonData* SpineWasmUtil::createSpineSkeletonDataWithJson(const String& jsonS logToConsole(errorMsg.buffer(), LOG_LEVEL_WARN); } - updateAttachmentVerticesTextureId(skeletonData, textureNames, textureUUIDs); + saveAttachmentVertices(skeletonData, textureNames, textureUUIDs); return skeletonData; #else @@ -161,7 +216,7 @@ SkeletonData* SpineWasmUtil::createSpineSkeletonDataWithBinary(uint32_t byteSize logToConsole(errorMsg.buffer(), LOG_LEVEL_WARN); } - updateAttachmentVerticesTextureId(skeletonData, textureNames, textureUUIDs); + saveAttachmentVertices(skeletonData, textureNames, textureUUIDs); return skeletonData; #else @@ -178,38 +233,19 @@ void SpineWasmUtil::registerSpineSkeletonDataWithUUID(SkeletonData* data, const void SpineWasmUtil::destroySpineSkeletonDataWithUUID(const String& uuid) { if (skeletonDataMap.containsKey(uuid)) { auto* data = skeletonDataMap[uuid]; -#if CC_USE_SPINE_4_2 - auto& skins = data->getSkins(); - auto skinSize = skins.size(); - // release AttachmentVertices - for (int i = 0; i < skinSize; ++i) { - auto* skin = skins[i]; - auto entries = skin->getAttachments(); + HashMap *attachmentVerticesMap = nullptr; + if (spineAttachmentVerticesMap.containsKey(data)) { + attachmentVerticesMap = spineAttachmentVerticesMap[data]; + auto entries = attachmentVerticesMap->getEntries(); while (entries.hasNext()) { - Skin::AttachmentMap::Entry& entry = entries.next(); - auto* attachment = entry._attachment; - spine::HashMap *map = nullptr; - if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) { - auto* meshAttachment = static_cast(attachment); - map = static_cast *>(meshAttachment->getRegion()->rendererObject); - meshAttachment->getRegion()->rendererObject = nullptr; - } else if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) { - auto* regionAttachment = static_cast(attachment); - map = static_cast *>(regionAttachment->getRegion()->rendererObject); - regionAttachment->getRegion()->rendererObject = nullptr; - } - if (map) { - auto attachmentVerticesEntries = map->getEntries(); - while (attachmentVerticesEntries.hasNext()) { - auto entryTmp = attachmentVerticesEntries.next(); - auto *attachmentVertices = entryTmp.value; - delete attachmentVertices; - } - delete map; - } + auto entry = entries.next(); + auto* attachmentVertices = entry.value; + delete attachmentVertices; } + delete attachmentVerticesMap; + + spineAttachmentVerticesMap.remove(data); } -#endif delete data; skeletonDataMap.remove(uuid); } diff --git a/native/external-config.json b/native/external-config.json index 880eede7fb..04ee6f3925 100644 --- a/native/external-config.json +++ b/native/external-config.json @@ -3,6 +3,6 @@ "type": "github", "owner": "cocos", "name": "cocos-engine-external", - "checkout": "v3.8.7-14" + "checkout": "v3.8.7-15" } } \ No newline at end of file diff --git a/native/tools/swig-config/spine_3_8.i b/native/tools/swig-config/spine_3_8.i index 5bc330def1..5c29aab186 100644 --- a/native/tools/swig-config/spine_3_8.i +++ b/native/tools/swig-config/spine_3_8.i @@ -115,6 +115,7 @@ using namespace spine; %ignore cc::SkeletonDataMgr::setSkeletonData; %ignore cc::SkeletonDataMgr::retainByUUID; %ignore cc::SkeletonDataMgr::releaseByUUID; +%ignore cc::SkeletonDataMgr::getSkeletonDataInfo; %ignore cc::SkeletonCacheAnimation::render; %ignore cc::SkeletonCacheAnimation::requestDrawInfo; %ignore cc::SkeletonCacheAnimation::requestMaterial; diff --git a/native/tools/swig-config/spine_4_2.i b/native/tools/swig-config/spine_4_2.i index 6d3b389bab..31b632acc9 100644 --- a/native/tools/swig-config/spine_4_2.i +++ b/native/tools/swig-config/spine_4_2.i @@ -113,6 +113,7 @@ using namespace spine; %ignore cc::SkeletonDataMgr::setSkeletonData; %ignore cc::SkeletonDataMgr::retainByUUID; %ignore cc::SkeletonDataMgr::releaseByUUID; +%ignore cc::SkeletonDataMgr::getSkeletonDataInfo; %ignore cc::SkeletonCacheAnimation::render; %ignore cc::SkeletonCacheAnimation::requestDrawInfo; %ignore cc::SkeletonCacheAnimation::requestMaterial;