AttachmentRegionExtensions.cs 9.3 KB

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