[cpp] Ported rotated mesh region UV loading. See #1327.

This commit is contained in:
badlogic 2019-04-17 17:26:22 +02:00
parent 3a4151baf0
commit 0a3f6cc28c
5 changed files with 67 additions and 22 deletions

View File

@ -93,6 +93,7 @@ public:
int originalWidth, originalHeight; int originalWidth, originalHeight;
int index; int index;
bool rotate; bool rotate;
int degrees;
Vector<int> splits; Vector<int> splits;
Vector<int> pads; Vector<int> pads;
}; };

View File

@ -136,6 +136,7 @@ namespace spine {
int _hullLength; int _hullLength;
bool _inheritDeform; bool _inheritDeform;
bool _regionRotate; bool _regionRotate;
int _regionDegrees;
}; };
} }

View File

@ -176,7 +176,14 @@ void Atlas::load(const char *begin, int length, const char *dir) {
region->name = String(mallocString(&str), true); region->name = String(mallocString(&str), true);
readValue(&begin, end, &str); readValue(&begin, end, &str);
region->rotate = equals(&str, "true") ? true : false; if (equals(&str, "true")) {
region->degrees = 90;
} else if (equals(&str, "false")) {
region->degrees = 0;
} else {
region->degrees = toInt(&str);
}
region->rotate = region->degrees == 90;
readTuple(&begin, end, tuple); readTuple(&begin, end, tuple);
region->x = toInt(tuple); region->x = toInt(tuple);

View File

@ -88,6 +88,7 @@ MeshAttachment *AtlasAttachmentLoader::newMeshAttachment(Skin &skin, const Strin
attachment._regionU2 = region.u2; attachment._regionU2 = region.u2;
attachment._regionV2 = region.v2; attachment._regionV2 = region.v2;
attachment._regionRotate = region.rotate; attachment._regionRotate = region.rotate;
attachment._regionDegrees = region.degrees;
attachment._regionOffsetX = region.offsetX; attachment._regionOffsetX = region.offsetX;
attachment._regionOffsetY = region.offsetY; attachment._regionOffsetY = region.offsetY;
attachment._regionWidth = (float)region.width; attachment._regionWidth = (float)region.width;

View File

@ -57,7 +57,8 @@ MeshAttachment::MeshAttachment(const String &name) : VertexAttachment(name), Has
_color(1, 1, 1, 1), _color(1, 1, 1, 1),
_hullLength(0), _hullLength(0),
_inheritDeform(false), _inheritDeform(false),
_regionRotate(false) { _regionRotate(false),
_regionDegrees(0) {
} }
MeshAttachment::~MeshAttachment() {} MeshAttachment::~MeshAttachment() {}
@ -67,27 +68,61 @@ void MeshAttachment::updateUVs() {
_uvs.setSize(_regionUVs.size(), 0); _uvs.setSize(_regionUVs.size(), 0);
} }
if (_regionRotate) { int i = 0, n = _regionUVs.size();
float textureHeight = _regionWidth / (_regionV2 - _regionV); float u = _regionU, v = _regionV;
float textureWidth = _regionHeight / (_regionU2 - _regionU); float width = 0, height = 0;
float u = _regionU - (_regionOriginalHeight - _regionOffsetY - _regionHeight) / textureWidth;
float v = _regionV - (_regionOriginalWidth - _regionOffsetX - _regionWidth) / textureHeight; switch (_regionDegrees) {
float width = _regionOriginalHeight / textureWidth; case 90: {
float height = _regionOriginalWidth / textureHeight; float textureWidth = _regionHeight / (_regionU2 - _regionU);
for (size_t i = 0, n = _uvs.size(); i < n; i += 2) { float textureHeight = _regionWidth / (_regionV2 - _regionV);
_uvs[i] = u + _regionUVs[i + 1] * width; u -= (_regionOriginalHeight - _regionOffsetY - _regionHeight) / textureWidth;
_uvs[i + 1] = v + height - _regionUVs[i] * height; v -= (_regionOriginalWidth - _regionOffsetX - _regionWidth) / textureHeight;
width = _regionOriginalHeight / textureWidth;
height = _regionOriginalWidth / textureHeight;
for (i = 0; i < n; i += 2) {
_uvs[i] = u + _regionUVs[i + 1] * width;
_uvs[i + 1] = v + (1 - _regionUVs[i]) * height;
}
return;
} }
} else { case 180: {
float textureWidth = _regionWidth / (_regionU2 - _regionU); float textureWidth = _regionWidth / (_regionU2 - _regionU);
float textureHeight = _regionHeight / (_regionV2 - _regionV); float textureHeight = _regionHeight / (_regionV2 - _regionV);
float u = _regionU - _regionOffsetX / textureWidth; u -= (_regionOriginalWidth - _regionOffsetX - _regionWidth) / textureWidth;
float v = _regionV - (_regionOriginalHeight - _regionOffsetY - _regionHeight) / textureHeight; v -= _regionOffsetY / textureHeight;
float width = _regionOriginalWidth / textureWidth; width = _regionOriginalWidth / textureWidth;
float height = _regionOriginalHeight / textureHeight; height = _regionOriginalHeight / textureHeight;
for (size_t i = 0, n = _uvs.size(); i < n; i += 2) { for (i = 0; i < n; i += 2) {
_uvs[i] = u + _regionUVs[i] * width; _uvs[i] = u + (1 - _regionUVs[i]) * width;
_uvs[i + 1] = v + _regionUVs[i + 1] * height; _uvs[i + 1] = v + (1 - _regionUVs[i + 1]) * height;
}
return;
}
case 270: {
float textureHeight = _regionHeight / (_regionV2 - _regionV);
float textureWidth = _regionWidth / (_regionU2 - _regionU);
u -= _regionOffsetY / textureWidth;
v -= _regionOffsetX / textureHeight;
width = _regionOriginalHeight / textureWidth;
height = _regionOriginalWidth / textureHeight;
for (i = 0; i < n; i += 2) {
_uvs[i] = u + (1 - _regionUVs[i + 1]) * width;
_uvs[i + 1] = v + _regionUVs[i] * height;
}
return;
}
default: {
float textureWidth = _regionWidth / (_regionU2 - _regionU);
float textureHeight = _regionHeight / (_regionV2 - _regionV);
u -= _regionOffsetX / textureWidth;
v -= (_regionOriginalHeight - _regionOffsetY - _regionHeight) / textureHeight;
width = _regionOriginalWidth / textureWidth;
height = _regionOriginalHeight / textureHeight;
for (i = 0; i < n; i += 2) {
_uvs[i] = u + _regionUVs[i] * width;
_uvs[i + 1] = v + _regionUVs[i + 1] * height;
}
} }
} }
} }