Fixed meshes.

This commit is contained in:
badlogic 2018-05-30 18:31:36 +02:00
parent dc4ab94644
commit 162ce9f5aa
10 changed files with 83 additions and 44 deletions

View File

@ -779,6 +779,7 @@ spAttachment* spSkeletonBinary_readAttachment(spSkeletonBinary* self, _dataInput
}
if (nonessential) readInt(input); /* Skip color. */
if (freeName) FREE(name);
spAttachmentLoader_configureAttachment(self->attachmentLoader, attachment);
return attachment;
}
case SP_ATTACHMENT_POINT: {
@ -791,6 +792,7 @@ spAttachment* spSkeletonBinary_readAttachment(spSkeletonBinary* self, _dataInput
if (nonessential) {
readColor(input, &point->color.r, &point->color.g, &point->color.b, &point->color.a);
}
spAttachmentLoader_configureAttachment(self->attachmentLoader, attachment);
return attachment;
}
case SP_ATTACHMENT_CLIPPING: {

View File

@ -52,7 +52,7 @@ bool BatchingExample::init () {
// This attachment loader configures attachments with data needed for cocos2d-x rendering.
// Do not dispose the attachment loader until the skeleton data is disposed!
_attachmentLoader = new (__FILE__, __LINE__) AtlasAttachmentLoader(_atlas);
_attachmentLoader = new (__FILE__, __LINE__) Cocos2dAtlasAttachmentLoader(_atlas);
// Load the skeleton data.
SkeletonJson* json = new (__FILE__, __LINE__) SkeletonJson(_attachmentLoader);

View File

@ -189,7 +189,7 @@ void SkeletonRenderer::initWithData (SkeletonData* skeletonData, bool ownsSkelet
void SkeletonRenderer::initWithJsonFile (const std::string& skeletonDataFile, Atlas* atlas, float scale) {
_atlas = atlas;
_attachmentLoader = new (__FILE__, __LINE__) AtlasAttachmentLoader(_atlas);
_attachmentLoader = new (__FILE__, __LINE__) Cocos2dAtlasAttachmentLoader(_atlas);
SkeletonJson* json = new (__FILE__, __LINE__) SkeletonJson(_attachmentLoader);
json->setScale(scale);
@ -207,7 +207,7 @@ void SkeletonRenderer::initWithJsonFile (const std::string& skeletonDataFile, co
_atlas = new (__FILE__, __LINE__) Atlas(atlasFile.c_str(), &textureLoader);
CCASSERT(_atlas, "Error reading atlas file.");
_attachmentLoader = new (__FILE__, __LINE__) AtlasAttachmentLoader(_atlas);
_attachmentLoader = new (__FILE__, __LINE__) Cocos2dAtlasAttachmentLoader(_atlas);
SkeletonJson* json = new (__FILE__, __LINE__) SkeletonJson(_attachmentLoader);
json->setScale(scale);
@ -224,7 +224,7 @@ void SkeletonRenderer::initWithJsonFile (const std::string& skeletonDataFile, co
void SkeletonRenderer::initWithBinaryFile (const std::string& skeletonDataFile, Atlas* atlas, float scale) {
_atlas = atlas;
_attachmentLoader = new (__FILE__, __LINE__) AtlasAttachmentLoader(_atlas);
_attachmentLoader = new (__FILE__, __LINE__) Cocos2dAtlasAttachmentLoader(_atlas);
SkeletonBinary* binary = new (__FILE__, __LINE__) SkeletonBinary(_attachmentLoader);
binary->setScale(scale);
@ -241,7 +241,7 @@ void SkeletonRenderer::initWithBinaryFile (const std::string& skeletonDataFile,
_atlas = new (__FILE__, __LINE__) Atlas(atlasFile.c_str(), &textureLoader);
CCASSERT(_atlas, "Error reading atlas file.");
_attachmentLoader = new (__FILE__, __LINE__) AtlasAttachmentLoader(_atlas);
_attachmentLoader = new (__FILE__, __LINE__) Cocos2dAtlasAttachmentLoader(_atlas);
SkeletonBinary* binary = new (__FILE__, __LINE__) SkeletonBinary(_attachmentLoader);
binary->setScale(scale);
@ -260,37 +260,6 @@ void SkeletonRenderer::update (float deltaTime) {
if (_ownsSkeleton) _skeleton->update(deltaTime * _timeScale);
}
static void deleteAttachmentVertices (void* vertices) {
delete (AttachmentVertices *) vertices;
}
static unsigned short quadTriangles[6] = {0, 1, 2, 2, 3, 0};
static void setAttachmentVertices(RegionAttachment* attachment) {
if (!attachment->getRendererObject()) {
AtlasRegion* region = (AtlasRegion*)attachment->getRendererObject();
AttachmentVertices* attachmentVertices = new AttachmentVertices((Texture2D*)region->page->getRendererObject(), 4, quadTriangles, 6);
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);
}
}
static void setAttachmentVertices(MeshAttachment* attachment) {
AtlasRegion* region = (AtlasRegion*)attachment->getRendererObject();
AttachmentVertices* attachmentVertices = new AttachmentVertices((Texture2D*)region->page->getRendererObject(),
attachment->getWorldVerticesLength() >> 1, attachment->getTriangles().buffer(), attachment->getTriangles().size());
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);
}
void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t transformFlags) {
SkeletonBatch* batch = SkeletonBatch::getInstance();
SkeletonTwoColorBatch* twoColorBatch = SkeletonTwoColorBatch::getInstance();
@ -336,7 +305,6 @@ void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t
if (slot->getAttachment()->getRTTI().isExactly(RegionAttachment::rtti)) {
RegionAttachment* attachment = (RegionAttachment*)slot->getAttachment();
setAttachmentVertices(attachment);
attachmentVertices = (AttachmentVertices*)attachment->getRendererObject();
if (!isTwoColorTint) {
@ -364,7 +332,6 @@ void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t
}
else if (slot->getAttachment()->getRTTI().isExactly(MeshAttachment::rtti)) {
MeshAttachment* attachment = (MeshAttachment*)slot->getAttachment();
setAttachmentVertices(attachment);
attachmentVertices = (AttachmentVertices*)attachment->getRendererObject();
if (!isTwoColorTint) {
@ -373,7 +340,7 @@ void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t
triangles.verts = batch->allocateVertices(attachmentVertices->_triangles->vertCount);
triangles.vertCount = attachmentVertices->_triangles->vertCount;
memcpy(triangles.verts, attachmentVertices->_triangles->verts, sizeof(cocos2d::V3F_C4B_T2F) * attachmentVertices->_triangles->vertCount);
attachment->computeWorldVertices(*slot, 0, triangles.vertCount * sizeof(cocos2d::V3F_C4B_T2F) / 4, (float*)triangles.verts, 0, 6);
attachment->computeWorldVertices(*slot, 0, attachment->getWorldVerticesLength(), (float*)triangles.verts, 0, 6);
} else {
trianglesTwoColor.indices = attachmentVertices->_triangles->indices;
trianglesTwoColor.indexCount = attachmentVertices->_triangles->indexCount;
@ -382,7 +349,7 @@ void SkeletonRenderer::draw (Renderer* renderer, const Mat4& transform, uint32_t
for (int i = 0; i < trianglesTwoColor.vertCount; i++) {
trianglesTwoColor.verts[i].texCoords = attachmentVertices->_triangles->verts[i].texCoords;
}
attachment->computeWorldVertices(*slot, 0, trianglesTwoColor.vertCount * sizeof(V3F_C4B_C4B_T2F) / 4, (float*)trianglesTwoColor.verts, 0, 7);
attachment->computeWorldVertices(*slot, 0, attachment->getWorldVerticesLength(), (float*)trianglesTwoColor.verts, 0, 7);
}
color.r = attachment->getColor().r;

View File

@ -35,6 +35,48 @@
USING_NS_CC;
using namespace spine;
static void deleteAttachmentVertices (void* vertices) {
delete (AttachmentVertices *) vertices;
}
static unsigned short quadTriangles[6] = {0, 1, 2, 2, 3, 0};
static void setAttachmentVertices(RegionAttachment* attachment) {
AtlasRegion* region = (AtlasRegion*)attachment->getRendererObject();
AttachmentVertices* attachmentVertices = new AttachmentVertices((Texture2D*)region->page->getRendererObject(), 4, quadTriangles, 6);
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);
}
static void setAttachmentVertices(MeshAttachment* attachment) {
AtlasRegion* region = (AtlasRegion*)attachment->getRendererObject();
AttachmentVertices* attachmentVertices = new AttachmentVertices((Texture2D*)region->page->getRendererObject(),
attachment->getWorldVerticesLength() >> 1, attachment->getTriangles().buffer(), attachment->getTriangles().size());
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);
}
Cocos2dAtlasAttachmentLoader::Cocos2dAtlasAttachmentLoader(Atlas* atlas): AtlasAttachmentLoader(atlas) {
}
Cocos2dAtlasAttachmentLoader::~Cocos2dAtlasAttachmentLoader() { }
void Cocos2dAtlasAttachmentLoader::configureAttachment(Attachment* attachment) {
if (attachment->getRTTI().isExactly(RegionAttachment::rtti)) {
setAttachmentVertices((RegionAttachment*)attachment);
} else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) {
setAttachmentVertices((MeshAttachment*)attachment);
}
}
GLuint wrap (TextureWrap wrap) {
return wrap == TextureWrap_ClampToEdge ? GL_CLAMP_TO_EDGE : GL_REPEAT;
}

View File

@ -39,6 +39,13 @@
#include <spine/SkeletonBatch.h>
namespace spine {
class Cocos2dAtlasAttachmentLoader: public AtlasAttachmentLoader {
public:
Cocos2dAtlasAttachmentLoader(Atlas* atlas);
virtual ~Cocos2dAtlasAttachmentLoader();
virtual void configureAttachment(Attachment* attachment);
};
class Cocos2dTextureLoader: public TextureLoader {
public:
Cocos2dTextureLoader();

View File

@ -62,6 +62,8 @@ namespace spine {
virtual ClippingAttachment* newClippingAttachment(Skin& skin, const String& name);
virtual void configureAttachment(Attachment* attachment);
AtlasRegion* findRegion(const String& name);
private:

View File

@ -37,6 +37,7 @@
namespace spine {
class Skin;
class Attachment;
class RegionAttachment;
class MeshAttachment;
class BoundingBoxAttachment;
@ -67,6 +68,8 @@ namespace spine {
virtual PointAttachment* newPointAttachment(Skin& skin, const String& name) = 0;
virtual ClippingAttachment* newClippingAttachment(Skin& skin, const String& name) = 0;
virtual void configureAttachment(Attachment* attachment) = 0;
};
}

View File

@ -115,6 +115,10 @@ ClippingAttachment *AtlasAttachmentLoader::newClippingAttachment(Skin &skin, con
return new(__FILE__, __LINE__) ClippingAttachment(name);
}
void AtlasAttachmentLoader::configureAttachment(Attachment* attachment) {
SP_UNUSED(attachment);
}
AtlasRegion *AtlasAttachmentLoader::findRegion(const String &name) {
return _atlas->findRegion(name);
}

View File

@ -294,6 +294,7 @@ SkeletonData *SkeletonBinary::readSkeletonData(const unsigned char *binary, cons
}
linkedMesh->_mesh->setParentMesh(static_cast<MeshAttachment *>(parent));
linkedMesh->_mesh->updateUVs();
_attachmentLoader->configureAttachment(linkedMesh->_mesh);
}
ContainerUtil::cleanUpVectorOfPointers(_linkedMeshes);
_linkedMeshes.clear();
@ -469,6 +470,7 @@ Attachment *SkeletonBinary::readAttachment(DataInput *input, Skin *skin, int slo
region->_height = readFloat(input) * _scale;
readColor(input, region->getColor());
region->updateOffset();
_attachmentLoader->configureAttachment(region);
return region;
}
case AttachmentType_Boundingbox: {
@ -479,6 +481,7 @@ Attachment *SkeletonBinary::readAttachment(DataInput *input, Skin *skin, int slo
/* Skip color. */
readInt(input);
}
_attachmentLoader->configureAttachment(box);
return box;
}
case AttachmentType_Mesh: {
@ -504,6 +507,7 @@ Attachment *SkeletonBinary::readAttachment(DataInput *input, Skin *skin, int slo
mesh->_width = 0;
mesh->_height = 0;
}
_attachmentLoader->configureAttachment(mesh);
return mesh;
}
case AttachmentType_Linkedmesh: {
@ -541,6 +545,7 @@ Attachment *SkeletonBinary::readAttachment(DataInput *input, Skin *skin, int slo
/* Skip color. */
readInt(input);
}
_attachmentLoader->configureAttachment(path);
return path;
}
case AttachmentType_Point: {
@ -553,7 +558,7 @@ Attachment *SkeletonBinary::readAttachment(DataInput *input, Skin *skin, int slo
/* Skip color. */
readInt(input);
}
_attachmentLoader->configureAttachment(point);
return point;
}
case AttachmentType_Clipping: {
@ -566,6 +571,7 @@ Attachment *SkeletonBinary::readAttachment(DataInput *input, Skin *skin, int slo
/* Skip color. */
readInt(input);
}
_attachmentLoader->configureAttachment(clip);
return clip;
}
}

View File

@ -495,7 +495,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) {
}
region->updateOffset();
_attachmentLoader->configureAttachment(region);
break;
}
case AttachmentType_Mesh:
@ -548,6 +548,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) {
mesh->_edges[ii] = entry->_valueInt;
}
}
_attachmentLoader->configureAttachment(mesh);
} else {
mesh->_inheritDeform = Json::getInt(attachmentMap, "deform", 1) ? true : false;
LinkedMesh *linkedMesh = new(__FILE__, __LINE__) LinkedMesh(mesh,
@ -567,6 +568,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) {
int vertexCount = Json::getInt(attachmentMap, "vertexCount", 0) << 1;
readVertices(attachmentMap, box, vertexCount);
_attachmentLoader->configureAttachment(attachment);
break;
}
case AttachmentType_Path: {
@ -587,6 +589,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) {
for (curves = curves->_child, ii = 0; curves; curves = curves->_next, ++ii) {
pathAttatchment->_lengths[ii] = curves->_valueFloat * _scale;
}
_attachmentLoader->configureAttachment(attachment);
break;
}
case AttachmentType_Point: {
@ -597,6 +600,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) {
point->_x = Json::getFloat(attachmentMap, "x", 0) * _scale;
point->_y = Json::getFloat(attachmentMap, "y", 0) * _scale;
point->_rotation = Json::getFloat(attachmentMap, "rotation", 0);
_attachmentLoader->configureAttachment(attachment);
break;
}
case AttachmentType_Clipping: {
@ -612,6 +616,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) {
}
vertexCount = Json::getInt(attachmentMap, "vertexCount", 0) << 1;
readVertices(attachmentMap, clip, vertexCount);
_attachmentLoader->configureAttachment(attachment);
break;
}
}
@ -640,6 +645,7 @@ SkeletonData *SkeletonJson::readSkeletonData(const char *json) {
}
linkedMesh->_mesh->setParentMesh(static_cast<MeshAttachment *>(parent));
linkedMesh->_mesh->updateUVs();
_attachmentLoader->configureAttachment(linkedMesh->_mesh);
}
ContainerUtil::cleanUpVectorOfPointers(_linkedMeshes);
_linkedMeshes.clear();