AttachmentRegionExtensions.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.RegionDegrees = region.degrees;
  88. attachment.regionOffsetX = region.offsetX;
  89. attachment.regionOffsetY = region.offsetY;
  90. attachment.regionWidth = region.width;
  91. attachment.regionHeight = region.height;
  92. attachment.regionOriginalWidth = region.originalWidth;
  93. attachment.regionOriginalHeight = region.originalHeight;
  94. if (updateUVs) attachment.UpdateUVs();
  95. }
  96. #endregion
  97. #region Runtime RegionAttachments
  98. /// <summary>
  99. /// 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>
  100. public static RegionAttachment ToRegionAttachment (this Sprite sprite, Material material, float rotation = 0f) {
  101. return sprite.ToRegionAttachment(material.ToSpineAtlasPage(), rotation);
  102. }
  103. /// <summary>
  104. /// Creates a RegionAttachment based on a sprite. This method creates a real, usable AtlasRegion. That AtlasRegion uses the AtlasPage provided./// </summary>
  105. public static RegionAttachment ToRegionAttachment (this Sprite sprite, AtlasPage page, float rotation = 0f) {
  106. if (sprite == null) throw new System.ArgumentNullException("sprite");
  107. if (page == null) throw new System.ArgumentNullException("page");
  108. var region = sprite.ToAtlasRegion(page);
  109. var unitsPerPixel = 1f / sprite.pixelsPerUnit;
  110. return region.ToRegionAttachment(sprite.name, unitsPerPixel, rotation);
  111. }
  112. /// <summary>
  113. /// Creates a Spine.AtlasRegion that uses a premultiplied alpha duplicate texture of the Sprite's texture data.
  114. /// Returns a RegionAttachment that uses it. Use this if you plan to use a premultiply alpha shader such as "Spine/Skeleton".</summary>
  115. /// <remarks>The duplicate texture is cached for later re-use. See documentation of
  116. /// <see cref="AttachmentCloneExtensions.GetRemappedClone"/> for additional details.</remarks>
  117. public static RegionAttachment ToRegionAttachmentPMAClone (this Sprite sprite, Shader shader, TextureFormat textureFormat = AtlasUtilities.SpineTextureFormat, bool mipmaps = AtlasUtilities.UseMipMaps, Material materialPropertySource = null, float rotation = 0f) {
  118. if (sprite == null) throw new System.ArgumentNullException("sprite");
  119. if (shader == null) throw new System.ArgumentNullException("shader");
  120. var region = sprite.ToAtlasRegionPMAClone(shader, textureFormat, mipmaps, materialPropertySource);
  121. var unitsPerPixel = 1f / sprite.pixelsPerUnit;
  122. return region.ToRegionAttachment(sprite.name, unitsPerPixel, rotation);
  123. }
  124. public static RegionAttachment ToRegionAttachmentPMAClone (this Sprite sprite, Material materialPropertySource, TextureFormat textureFormat = AtlasUtilities.SpineTextureFormat, bool mipmaps = AtlasUtilities.UseMipMaps, float rotation = 0f) {
  125. return sprite.ToRegionAttachmentPMAClone(materialPropertySource.shader, textureFormat, mipmaps, materialPropertySource, rotation);
  126. }
  127. /// <summary>
  128. /// Creates a new RegionAttachment from a given AtlasRegion.</summary>
  129. public static RegionAttachment ToRegionAttachment (this AtlasRegion region, string attachmentName, float scale = 0.01f, float rotation = 0f) {
  130. if (string.IsNullOrEmpty(attachmentName)) throw new System.ArgumentException("attachmentName can't be null or empty.", "attachmentName");
  131. if (region == null) throw new System.ArgumentNullException("region");
  132. // (AtlasAttachmentLoader.cs)
  133. var attachment = new RegionAttachment(attachmentName);
  134. attachment.RendererObject = region;
  135. attachment.SetUVs(region.u, region.v, region.u2, region.v2, region.rotate);
  136. attachment.regionOffsetX = region.offsetX;
  137. attachment.regionOffsetY = region.offsetY;
  138. attachment.regionWidth = region.width;
  139. attachment.regionHeight = region.height;
  140. attachment.regionOriginalWidth = region.originalWidth;
  141. attachment.regionOriginalHeight = region.originalHeight;
  142. attachment.Path = region.name;
  143. attachment.scaleX = 1;
  144. attachment.scaleY = 1;
  145. attachment.rotation = rotation;
  146. attachment.r = 1;
  147. attachment.g = 1;
  148. attachment.b = 1;
  149. attachment.a = 1;
  150. // pass OriginalWidth and OriginalHeight because UpdateOffset uses it in its calculation.
  151. attachment.width = attachment.regionOriginalWidth * scale;
  152. attachment.height = attachment.regionOriginalHeight * scale;
  153. attachment.SetColor(Color.white);
  154. attachment.UpdateOffset();
  155. return attachment;
  156. }
  157. /// <summary> Sets the scale. Call regionAttachment.UpdateOffset to apply the change.</summary>
  158. public static void SetScale (this RegionAttachment regionAttachment, Vector2 scale) {
  159. regionAttachment.scaleX = scale.x;
  160. regionAttachment.scaleY = scale.y;
  161. }
  162. /// <summary> Sets the scale. Call regionAttachment.UpdateOffset to apply the change.</summary>
  163. public static void SetScale (this RegionAttachment regionAttachment, float x, float y) {
  164. regionAttachment.scaleX = x;
  165. regionAttachment.scaleY = y;
  166. }
  167. /// <summary> Sets the position offset. Call regionAttachment.UpdateOffset to apply the change.</summary>
  168. public static void SetPositionOffset (this RegionAttachment regionAttachment, Vector2 offset) {
  169. regionAttachment.x = offset.x;
  170. regionAttachment.y = offset.y;
  171. }
  172. /// <summary> Sets the position offset. Call regionAttachment.UpdateOffset to apply the change.</summary>
  173. public static void SetPositionOffset (this RegionAttachment regionAttachment, float x, float y) {
  174. regionAttachment.x = x;
  175. regionAttachment.y = y;
  176. }
  177. /// <summary> Sets the rotation. Call regionAttachment.UpdateOffset to apply the change.</summary>
  178. public static void SetRotation (this RegionAttachment regionAttachment, float rotation) {
  179. regionAttachment.rotation = rotation;
  180. }
  181. #endregion
  182. }
  183. }