MeshAttachment.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, 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 or
  13. * 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 THE
  27. * 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, IHasTextureRegion {
  33. internal TextureRegion region;
  34. internal string path;
  35. internal float[] regionUVs, uvs;
  36. internal int[] triangles;
  37. internal float r = 1, g = 1, b = 1, a = 1;
  38. internal int hullLength;
  39. private MeshAttachment parentMesh;
  40. private Sequence sequence;
  41. public TextureRegion Region {
  42. get { return region; }
  43. set {
  44. if (value == null) throw new ArgumentNullException("region", "region cannot be null.");
  45. region = value;
  46. }
  47. }
  48. public int HullLength { get { return hullLength; } set { hullLength = value; } }
  49. public float[] RegionUVs { get { return regionUVs; } set { regionUVs = value; } }
  50. /// <summary>The UV pair for each vertex, normalized within the entire texture.
  51. /// <seealso cref="MeshAttachment.UpdateRegion"/></summary>
  52. public float[] UVs { get { return uvs; } set { uvs = value; } }
  53. public int[] Triangles { get { return triangles; } set { triangles = value; } }
  54. public float R { get { return r; } set { r = value; } }
  55. public float G { get { return g; } set { g = value; } }
  56. public float B { get { return b; } set { b = value; } }
  57. public float A { get { return a; } set { a = value; } }
  58. public string Path { get { return path; } set { path = value; } }
  59. public Sequence Sequence { get { return sequence; } set { sequence = value; } }
  60. public MeshAttachment ParentMesh {
  61. get { return parentMesh; }
  62. set {
  63. parentMesh = value;
  64. if (value != null) {
  65. bones = value.bones;
  66. vertices = value.vertices;
  67. worldVerticesLength = value.worldVerticesLength;
  68. regionUVs = value.regionUVs;
  69. triangles = value.triangles;
  70. HullLength = value.HullLength;
  71. Edges = value.Edges;
  72. Width = value.Width;
  73. Height = value.Height;
  74. }
  75. }
  76. }
  77. // Nonessential.
  78. public int[] Edges { get; set; }
  79. public float Width { get; set; }
  80. public float Height { get; set; }
  81. public MeshAttachment (string name)
  82. : base(name) {
  83. }
  84. /// <summary>Copy constructor. Use <see cref="NewLinkedMesh"/> if the other mesh is a linked mesh.</summary>
  85. protected MeshAttachment (MeshAttachment other)
  86. : base(other) {
  87. if (parentMesh != null) throw new ArgumentException("Use newLinkedMesh to copy a linked mesh.");
  88. region = other.region;
  89. path = other.path;
  90. r = other.r;
  91. g = other.g;
  92. b = other.b;
  93. a = other.a;
  94. regionUVs = new float[other.regionUVs.Length];
  95. Array.Copy(other.regionUVs, 0, regionUVs, 0, regionUVs.Length);
  96. uvs = new float[other.uvs.Length];
  97. Array.Copy(other.uvs, 0, uvs, 0, uvs.Length);
  98. triangles = new int[other.triangles.Length];
  99. Array.Copy(other.triangles, 0, triangles, 0, triangles.Length);
  100. hullLength = other.hullLength;
  101. sequence = other.sequence == null ? null : new Sequence(other.sequence);
  102. // Nonessential.
  103. if (other.Edges != null) {
  104. Edges = new int[other.Edges.Length];
  105. Array.Copy(other.Edges, 0, Edges, 0, Edges.Length);
  106. }
  107. Width = other.Width;
  108. Height = other.Height;
  109. }
  110. public void UpdateRegion () {
  111. float[] regionUVs = this.regionUVs;
  112. if (this.uvs == null || this.uvs.Length != regionUVs.Length) this.uvs = new float[regionUVs.Length];
  113. float[] uvs = this.uvs;
  114. int n = uvs.Length;
  115. float u, v, width, height;
  116. if (region is AtlasRegion) {
  117. u = this.region.u;
  118. v = this.region.v;
  119. AtlasRegion region = (AtlasRegion)this.region;
  120. // Note: difference from reference implementation.
  121. // Covers rotation since region.width and height are already setup accordingly.
  122. float textureWidth = this.region.width / (region.u2 - region.u);
  123. float textureHeight = this.region.height / (region.v2 - region.v);
  124. switch (region.degrees) {
  125. case 90:
  126. u -= (region.originalHeight - region.offsetY - region.packedWidth) / textureWidth;
  127. v -= (region.originalWidth - region.offsetX - region.packedHeight) / textureHeight;
  128. width = region.originalHeight / textureWidth;
  129. height = region.originalWidth / textureHeight;
  130. for (int i = 0; i < n; i += 2) {
  131. uvs[i] = u + regionUVs[i + 1] * width;
  132. uvs[i + 1] = v + (1 - regionUVs[i]) * height;
  133. }
  134. return;
  135. case 180:
  136. u -= (region.originalWidth - region.offsetX - region.packedWidth) / textureWidth;
  137. v -= region.offsetY / textureHeight;
  138. width = region.originalWidth / textureWidth;
  139. height = region.originalHeight / textureHeight;
  140. for (int i = 0; i < n; i += 2) {
  141. uvs[i] = u + (1 - regionUVs[i]) * width;
  142. uvs[i + 1] = v + (1 - regionUVs[i + 1]) * height;
  143. }
  144. return;
  145. case 270:
  146. u -= region.offsetY / textureWidth;
  147. v -= region.offsetX / textureHeight;
  148. width = region.originalHeight / textureWidth;
  149. height = region.originalWidth / textureHeight;
  150. for (int i = 0; i < n; i += 2) {
  151. uvs[i] = u + (1 - regionUVs[i + 1]) * width;
  152. uvs[i + 1] = v + regionUVs[i] * height;
  153. }
  154. return;
  155. }
  156. u -= region.offsetX / textureWidth;
  157. v -= (region.originalHeight - region.offsetY - region.packedHeight) / textureHeight;
  158. width = region.originalWidth / textureWidth;
  159. height = region.originalHeight / textureHeight;
  160. } else if (region == null) {
  161. u = v = 0;
  162. width = height = 1;
  163. } else {
  164. u = region.u;
  165. v = region.v;
  166. width = region.u2 - u;
  167. height = region.v2 - v;
  168. }
  169. for (int i = 0; i < n; i += 2) {
  170. uvs[i] = u + regionUVs[i] * width;
  171. uvs[i + 1] = v + regionUVs[i + 1] * height;
  172. }
  173. }
  174. /// <summary>If the attachment has a <see cref="Sequence"/>, the region may be changed.</summary>
  175. override public void ComputeWorldVertices (Slot slot, int start, int count, float[] worldVertices, int offset, int stride = 2) {
  176. if (sequence != null) sequence.Apply(slot, this);
  177. base.ComputeWorldVertices(slot, start, count, worldVertices, offset, stride);
  178. }
  179. /// <summary>Returns a new mesh with this mesh set as the <see cref="ParentMesh"/>.
  180. public MeshAttachment NewLinkedMesh () {
  181. MeshAttachment mesh = new MeshAttachment(Name);
  182. mesh.timelineAttachment = timelineAttachment;
  183. mesh.region = region;
  184. mesh.path = path;
  185. mesh.r = r;
  186. mesh.g = g;
  187. mesh.b = b;
  188. mesh.a = a;
  189. mesh.ParentMesh = parentMesh != null ? parentMesh : this;
  190. if (mesh.Region != null) mesh.UpdateRegion();
  191. return mesh;
  192. }
  193. public override Attachment Copy () {
  194. return parentMesh != null ? NewLinkedMesh() : new MeshAttachment(this);
  195. }
  196. }
  197. }