AttachmentRegionExtensions.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. using UnityEngine;
  30. using System.Collections.Generic;
  31. using System.Collections;
  32. namespace Spine.Unity.AttachmentTools {
  33. public static class AttachmentRegionExtensions {
  34. #region GetRegion
  35. /// <summary>
  36. /// Tries to get the region (image) of a renderable attachment. If the attachment is not renderable, it returns null.</summary>
  37. public static AtlasRegion GetRegion (this Attachment attachment) {
  38. var renderableAttachment = attachment as IHasRendererObject;
  39. if (renderableAttachment != null)
  40. return renderableAttachment.RendererObject as AtlasRegion;
  41. return null;
  42. }
  43. /// <summary>Gets the region (image) of a RegionAttachment</summary>
  44. public static AtlasRegion GetRegion (this RegionAttachment regionAttachment) {
  45. return regionAttachment.RendererObject as AtlasRegion;
  46. }
  47. /// <summary>Gets the region (image) of a MeshAttachment</summary>
  48. public static AtlasRegion GetRegion (this MeshAttachment meshAttachment) {
  49. return meshAttachment.RendererObject as AtlasRegion;
  50. }
  51. #endregion
  52. #region SetRegion
  53. /// <summary>
  54. /// Tries to set the region (image) of a renderable attachment. If the attachment is not renderable, nothing is applied.</summary>
  55. public static void SetRegion (this Attachment attachment, AtlasRegion region, bool updateOffset = true) {
  56. var regionAttachment = attachment as RegionAttachment;
  57. if (regionAttachment != null)
  58. regionAttachment.SetRegion(region, updateOffset);
  59. var meshAttachment = attachment as MeshAttachment;
  60. if (meshAttachment != null)
  61. meshAttachment.SetRegion(region, updateOffset);
  62. }
  63. /// <summary>Sets the region (image) of a RegionAttachment</summary>
  64. public static void SetRegion (this RegionAttachment attachment, AtlasRegion region, bool updateOffset = true) {
  65. if (region == null) throw new System.ArgumentNullException("region");
  66. // (AtlasAttachmentLoader.cs)
  67. attachment.RendererObject = region;
  68. attachment.SetUVs(region.u, region.v, region.u2, region.v2, region.rotate);
  69. attachment.regionOffsetX = region.offsetX;
  70. attachment.regionOffsetY = region.offsetY;
  71. attachment.regionWidth = region.width;
  72. attachment.regionHeight = region.height;
  73. attachment.regionOriginalWidth = region.originalWidth;
  74. attachment.regionOriginalHeight = region.originalHeight;
  75. if (updateOffset) attachment.UpdateOffset();
  76. }
  77. /// <summary>Sets the region (image) of a MeshAttachment</summary>
  78. public static void SetRegion (this MeshAttachment attachment, AtlasRegion region, bool updateUVs = true) {
  79. if (region == null) throw new System.ArgumentNullException("region");
  80. // (AtlasAttachmentLoader.cs)
  81. attachment.RendererObject = region;
  82. attachment.RegionU = region.u;
  83. attachment.RegionV = region.v;
  84. attachment.RegionU2 = region.u2;
  85. attachment.RegionV2 = region.v2;
  86. attachment.RegionRotate = region.rotate;
  87. attachment.regionOffsetX = region.offsetX;
  88. attachment.regionOffsetY = region.offsetY;
  89. attachment.regionWidth = region.width;
  90. attachment.regionHeight = region.height;
  91. attachment.regionOriginalWidth = region.originalWidth;
  92. attachment.regionOriginalHeight = region.originalHeight;
  93. if (updateUVs) attachment.UpdateUVs();
  94. }
  95. #endregion
  96. #region Runtime RegionAttachments
  97. /// <summary>
  98. /// Creates a RegionAttachment based on a sprite. This method creates a real, usable AtlasRegion. That AtlasRegion uses a new AtlasPage with the Material provided./// </summary>
  99. public static RegionAttachment ToRegionAttachment (this Sprite sprite, Material material, float rotation = 0f) {
  100. return sprite.ToRegionAttachment(material.ToSpineAtlasPage(), rotation);
  101. }
  102. /// <summary>
  103. /// Creates a RegionAttachment based on a sprite. This method creates a real, usable AtlasRegion. That AtlasRegion uses the AtlasPage provided./// </summary>
  104. public static RegionAttachment ToRegionAttachment (this Sprite sprite, AtlasPage page, float rotation = 0f) {
  105. if (sprite == null) throw new System.ArgumentNullException("sprite");
  106. if (page == null) throw new System.ArgumentNullException("page");
  107. var region = sprite.ToAtlasRegion(page);
  108. var unitsPerPixel = 1f / sprite.pixelsPerUnit;
  109. return region.ToRegionAttachment(sprite.name, unitsPerPixel, rotation);
  110. }
  111. /// <summary>
  112. /// Creates a Spine.AtlasRegion that uses a premultiplied alpha duplicate texture of the Sprite's texture data. Returns a RegionAttachment that uses it. Use this if you plan to use a premultiply alpha shader such as "Spine/Skeleton"</summary>
  113. public static RegionAttachment ToRegionAttachmentPMAClone (this Sprite sprite, Shader shader, TextureFormat textureFormat = AtlasUtilities.SpineTextureFormat, bool mipmaps = AtlasUtilities.UseMipMaps, Material materialPropertySource = null, float rotation = 0f) {
  114. if (sprite == null) throw new System.ArgumentNullException("sprite");
  115. if (shader == null) throw new System.ArgumentNullException("shader");
  116. var region = sprite.ToAtlasRegionPMAClone(shader, textureFormat, mipmaps, materialPropertySource);
  117. var unitsPerPixel = 1f / sprite.pixelsPerUnit;
  118. return region.ToRegionAttachment(sprite.name, unitsPerPixel, rotation);
  119. }
  120. public static RegionAttachment ToRegionAttachmentPMAClone (this Sprite sprite, Material materialPropertySource, TextureFormat textureFormat = AtlasUtilities.SpineTextureFormat, bool mipmaps = AtlasUtilities.UseMipMaps, float rotation = 0f) {
  121. return sprite.ToRegionAttachmentPMAClone(materialPropertySource.shader, textureFormat, mipmaps, materialPropertySource, rotation);
  122. }
  123. /// <summary>
  124. /// Creates a new RegionAttachment from a given AtlasRegion.</summary>
  125. public static RegionAttachment ToRegionAttachment (this AtlasRegion region, string attachmentName, float scale = 0.01f, float rotation = 0f) {
  126. if (string.IsNullOrEmpty(attachmentName)) throw new System.ArgumentException("attachmentName can't be null or empty.", "attachmentName");
  127. if (region == null) throw new System.ArgumentNullException("region");
  128. // (AtlasAttachmentLoader.cs)
  129. var attachment = new RegionAttachment(attachmentName);
  130. attachment.RendererObject = region;
  131. attachment.SetUVs(region.u, region.v, region.u2, region.v2, region.rotate);
  132. attachment.regionOffsetX = region.offsetX;
  133. attachment.regionOffsetY = region.offsetY;
  134. attachment.regionWidth = region.width;
  135. attachment.regionHeight = region.height;
  136. attachment.regionOriginalWidth = region.originalWidth;
  137. attachment.regionOriginalHeight = region.originalHeight;
  138. attachment.Path = region.name;
  139. attachment.scaleX = 1;
  140. attachment.scaleY = 1;
  141. attachment.rotation = rotation;
  142. attachment.r = 1;
  143. attachment.g = 1;
  144. attachment.b = 1;
  145. attachment.a = 1;
  146. // pass OriginalWidth and OriginalHeight because UpdateOffset uses it in its calculation.
  147. attachment.width = attachment.regionOriginalWidth * scale;
  148. attachment.height = attachment.regionOriginalHeight * scale;
  149. attachment.SetColor(Color.white);
  150. attachment.UpdateOffset();
  151. return attachment;
  152. }
  153. /// <summary> Sets the scale. Call regionAttachment.UpdateOffset to apply the change.</summary>
  154. public static void SetScale (this RegionAttachment regionAttachment, Vector2 scale) {
  155. regionAttachment.scaleX = scale.x;
  156. regionAttachment.scaleY = scale.y;
  157. }
  158. /// <summary> Sets the scale. Call regionAttachment.UpdateOffset to apply the change.</summary>
  159. public static void SetScale (this RegionAttachment regionAttachment, float x, float y) {
  160. regionAttachment.scaleX = x;
  161. regionAttachment.scaleY = y;
  162. }
  163. /// <summary> Sets the position offset. Call regionAttachment.UpdateOffset to apply the change.</summary>
  164. public static void SetPositionOffset (this RegionAttachment regionAttachment, Vector2 offset) {
  165. regionAttachment.x = offset.x;
  166. regionAttachment.y = offset.y;
  167. }
  168. /// <summary> Sets the position offset. Call regionAttachment.UpdateOffset to apply the change.</summary>
  169. public static void SetPositionOffset (this RegionAttachment regionAttachment, float x, float y) {
  170. regionAttachment.x = x;
  171. regionAttachment.y = y;
  172. }
  173. /// <summary> Sets the rotation. Call regionAttachment.UpdateOffset to apply the change.</summary>
  174. public static void SetRotation (this RegionAttachment regionAttachment, float rotation) {
  175. regionAttachment.rotation = rotation;
  176. }
  177. #endregion
  178. }
  179. }