AnimancerLayerMixerList.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. using UnityEngine;
  3. using UnityEngine.Animations;
  4. namespace Animancer
  5. {
  6. /// <summary>An <see cref="AnimancerLayerList"/> which uses an <see cref="AnimationLayerMixerPlayable"/>.</summary>
  7. /// <remarks>
  8. /// <strong>Documentation:</strong>
  9. /// <see href="https://kybernetik.com.au/animancer/docs/manual/blending/layers">
  10. /// Layers</see>
  11. /// </remarks>
  12. /// https://kybernetik.com.au/animancer/api/Animancer/AnimancerLayerMixerList
  13. public class AnimancerLayerMixerList : AnimancerLayerList
  14. {
  15. /************************************************************************************************************************/
  16. /// <summary>Creates a new <see cref="AnimancerLayerMixerList"/>.</summary>
  17. public AnimancerLayerMixerList(AnimancerGraph graph)
  18. : base(graph)
  19. {
  20. LayerMixer = AnimationLayerMixerPlayable.Create(graph._PlayableGraph, 1);
  21. Playable = LayerMixer;
  22. }
  23. /************************************************************************************************************************/
  24. /// <summary>The <see cref="AnimationLayerMixerPlayable"/> which blends the layers.</summary>
  25. public AnimationLayerMixerPlayable LayerMixer { get; protected set; }
  26. /************************************************************************************************************************/
  27. /// <inheritdoc/>
  28. public override bool IsAdditive(int index)
  29. => LayerMixer.IsLayerAdditive((uint)index);
  30. /// <inheritdoc/>
  31. public override void SetAdditive(int index, bool value)
  32. {
  33. SetMinCount(index + 1);
  34. LayerMixer.SetLayerAdditive((uint)index, value);
  35. }
  36. /************************************************************************************************************************/
  37. private static AvatarMask _DefaultMask;
  38. /// <inheritdoc/>
  39. public override void SetMask(int index, AvatarMask mask)
  40. {
  41. var layer = this[index];
  42. if (mask == null)
  43. {
  44. // If the existing mask was already null, do nothing.
  45. // If it was destroyed, we still need to continue and set it to the default.
  46. if (layer._Mask is null)
  47. return;
  48. _DefaultMask ??= new();
  49. mask = _DefaultMask;
  50. }
  51. // Don't check if the same mask was already assigned because it might have been modified.
  52. layer._Mask = mask;
  53. LayerMixer.SetLayerMaskFromAvatarMask((uint)index, mask);
  54. }
  55. /************************************************************************************************************************/
  56. }
  57. }