AttachmentRegionExtensions.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. using System.Collections;
  30. using System.Collections.Generic;
  31. using UnityEngine;
  32. namespace Spine.Unity.AttachmentTools {
  33. public static class AttachmentRegionExtensions {
  34. #region Runtime RegionAttachments
  35. /// <summary>
  36. /// 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>
  37. public static RegionAttachment ToRegionAttachment (this Sprite sprite, Material material, float rotation = 0f) {
  38. return sprite.ToRegionAttachment(material.ToSpineAtlasPage(), rotation);
  39. }
  40. /// <summary>
  41. /// Creates a RegionAttachment based on a sprite. This method creates a real, usable AtlasRegion. That AtlasRegion uses the AtlasPage provided./// </summary>
  42. public static RegionAttachment ToRegionAttachment (this Sprite sprite, AtlasPage page, float rotation = 0f) {
  43. if (sprite == null) throw new System.ArgumentNullException("sprite");
  44. if (page == null) throw new System.ArgumentNullException("page");
  45. AtlasRegion region = sprite.ToAtlasRegion(page);
  46. float unitsPerPixel = 1f / sprite.pixelsPerUnit;
  47. return region.ToRegionAttachment(sprite.name, unitsPerPixel, rotation);
  48. }
  49. /// <summary>
  50. /// Creates a Spine.AtlasRegion that uses a premultiplied alpha duplicate texture of the Sprite's texture data.
  51. /// Returns a RegionAttachment that uses it. Use this if you plan to use a premultiply alpha shader such as "Spine/Skeleton".</summary>
  52. /// <remarks>The duplicate texture is cached for later re-use. See documentation of
  53. /// <see cref="AttachmentCloneExtensions.GetRemappedClone"/> for additional details.</remarks>
  54. public static RegionAttachment ToRegionAttachmentPMAClone (this Sprite sprite, Shader shader, TextureFormat textureFormat = AtlasUtilities.SpineTextureFormat, bool mipmaps = AtlasUtilities.UseMipMaps, Material materialPropertySource = null, float rotation = 0f) {
  55. if (sprite == null) throw new System.ArgumentNullException("sprite");
  56. if (shader == null) throw new System.ArgumentNullException("shader");
  57. AtlasRegion region = sprite.ToAtlasRegionPMAClone(shader, textureFormat, mipmaps, materialPropertySource);
  58. float unitsPerPixel = 1f / sprite.pixelsPerUnit;
  59. return region.ToRegionAttachment(sprite.name, unitsPerPixel, rotation);
  60. }
  61. public static RegionAttachment ToRegionAttachmentPMAClone (this Sprite sprite, Material materialPropertySource, TextureFormat textureFormat = AtlasUtilities.SpineTextureFormat, bool mipmaps = AtlasUtilities.UseMipMaps, float rotation = 0f) {
  62. return sprite.ToRegionAttachmentPMAClone(materialPropertySource.shader, textureFormat, mipmaps, materialPropertySource, rotation);
  63. }
  64. /// <summary>
  65. /// Creates a new RegionAttachment from a given AtlasRegion.</summary>
  66. public static RegionAttachment ToRegionAttachment (this AtlasRegion region, string attachmentName, float scale = 0.01f, float rotation = 0f) {
  67. if (string.IsNullOrEmpty(attachmentName)) throw new System.ArgumentException("attachmentName can't be null or empty.", "attachmentName");
  68. if (region == null) throw new System.ArgumentNullException("region");
  69. // (AtlasAttachmentLoader.cs)
  70. RegionAttachment attachment = new RegionAttachment(attachmentName);
  71. attachment.Region = region;
  72. attachment.Path = region.name;
  73. attachment.ScaleX = 1;
  74. attachment.ScaleY = 1;
  75. attachment.Rotation = rotation;
  76. attachment.R = 1;
  77. attachment.G = 1;
  78. attachment.B = 1;
  79. attachment.A = 1;
  80. // pass OriginalWidth and OriginalHeight because UpdateOffset uses it in its calculation.
  81. TextureRegion textreRegion = attachment.Region;
  82. AtlasRegion atlasRegion = textreRegion as AtlasRegion;
  83. float originalWidth = atlasRegion != null ? atlasRegion.originalWidth : textreRegion.width;
  84. float originalHeight = atlasRegion != null ? atlasRegion.originalHeight : textreRegion.height;
  85. attachment.Width = originalWidth * scale;
  86. attachment.Height = originalHeight * scale;
  87. attachment.SetColor(Color.white);
  88. attachment.UpdateRegion();
  89. return attachment;
  90. }
  91. /// <summary> Sets the scale. Call regionAttachment.UpdateOffset to apply the change.</summary>
  92. public static void SetScale (this RegionAttachment regionAttachment, Vector2 scale) {
  93. regionAttachment.ScaleX = scale.x;
  94. regionAttachment.ScaleY = scale.y;
  95. }
  96. /// <summary> Sets the scale. Call regionAttachment.UpdateOffset to apply the change.</summary>
  97. public static void SetScale (this RegionAttachment regionAttachment, float x, float y) {
  98. regionAttachment.ScaleX = x;
  99. regionAttachment.ScaleY = y;
  100. }
  101. /// <summary> Sets the position offset. Call regionAttachment.UpdateOffset to apply the change.</summary>
  102. public static void SetPositionOffset (this RegionAttachment regionAttachment, Vector2 offset) {
  103. regionAttachment.X = offset.x;
  104. regionAttachment.Y = offset.y;
  105. }
  106. /// <summary> Sets the position offset. Call regionAttachment.UpdateOffset to apply the change.</summary>
  107. public static void SetPositionOffset (this RegionAttachment regionAttachment, float x, float y) {
  108. regionAttachment.X = x;
  109. regionAttachment.Y = y;
  110. }
  111. /// <summary> Sets the rotation. Call regionAttachment.UpdateOffset to apply the change.</summary>
  112. public static void SetRotation (this RegionAttachment regionAttachment, float rotation) {
  113. regionAttachment.Rotation = rotation;
  114. }
  115. #endregion
  116. }
  117. }