MeshAttachment.lua 3.6 KB

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