源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 line
5.9 KiB

  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. using System;
  31. namespace Spine {
  32. /// <summary>>An attachment with vertices that are transformed by one or more bones and can be deformed by a slot's vertices.</summary>
  33. public 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. /// <summary>Gets a unique ID for this attachment.</summary>
  41. public int Id { get { return id; } }
  42. public int[] Bones { get { return bones; } set { bones = value; } }
  43. public float[] Vertices { get { return vertices; } set { vertices = value; } }
  44. public int WorldVerticesLength { get { return worldVerticesLength; } set { worldVerticesLength = value; } }
  45. public VertexAttachment (string name)
  46. : base(name) {
  47. lock (VertexAttachment.nextIdLock) {
  48. id = (VertexAttachment.nextID++ & 65535) << 11;
  49. }
  50. }
  51. public void ComputeWorldVertices (Slot slot, float[] worldVertices) {
  52. ComputeWorldVertices(slot, 0, worldVerticesLength, worldVertices, 0);
  53. }
  54. /// <summary>Transforms local vertices to world coordinates.</summary>
  55. /// <param name="start">The index of the first <see cref="Vertices"/> value to transform. Each vertex has 2 values, x and y.</param>
  56. /// <param name="count">The number of world vertex values to output. Must be less than or equal to <see cref="WorldVerticesLength"/> - start.</param>
  57. /// <param name="worldVertices">The output world vertices. Must have a length greater than or equal to <paramref name="offset"/> + <paramref name="count"/>.</param>
  58. /// <param name="offset">The <paramref name="worldVertices"/> index to begin writing values.</param>
  59. /// <param name="stride">The number of <paramref name="worldVertices"/> entries between the value pairs written.</param>
  60. public void ComputeWorldVertices (Slot slot, int start, int count, float[] worldVertices, int offset, int stride = 2) {
  61. count = offset + (count >> 1) * stride;
  62. Skeleton skeleton = slot.bone.skeleton;
  63. var deformArray = slot.attachmentVertices;
  64. float[] vertices = this.vertices;
  65. int[] bones = this.bones;
  66. if (bones == null) {
  67. if (deformArray.Count > 0) vertices = deformArray.Items;
  68. Bone bone = slot.bone;
  69. float x = bone.worldX, y = bone.worldY;
  70. float a = bone.a, b = bone.b, c = bone.c, d = bone.d;
  71. for (int vv = start, w = offset; w < count; vv += 2, w += stride) {
  72. float vx = vertices[vv], vy = vertices[vv + 1];
  73. worldVertices[w] = vx * a + vy * b + x;
  74. worldVertices[w + 1] = vx * c + vy * d + y;
  75. }
  76. return;
  77. }
  78. int v = 0, skip = 0;
  79. for (int i = 0; i < start; i += 2) {
  80. int n = bones[v];
  81. v += n + 1;
  82. skip += n;
  83. }
  84. var skeletonBones = skeleton.bones.Items;
  85. if (deformArray.Count == 0) {
  86. for (int w = offset, b = skip * 3; w < count; w += stride) {
  87. float wx = 0, wy = 0;
  88. int n = bones[v++];
  89. n += v;
  90. for (; v < n; v++, b += 3) {
  91. Bone bone = skeletonBones[bones[v]];
  92. float vx = vertices[b], vy = vertices[b + 1], weight = vertices[b + 2];
  93. wx += (vx * bone.a + vy * bone.b + bone.worldX) * weight;
  94. wy += (vx * bone.c + vy * bone.d + bone.worldY) * weight;
  95. }
  96. worldVertices[w] = wx;
  97. worldVertices[w + 1] = wy;
  98. }
  99. } else {
  100. float[] deform = deformArray.Items;
  101. for (int w = offset, b = skip * 3, f = skip << 1; w < count; w += stride) {
  102. float wx = 0, wy = 0;
  103. int n = bones[v++];
  104. n += v;
  105. for (; v < n; v++, b += 3, f += 2) {
  106. Bone bone = skeletonBones[bones[v]];
  107. float vx = vertices[b] + deform[f], vy = vertices[b + 1] + deform[f + 1], weight = vertices[b + 2];
  108. wx += (vx * bone.a + vy * bone.b + bone.worldX) * weight;
  109. wy += (vx * bone.c + vy * bone.d + bone.worldY) * weight;
  110. }
  111. worldVertices[w] = wx;
  112. worldVertices[w + 1] = wy;
  113. }
  114. }
  115. }
  116. /// <summary>Returns true if a deform originally applied to the specified attachment should be applied to this attachment.</summary>
  117. virtual public bool ApplyDeform (VertexAttachment sourceAttachment) {
  118. return this == sourceAttachment;
  119. }
  120. }
  121. }