From 470c2f5953af8cce9071341181052406f43488f0 Mon Sep 17 00:00:00 2001 From: badlogic Date: Thu, 18 Apr 2019 18:21:49 +0200 Subject: [PATCH] [csharp] Ported rotated mesh region UV loading. See #1327. --- spine-csharp/src/Atlas.cs | 10 +++- .../src/Attachments/AtlasAttachmentLoader.cs | 1 + .../src/Attachments/MeshAttachment.cs | 46 +++++++++++++++---- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/spine-csharp/src/Atlas.cs b/spine-csharp/src/Atlas.cs index 5dd20a10a..b37952106 100644 --- a/spine-csharp/src/Atlas.cs +++ b/spine-csharp/src/Atlas.cs @@ -153,7 +153,14 @@ namespace Spine { region.name = line; region.page = page; - region.rotate = Boolean.Parse(ReadValue(reader)); + string rotateValue = ReadValue(reader); + if (rotateValue == "true") + region.degrees = 90; + else if (rotateValue == "false") + region.degrees = 0; + else + region.degrees = int.Parse(rotateValue); + region.rotate = region.degrees == 90; ReadTuple(reader, tuple); int x = int.Parse(tuple[0]); @@ -300,6 +307,7 @@ namespace Spine { public int originalWidth, originalHeight; public int index; public bool rotate; + public int degrees; public int[] splits; public int[] pads; diff --git a/spine-csharp/src/Attachments/AtlasAttachmentLoader.cs b/spine-csharp/src/Attachments/AtlasAttachmentLoader.cs index b9ebdf54d..2b88fd489 100644 --- a/spine-csharp/src/Attachments/AtlasAttachmentLoader.cs +++ b/spine-csharp/src/Attachments/AtlasAttachmentLoader.cs @@ -69,6 +69,7 @@ namespace Spine { 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 = region.width; diff --git a/spine-csharp/src/Attachments/MeshAttachment.cs b/spine-csharp/src/Attachments/MeshAttachment.cs index 62c7cd703..f018d88a3 100644 --- a/spine-csharp/src/Attachments/MeshAttachment.cs +++ b/spine-csharp/src/Attachments/MeshAttachment.cs @@ -59,6 +59,7 @@ namespace Spine { public float RegionU2 { get; set; } public float RegionV2 { get; set; } public bool RegionRotate { get; set; } + public int RegionDegrees { get; set; } public float RegionOffsetX { get { return regionOffsetX; } set { regionOffsetX = value; } } public float RegionOffsetY { get { return regionOffsetY; } set { regionOffsetY = value; } } // Pixels stripped from the bottom left, unrotated. public float RegionWidth { get { return regionWidth; } set { regionWidth = value; } } @@ -99,26 +100,51 @@ namespace Spine { float[] regionUVs = this.regionUVs; if (this.uvs == null || this.uvs.Length != regionUVs.Length) this.uvs = new float[regionUVs.Length]; float[] uvs = this.uvs; + float u = RegionU, v = RegionV, width = 0, height = 0; - if (RegionRotate) { + if (RegionDegrees == 90) { float textureHeight = this.regionWidth / (RegionV2 - RegionV); float textureWidth = this.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; + u -= (RegionOriginalHeight - RegionOffsetY - RegionHeight) / textureWidth; + v -= (RegionOriginalWidth - RegionOffsetX - RegionWidth) / textureHeight; + width = RegionOriginalHeight / textureWidth; + height = RegionOriginalWidth / textureHeight; for (int i = 0, n = uvs.Length; i < n; i += 2) { uvs[i] = u + regionUVs[i + 1] * width; - uvs[i + 1] = v + height - regionUVs[i] * height; + uvs[i + 1] = v + (1 - regionUVs[i]) * height; + } + } else if (RegionDegrees == 180) { + float textureWidth = this.regionWidth / (RegionU2 - RegionU); + float textureHeight = this.regionHeight / (RegionV2 - RegionV); + u -= (RegionOriginalWidth - RegionOffsetX - RegionWidth) / textureWidth; + v -= RegionOffsetY / textureHeight; + width = RegionOriginalWidth / textureWidth; + height = RegionOriginalHeight / textureHeight; + + for (int i = 0, n = uvs.Length; i < n; i += 2) { + uvs[i] = u + (1 - regionUVs[i]) * width; + uvs[i + 1] = v + (1 - regionUVs[i + 1]) * height; + } + } else if (RegionDegrees == 270) { + float textureWidth = this.regionWidth / (RegionU2 - RegionU); + float textureHeight = this.regionHeight / (RegionV2 - RegionV); + u -= RegionOffsetY / textureWidth; + v -= RegionOffsetX / textureHeight; + width = RegionOriginalHeight / textureWidth; + height = RegionOriginalWidth / textureHeight; + + for (int i = 0, n = uvs.Length; i