AttachmentCloneExtensions.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.</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. public static Attachment GetRemappedClone (this Attachment o, Sprite sprite, Material sourceMaterial, bool premultiplyAlpha = true, bool cloneMeshAsLinked = true, bool useOriginalRegionSize = false) {
  80. var atlasRegion = premultiplyAlpha ? sprite.ToAtlasRegionPMAClone(sourceMaterial) : sprite.ToAtlasRegion(new Material(sourceMaterial) { mainTexture = sprite.texture } );
  81. return o.GetRemappedClone(atlasRegion, cloneMeshAsLinked, useOriginalRegionSize, 1f/sprite.pixelsPerUnit);
  82. }
  83. /// <summary>
  84. /// Gets a clone of the attachment remapped with an atlasRegion image.</summary>
  85. /// <returns>The remapped clone.</returns>
  86. /// <param name="o">The original attachment.</param>
  87. /// <param name="atlasRegion">Atlas region.</param>
  88. /// <param name="cloneMeshAsLinked">If <c>true</c> MeshAttachments will be cloned as linked meshes and will inherit animation from the original attachment.</param>
  89. /// <param name="useOriginalRegionSize">If <c>true</c> the size of the original attachment will be followed, instead of using the Sprite size.</param>
  90. /// <param name="scale">Unity units per pixel scale used to scale the atlas region size when not using the original region size.</param>
  91. public static Attachment GetRemappedClone (this Attachment o, AtlasRegion atlasRegion, bool cloneMeshAsLinked = true, bool useOriginalRegionSize = false, float scale = 0.01f) {
  92. var regionAttachment = o as RegionAttachment;
  93. if (regionAttachment != null) {
  94. RegionAttachment newAttachment = (RegionAttachment)regionAttachment.Copy();
  95. newAttachment.SetRegion(atlasRegion, false);
  96. if (!useOriginalRegionSize) {
  97. newAttachment.width = atlasRegion.width * scale;
  98. newAttachment.height = atlasRegion.height * scale;
  99. }
  100. newAttachment.UpdateOffset();
  101. return newAttachment;
  102. } else {
  103. var meshAttachment = o as MeshAttachment;
  104. if (meshAttachment != null) {
  105. MeshAttachment newAttachment = cloneMeshAsLinked ? meshAttachment.NewLinkedMesh() : (MeshAttachment)meshAttachment.Copy();
  106. newAttachment.SetRegion(atlasRegion);
  107. return newAttachment;
  108. }
  109. }
  110. return o.GetCopy(true); // Non-renderable Attachments will return as normal cloned attachments.
  111. }
  112. #endregion
  113. }
  114. }