AttachmentCloneExtensions.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 AttachmentCloneExtensions {
  34. #region RemappedClone Convenience Methods
  35. /// <summary>
  36. /// Gets a clone of the attachment remapped with a sprite image.</summary>
  37. /// <returns>The remapped clone.</returns>
  38. /// <param name="o">The original attachment.</param>
  39. /// <param name="sprite">The sprite whose texture to use.</param>
  40. /// <param name="sourceMaterial">The source material used to copy the shader and material properties from.</param>
  41. /// <param name="premultiplyAlpha">If <c>true</c>, a premultiply alpha clone of the original texture will be created.
  42. /// See remarks below for additional info.</param>
  43. /// <param name="cloneMeshAsLinked">If <c>true</c> MeshAttachments will be cloned as linked meshes and will inherit animation from the original attachment.</param>
  44. /// <param name="useOriginalRegionSize">If <c>true</c> the size of the original attachment will be followed, instead of using the Sprite size.</param>
  45. /// <param name="pivotShiftsMeshUVCoords">If <c>true</c> and the original Attachment is a MeshAttachment, then
  46. /// a non-central sprite pivot will shift uv coords in the opposite direction. Vertices will not be offset in
  47. /// any case when the original Attachment is a MeshAttachment.</param>
  48. /// <param name="useOriginalRegionScale">If <c>true</c> and the original Attachment is a RegionAttachment, then
  49. /// the original region's scale value is used instead of the Sprite's pixels per unit property. Since uniform scale is used,
  50. /// x scale of the original attachment (width scale) is used, scale in y direction (height scale) is ignored.</param>
  51. /// <param name="pmaCloneTextureFormat">If <c>premultiplyAlpha</c> is <c>true></c>, the TextureFormat of the
  52. /// newly created PMA attachment Texture.</param>
  53. /// <param name="pmaCloneMipmaps">If <c>premultiplyAlpha</c> is <ctrue></c>, whether the newly created
  54. /// PMA attachment Texture has mipmaps enabled.</param>
  55. /// <remarks>When parameter <c>premultiplyAlpha</c> is set to <c>true</c>, a premultiply alpha clone of the
  56. /// original texture will be created. Additionally, this PMA Texture clone is cached for later re-use,
  57. /// which might steadily increase the Texture memory footprint when used excessively.
  58. /// See <see cref="AtlasUtilities.ClearCache()"/> on how to clear these cached textures.</remarks>
  59. public static Attachment GetRemappedClone (this Attachment o, Sprite sprite, Material sourceMaterial,
  60. bool premultiplyAlpha = true, bool cloneMeshAsLinked = true, bool useOriginalRegionSize = false,
  61. bool pivotShiftsMeshUVCoords = true, bool useOriginalRegionScale = false,
  62. TextureFormat pmaCloneTextureFormat = AtlasUtilities.SpineTextureFormat,
  63. bool pmaCloneMipmaps = AtlasUtilities.UseMipMaps) {
  64. AtlasRegion atlasRegion = premultiplyAlpha ?
  65. sprite.ToAtlasRegionPMAClone(sourceMaterial, pmaCloneTextureFormat, pmaCloneMipmaps) :
  66. sprite.ToAtlasRegion(new Material(sourceMaterial) { mainTexture = sprite.texture });
  67. if (!pivotShiftsMeshUVCoords && o is MeshAttachment) {
  68. // prevent non-central sprite pivot setting offsetX/Y and shifting uv coords out of mesh bounds
  69. atlasRegion.offsetX = 0;
  70. atlasRegion.offsetY = 0;
  71. }
  72. float scale = 1f / sprite.pixelsPerUnit;
  73. if (useOriginalRegionScale) {
  74. RegionAttachment regionAttachment = o as RegionAttachment;
  75. if (regionAttachment != null)
  76. scale = regionAttachment.Width / regionAttachment.Region.OriginalWidth;
  77. }
  78. return o.GetRemappedClone(atlasRegion, cloneMeshAsLinked, useOriginalRegionSize, scale);
  79. }
  80. /// <summary>
  81. /// Gets a clone of the attachment remapped with an atlasRegion image.</summary>
  82. /// <returns>The remapped clone.</returns>
  83. /// <param name="o">The original attachment.</param>
  84. /// <param name="atlasRegion">Atlas region.</param>
  85. /// <param name="cloneMeshAsLinked">If <c>true</c> MeshAttachments will be cloned as linked meshes and will inherit animation from the original attachment.</param>
  86. /// <param name="useOriginalRegionSize">If <c>true</c> the size of the original attachment will be followed, instead of using the Sprite size.</param>
  87. /// <param name="scale">Unity units per pixel scale used to scale the atlas region size when not using the original region size.</param>
  88. public static Attachment GetRemappedClone (this Attachment o, AtlasRegion atlasRegion, bool cloneMeshAsLinked = true, bool useOriginalRegionSize = false, float scale = 0.01f) {
  89. RegionAttachment regionAttachment = o as RegionAttachment;
  90. if (regionAttachment != null) {
  91. RegionAttachment newAttachment = (RegionAttachment)regionAttachment.Copy();
  92. newAttachment.Region = atlasRegion;
  93. if (!useOriginalRegionSize) {
  94. newAttachment.Width = atlasRegion.width * scale;
  95. newAttachment.Height = atlasRegion.height * scale;
  96. }
  97. newAttachment.UpdateRegion();
  98. return newAttachment;
  99. } else {
  100. MeshAttachment meshAttachment = o as MeshAttachment;
  101. if (meshAttachment != null) {
  102. MeshAttachment newAttachment = cloneMeshAsLinked ? meshAttachment.NewLinkedMesh() : (MeshAttachment)meshAttachment.Copy();
  103. newAttachment.Region = atlasRegion;
  104. newAttachment.UpdateRegion();
  105. return newAttachment;
  106. }
  107. }
  108. return o.Copy();
  109. }
  110. #endregion
  111. }
  112. }