diff --git a/cocos/2d/CCSprite.cpp b/cocos/2d/CCSprite.cpp index 7974561baa..04366bd4d1 100644 --- a/cocos/2d/CCSprite.cpp +++ b/cocos/2d/CCSprite.cpp @@ -1029,6 +1029,11 @@ void Sprite::setSpriteFrame(SpriteFrame *spriteFrame) // update rect _rectRotated = spriteFrame->isRotated(); setTextureRect(spriteFrame->getRect(), _rectRotated, spriteFrame->getOriginalSize()); + + if(spriteFrame->hasPolygonInfo()) + { + _polyInfo = spriteFrame->getPolygonInfo(); + } } void Sprite::setDisplayFrameWithAnimationName(const std::string& animationName, ssize_t frameIndex) diff --git a/cocos/2d/CCSpriteFrame.cpp b/cocos/2d/CCSpriteFrame.cpp index 4e6849ab2c..0fc981c32f 100644 --- a/cocos/2d/CCSpriteFrame.cpp +++ b/cocos/2d/CCSpriteFrame.cpp @@ -195,4 +195,19 @@ Texture2D* SpriteFrame::getTexture() return nullptr; } +void SpriteFrame::setPolygonInfo(const PolygonInfo &polygonInfo) +{ + _polygonInfo = polygonInfo; +} + +const PolygonInfo &SpriteFrame::getPolygonInfo() const +{ + return _polygonInfo; +} + +bool SpriteFrame::hasPolygonInfo() const +{ + return _polygonInfo.triangles.vertCount != 0; +} + NS_CC_END diff --git a/cocos/2d/CCSpriteFrame.h b/cocos/2d/CCSpriteFrame.h index 186be7813b..8989431c4a 100644 --- a/cocos/2d/CCSpriteFrame.h +++ b/cocos/2d/CCSpriteFrame.h @@ -29,6 +29,7 @@ THE SOFTWARE. #define __SPRITE_CCSPRITE_FRAME_H__ #include "2d/CCNode.h" +#include "2d/CCAutoPolygon.h" #include "base/CCRef.h" #include "math/CCGeometry.h" @@ -187,7 +188,25 @@ public: // Overrides virtual SpriteFrame *clone() const override; - + + /** Set the polygon info for polygon mesh sprites + * + * @param polygonInfo triangle mesh of teh sprite + */ + void setPolygonInfo(const PolygonInfo &polygonInfo); + + /** Get the polygonInfo for this sprite + * + * @return polygonInfo structure + */ + const PolygonInfo &getPolygonInfo() const; + + /** Check if sprite frame is a polygon sprite + * + * @return true if polygonInfo is available + */ + bool hasPolygonInfo() const; + CC_CONSTRUCTOR_ACCESS: /** * @lua NA @@ -231,6 +250,7 @@ protected: Size _originalSizeInPixels; Texture2D *_texture; std::string _textureFilename; + PolygonInfo _polygonInfo; }; // end of _2d group diff --git a/cocos/2d/CCSpriteFrameCache.cpp b/cocos/2d/CCSpriteFrameCache.cpp index 3588ad2b7c..7bea90a62e 100644 --- a/cocos/2d/CCSpriteFrameCache.cpp +++ b/cocos/2d/CCSpriteFrameCache.cpp @@ -33,6 +33,7 @@ THE SOFTWARE. #include "2d/CCSprite.h" +#include "2d/CCAutoPolygon.h" #include "platform/CCFileUtils.h" #include "base/CCNS.h" #include "base/ccMacros.h" @@ -79,6 +80,63 @@ SpriteFrameCache::~SpriteFrameCache() CC_SAFE_DELETE(_loadedFileNames); } +void SpriteFrameCache::parseIntegerList(const std::string string, std::vector &res) +{ + std::string delim(" "); + + size_t n = std::count(string.begin(), string.end(), ' '); + res.resize(n+1); + + size_t start = 0U; + size_t end = string.find(delim); + + int i=0; + while (end != std::string::npos) + { + res[i++] = std::stoi(string.substr(start, end - start)); + start = end + delim.length(); + end = string.find(delim, start); + } + + res[i] = std::stoi(string.substr(start, end)); +} + +void SpriteFrameCache::initializePolygonInfo(const Size &textureSize, + const Size &spriteSize, + const std::vector &vertices, + const std::vector &verticesUV, + const std::vector &triangleIndices, + PolygonInfo &info) +{ + size_t vertexCount = vertices.size(); + size_t indexCount = triangleIndices.size(); + + float scaleFactor = CC_CONTENT_SCALE_FACTOR(); + + V3F_C4B_T2F *vertexData = new V3F_C4B_T2F[vertexCount]; + for (size_t i = 0; i < vertexCount/2; i++) + { + vertexData[i].colors = Color4B::WHITE; + vertexData[i].vertices = Vec3(vertices[i*2] / scaleFactor, + (spriteSize.height - vertices[i*2+1]) / scaleFactor, + 0); + vertexData[i].texCoords = Tex2F(verticesUV[i*2] / textureSize.width, + verticesUV[i*2+1] / textureSize.height); + } + + unsigned short *indexData = new unsigned short[indexCount]; + for (size_t i = 0; i < indexCount; i++) + { + indexData[i] = static_cast(triangleIndices[i]); + } + + info.triangles.vertCount = vertexCount; + info.triangles.verts = vertexData; + info.triangles.indexCount = indexCount; + info.triangles.indices = indexData; + info.rect = Rect(0, 0, spriteSize.width, spriteSize.height); +} + void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Texture2D* texture) { /* @@ -88,17 +146,26 @@ void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Textu ZWTCoordinatesFormatOptionXML1_0 = 1, // Desktop Version 0.0 - 0.4b ZWTCoordinatesFormatOptionXML1_1 = 2, // Desktop Version 1.0.0 - 1.0.1 ZWTCoordinatesFormatOptionXML1_2 = 3, // Desktop Version 1.0.2+ + + Version 3 with TexturePacker 4.0 polygon mesh packing */ ValueMap& framesDict = dictionary["frames"].asValueMap(); int format = 0; + Size textureSize; + // get the format if (dictionary.find("metadata") != dictionary.end()) { ValueMap& metadataDict = dictionary["metadata"].asValueMap(); format = metadataDict["format"].asInt(); + + if(metadataDict.find("size") != metadataDict.end()) + { + textureSize = SizeFromString(metadataDict["size"].asString()); + } } // check the format @@ -194,6 +261,20 @@ void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Textu textureRotated, spriteOffset, spriteSourceSize); + + if(frameDict.find("vertices") != frameDict.end()) + { + std::vector vertices; + parseIntegerList(frameDict["vertices"].asString(), vertices); + std::vector verticesUV; + parseIntegerList(frameDict["verticesUV"].asString(), verticesUV); + std::vector indices; + parseIntegerList(frameDict["triangles"].asString(), indices); + + PolygonInfo info; + initializePolygonInfo(textureSize, spriteSourceSize, vertices, verticesUV, indices, info); + spriteFrame->setPolygonInfo(info); + } } bool flag = NinePatchImageParser::isNinePatchImage(spriteFrameName); diff --git a/cocos/2d/CCSpriteFrameCache.h b/cocos/2d/CCSpriteFrameCache.h index 74c8f9f2db..0e7a059d1e 100644 --- a/cocos/2d/CCSpriteFrameCache.h +++ b/cocos/2d/CCSpriteFrameCache.h @@ -45,6 +45,7 @@ NS_CC_BEGIN class Sprite; class Texture2D; +class PolygonInfo; /** * @addtogroup _2d @@ -223,6 +224,16 @@ protected: */ void removeSpriteFramesFromDictionary(ValueMap& dictionary); + /** Parses list of space-separated integers */ + void parseIntegerList(const std::string string, std::vector &res); + + /** Configures PolygonInfo class with the passed sizes + triangles */ + void initializePolygonInfo(const Size &textureSize, + const Size &spriteSize, + const std::vector &vertices, + const std::vector &verticesUV, + const std::vector &triangleIndices, + PolygonInfo &polygonInfo); Map _spriteFrames; ValueMap _spriteFramesAliases;