mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-02-08 08:14:53 +08:00
[cpp] Ported rotated mesh region UV loading. See #1327.
This commit is contained in:
parent
3a4151baf0
commit
0a3f6cc28c
@ -93,6 +93,7 @@ public:
|
||||
int originalWidth, originalHeight;
|
||||
int index;
|
||||
bool rotate;
|
||||
int degrees;
|
||||
Vector<int> splits;
|
||||
Vector<int> pads;
|
||||
};
|
||||
|
||||
@ -136,6 +136,7 @@ namespace spine {
|
||||
int _hullLength;
|
||||
bool _inheritDeform;
|
||||
bool _regionRotate;
|
||||
int _regionDegrees;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -176,7 +176,14 @@ void Atlas::load(const char *begin, int length, const char *dir) {
|
||||
region->name = String(mallocString(&str), true);
|
||||
|
||||
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);
|
||||
region->x = toInt(tuple);
|
||||
|
||||
@ -88,6 +88,7 @@ MeshAttachment *AtlasAttachmentLoader::newMeshAttachment(Skin &skin, const Strin
|
||||
attachment._regionU2 = region.u2;
|
||||
attachment._regionV2 = region.v2;
|
||||
attachment._regionRotate = region.rotate;
|
||||
attachment._regionDegrees = region.degrees;
|
||||
attachment._regionOffsetX = region.offsetX;
|
||||
attachment._regionOffsetY = region.offsetY;
|
||||
attachment._regionWidth = (float)region.width;
|
||||
|
||||
@ -57,7 +57,8 @@ MeshAttachment::MeshAttachment(const String &name) : VertexAttachment(name), Has
|
||||
_color(1, 1, 1, 1),
|
||||
_hullLength(0),
|
||||
_inheritDeform(false),
|
||||
_regionRotate(false) {
|
||||
_regionRotate(false),
|
||||
_regionDegrees(0) {
|
||||
}
|
||||
|
||||
MeshAttachment::~MeshAttachment() {}
|
||||
@ -67,27 +68,61 @@ void MeshAttachment::updateUVs() {
|
||||
_uvs.setSize(_regionUVs.size(), 0);
|
||||
}
|
||||
|
||||
if (_regionRotate) {
|
||||
float textureHeight = _regionWidth / (_regionV2 - _regionV);
|
||||
float textureWidth = _regionHeight / (_regionU2 - _regionU);
|
||||
float u = _regionU - (_regionOriginalHeight - _regionOffsetY - _regionHeight) / textureWidth;
|
||||
float v = _regionV - (_regionOriginalWidth - _regionOffsetX - _regionWidth) / textureHeight;
|
||||
float width = _regionOriginalHeight / textureWidth;
|
||||
float height = _regionOriginalWidth / textureHeight;
|
||||
for (size_t i = 0, n = _uvs.size(); i < n; i += 2) {
|
||||
_uvs[i] = u + _regionUVs[i + 1] * width;
|
||||
_uvs[i + 1] = v + height - _regionUVs[i] * height;
|
||||
int i = 0, n = _regionUVs.size();
|
||||
float u = _regionU, v = _regionV;
|
||||
float width = 0, height = 0;
|
||||
|
||||
switch (_regionDegrees) {
|
||||
case 90: {
|
||||
float textureWidth = _regionHeight / (_regionU2 - _regionU);
|
||||
float textureHeight = _regionWidth / (_regionV2 - _regionV);
|
||||
u -= (_regionOriginalHeight - _regionOffsetY - _regionHeight) / textureWidth;
|
||||
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 {
|
||||
float textureWidth = _regionWidth / (_regionU2 - _regionU);
|
||||
float textureHeight = _regionHeight / (_regionV2 - _regionV);
|
||||
float u = _regionU - _regionOffsetX / textureWidth;
|
||||
float v = _regionV - (_regionOriginalHeight - _regionOffsetY - _regionHeight) / textureHeight;
|
||||
float width = _regionOriginalWidth / textureWidth;
|
||||
float height = _regionOriginalHeight / textureHeight;
|
||||
for (size_t i = 0, n = _uvs.size(); i < n; i += 2) {
|
||||
_uvs[i] = u + _regionUVs[i] * width;
|
||||
_uvs[i + 1] = v + _regionUVs[i + 1] * height;
|
||||
case 180: {
|
||||
float textureWidth = _regionWidth / (_regionU2 - _regionU);
|
||||
float textureHeight = _regionHeight / (_regionV2 - _regionV);
|
||||
u -= (_regionOriginalWidth - _regionOffsetX - _regionWidth) / textureWidth;
|
||||
v -= _regionOffsetY / textureHeight;
|
||||
width = _regionOriginalWidth / textureWidth;
|
||||
height = _regionOriginalHeight / textureHeight;
|
||||
for (i = 0; i < n; i += 2) {
|
||||
_uvs[i] = u + (1 - _regionUVs[i]) * width;
|
||||
_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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user