SpriteCollectionAttachmentLoader.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated July 28, 2023. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2023, 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 or
  13. * 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 THE
  27. * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #if SPINE_TK2D
  30. using Spine;
  31. using System;
  32. using UnityEngine;
  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 AtlasRegion 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. if (regionRotated) {
  83. float tempSwap = regionWidth;
  84. regionWidth = regionHeight;
  85. regionHeight = tempSwap;
  86. }
  87. float x0 = def.untrimmedBoundsData[0].x - def.untrimmedBoundsData[1].x / 2;
  88. float x1 = def.boundsData[0].x - def.boundsData[1].x / 2;
  89. regionOffsetX = (int)((x1 - x0) / def.texelSize.x);
  90. float y0 = def.untrimmedBoundsData[0].y - def.untrimmedBoundsData[1].y / 2;
  91. float y1 = def.boundsData[0].y - def.boundsData[1].y / 2;
  92. regionOffsetY = (int)((y1 - y0) / def.texelSize.y);
  93. material = def.materialInst;
  94. AtlasRegion region = new AtlasRegion();
  95. region.name = name;
  96. AtlasPage page = new AtlasPage();
  97. page.rendererObject = material;
  98. region.page = page;
  99. region.u = u;
  100. region.v = v;
  101. region.u2 = u2;
  102. region.v2 = v2;
  103. region.rotate = regionRotated;
  104. region.degrees = regionRotated ? 90 : 0;
  105. region.originalWidth = (int)regionOriginalWidth;
  106. region.originalHeight = (int)regionOriginalHeight;
  107. region.width = (int)regionWidth;
  108. region.height = (int)regionHeight;
  109. region.offsetX = regionOffsetX;
  110. region.offsetY = regionOffsetY;
  111. return region;
  112. }
  113. private void LoadSequence (string name, string basePath, Sequence sequence) {
  114. TextureRegion[] regions = sequence.Regions;
  115. for (int i = 0, n = regions.Length; i < n; i++) {
  116. string path = sequence.GetPath(basePath, i);
  117. regions[i] = ProcessSpriteDefinition(path);
  118. if (regions[i] == null) throw new ArgumentException(string.Format("Region not found in atlas: {0} (region attachment: {1})", path, name));
  119. }
  120. }
  121. public RegionAttachment NewRegionAttachment (Skin skin, String name, String path, Sequence sequence) {
  122. RegionAttachment attachment = new RegionAttachment(name);
  123. if (sequence != null)
  124. LoadSequence(name, path, sequence);
  125. else {
  126. AtlasRegion region = ProcessSpriteDefinition(path);
  127. if (region == null)
  128. throw new ArgumentException(string.Format("Region not found in atlas: {0} (region attachment: {1})", path, name));
  129. attachment.Region = region;
  130. attachment.Path = path;
  131. }
  132. return attachment;
  133. }
  134. public MeshAttachment NewMeshAttachment (Skin skin, String name, String path, Sequence sequence) {
  135. MeshAttachment attachment = new MeshAttachment(name);
  136. if (sequence != null)
  137. LoadSequence(name, path, sequence);
  138. else {
  139. AtlasRegion region = ProcessSpriteDefinition(path);
  140. if (region == null)
  141. throw new ArgumentException(string.Format("Region not found in atlas: {0} (region attachment: {1})", path, name));
  142. attachment.Region = region;
  143. attachment.Path = path;
  144. }
  145. return attachment;
  146. }
  147. public BoundingBoxAttachment NewBoundingBoxAttachment (Skin skin, String name) {
  148. return new BoundingBoxAttachment(name);
  149. }
  150. public PathAttachment NewPathAttachment (Skin skin, string name) {
  151. return new PathAttachment(name);
  152. }
  153. public PointAttachment NewPointAttachment (Skin skin, string name) {
  154. return new PointAttachment(name);
  155. }
  156. public ClippingAttachment NewClippingAttachment (Skin skin, string name) {
  157. return new ClippingAttachment(name);
  158. }
  159. }
  160. }
  161. #endif