BlendModeMaterialsAsset.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 Spine;
  30. using Spine.Unity;
  31. using System;
  32. using System.Collections;
  33. using System.Collections.Generic;
  34. using UnityEngine;
  35. namespace Spine.Unity {
  36. [CreateAssetMenu(menuName = "Spine/SkeletonData Modifiers/Blend Mode Materials", order = 200)]
  37. public class BlendModeMaterialsAsset : SkeletonDataModifierAsset {
  38. public Material multiplyMaterialTemplate;
  39. public Material screenMaterialTemplate;
  40. public Material additiveMaterialTemplate;
  41. public bool applyAdditiveMaterial = true;
  42. public override void Apply (SkeletonData skeletonData) {
  43. ApplyMaterials(skeletonData, multiplyMaterialTemplate, screenMaterialTemplate, additiveMaterialTemplate, applyAdditiveMaterial);
  44. }
  45. public static void ApplyMaterials (SkeletonData skeletonData, Material multiplyTemplate, Material screenTemplate, Material additiveTemplate, bool includeAdditiveSlots) {
  46. if (skeletonData == null) throw new ArgumentNullException("skeletonData");
  47. using (AtlasMaterialCache materialCache = new AtlasMaterialCache()) {
  48. List<Skin.SkinEntry> entryBuffer = new List<Skin.SkinEntry>();
  49. SlotData[] slotsItems = skeletonData.Slots.Items;
  50. for (int slotIndex = 0, slotCount = skeletonData.Slots.Count; slotIndex < slotCount; slotIndex++) {
  51. SlotData slot = slotsItems[slotIndex];
  52. if (slot.BlendMode == BlendMode.Normal) continue;
  53. if (!includeAdditiveSlots && slot.BlendMode == BlendMode.Additive) continue;
  54. entryBuffer.Clear();
  55. foreach (Skin skin in skeletonData.Skins)
  56. skin.GetAttachments(slotIndex, entryBuffer);
  57. Material templateMaterial = null;
  58. switch (slot.BlendMode) {
  59. case BlendMode.Multiply:
  60. templateMaterial = multiplyTemplate;
  61. break;
  62. case BlendMode.Screen:
  63. templateMaterial = screenTemplate;
  64. break;
  65. case BlendMode.Additive:
  66. templateMaterial = additiveTemplate;
  67. break;
  68. }
  69. if (templateMaterial == null) continue;
  70. foreach (Skin.SkinEntry entry in entryBuffer) {
  71. IHasTextureRegion renderableAttachment = entry.Attachment as IHasTextureRegion;
  72. if (renderableAttachment != null) {
  73. renderableAttachment.Region = materialCache.CloneAtlasRegionWithMaterial(
  74. (AtlasRegion)renderableAttachment.Region, templateMaterial);
  75. }
  76. }
  77. }
  78. }
  79. //attachmentBuffer.Clear();
  80. }
  81. class AtlasMaterialCache : IDisposable {
  82. readonly Dictionary<KeyValuePair<AtlasPage, Material>, AtlasPage> cache = new Dictionary<KeyValuePair<AtlasPage, Material>, AtlasPage>();
  83. /// <summary>Creates a clone of an AtlasRegion that uses different Material settings, while retaining the original texture.</summary>
  84. public AtlasRegion CloneAtlasRegionWithMaterial (AtlasRegion originalRegion, Material materialTemplate) {
  85. AtlasRegion newRegion = originalRegion.Clone();
  86. newRegion.page = GetAtlasPageWithMaterial(originalRegion.page, materialTemplate);
  87. return newRegion;
  88. }
  89. AtlasPage GetAtlasPageWithMaterial (AtlasPage originalPage, Material materialTemplate) {
  90. if (originalPage == null) throw new ArgumentNullException("originalPage");
  91. AtlasPage newPage = null;
  92. var key = new KeyValuePair<AtlasPage, Material>(originalPage, materialTemplate);
  93. cache.TryGetValue(key, out newPage);
  94. if (newPage == null) {
  95. newPage = originalPage.Clone();
  96. Material originalMaterial = originalPage.rendererObject as Material;
  97. newPage.rendererObject = new Material(materialTemplate) {
  98. name = originalMaterial.name + " " + materialTemplate.name,
  99. mainTexture = originalMaterial.mainTexture
  100. };
  101. cache.Add(key, newPage);
  102. }
  103. return newPage;
  104. }
  105. public void Dispose () {
  106. cache.Clear();
  107. }
  108. }
  109. }
  110. }