BlendModeMaterialsAsset.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using UnityEngine;
  33. using Spine;
  34. using Spine.Unity;
  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 (var materialCache = new AtlasMaterialCache()) {
  48. var entryBuffer = new List<Skin.SkinEntry>();
  49. var slotsItems = skeletonData.Slots.Items;
  50. for (int slotIndex = 0, slotCount = skeletonData.Slots.Count; slotIndex < slotCount; slotIndex++) {
  51. var slot = slotsItems[slotIndex];
  52. if (slot.blendMode == BlendMode.Normal) continue;
  53. if (!includeAdditiveSlots && slot.blendMode == BlendMode.Additive) continue;
  54. entryBuffer.Clear();
  55. foreach (var 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 (var entry in entryBuffer) {
  71. var renderableAttachment = entry.Attachment as IHasRendererObject;
  72. if (renderableAttachment != null) {
  73. renderableAttachment.RendererObject = materialCache.CloneAtlasRegionWithMaterial((AtlasRegion)renderableAttachment.RendererObject, templateMaterial);
  74. }
  75. }
  76. }
  77. }
  78. //attachmentBuffer.Clear();
  79. }
  80. class AtlasMaterialCache : IDisposable {
  81. readonly Dictionary<KeyValuePair<AtlasPage, Material>, AtlasPage> cache = new Dictionary<KeyValuePair<AtlasPage, Material>, AtlasPage>();
  82. /// <summary>Creates a clone of an AtlasRegion that uses different Material settings, while retaining the original texture.</summary>
  83. public AtlasRegion CloneAtlasRegionWithMaterial (AtlasRegion originalRegion, Material materialTemplate) {
  84. var newRegion = originalRegion.Clone();
  85. newRegion.page = GetAtlasPageWithMaterial(originalRegion.page, materialTemplate);
  86. return newRegion;
  87. }
  88. AtlasPage GetAtlasPageWithMaterial (AtlasPage originalPage, Material materialTemplate) {
  89. if (originalPage == null) throw new ArgumentNullException("originalPage");
  90. AtlasPage newPage = null;
  91. var key = new KeyValuePair<AtlasPage, Material>(originalPage, materialTemplate);
  92. cache.TryGetValue(key, out newPage);
  93. if (newPage == null) {
  94. newPage = originalPage.Clone();
  95. var originalMaterial = originalPage.rendererObject as Material;
  96. newPage.rendererObject = new Material(materialTemplate) {
  97. name = originalMaterial.name + " " + materialTemplate.name,
  98. mainTexture = originalMaterial.mainTexture
  99. };
  100. cache.Add(key, newPage);
  101. }
  102. return newPage;
  103. }
  104. public void Dispose () {
  105. cache.Clear();
  106. }
  107. }
  108. }
  109. }