MeshAttachment.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 int RegionDegrees { get; set; }
  55. public float RegionOffsetX { get { return regionOffsetX; } set { regionOffsetX = value; } }
  56. public float RegionOffsetY { get { return regionOffsetY; } set { regionOffsetY = value; } } // Pixels stripped from the bottom left, unrotated.
  57. public float RegionWidth { get { return regionWidth; } set { regionWidth = value; } }
  58. public float RegionHeight { get { return regionHeight; } set { regionHeight = value; } } // Unrotated, stripped size.
  59. public float RegionOriginalWidth { get { return regionOriginalWidth; } set { regionOriginalWidth = value; } }
  60. public float RegionOriginalHeight { get { return regionOriginalHeight; } set { regionOriginalHeight = value; } } // Unrotated, unstripped size.
  61. public MeshAttachment ParentMesh {
  62. get { return parentMesh; }
  63. set {
  64. parentMesh = value;
  65. if (value != null) {
  66. bones = value.bones;
  67. vertices = value.vertices;
  68. worldVerticesLength = value.worldVerticesLength;
  69. regionUVs = value.regionUVs;
  70. triangles = value.triangles;
  71. HullLength = value.HullLength;
  72. Edges = value.Edges;
  73. Width = value.Width;
  74. Height = value.Height;
  75. }
  76. }
  77. }
  78. // Nonessential.
  79. public int[] Edges { get; set; }
  80. public float Width { get; set; }
  81. public float Height { get; set; }
  82. public MeshAttachment (string name)
  83. : base(name) {
  84. }
  85. public void UpdateUVs () {
  86. float[] regionUVs = this.regionUVs;
  87. if (this.uvs == null || this.uvs.Length != regionUVs.Length) this.uvs = new float[regionUVs.Length];
  88. float[] uvs = this.uvs;
  89. float u = RegionU, v = RegionV, width = 0, height = 0;
  90. if (RegionDegrees == 90) {
  91. float textureHeight = this.regionWidth / (RegionV2 - RegionV);
  92. float textureWidth = this.regionHeight / (RegionU2 - RegionU);
  93. u -= (RegionOriginalHeight - RegionOffsetY - RegionHeight) / textureWidth;
  94. v -= (RegionOriginalWidth - RegionOffsetX - RegionWidth) / textureHeight;
  95. width = RegionOriginalHeight / textureWidth;
  96. height = RegionOriginalWidth / textureHeight;
  97. for (int i = 0, n = uvs.Length; i < n; i += 2) {
  98. uvs[i] = u + regionUVs[i + 1] * width;
  99. uvs[i + 1] = v + (1 - regionUVs[i]) * height;
  100. }
  101. } else if (RegionDegrees == 180) {
  102. float textureWidth = this.regionWidth / (RegionU2 - RegionU);
  103. float textureHeight = this.regionHeight / (RegionV2 - RegionV);
  104. u -= (RegionOriginalWidth - RegionOffsetX - RegionWidth) / textureWidth;
  105. v -= RegionOffsetY / textureHeight;
  106. width = RegionOriginalWidth / textureWidth;
  107. height = RegionOriginalHeight / textureHeight;
  108. for (int i = 0, n = uvs.Length; i < n; i += 2) {
  109. uvs[i] = u + (1 - regionUVs[i]) * width;
  110. uvs[i + 1] = v + (1 - regionUVs[i + 1]) * height;
  111. }
  112. } else if (RegionDegrees == 270) {
  113. float textureWidth = this.regionWidth / (RegionU2 - RegionU);
  114. float textureHeight = this.regionHeight / (RegionV2 - RegionV);
  115. u -= RegionOffsetY / textureWidth;
  116. v -= RegionOffsetX / textureHeight;
  117. width = RegionOriginalHeight / textureWidth;
  118. height = RegionOriginalWidth / textureHeight;
  119. for (int i = 0, n = uvs.Length; i < n; i += 2) {
  120. uvs[i] = u + (1 - regionUVs[i + 1]) * width;
  121. uvs[i + 1] = v + regionUVs[i] * height;
  122. }
  123. } else {
  124. float textureWidth = this.regionWidth / (RegionU2 - RegionU);
  125. float textureHeight = this.regionHeight / (RegionV2 - RegionV);
  126. u -= RegionOffsetX / textureWidth;
  127. v -= (RegionOriginalHeight - RegionOffsetY - RegionHeight) / textureHeight;
  128. width = RegionOriginalWidth / textureWidth;
  129. height = RegionOriginalHeight / textureHeight;
  130. for (int i = 0, n = uvs.Length; i < n; i += 2) {
  131. uvs[i] = u + regionUVs[i] * width;
  132. uvs[i + 1] = v + regionUVs[i + 1] * height;
  133. }
  134. }
  135. }
  136. public override Attachment Copy () {
  137. if (parentMesh != null) return NewLinkedMesh();
  138. MeshAttachment copy = new MeshAttachment(this.Name);
  139. copy.RendererObject = RendererObject;
  140. copy.regionOffsetX = regionOffsetX;
  141. copy.regionOffsetY = regionOffsetY;
  142. copy.regionWidth = regionWidth;
  143. copy.regionHeight = regionHeight;
  144. copy.regionOriginalWidth = regionOriginalWidth;
  145. copy.regionOriginalHeight = regionOriginalHeight;
  146. copy.RegionDegrees = RegionDegrees;
  147. copy.RegionU = RegionU;
  148. copy.RegionV = RegionV;
  149. copy.RegionU2 = RegionU2;
  150. copy.RegionV2 = RegionV2;
  151. copy.Path = Path;
  152. copy.r = r;
  153. copy.g = g;
  154. copy.b = b;
  155. copy.a = a;
  156. CopyTo(copy);
  157. copy.regionUVs = new float[regionUVs.Length];
  158. Array.Copy(regionUVs, 0, copy.regionUVs, 0, regionUVs.Length);
  159. copy.uvs = new float[uvs.Length];
  160. Array.Copy(uvs, 0, copy.uvs, 0, uvs.Length);
  161. copy.triangles = new int[triangles.Length];
  162. Array.Copy(triangles, 0, copy.triangles, 0, triangles.Length);
  163. copy.HullLength = HullLength;
  164. // Nonessential.
  165. if (Edges != null) {
  166. copy.Edges = new int[Edges.Length];
  167. Array.Copy(Edges, 0, copy.Edges, 0, Edges.Length);
  168. }
  169. copy.Width = Width;
  170. copy.Height = Height;
  171. return copy;
  172. }
  173. ///<summary>Returns a new mesh with this mesh set as the <see cref="ParentMesh"/>.
  174. public MeshAttachment NewLinkedMesh () {
  175. MeshAttachment mesh = new MeshAttachment(Name);
  176. mesh.RendererObject = RendererObject;
  177. mesh.regionOffsetX = regionOffsetX;
  178. mesh.regionOffsetY = regionOffsetY;
  179. mesh.regionWidth = regionWidth;
  180. mesh.regionHeight = regionHeight;
  181. mesh.regionOriginalWidth = regionOriginalWidth;
  182. mesh.regionOriginalHeight = regionOriginalHeight;
  183. mesh.RegionDegrees = RegionDegrees;
  184. mesh.RegionU = RegionU;
  185. mesh.RegionV = RegionV;
  186. mesh.RegionU2 = RegionU2;
  187. mesh.RegionV2 = RegionV2;
  188. mesh.Path = Path;
  189. mesh.r = r;
  190. mesh.g = g;
  191. mesh.b = b;
  192. mesh.a = a;
  193. mesh.deformAttachment = deformAttachment;
  194. mesh.ParentMesh = parentMesh != null ? parentMesh : this;
  195. mesh.UpdateUVs();
  196. return mesh;
  197. }
  198. }
  199. }