VertexAttachment.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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>>An attachment with vertices that are transformed by one or more bones and can be deformed by a slot's
  32. /// <see cref="Slot.Deform"/>.</summary>
  33. public abstract class VertexAttachment : Attachment {
  34. static int nextID = 0;
  35. static readonly Object nextIdLock = new Object();
  36. internal readonly int id;
  37. internal VertexAttachment timelineAttachment;
  38. internal int[] bones;
  39. internal float[] vertices;
  40. internal int worldVerticesLength;
  41. /// <summary>Gets a unique ID for this attachment.</summary>
  42. public int Id { get { return id; } }
  43. public int[] Bones { get { return bones; } set { bones = value; } }
  44. public float[] Vertices { get { return vertices; } set { vertices = value; } }
  45. public int WorldVerticesLength { get { return worldVerticesLength; } set { worldVerticesLength = value; } }
  46. /// <summary>Timelines for the timeline attachment are also applied to this attachment.
  47. /// May be null if no attachment-specific timelines should be applied.</summary>
  48. public VertexAttachment TimelineAttachment { get { return timelineAttachment; } set { timelineAttachment = value; } }
  49. public VertexAttachment (string name)
  50. : base(name) {
  51. lock (VertexAttachment.nextIdLock) {
  52. id = VertexAttachment.nextID++;
  53. }
  54. timelineAttachment = this;
  55. }
  56. /// <summary>Copy constructor.</summary>
  57. public VertexAttachment (VertexAttachment other)
  58. : base(other) {
  59. lock (VertexAttachment.nextIdLock) {
  60. id = VertexAttachment.nextID++;
  61. }
  62. timelineAttachment = other.timelineAttachment;
  63. if (other.bones != null) {
  64. bones = new int[other.bones.Length];
  65. Array.Copy(other.bones, 0, bones, 0, bones.Length);
  66. } else
  67. bones = null;
  68. if (other.vertices != null) {
  69. vertices = new float[other.vertices.Length];
  70. Array.Copy(other.vertices, 0, vertices, 0, vertices.Length);
  71. } else
  72. vertices = null;
  73. worldVerticesLength = other.worldVerticesLength;
  74. }
  75. public void ComputeWorldVertices (Slot slot, float[] worldVertices) {
  76. ComputeWorldVertices(slot, 0, worldVerticesLength, worldVertices, 0);
  77. }
  78. /// <summary>
  79. /// Transforms the attachment's local <see cref="Vertices"/> to world coordinates. If the slot's <see cref="Slot.Deform"/> is
  80. /// not empty, it is used to deform the vertices.
  81. /// <para />
  82. /// See <a href="http://esotericsoftware.com/spine-runtime-skeletons#World-transforms">World transforms</a> in the Spine
  83. /// Runtimes Guide.
  84. /// </summary>
  85. /// <param name="start">The index of the first <see cref="Vertices"/> value to transform. Each vertex has 2 values, x and y.</param>
  86. /// <param name="count">The number of world vertex values to output. Must be less than or equal to <see cref="WorldVerticesLength"/> - start.</param>
  87. /// <param name="worldVertices">The output world vertices. Must have a length greater than or equal to <paramref name="offset"/> + <paramref name="count"/>.</param>
  88. /// <param name="offset">The <paramref name="worldVertices"/> index to begin writing values.</param>
  89. /// <param name="stride">The number of <paramref name="worldVertices"/> entries between the value pairs written.</param>
  90. public virtual void ComputeWorldVertices (Slot slot, int start, int count, float[] worldVertices, int offset, int stride = 2) {
  91. count = offset + (count >> 1) * stride;
  92. ExposedList<float> deformArray = slot.deform;
  93. float[] vertices = this.vertices;
  94. int[] bones = this.bones;
  95. if (bones == null) {
  96. if (deformArray.Count > 0) vertices = deformArray.Items;
  97. Bone bone = slot.bone;
  98. float x = bone.worldX, y = bone.worldY;
  99. float a = bone.a, b = bone.b, c = bone.c, d = bone.d;
  100. for (int vv = start, w = offset; w < count; vv += 2, w += stride) {
  101. float vx = vertices[vv], vy = vertices[vv + 1];
  102. worldVertices[w] = vx * a + vy * b + x;
  103. worldVertices[w + 1] = vx * c + vy * d + y;
  104. }
  105. return;
  106. }
  107. int v = 0, skip = 0;
  108. for (int i = 0; i < start; i += 2) {
  109. int n = bones[v];
  110. v += n + 1;
  111. skip += n;
  112. }
  113. Bone[] skeletonBones = slot.bone.skeleton.bones.Items;
  114. if (deformArray.Count == 0) {
  115. for (int w = offset, b = skip * 3; w < count; w += stride) {
  116. float wx = 0, wy = 0;
  117. int n = bones[v++];
  118. n += v;
  119. for (; v < n; v++, b += 3) {
  120. Bone bone = skeletonBones[bones[v]];
  121. float vx = vertices[b], vy = vertices[b + 1], weight = vertices[b + 2];
  122. wx += (vx * bone.a + vy * bone.b + bone.worldX) * weight;
  123. wy += (vx * bone.c + vy * bone.d + bone.worldY) * weight;
  124. }
  125. worldVertices[w] = wx;
  126. worldVertices[w + 1] = wy;
  127. }
  128. } else {
  129. float[] deform = deformArray.Items;
  130. for (int w = offset, b = skip * 3, f = skip << 1; w < count; w += stride) {
  131. float wx = 0, wy = 0;
  132. int n = bones[v++];
  133. n += v;
  134. for (; v < n; v++, b += 3, f += 2) {
  135. Bone bone = skeletonBones[bones[v]];
  136. float vx = vertices[b] + deform[f], vy = vertices[b + 1] + deform[f + 1], weight = vertices[b + 2];
  137. wx += (vx * bone.a + vy * bone.b + bone.worldX) * weight;
  138. wy += (vx * bone.c + vy * bone.d + bone.worldY) * weight;
  139. }
  140. worldVertices[w] = wx;
  141. worldVertices[w + 1] = wy;
  142. }
  143. }
  144. }
  145. }
  146. }