Skip to content

Commit

Permalink
refine
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeng-song committed Dec 5, 2024
1 parent 67d84e3 commit 595cb0c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 24 deletions.
3 changes: 0 additions & 3 deletions cocos/spine/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,6 @@ export class Skeleton extends UIRenderer {
this._vBuffer = null;
this._iBuffer = null;
this.attachUtil.reset();
//this._textures.length = 0;
this._slotTextures?.clear();
this._slotTextures = null;
this._cachedSockets.clear();
this._socketNodes.clear();
//if (this._cacheMode == SpineAnimationCacheMode.PRIVATE_CACHE) this._animCache?.destroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using namespace spine;

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

AttachmentVertices::AttachmentVertices(int verticesCount, uint16_t *triangles, int trianglesCount, spine::String textureId) {
AttachmentVertices::AttachmentVertices(int verticesCount, uint16_t *triangles, int trianglesCount, const spine::String& textureId) {
_triangles = new Triangles();
_triangles->verts = new V3F_T2F_C4B[verticesCount];
_triangles->vertCount = verticesCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

class AttachmentVertices {
public:
AttachmentVertices(int verticesCount, uint16_t *triangles, int trianglesCount, spine::String textureId);
AttachmentVertices(int verticesCount, uint16_t *triangles, int trianglesCount, const spine::String& textureId);
virtual ~AttachmentVertices();
AttachmentVertices *copy();
Triangles *_triangles = nullptr;
spine::String _textureId{""};
spine::String _textureId;
};

class AtlasAttachmentLoaderExtension : public spine::AtlasAttachmentLoader {
Expand Down
2 changes: 1 addition & 1 deletion native/cocos/editor-support/spine-wasm/spine-model.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SlotMesh {
uint32_t vCount{};
uint32_t iCount{};
uint32_t blendMode{};
spine::String textureID{""};
spine::String textureID;
};

class SpineModel {
Expand Down
8 changes: 4 additions & 4 deletions native/cocos/editor-support/spine-wasm/spine-type-export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1387,8 +1387,8 @@ EMSCRIPTEN_BINDINGS(cocos_spine) {
.class_function("querySpineSkeletonDataByUUID", &SpineWasmUtil::querySpineSkeletonDataByUUID, allow_raw_pointers())
.class_function("createSpineSkeletonDataWithJson", optional_override([](String jsonStr, String atlasStr, emscripten::val nameArray, emscripten::val uuidArray){
unsigned count = nameArray["length"].as<unsigned>();
Vector<String> names = Vector<String>();
Vector<String> ids = Vector<String>();
Vector<String> names;
Vector<String> ids;
names.setSize(count, "");
ids.setSize(count, "");
for (int i = 0; i < count; i++) {
Expand All @@ -1399,8 +1399,8 @@ EMSCRIPTEN_BINDINGS(cocos_spine) {
}), allow_raw_pointers())
.class_function("createSpineSkeletonDataWithBinary", optional_override([](uint32_t byteSize, String atlasStr, emscripten::val nameArray, emscripten::val uuidArray){
unsigned count = nameArray["length"].as<unsigned>();
Vector<String> names = Vector<String>();
Vector<String> ids = Vector<String>();
Vector<String> names;
Vector<String> ids;
names.setSize(count, "");
ids.setSize(count, "");
for (int i = 0; i < count; i++) {
Expand Down
36 changes: 25 additions & 11 deletions native/cocos/editor-support/spine-wasm/spine-wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@
#include "util-function.h"
#include "wasmSpineExtension.h"

#include <map>
#include <emscripten/emscripten.h>
#include <emscripten/val.h>

#include "spine/HashMap.h"

using namespace spine;

static void logToConsole(const char* message) {
EM_ASM({
console.log(UTF8ToString($0));
}, message);
}

namespace {
HashMap<String, SkeletonData*> skeletonDataMap{};

static void updateAttachmentVerticesTextureId(SkeletonData* skeletonData, spine::Vector<spine::String>& textureNames, spine::Vector<spine::String>& textureUUIDs) {
std::map<std::string, std::string> textureMap;
static void updateAttachmentVerticesTextureId(SkeletonData* skeletonData, const spine::Vector<spine::String>& textureNames, const spine::Vector<spine::String>& textureUUIDs) {
spine::HashMap<spine::String, spine::String> textureMap{};
int textureSize = textureNames.size();
for (int i = 0; i < textureSize; ++i) {
textureMap[textureNames[i].buffer()] = textureUUIDs[i].buffer();
textureMap.put(textureNames[i], textureUUIDs[i]);
}

auto& skins = skeletonData->getSkins();
Expand All @@ -24,7 +33,7 @@ namespace {
auto* skin = skins[i];
auto entries = skin->getAttachments();
while (entries.hasNext()) {
Skin::AttachmentMap::Entry entry = entries.next();
Skin::AttachmentMap::Entry& entry = entries.next();
AttachmentVertices* attachmentVertices;
auto* attachment = entry._attachment;
if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) {
Expand All @@ -34,10 +43,15 @@ namespace {
auto* regionAttachment = static_cast<RegionAttachment *>(attachment);
attachmentVertices = static_cast<AttachmentVertices*>(regionAttachment->getRendererObject());
}
auto* textureName = attachmentVertices->_textureId.buffer();
auto iter = textureMap.find(textureName);
if (iter == textureMap.end()) continue;
attachmentVertices->_textureId = String(iter->second.c_str());
auto& textureName = attachmentVertices->_textureId;
if (textureMap.containsKey(textureName)) {
attachmentVertices->_textureId = textureMap[textureName];
} else {
spine::String logInfo(attachment->getName());
logInfo.append(" attachment's texture is not exist ");
logInfo.append(textureName);
logToConsole(logInfo.buffer());
}
}
}
}
Expand Down Expand Up @@ -75,7 +89,7 @@ SkeletonData* SpineWasmUtil::querySpineSkeletonDataByUUID(const String& uuid) {
return skeletonDataMap[uuid];
}

SkeletonData* SpineWasmUtil::createSpineSkeletonDataWithJson(const String& jsonStr, const String& altasStr, spine::Vector<spine::String>& textureNames, spine::Vector<spine::String>& textureUUIDs) {
SkeletonData* SpineWasmUtil::createSpineSkeletonDataWithJson(const String& jsonStr, const String& altasStr, const spine::Vector<spine::String>& textureNames, const spine::Vector<spine::String>& textureUUIDs) {
#if ENABLE_JSON_PARSER
auto* atlas = new Atlas(altasStr.buffer(), altasStr.length(), "", nullptr, false);
if (!atlas) {
Expand All @@ -94,7 +108,7 @@ SkeletonData* SpineWasmUtil::createSpineSkeletonDataWithJson(const String& jsonS
#endif
}

SkeletonData* SpineWasmUtil::createSpineSkeletonDataWithBinary(uint32_t byteSize, const String& altasStr, spine::Vector<spine::String>& textureNames, spine::Vector<spine::String>& textureUUIDs) {
SkeletonData* SpineWasmUtil::createSpineSkeletonDataWithBinary(uint32_t byteSize, const String& altasStr, const spine::Vector<spine::String>& textureNames, const spine::Vector<spine::String>& textureUUIDs) {
#if ENABLE_BINARY_PARSER
auto* atlas = new Atlas(altasStr.buffer(), altasStr.length(), "", nullptr, false);
if (!atlas) {
Expand Down
4 changes: 2 additions & 2 deletions native/cocos/editor-support/spine-wasm/spine-wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class SpineWasmUtil {
static void freeStoreMemory();

static spine::SkeletonData* querySpineSkeletonDataByUUID(const spine::String& uuid);
static spine::SkeletonData* createSpineSkeletonDataWithJson(const spine::String& jsonStr, const spine::String& altasStr, spine::Vector<spine::String>& textureNames, spine::Vector<spine::String>& textureUUIDs);
static spine::SkeletonData* createSpineSkeletonDataWithBinary(uint32_t byteSize, const spine::String& altasStr, spine::Vector<spine::String>& textureNames, spine::Vector<spine::String>& textureUUIDs);
static spine::SkeletonData* createSpineSkeletonDataWithJson(const spine::String& jsonStr, const spine::String& altasStr, const spine::Vector<spine::String>& textureNames, const spine::Vector<spine::String>& textureUUIDs);
static spine::SkeletonData* createSpineSkeletonDataWithBinary(uint32_t byteSize, const spine::String& altasStr, const spine::Vector<spine::String>& textureNames, const spine::Vector<spine::String>& textureUUIDs);
static void registerSpineSkeletonDataWithUUID(spine::SkeletonData* data, const spine::String& uuid);
static void destroySpineSkeletonDataWithUUID(const spine::String& uuid);
static void destroySpineSkeleton(spine::Skeleton* skeleton);
Expand Down

0 comments on commit 595cb0c

Please sign in to comment.