Skip to content

Commit 1d3785d

Browse files
authored
Fix display abnormaly due to use wrong AttachementVertices (#18692)
* Fix display abnormaly due to use wrong AttachementVertices * refine * Update external version for spine
1 parent 0015e7e commit 1d3785d

File tree

7 files changed

+80
-22
lines changed

7 files changed

+80
-22
lines changed

native/cocos/editor-support/spine-creator-support/SkeletonDataMgr.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ class SkeletonDataInfo {
5555
for (int i = 0; i < size; i++) {
5656
auto *region = regions[i];
5757
if (region->rendererObject) {
58-
delete static_cast<AttachmentVertices *>(region->rendererObject);
58+
auto *map = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(region->rendererObject);
59+
auto entries = map->getEntries();
60+
while (entries.hasNext()) {
61+
auto entry = entries.next();
62+
auto *attachmentVertices = entry.value;
63+
delete attachmentVertices;
64+
}
65+
delete map;
5966
region->rendererObject = nullptr;
6067
}
6168
}

native/cocos/editor-support/spine-creator-support/SkeletonRenderer.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,8 @@ void SkeletonRenderer::render(float /*deltaTime*/) {
453453
#if CC_USE_SPINE_3_8
454454
attachmentVertices = reinterpret_cast<AttachmentVertices *>(attachment->getRendererObject());
455455
#else
456-
attachmentVertices = reinterpret_cast<AttachmentVertices *>(attachment->getRegion()->rendererObject);
456+
auto *tmpMap = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(attachment->getRegion()->rendererObject);
457+
attachmentVertices = (*tmpMap)[attachment];
457458
#endif
458459
}
459460

@@ -524,7 +525,8 @@ void SkeletonRenderer::render(float /*deltaTime*/) {
524525
#if CC_USE_SPINE_3_8
525526
attachmentVertices = static_cast<AttachmentVertices *>(attachment->getRendererObject());
526527
#else
527-
attachmentVertices = static_cast<AttachmentVertices *>(attachment->getRegion()->rendererObject);
528+
auto *tmpMap = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(attachment->getRegion()->rendererObject);
529+
attachmentVertices = (*tmpMap)[attachment];
528530
#endif
529531
}
530532

@@ -1180,7 +1182,8 @@ void SkeletonRenderer::setSlotTexture(const std::string &slotName, cc::Texture2D
11801182
uvs[0] = 1;
11811183
uvs[1] = 1;
11821184
region->updateRegion();
1183-
attachmentVertices = static_cast<AttachmentVertices *>(textureRegion->rendererObject);
1185+
auto *tmpMap = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(textureRegion->rendererObject);
1186+
attachmentVertices = (*tmpMap)[slot->getAttachment()];
11841187
if (createAttachment) {
11851188
attachmentVertices = attachmentVertices->copy();
11861189
slotCacheInfo.attachmentVertices = attachmentVertices;
@@ -1229,7 +1232,8 @@ void SkeletonRenderer::setSlotTexture(const std::string &slotName, cc::Texture2D
12291232
mesh->setWidth(width);
12301233
mesh->setHeight(height);
12311234
mesh->updateRegion();
1232-
attachmentVertices = static_cast<AttachmentVertices *>(mesh->getRegion()->rendererObject);
1235+
auto *tmpMap = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(mesh->getRegion()->rendererObject);
1236+
attachmentVertices = (*tmpMap)[slot->getAttachment()];
12331237
if (createAttachment) {
12341238
attachmentVertices = attachmentVertices->copy();
12351239
slotCacheInfo.attachmentVertices = attachmentVertices;

native/cocos/editor-support/spine-creator-support/spine-cocos2dx.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,30 @@ USING_NS_MW; // NOLINT(google-build-using-namespace)
4949
using namespace cc; // NOLINT(google-build-using-namespace)
5050
using namespace spine; // NOLINT(google-build-using-namespace)
5151

52+
#if CC_USE_SPINE_3_8
5253
static void deleteAttachmentVertices(void *vertices) {
5354
delete static_cast<AttachmentVertices *>(vertices);
5455
}
56+
#endif
5557

5658
static uint16_t quadTriangles[6] = {0, 1, 2, 2, 3, 0};
5759

60+
#if CC_USE_SPINE_4_2
61+
/**
62+
* The relationship between ​​AtlasRegion​​ and ​​AttachmentVertices​​ is ​​one-to-many​​.
63+
*/
64+
static void saveAttachmentVertices(Attachment *attachment, AtlasRegion *region, AttachmentVertices *attachmentVertices) {
65+
spine::HashMap<Attachment *, AttachmentVertices *> *map = nullptr;
66+
if (region->rendererObject && region->rendererObject != region->page->texture) {
67+
map = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(region->rendererObject);
68+
} else {
69+
map = new spine::HashMap<Attachment *, AttachmentVertices *>();
70+
region->rendererObject = map;
71+
}
72+
map->put(attachment, attachmentVertices);
73+
}
74+
#endif
75+
5876
static void setAttachmentVertices(RegionAttachment *attachment) {
5977
#if CC_USE_SPINE_3_8
6078
auto *region = static_cast<AtlasRegion *>(attachment->getRendererObject());
@@ -71,7 +89,7 @@ static void setAttachmentVertices(RegionAttachment *attachment) {
7189
#if CC_USE_SPINE_3_8
7290
attachment->setRendererObject(attachmentVertices, deleteAttachmentVertices);
7391
#else
74-
attachment->getRegion()->rendererObject = attachmentVertices;
92+
saveAttachmentVertices(attachment, region, attachmentVertices);
7593
#endif
7694
}
7795

@@ -93,7 +111,7 @@ static void setAttachmentVertices(MeshAttachment *attachment) {
93111
#if CC_USE_SPINE_3_8
94112
attachment->setRendererObject(attachmentVertices, deleteAttachmentVertices);
95113
#else
96-
attachment->getRegion()->rendererObject = attachmentVertices;
114+
saveAttachmentVertices(attachment, region, attachmentVertices);
97115
#endif
98116
}
99117

native/cocos/editor-support/spine-wasm/AtlasAttachmentLoaderExtension.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ AttachmentVertices *AttachmentVertices::copy() {
4141
static void deleteAttachmentVertices(void *vertices) {
4242
delete static_cast<AttachmentVertices *>(vertices);
4343
}
44+
#else
45+
/**
46+
* The relationship between ​​AtlasRegion​​ and ​​AttachmentVertices​​ is ​​one-to-many​​.
47+
*/
48+
static void saveAttachmentVertices(Attachment *attachment, AtlasRegion *region, AttachmentVertices *attachmentVertices) {
49+
spine::HashMap<Attachment *, AttachmentVertices *> *map = nullptr;
50+
if (region->rendererObject && region->rendererObject != region->page->texture) {
51+
map = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(region->rendererObject);
52+
} else {
53+
map = new spine::HashMap<Attachment *, AttachmentVertices *>();
54+
region->rendererObject = map;
55+
}
56+
map->put(attachment, attachmentVertices);
57+
}
4458
#endif
4559

4660
AtlasAttachmentLoaderExtension::AtlasAttachmentLoaderExtension(Atlas *atlas) : AtlasAttachmentLoader(atlas), _atlasCache(atlas) {
@@ -67,7 +81,7 @@ void AtlasAttachmentLoaderExtension::configureAttachment(Attachment *attachment)
6781
#ifdef CC_SPINE_VERSION_3_8
6882
regionAttachment->setRendererObject(attachmentVertices, deleteAttachmentVertices);
6983
#else
70-
regionAttachment->getRegion()->rendererObject = attachmentVertices;
84+
saveAttachmentVertices(attachment, region, attachmentVertices);
7185
#endif
7286
} else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) {
7387
auto *meshAttachment = static_cast<MeshAttachment *>(attachment);
@@ -88,7 +102,7 @@ void AtlasAttachmentLoaderExtension::configureAttachment(Attachment *attachment)
88102
#ifdef CC_SPINE_VERSION_3_8
89103
meshAttachment->setRendererObject(attachmentVertices, deleteAttachmentVertices);
90104
#else
91-
meshAttachment->getRegion()->rendererObject = attachmentVertices;
105+
saveAttachmentVertices(attachment, region, attachmentVertices);
92106
#endif
93107
} else {
94108
wasmLog(attachment->getName().buffer());

native/cocos/editor-support/spine-wasm/spine-skeleton-instance.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ void SpineSkeletonInstance::collectMeshData() {
209209
#ifdef CC_SPINE_VERSION_3_8
210210
attachmentVertices = reinterpret_cast<AttachmentVertices *>(attachment->getRendererObject());
211211
#else
212-
attachmentVertices = reinterpret_cast<AttachmentVertices *>(attachment->getRegion()->rendererObject);
212+
auto *tmpMap = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(attachment->getRegion()->rendererObject);
213+
attachmentVertices = (*tmpMap)[slot->getAttachment()];
213214
#endif
214215
}
215216

@@ -251,7 +252,8 @@ void SpineSkeletonInstance::collectMeshData() {
251252
#ifdef CC_SPINE_VERSION_3_8
252253
attachmentVertices = static_cast<AttachmentVertices *>(attachment->getRendererObject());
253254
#else
254-
attachmentVertices = static_cast<AttachmentVertices *>(attachment->getRegion()->rendererObject);
255+
auto *tmpMap = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(attachment->getRegion()->rendererObject);
256+
attachmentVertices = (*tmpMap)[slot->getAttachment()];
255257
#endif
256258
}
257259

@@ -579,7 +581,8 @@ void SpineSkeletonInstance::resizeSlotRegion(const spine::String &slotName, uint
579581
uvs[0] = 1;
580582
uvs[1] = 1;
581583
region->updateRegion();
582-
auto *attachmentVertices = static_cast<AttachmentVertices *>(region->getRegion()->rendererObject);
584+
auto *tmpMap = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(textureRegion->rendererObject);
585+
auto *attachmentVertices = (*tmpMap)[slot->getAttachment()];
583586
if (createNew) {
584587
attachmentVertices = attachmentVertices->copy();
585588
info.attachmentVertices = attachmentVertices;
@@ -628,7 +631,8 @@ void SpineSkeletonInstance::resizeSlotRegion(const spine::String &slotName, uint
628631
mesh->setWidth(width);
629632
mesh->setHeight(height);
630633
mesh->updateRegion();
631-
auto *attachmentVertices = static_cast<AttachmentVertices *>(mesh->getRegion()->rendererObject);
634+
auto *tmpMap = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(mesh->getRegion()->rendererObject);
635+
auto *attachmentVertices = (*tmpMap)[slot->getAttachment()];
632636
if (createNew) {
633637
attachmentVertices = attachmentVertices->copy();
634638
info.attachmentVertices = attachmentVertices;

native/cocos/editor-support/spine-wasm/spine-wasm.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,24 @@ void updateAttachmentVerticesTextureId(SkeletonData* skeletonData, const spine::
4848
auto entries = skin->getAttachments();
4949
while (entries.hasNext()) {
5050
Skin::AttachmentMap::Entry& entry = entries.next();
51-
AttachmentVertices* attachmentVertices;
51+
AttachmentVertices* attachmentVertices = nullptr;
5252
auto* attachment = entry._attachment;
53+
spine::HashMap<Attachment *, AttachmentVertices *> *map = nullptr;
5354
if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) {
5455
auto* meshAttachment = static_cast<MeshAttachment*>(attachment);
5556
#ifdef CC_SPINE_VERSION_3_8
5657
attachmentVertices = static_cast<AttachmentVertices*>(meshAttachment->getRendererObject());
5758
#else
58-
attachmentVertices = static_cast<AttachmentVertices*>(meshAttachment->getRegion()->rendererObject);
59+
map = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(meshAttachment->getRegion()->rendererObject);
60+
attachmentVertices = (*map)[attachment];
5961
#endif
6062
} else if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) {
6163
auto* regionAttachment = static_cast<RegionAttachment*>(attachment);
6264
#ifdef CC_SPINE_VERSION_3_8
6365
attachmentVertices = static_cast<AttachmentVertices*>(regionAttachment->getRendererObject());
6466
#else
65-
attachmentVertices = static_cast<AttachmentVertices*>(regionAttachment->getRegion()->rendererObject);
67+
map = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(regionAttachment->getRegion()->rendererObject);
68+
attachmentVertices = (*map)[attachment];
6669
#endif
6770
}
6871
if (attachmentVertices) {
@@ -184,17 +187,25 @@ void SpineWasmUtil::destroySpineSkeletonDataWithUUID(const String& uuid) {
184187
auto entries = skin->getAttachments();
185188
while (entries.hasNext()) {
186189
Skin::AttachmentMap::Entry& entry = entries.next();
187-
AttachmentVertices* attachmentVertices;
188190
auto* attachment = entry._attachment;
191+
spine::HashMap<Attachment *, AttachmentVertices *> *map = nullptr;
189192
if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) {
190193
auto* meshAttachment = static_cast<MeshAttachment*>(attachment);
191-
attachmentVertices = static_cast<AttachmentVertices*>(meshAttachment->getRegion()->rendererObject);
194+
map = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(meshAttachment->getRegion()->rendererObject);
195+
meshAttachment->getRegion()->rendererObject = nullptr;
192196
} else if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) {
193197
auto* regionAttachment = static_cast<RegionAttachment*>(attachment);
194-
attachmentVertices = static_cast<AttachmentVertices*>(regionAttachment->getRegion()->rendererObject);
198+
map = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(regionAttachment->getRegion()->rendererObject);
199+
regionAttachment->getRegion()->rendererObject = nullptr;
195200
}
196-
if (attachmentVertices) {
197-
delete attachmentVertices;
201+
if (map) {
202+
auto attachmentVerticesEntries = map->getEntries();
203+
while (attachmentVerticesEntries.hasNext()) {
204+
auto entryTmp = attachmentVerticesEntries.next();
205+
auto *attachmentVertices = entryTmp.value;
206+
delete attachmentVertices;
207+
}
208+
delete map;
198209
}
199210
}
200211
}

native/external-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"type": "github",
44
"owner": "cocos",
55
"name": "cocos-engine-external",
6-
"checkout": "v3.8.7-12"
6+
"checkout": "v3.8.7-13"
77
}
88
}

0 commit comments

Comments
 (0)