SpriteCollectionAttachmentLoader.cs 5.9 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. #if SPINE_TK2D
  30. using System;
  31. using UnityEngine;
  32. using Spine;
  33. // MITCH: handle TPackerCW flip mode (probably not swap uv horizontaly)
  34. namespace Spine.Unity.TK2D {
  35. public class SpriteCollectionAttachmentLoader : AttachmentLoader {
  36. private tk2dSpriteCollectionData sprites;
  37. private float u, v, u2, v2;
  38. private bool regionRotated;
  39. private float regionOriginalWidth, regionOriginalHeight;
  40. private float regionWidth, regionHeight;
  41. private float regionOffsetX, regionOffsetY;
  42. private Material material;
  43. public SpriteCollectionAttachmentLoader (tk2dSpriteCollectionData sprites) {
  44. if (sprites == null)
  45. throw new ArgumentNullException("sprites cannot be null.");
  46. this.sprites = sprites;
  47. }
  48. private void ProcessSpriteDefinition (String name) {
  49. // Strip folder names.
  50. int index = name.LastIndexOfAny(new char[] {'/', '\\'});
  51. if (index != -1)
  52. name = name.Substring(index + 1);
  53. tk2dSpriteDefinition def = sprites.inst.GetSpriteDefinition(name);
  54. if (def == null) {
  55. Debug.Log("Sprite not found in atlas: " + name, sprites);
  56. throw new Exception("Sprite not found in atlas: " + name);
  57. }
  58. if (def.complexGeometry)
  59. throw new NotImplementedException("Complex geometry is not supported: " + name);
  60. if (def.flipped == tk2dSpriteDefinition.FlipMode.TPackerCW)
  61. throw new NotImplementedException("Only 2D Toolkit atlases are supported: " + name);
  62. Vector2 minTexCoords = Vector2.one, maxTexCoords = Vector2.zero;
  63. for (int i = 0; i < def.uvs.Length; ++i) {
  64. Vector2 uv = def.uvs[i];
  65. minTexCoords = Vector2.Min(minTexCoords, uv);
  66. maxTexCoords = Vector2.Max(maxTexCoords, uv);
  67. }
  68. regionRotated = def.flipped == tk2dSpriteDefinition.FlipMode.Tk2d;
  69. if (regionRotated) {
  70. float temp = minTexCoords.x;
  71. minTexCoords.x = maxTexCoords.x;
  72. maxTexCoords.x = temp;
  73. }
  74. u = minTexCoords.x;
  75. v = maxTexCoords.y;
  76. u2 = maxTexCoords.x;
  77. v2 = minTexCoords.y;
  78. regionOriginalWidth = (int)(def.untrimmedBoundsData[1].x / def.texelSize.x);
  79. regionOriginalHeight = (int)(def.untrimmedBoundsData[1].y / def.texelSize.y);
  80. regionWidth = (int)(def.boundsData[1].x / def.texelSize.x);
  81. regionHeight = (int)(def.boundsData[1].y / def.texelSize.y);
  82. float x0 = def.untrimmedBoundsData[0].x - def.untrimmedBoundsData[1].x / 2;
  83. float x1 = def.boundsData[0].x - def.boundsData[1].x / 2;
  84. regionOffsetX = (int)((x1 - x0) / def.texelSize.x);
  85. float y0 = def.untrimmedBoundsData[0].y - def.untrimmedBoundsData[1].y / 2;
  86. float y1 = def.boundsData[0].y - def.boundsData[1].y / 2;
  87. regionOffsetY = (int)((y1 - y0) / def.texelSize.y);
  88. material = def.materialInst;
  89. }
  90. public RegionAttachment NewRegionAttachment (Skin skin, String name, String path) {
  91. ProcessSpriteDefinition(path);
  92. RegionAttachment region = new RegionAttachment(name);
  93. region.Path = path;
  94. region.RendererObject = material;
  95. region.SetUVs(u, v, u2, v2, regionRotated);
  96. region.RegionOriginalWidth = regionOriginalWidth;
  97. region.RegionOriginalHeight = regionOriginalHeight;
  98. region.RegionWidth = regionWidth;
  99. region.RegionHeight = regionHeight;
  100. region.RegionOffsetX = regionOffsetX;
  101. region.RegionOffsetY = regionOffsetY;
  102. return region;
  103. }
  104. public MeshAttachment NewMeshAttachment (Skin skin, String name, String path) {
  105. ProcessSpriteDefinition(path);
  106. MeshAttachment mesh = new MeshAttachment(name);
  107. mesh.Path = path;
  108. mesh.RendererObject = material;
  109. mesh.RegionU = u;
  110. mesh.RegionV = v;
  111. mesh.RegionU2 = u2;
  112. mesh.RegionV2 = v2;
  113. mesh.RegionRotate = regionRotated;
  114. mesh.RegionOriginalWidth = regionOriginalWidth;
  115. mesh.RegionOriginalHeight = regionOriginalHeight;
  116. mesh.RegionWidth = regionWidth;
  117. mesh.RegionHeight = regionHeight;
  118. mesh.RegionOffsetX = regionOffsetX;
  119. mesh.RegionOffsetY = regionOffsetY;
  120. return mesh;
  121. }
  122. public BoundingBoxAttachment NewBoundingBoxAttachment (Skin skin, String name) {
  123. return new BoundingBoxAttachment(name);
  124. }
  125. public PathAttachment NewPathAttachment (Skin skin, string name) {
  126. return new PathAttachment(name);
  127. }
  128. public PointAttachment NewPointAttachment (Skin skin, string name) {
  129. return new PointAttachment(name);
  130. }
  131. public ClippingAttachment NewClippingAttachment (Skin skin, string name) {
  132. return new ClippingAttachment(name);
  133. }
  134. }
  135. }
  136. #endif