AttachmentCloneExtensions.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 AttachmentCloneExtensions {
  34. /// <summary>
  35. /// Clones the attachment.</summary>
  36. public static Attachment GetCopy (this Attachment o, bool cloneMeshesAsLinked) {
  37. var meshAttachment = o as MeshAttachment;
  38. if (meshAttachment != null && cloneMeshesAsLinked)
  39. return meshAttachment.NewLinkedMesh();
  40. return o.Copy();
  41. }
  42. #region Runtime Linked MeshAttachments
  43. /// <summary>
  44. /// Returns a new linked mesh linked to this MeshAttachment. It will be mapped to the AtlasRegion provided.</summary>
  45. public static MeshAttachment GetLinkedMesh (this MeshAttachment o, string newLinkedMeshName, AtlasRegion region) {
  46. if (region == null) throw new System.ArgumentNullException("region");
  47. MeshAttachment mesh = o.NewLinkedMesh();
  48. mesh.SetRegion(region, false);
  49. return mesh;
  50. }
  51. /// <summary>
  52. /// Returns a new linked mesh linked to this MeshAttachment. It will be mapped to an AtlasRegion generated from a Sprite. The AtlasRegion will be mapped to a new Material based on the shader.
  53. /// For better caching and batching, use GetLinkedMesh(string, AtlasRegion, bool)</summary>
  54. public static MeshAttachment GetLinkedMesh (this MeshAttachment o, Sprite sprite, Shader shader, Material materialPropertySource = null) {
  55. var m = new Material(shader);
  56. if (materialPropertySource != null) {
  57. m.CopyPropertiesFromMaterial(materialPropertySource);
  58. m.shaderKeywords = materialPropertySource.shaderKeywords;
  59. }
  60. return o.GetLinkedMesh(sprite.name, sprite.ToAtlasRegion());
  61. }
  62. /// <summary>
  63. /// Returns a new linked mesh linked to this MeshAttachment. It will be mapped to an AtlasRegion generated from a Sprite. The AtlasRegion will be mapped to a new Material based on the shader.
  64. /// For better caching and batching, use GetLinkedMesh(string, AtlasRegion, bool)</summary>
  65. public static MeshAttachment GetLinkedMesh (this MeshAttachment o, Sprite sprite, Material materialPropertySource) {
  66. return o.GetLinkedMesh(sprite, materialPropertySource.shader, materialPropertySource);
  67. }
  68. #endregion
  69. #region RemappedClone Convenience Methods
  70. /// <summary>
  71. /// Gets a clone of the attachment remapped with a sprite image.</summary>
  72. /// <returns>The remapped clone.</returns>
  73. /// <param name="o">The original attachment.</param>
  74. /// <param name="sprite">The sprite whose texture to use.</param>
  75. /// <param name="sourceMaterial">The source material used to copy the shader and material properties from.</param>
  76. /// <param name="premultiplyAlpha">If <c>true</c>, a premultiply alpha clone of the original texture will be created.
  77. /// See remarks below for additional info.</param>
  78. /// <param name="cloneMeshAsLinked">If <c>true</c> MeshAttachments will be cloned as linked meshes and will inherit animation from the original attachment.</param>
  79. /// <param name="useOriginalRegionSize">If <c>true</c> the size of the original attachment will be followed, instead of using the Sprite size.</param>
  80. /// <param name="pivotShiftsMeshUVCoords">If <c>true</c> and the original Attachment is a MeshAttachment, then
  81. /// a non-central sprite pivot will shift uv coords in the opposite direction. Vertices will not be offset in
  82. /// any case when the original Attachment is a MeshAttachment.</param>
  83. /// <param name="useOriginalRegionScale">If <c>true</c> and the original Attachment is a RegionAttachment, then
  84. /// the original region's scale value is used instead of the Sprite's pixels per unit property. Since uniform scale is used,
  85. /// x scale of the original attachment (width scale) is used, scale in y direction (height scale) is ignored.</param>
  86. /// <remarks>When parameter <c>premultiplyAlpha</c> is set to <c>true</c>, a premultiply alpha clone of the
  87. /// original texture will be created. Additionally, this PMA Texture clone is cached for later re-use,
  88. /// which might steadily increase the Texture memory footprint when used excessively.
  89. /// See <see cref="AtlasUtilities.ClearCache()"/> on how to clear these cached textures.</remarks>
  90. public static Attachment GetRemappedClone (this Attachment o, Sprite sprite, Material sourceMaterial,
  91. bool premultiplyAlpha = true, bool cloneMeshAsLinked = true, bool useOriginalRegionSize = false,
  92. bool pivotShiftsMeshUVCoords = true, bool useOriginalRegionScale = false) {
  93. var atlasRegion = premultiplyAlpha ? sprite.ToAtlasRegionPMAClone(sourceMaterial) : sprite.ToAtlasRegion(new Material(sourceMaterial) { mainTexture = sprite.texture } );
  94. if (!pivotShiftsMeshUVCoords && o is MeshAttachment) {
  95. // prevent non-central sprite pivot setting offsetX/Y and shifting uv coords out of mesh bounds
  96. atlasRegion.offsetX = 0;
  97. atlasRegion.offsetY = 0;
  98. }
  99. float scale = 1f / sprite.pixelsPerUnit;
  100. if (useOriginalRegionScale) {
  101. var regionAttachment = o as RegionAttachment;
  102. if (regionAttachment != null)
  103. scale = regionAttachment.width / regionAttachment.regionOriginalWidth;
  104. }
  105. return o.GetRemappedClone(atlasRegion, cloneMeshAsLinked, useOriginalRegionSize, scale);
  106. }
  107. /// <summary>
  108. /// Gets a clone of the attachment remapped with an atlasRegion image.</summary>
  109. /// <returns>The remapped clone.</returns>
  110. /// <param name="o">The original attachment.</param>
  111. /// <param name="atlasRegion">Atlas region.</param>
  112. /// <param name="cloneMeshAsLinked">If <c>true</c> MeshAttachments will be cloned as linked meshes and will inherit animation from the original attachment.</param>
  113. /// <param name="useOriginalRegionSize">If <c>true</c> the size of the original attachment will be followed, instead of using the Sprite size.</param>
  114. /// <param name="scale">Unity units per pixel scale used to scale the atlas region size when not using the original region size.</param>
  115. public static Attachment GetRemappedClone (this Attachment o, AtlasRegion atlasRegion, bool cloneMeshAsLinked = true, bool useOriginalRegionSize = false, float scale = 0.01f) {
  116. var regionAttachment = o as RegionAttachment;
  117. if (regionAttachment != null) {
  118. RegionAttachment newAttachment = (RegionAttachment)regionAttachment.Copy();
  119. newAttachment.SetRegion(atlasRegion, false);
  120. if (!useOriginalRegionSize) {
  121. newAttachment.width = atlasRegion.width * scale;
  122. newAttachment.height = atlasRegion.height * scale;
  123. }
  124. newAttachment.UpdateOffset();
  125. return newAttachment;
  126. } else {
  127. var meshAttachment = o as MeshAttachment;
  128. if (meshAttachment != null) {
  129. MeshAttachment newAttachment = cloneMeshAsLinked ? meshAttachment.NewLinkedMesh() : (MeshAttachment)meshAttachment.Copy();
  130. newAttachment.SetRegion(atlasRegion);
  131. return newAttachment;
  132. }
  133. }
  134. return o.GetCopy(true); // Non-renderable Attachments will return as normal cloned attachments.
  135. }
  136. #endregion
  137. }
  138. }