SkinnedMeshAttachment.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes Software License
  3. -- Version 2.1
  4. --
  5. -- Copyright (c) 2013, Esoteric Software
  6. -- All rights reserved.
  7. --
  8. -- You are granted a perpetual, non-exclusive, non-sublicensable and
  9. -- non-transferable license to install, execute and perform the Spine Runtimes
  10. -- Software (the "Software") solely for internal use. Without the written
  11. -- permission of Esoteric Software (typically granted by licensing Spine), you
  12. -- may not (a) modify, translate, adapt or otherwise create derivative works,
  13. -- improvements of the Software or develop new applications using the Software
  14. -- or (b) remove, delete, alter or obscure any trademarks or any copyright,
  15. -- trademark, patent or other intellectual property or proprietary rights
  16. -- notices on or in the Software, including any copy thereof. Redistributions
  17. -- in binary or source 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 SOFTARE 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; LOSS OF USE, DATA, OR PROFITS;
  25. -- OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. -- WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. -- OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  28. -- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. -------------------------------------------------------------------------------
  30. local AttachmentType = require "lib/spine-lua/AttachmentType"
  31. local SkinnedMeshAttachment = {}
  32. function SkinnedMeshAttachment.new (name)
  33. if not name then error("name cannot be nil", 2) end
  34. local self = {
  35. name = name,
  36. type = AttachmentType.mesh,
  37. bones = nil,
  38. weights = nil,
  39. uvs = nil,
  40. regionUVs = nil,
  41. triangles = nil,
  42. hullLength = 0,
  43. r = 1, g = 1, b = 1, a = 1,
  44. path = nil,
  45. rendererObject = nil,
  46. regionU = 0, regionV = 0, regionU2 = 0, regionV2 = 0, regionRotate = false,
  47. regionOffsetX = 0, regionOffsetY = 0,
  48. regionWidth = 0, regionHeight = 0,
  49. regionOriginalWidth = 0, regionOriginalHeight = 0,
  50. edges = nil,
  51. width = 0, height = 0
  52. }
  53. function self:updateUVs ()
  54. local width, height = self.regionU2 - self.regionU, self.regionV2 - self.regionV
  55. local n = #self.regionUVs
  56. if not self.uvs or #self.uvs ~= n then
  57. self.uvs = {}
  58. end
  59. if self.regionRotate then
  60. for i = 1, n, 2 do
  61. self.uvs[i] = self.regionU + self.regionUVs[i + 1] * width
  62. self.uvs[i + 1] = self.regionV + height - self.regionUVs[i] * height
  63. end
  64. else
  65. for i = 1, n, 2 do
  66. self.uvs[i] = self.regionU + self.regionUVs[i] * width
  67. self.uvs[i + 1] = self.regionV + self.regionUVs[i + 1] * height
  68. end
  69. end
  70. end
  71. function self:computeWorldVertices (x, y, slot, worldVertices)
  72. local skeletonBones = slot.skeleton.bones
  73. local weights = self.weights
  74. local bones = self.bones
  75. local w, v, b, f = 0, 0, 0, 0
  76. local n = bones.length
  77. local wx, wy, bone, vx, vy, weight
  78. if #slot.attachmentVertices == 0 then
  79. while v < n do
  80. wx = 0
  81. wy = 0
  82. local nn = bones[v] + v
  83. v = v + 1
  84. while v <= nn do
  85. bone = skeletonBones[bones[v]]
  86. vx = weights[b]
  87. vy = weights[b + 1]
  88. weight = weights[b + 2]
  89. wx = wx + (vx * bone.m00 + vy * bone.m01 + bone.worldX) * weight
  90. wy = wy + (vx * bone.m10 + vy * bone.m11 + bone.worldY) * weight
  91. v = v + 1
  92. b = b + 3
  93. end
  94. worldVertices[w] = wx + x
  95. worldVertices[w + 1] = wy + y
  96. w = w + 2
  97. end
  98. else
  99. local ffd = slot.attachmentVertices
  100. while v < n do
  101. wx = 0
  102. wy = 0
  103. local nn = bones[v] + v
  104. v = v + 1
  105. while v <= nn do
  106. bone = skeletonBones[bones[v]]
  107. vx = weights[b] + ffd[f]
  108. vy = weights[b + 1] + ffd[f + 1]
  109. weight = weights[b + 2]
  110. wx = wx + (vx * bone.m00 + vy * bone.m01 + bone.worldX) * weight
  111. wy = wy + (vx * bone.m10 + vy * bone.m11 + bone.worldY) * weight
  112. v = v + 1
  113. b = b + 3
  114. f = f + 2
  115. end
  116. worldVertices[w] = wx + x
  117. worldVertices[w + 1] = wy + y
  118. w = w + 2
  119. end
  120. end
  121. end
  122. return self
  123. end
  124. return MeshAttachment