VertexAttachment.cs 6.7 KB

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