Skip to content

Commit

Permalink
optimize set slot texture api.
Browse files Browse the repository at this point in the history
  • Loading branch information
Canvasfull committed Jul 10, 2023
1 parent b6bd839 commit f0db7b7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 42 deletions.
27 changes: 4 additions & 23 deletions native/cocos/editor-support/spine-wasm/spine-skeleton-instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,9 @@ void SpineSkeletonInstance::collectMeshData() {
}

currMesh.blendMode = static_cast<uint32_t>(slot->getData().getBlendMode());
if (_userData.useSlotTexture) {
uint32_t textureID = findSlotTextureID(slot);
if(textureID == 10000) textureID = slot->getData().hash;
currMesh.textureID = textureID;
} else {
currMesh.textureID = slot->getData().hash;
}

currMesh.textureID = slot->getData().hash;

_model->addSlotMesh(currMesh);

_clipper->clipEnd(*slot);
Expand Down Expand Up @@ -537,24 +533,9 @@ void SpineSkeletonInstance::resizeSlotRegion(const std::string& slotName, uint32
}
}

uint32_t SpineSkeletonInstance::findSlotTextureID(Slot* slot) {
auto iter = slotTextureSet.find(slot);
if (iter != slotTextureSet.end()) {
return iter->second;
} else {
return 10000;
}
}

void SpineSkeletonInstance::setSlotTexture(const std::string& slotName, uint32_t textureID) {
if (!_skeleton) return;
auto slot = _skeleton->findSlot(slotName.c_str());
if (!slot) return;
_userData.useSlotTexture = true;
auto iter = slotTextureSet.find(slot);
if (iter != slotTextureSet.end()) {
iter->second = textureID;
} else {
slotTextureSet[slot] = textureID;
}
slot->getData().hash = textureID;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class SpineSkeletonInstance {
bool useTint = false;
bool premultipliedAlpha = false;
bool debugMode = false;
bool useSlotTexture = false;
Color4F color = Color4F(1.0F, 1.0F, 1.0F, 1.0F);
};

Expand Down Expand Up @@ -65,7 +64,6 @@ class SpineSkeletonInstance {

private:
void collectMeshData();
uint32_t findSlotTextureID(Slot* slot);
private:
Skeleton *_skeleton = nullptr;
SkeletonData *_skeletonData = nullptr;
Expand All @@ -82,7 +80,6 @@ class SpineSkeletonInstance {
uint32_t _eventListenerID = 0;
UserData _userData;
std::vector<SpineDebugShape> _debugShapes{};
std::map<Slot*, uint32_t> slotTextureSet{};
};

#endif
29 changes: 14 additions & 15 deletions native/cocos/editor-support/spine-wasm/spine-wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,22 @@ SkeletonData* SpineWasmUtil::createSpineSkeletonDataWithBinary(uint32_t byteSize

void SpineWasmUtil::calculateTextureID(Atlas* atlas, SkeletonData* skeletonData) {

spine::Vector<spine::Skin *> skins = skeletonData->getSkins();
auto& slotArray = skeletonData->getSlots();
const size_t count = slotArray.size();
for (size_t i = 0; i < count; i++) {
slotArray[i]->hash = 60000;
const spine::String attachmentName = slotArray[i]->getAttachmentName();
AtlasRegion* atlasRegion = atlas->findRegion(attachmentName);
if(atlasRegion != NULL) {
slotArray[i]->hash = atlas->getPages().indexOf(atlasRegion->page) + 10000;
}else{
const spine::String name = slotArray[i]->getName();
AtlasRegion* atlasRegion = atlas->findRegion(name);
if(atlasRegion) {
slotArray[i]->hash = atlas->getPages().indexOf(atlasRegion->page) + 10000;
}else{
slotArray[i]->hash = 20000;
}

const size_t count = skins.size();
for (int32_t i = 0; i < count; i++) {

spine::Skin *skin = skins[i];
auto entries = skin->getAttachments();
while (entries.hasNext())
{
auto &entry = entries.next();
AtlasRegion* atlasRegion = atlas->findRegion(entry._name);
if(atlasRegion)
slotArray[entry._slotIndex]->hash = atlas->getPages().indexOf(atlasRegion->page) + 10000;
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/editor-support/spine/SlotData.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class SP_API SlotData : public SpineObject {

int getIndex();

int hash;
uint32_t hash;

const String &getName();

Expand Down

0 comments on commit f0db7b7

Please sign in to comment.