AttachmentCloneExtensions.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 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. /// <remarks>When parameter <c>premultiplyAlpha</c> is set to <c>true</c>, a premultiply alpha clone of the
  52. /// original texture will be created. Additionally, this PMA Texture clone is cached for later re-use,
  53. /// which might steadily increase the Texture memory footprint when used excessively.
  54. /// See <see cref="AtlasUtilities.ClearCache()"/> on how to clear these cached textures.</remarks>
  55. public static Attachment GetRemappedClone (this Attachment o, Sprite sprite, Material sourceMaterial,
  56. bool premultiplyAlpha = true, bool cloneMeshAsLinked = true, bool useOriginalRegionSize = false,
  57. bool pivotShiftsMeshUVCoords = true, bool useOriginalRegionScale = false) {
  58. var atlasRegion = premultiplyAlpha ? sprite.ToAtlasRegionPMAClone(sourceMaterial) : sprite.ToAtlasRegion(new Material(sourceMaterial) { mainTexture = sprite.texture });
  59. if (!pivotShiftsMeshUVCoords && o is MeshAttachment) {
  60. // prevent non-central sprite pivot setting offsetX/Y and shifting uv coords out of mesh bounds
  61. atlasRegion.offsetX = 0;
  62. atlasRegion.offsetY = 0;
  63. }
  64. float scale = 1f / sprite.pixelsPerUnit;
  65. if (useOriginalRegionScale) {
  66. var regionAttachment = o as RegionAttachment;
  67. if (regionAttachment != null)
  68. scale = regionAttachment.Width / regionAttachment.RegionOriginalWidth;
  69. }
  70. return o.GetRemappedClone(atlasRegion, cloneMeshAsLinked, useOriginalRegionSize, scale);
  71. }
  72. /// <summary>
  73. /// Gets a clone of the attachment remapped with an atlasRegion image.</summary>
  74. /// <returns>The remapped clone.</returns>
  75. /// <param name="o">The original attachment.</param>
  76. /// <param name="atlasRegion">Atlas region.</param>
  77. /// <param name="cloneMeshAsLinked">If <c>true</c> MeshAttachments will be cloned as linked meshes and will inherit animation from the original attachment.</param>
  78. /// <param name="useOriginalRegionSize">If <c>true</c> the size of the original attachment will be followed, instead of using the Sprite size.</param>
  79. /// <param name="scale">Unity units per pixel scale used to scale the atlas region size when not using the original region size.</param>
  80. public static Attachment GetRemappedClone (this Attachment o, AtlasRegion atlasRegion, bool cloneMeshAsLinked = true, bool useOriginalRegionSize = false, float scale = 0.01f) {
  81. var regionAttachment = o as RegionAttachment;
  82. if (regionAttachment != null) {
  83. RegionAttachment newAttachment = (RegionAttachment)regionAttachment.Copy();
  84. newAttachment.SetRegion(atlasRegion, false);
  85. if (!useOriginalRegionSize) {
  86. newAttachment.Width = atlasRegion.width * scale;
  87. newAttachment.Height = atlasRegion.height * scale;
  88. }
  89. newAttachment.UpdateOffset();
  90. return newAttachment;
  91. } else {
  92. var meshAttachment = o as MeshAttachment;
  93. if (meshAttachment != null) {
  94. MeshAttachment newAttachment = cloneMeshAsLinked ? meshAttachment.NewLinkedMesh() : (MeshAttachment)meshAttachment.Copy();
  95. newAttachment.SetRegion(atlasRegion);
  96. return newAttachment;
  97. }
  98. }
  99. return o.Copy();
  100. }
  101. #endregion
  102. }
  103. }