MeshAttachment.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. using System;
  30. namespace Spine {
  31. /// <summary>Attachment that displays a texture region using a mesh.</summary>
  32. public class MeshAttachment : VertexAttachment, IHasRendererObject {
  33. internal float regionOffsetX, regionOffsetY, regionWidth, regionHeight, regionOriginalWidth, regionOriginalHeight;
  34. private MeshAttachment parentMesh;
  35. internal float[] uvs, regionUVs;
  36. internal int[] triangles;
  37. internal float r = 1, g = 1, b = 1, a = 1;
  38. internal int hulllength;
  39. public int HullLength { get { return hulllength; } set { hulllength = value; } }
  40. public float[] RegionUVs { get { return regionUVs; } set { regionUVs = value; } }
  41. /// <summary>The UV pair for each vertex, normalized within the entire texture. <seealso cref="MeshAttachment.UpdateUVs"/></summary>
  42. public float[] UVs { get { return uvs; } set { uvs = value; } }
  43. public int[] Triangles { get { return triangles; } set { triangles = value; } }
  44. public float R { get { return r; } set { r = value; } }
  45. public float G { get { return g; } set { g = value; } }
  46. public float B { get { return b; } set { b = value; } }
  47. public float A { get { return a; } set { a = value; } }
  48. public string Path { get; set; }
  49. public object RendererObject { get; set; }
  50. public float RegionU { get; set; }
  51. public float RegionV { get; set; }
  52. public float RegionU2 { get; set; }
  53. public float RegionV2 { get; set; }
  54. public bool RegionRotate { get; set; }
  55. public int RegionDegrees { get; set; }
  56. public float RegionOffsetX { get { return regionOffsetX; } set { regionOffsetX = value; } }
  57. public float RegionOffsetY { get { return regionOffsetY; } set { regionOffsetY = value; } } // Pixels stripped from the bottom left, unrotated.
  58. public float RegionWidth { get { return regionWidth; } set { regionWidth = value; } }
  59. public float RegionHeight { get { return regionHeight; } set { regionHeight = value; } } // Unrotated, stripped size.
  60. public float RegionOriginalWidth { get { return regionOriginalWidth; } set { regionOriginalWidth = value; } }
  61. public float RegionOriginalHeight { get { return regionOriginalHeight; } set { regionOriginalHeight = value; } } // Unrotated, unstripped size.
  62. public MeshAttachment ParentMesh {
  63. get { return parentMesh; }
  64. set {
  65. parentMesh = value;
  66. if (value != null) {
  67. bones = value.bones;
  68. vertices = value.vertices;
  69. worldVerticesLength = value.worldVerticesLength;
  70. regionUVs = value.regionUVs;
  71. triangles = value.triangles;
  72. HullLength = value.HullLength;
  73. Edges = value.Edges;
  74. Width = value.Width;
  75. Height = value.Height;
  76. }
  77. }
  78. }
  79. // Nonessential.
  80. public int[] Edges { get; set; }
  81. public float Width { get; set; }
  82. public float Height { get; set; }
  83. public MeshAttachment (string name)
  84. : base(name) {
  85. }
  86. public void UpdateUVs () {
  87. float[] regionUVs = this.regionUVs;
  88. if (this.uvs == null || this.uvs.Length != regionUVs.Length) this.uvs = new float[regionUVs.Length];
  89. float[] uvs = this.uvs;
  90. float u = RegionU, v = RegionV, width = 0, height = 0;
  91. if (RegionDegrees == 90) {
  92. float textureHeight = this.regionWidth / (RegionV2 - RegionV);
  93. float textureWidth = this.regionHeight / (RegionU2 - RegionU);
  94. u -= (RegionOriginalHeight - RegionOffsetY - RegionHeight) / textureWidth;
  95. v -= (RegionOriginalWidth - RegionOffsetX - RegionWidth) / textureHeight;
  96. width = RegionOriginalHeight / textureWidth;
  97. height = RegionOriginalWidth / textureHeight;
  98. for (int i = 0, n = uvs.Length; i < n; i += 2) {
  99. uvs[i] = u + regionUVs[i + 1] * width;
  100. uvs[i + 1] = v + (1 - regionUVs[i]) * height;
  101. }
  102. } else if (RegionDegrees == 180) {
  103. float textureWidth = this.regionWidth / (RegionU2 - RegionU);
  104. float textureHeight = this.regionHeight / (RegionV2 - RegionV);
  105. u -= (RegionOriginalWidth - RegionOffsetX - RegionWidth) / textureWidth;
  106. v -= RegionOffsetY / textureHeight;
  107. width = RegionOriginalWidth / textureWidth;
  108. height = RegionOriginalHeight / textureHeight;
  109. for (int i = 0, n = uvs.Length; i < n; i += 2) {
  110. uvs[i] = u + (1 - regionUVs[i]) * width;
  111. uvs[i + 1] = v + (1 - regionUVs[i + 1]) * height;
  112. }
  113. } else if (RegionDegrees == 270) {
  114. float textureWidth = this.regionWidth / (RegionU2 - RegionU);
  115. float textureHeight = this.regionHeight / (RegionV2 - RegionV);
  116. u -= RegionOffsetY / textureWidth;
  117. v -= RegionOffsetX / textureHeight;
  118. width = RegionOriginalHeight / textureWidth;
  119. height = RegionOriginalWidth / textureHeight;
  120. for (int i = 0, n = uvs.Length; i<n; i += 2) {
  121. uvs[i] = u + (1 - regionUVs[i + 1]) * width;
  122. uvs[i + 1] = v + regionUVs[i] * height;
  123. }
  124. } else {
  125. float textureWidth = this.regionWidth / (RegionU2 - RegionU);
  126. float textureHeight = this.regionHeight / (RegionV2 - RegionV);
  127. u -= RegionOffsetX / textureWidth;
  128. v -= (RegionOriginalHeight - RegionOffsetY - RegionHeight) / textureHeight;
  129. width = RegionOriginalWidth / textureWidth;
  130. height = RegionOriginalHeight / textureHeight;
  131. for (int i = 0, n = uvs.Length; i < n; i += 2) {
  132. uvs[i] = u + regionUVs[i] * width;
  133. uvs[i + 1] = v + regionUVs[i + 1] * height;
  134. }
  135. }
  136. }
  137. public override Attachment Copy () {
  138. if (parentMesh != null) return NewLinkedMesh();
  139. MeshAttachment copy = new MeshAttachment(this.Name);
  140. copy.RendererObject = RendererObject;
  141. copy.regionOffsetX = regionOffsetX;
  142. copy.regionOffsetY = regionOffsetY;
  143. copy.regionWidth = regionWidth;
  144. copy.regionHeight = regionHeight;
  145. copy.regionOriginalWidth = regionOriginalWidth;
  146. copy.regionOriginalHeight = regionOriginalHeight;
  147. copy.RegionRotate = RegionRotate;
  148. copy.RegionDegrees = RegionDegrees;
  149. copy.RegionU = RegionU;
  150. copy.RegionV = RegionV;
  151. copy.RegionU2 = RegionU2;
  152. copy.RegionV2 = RegionV2;
  153. copy.Path = Path;
  154. copy.r = r;
  155. copy.g = g;
  156. copy.b = b;
  157. copy.a = a;
  158. CopyTo(copy);
  159. copy.regionUVs = new float[regionUVs.Length];
  160. Array.Copy(regionUVs, 0, copy.regionUVs, 0, regionUVs.Length);
  161. copy.uvs = new float[uvs.Length];
  162. Array.Copy(uvs, 0, copy.uvs, 0, uvs.Length);
  163. copy.triangles = new int[triangles.Length];
  164. Array.Copy(triangles, 0, copy.triangles, 0, triangles.Length);
  165. copy.HullLength = HullLength;
  166. // Nonessential.
  167. if (Edges != null) {
  168. copy.Edges = new int[Edges.Length];
  169. Array.Copy(Edges, 0, copy.Edges, 0, Edges.Length);
  170. }
  171. copy.Width = Width;
  172. copy.Height = Height;
  173. return copy;
  174. }
  175. ///<summary>Returns a new mesh with this mesh set as the <see cref="ParentMesh"/>.
  176. public MeshAttachment NewLinkedMesh () {
  177. MeshAttachment mesh = new MeshAttachment(Name);
  178. mesh.RendererObject = RendererObject;
  179. mesh.regionOffsetX = regionOffsetX;
  180. mesh.regionOffsetY = regionOffsetY;
  181. mesh.regionWidth = regionWidth;
  182. mesh.regionHeight = regionHeight;
  183. mesh.regionOriginalWidth = regionOriginalWidth;
  184. mesh.regionOriginalHeight = regionOriginalHeight;
  185. mesh.RegionDegrees = RegionDegrees;
  186. mesh.RegionRotate = RegionRotate;
  187. mesh.RegionU = RegionU;
  188. mesh.RegionV = RegionV;
  189. mesh.RegionU2 = RegionU2;
  190. mesh.RegionV2 = RegionV2;
  191. mesh.Path = Path;
  192. mesh.r = r;
  193. mesh.g = g;
  194. mesh.b = b;
  195. mesh.a = a;
  196. mesh.deformAttachment = deformAttachment;
  197. mesh.ParentMesh = parentMesh != null ? parentMesh : this;
  198. mesh.UpdateUVs();
  199. return mesh;
  200. }
  201. }
  202. }