// // RUTextureAttachmentLoader.cpp // red_utils // // Created by RedInfinity on 2024/1/16. // #include "RUTextureAttachmentLoader.hpp" ///第三方库 #include "cocos2d.h" #include "editor-support/spine/AttachmentVertices.h" NS_RU_BEGIN static void deleteAttachmentVertices(void* vertices) { delete static_cast(vertices); } static unsigned short quadTriangles[6] = {0, 1, 2, 2, 3, 0}; TextureAttachmentLoader* TextureAttachmentLoader::_instance = nullptr; TextureAttachmentLoader* TextureAttachmentLoader::getInstance() { if (_instance == nullptr) { _instance = new TextureAttachmentLoader(); } return _instance; } void TextureAttachmentLoader::destoryInstance() { if (_instance) { delete _instance; _instance = nullptr; } } void TextureAttachmentLoader::rebindAttachmentTexture(spine::Attachment* attachment, const spine::String& imageName) { if (attachment->getRTTI().isExactly(spine::RegionAttachment::rtti)) { _rebindRegionAttachmentTexture(static_cast(attachment), imageName); } else if (attachment->getRTTI().isExactly(spine::MeshAttachment::rtti)) { _rebindMeshAttachmentTexture(static_cast(attachment), imageName); } } spine::RegionAttachment* TextureAttachmentLoader::newRegionAttachment(spine::Skin& skin, const spine::String& name, const spine::String& path) { spine::RegionAttachment* ret = nullptr; cocos2d::TextureCache* tc = cocos2d::Director::getInstance()->getTextureCache(); cocos2d::Texture2D* t2d = tc->addImage(path.buffer()); if (t2d) { ret = new spine::RegionAttachment(name); ret->setRendererObject(t2d); ret->setUVs(0, 0, 1, 1, false); ret->setRegionOffsetX(0.0f); ret->setRegionOffsetY(0.0f); const cocos2d::Size& t2dSize = t2d->getContentSizeInPixels(); ret->setRegionWidth(t2dSize.width); ret->setRegionHeight(t2dSize.width); ret->setRegionOriginalWidth(t2dSize.width); ret->setRegionOriginalHeight(t2dSize.width); } return ret; } spine::MeshAttachment* TextureAttachmentLoader::newMeshAttachment(spine::Skin& skin, const spine::String& name, const spine::String& path) { spine::MeshAttachment* ret = nullptr; cocos2d::TextureCache* tc = cocos2d::Director::getInstance()->getTextureCache(); cocos2d::Texture2D* t2d = tc->addImage(path.buffer()); if (t2d) { ret = new spine::MeshAttachment(name); ret->setRendererObject(t2d); ret->setRegionU(0.0f); ret->setRegionV(0.0f); ret->setRegionU2(1.0f); ret->setRegionV2(1.0f); ret->setRegionRotate(false); ret->setRegionDegrees(0); ret->setRegionOffsetX(0.0f); ret->setRegionOffsetY(0.0f); const cocos2d::Size& t2dSize = t2d->getContentSizeInPixels(); ret->setRegionWidth(t2dSize.width); ret->setRegionHeight(t2dSize.width); ret->setRegionOriginalWidth(t2dSize.width); ret->setRegionOriginalHeight(t2dSize.width); } return ret; } spine::BoundingBoxAttachment* TextureAttachmentLoader::newBoundingBoxAttachment(spine::Skin& skin, const spine::String& name) { return new spine::BoundingBoxAttachment(name); } spine::PathAttachment* TextureAttachmentLoader::newPathAttachment(spine::Skin& skin, const spine::String& name) { return new spine::PathAttachment(name); } spine::PointAttachment* TextureAttachmentLoader::newPointAttachment(spine::Skin& skin, const spine::String& name) { return new spine::PointAttachment(name); } spine::ClippingAttachment* TextureAttachmentLoader::newClippingAttachment(spine::Skin& skin, const spine::String& name) { return new spine::ClippingAttachment(name); } void TextureAttachmentLoader::configureAttachment(spine::Attachment* attachment) { if (attachment->getRTTI().isExactly(spine::RegionAttachment::rtti)) { _configureRegionAttachment(static_cast(attachment)); } else if (attachment->getRTTI().isExactly(spine::MeshAttachment::rtti)) { _configureMeshAttachment(static_cast(attachment)); } } void TextureAttachmentLoader::_rebindRegionAttachmentTexture(spine::RegionAttachment* attachment, const spine::String& imageName) { cocos2d::TextureCache* tc = cocos2d::Director::getInstance()->getTextureCache(); cocos2d::Texture2D* t2d = tc->addImage(imageName.buffer()); if (t2d) { attachment->setRendererObject(t2d); attachment->setUVs(0, 0, 1, 1, false); attachment->setRegionOffsetX(0.0f); attachment->setRegionOffsetY(0.0f); const cocos2d::Size& t2dSize = t2d->getContentSizeInPixels(); attachment->setRegionWidth(t2dSize.width); attachment->setRegionHeight(t2dSize.width); attachment->setRegionOriginalWidth(t2dSize.width); attachment->setRegionOriginalHeight(t2dSize.width); attachment->updateOffset(); _configureRegionAttachment(attachment); } } void TextureAttachmentLoader::_rebindMeshAttachmentTexture(spine::MeshAttachment* attachment, const spine::String& imageName) { cocos2d::TextureCache* tc = cocos2d::Director::getInstance()->getTextureCache(); cocos2d::Texture2D* t2d = tc->addImage(imageName.buffer()); if (t2d) { attachment->setRendererObject(t2d); attachment->setRegionU(0.0f); attachment->setRegionV(0.0f); attachment->setRegionU2(1.0f); attachment->setRegionV2(1.0f); attachment->setRegionRotate(false); attachment->setRegionDegrees(0); attachment->setRegionOffsetX(0.0f); attachment->setRegionOffsetY(0.0f); const cocos2d::Size& t2dSize = t2d->getContentSizeInPixels(); attachment->setRegionWidth(t2dSize.width); attachment->setRegionHeight(t2dSize.width); attachment->setRegionOriginalWidth(t2dSize.width); attachment->setRegionOriginalHeight(t2dSize.width); attachment->updateUVs(); _configureMeshAttachment(attachment); } } void TextureAttachmentLoader::_configureRegionAttachment(spine::RegionAttachment* attachment) { cocos2d::Texture2D* t2d = static_cast(attachment->getRendererObject()); spine::AttachmentVertices* attachmentVertices = new spine::AttachmentVertices(t2d, 4, quadTriangles, 6); cocos2d::V3F_C4B_T2F* vertices = attachmentVertices->_triangles->verts; for (int i = 0, ii = 0; i < 4; ++i, ii += 2) { vertices[i].texCoords.u = attachment->getUVs()[ii]; vertices[i].texCoords.v = attachment->getUVs()[ii + 1]; } attachment->setRendererObject(attachmentVertices, deleteAttachmentVertices); } void TextureAttachmentLoader::_configureMeshAttachment(spine::MeshAttachment* attachment) { cocos2d::Texture2D* t2d = static_cast(attachment->getRendererObject()); spine::AttachmentVertices* attachmentVertices = new spine::AttachmentVertices(t2d, attachment->getWorldVerticesLength() >> 1, attachment->getTriangles().buffer(), attachment->getTriangles().size()); cocos2d::V3F_C4B_T2F* vertices = attachmentVertices->_triangles->verts; for (int i = 0, ii = 0, nn = attachment->getWorldVerticesLength(); ii < nn; ++i, ii += 2) { vertices[i].texCoords.u = attachment->getUVs()[ii]; vertices[i].texCoords.v = attachment->getUVs()[ii + 1]; } attachment->setRendererObject(attachmentVertices, deleteAttachmentVertices); } NS_RU_END