MeshAttachment.lua 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. local AttachmentType = require "spine-lua.AttachmentType"
  31. local MeshAttachment = {}
  32. function MeshAttachment.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. vertices = nil,
  38. uvs = nil,
  39. regionUVs = nil,
  40. triangles = nil,
  41. hullLength = 0,
  42. r = 1, g = 1, b = 1, a = 1,
  43. path = nil,
  44. rendererObject = nil,
  45. regionU = 0, regionV = 0, regionU2 = 1, regionV2 = 1, regionRotate = false,
  46. regionOffsetX = 0, regionOffsetY = 0,
  47. regionWidth = 0, regionHeight = 0,
  48. regionOriginalWidth = 0, regionOriginalHeight = 0,
  49. edges = nil,
  50. width = 0, height = 0
  51. }
  52. function self:updateUVs ()
  53. local width, height = self.regionU2 - self.regionU, self.regionV2 - self.regionV
  54. local n = #self.regionUVs
  55. if not self.uvs or #self.uvs ~= n then
  56. self.uvs = {}
  57. end
  58. if self.regionRotate then
  59. for i = 1, n, 2 do
  60. self.uvs[i] = self.regionU + self.regionUVs[i + 1] * width
  61. self.uvs[i + 1] = self.regionV + height - self.regionUVs[i] * height
  62. end
  63. else
  64. for i = 1, n, 2 do
  65. self.uvs[i] = self.regionU + self.regionUVs[i] * width
  66. self.uvs[i + 1] = self.regionV + self.regionUVs[i + 1] * height
  67. end
  68. end
  69. end
  70. function self:computeWorldVertices (x, y, slot, worldVertices)
  71. local bone = slot.bone
  72. x,y=slot.bone.skeleton.x,slot.bone.skeleton.y
  73. x = x + bone.worldX
  74. y = y + bone.worldY
  75. local m00, m01, m10, m11 = bone.m00, bone.m01, bone.m10, bone.m11
  76. local vertices = self.vertices
  77. local verticesCount = #vertices
  78. if slot.attachmentVertices and #slot.attachmentVertices == verticesCount then vertices = slot.attachmentVertices end
  79. for i = 1, verticesCount, 2 do
  80. local vx = vertices[i]
  81. local vy = vertices[i + 1]
  82. worldVertices[i] = vx * m00 + vy * m01 + x
  83. worldVertices[i + 1] = vx * m10 + vy * m11 + y
  84. end
  85. end
  86. return self
  87. end
  88. return MeshAttachment