SimpleLean.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik //
  2. #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.
  3. using System;
  4. using Unity.Collections;
  5. using UnityEngine;
  6. using UnityEngine.Animations;
  7. namespace Animancer.Samples.Jobs
  8. {
  9. /// <summary>
  10. /// A wrapper that manages an Animation Job (the <see cref="Job"/> struct nested inside this class)
  11. /// which rotates a set of bones to allow the character to dynamically lean over independantly of their animations.
  12. /// </summary>
  13. ///
  14. /// <remarks>
  15. /// The axis around which the bones are rotated can be set to achieve several different effects:
  16. /// <list type="number">
  17. /// <item>The right axis allows bending forwards and backwards.</item>
  18. /// <item>The up axis allows turning to either side.</item>
  19. /// <item>The forward axis allows leaning to either side.</item>
  20. /// </list>
  21. /// <see cref="https://github.com/KybernetikGames/animancer/issues/48#issuecomment-632336377">
  22. /// This script is based on an implementation by ted-hou on GitHub.</see>
  23. /// <para></para>
  24. /// <strong>Sample:</strong>
  25. /// <see href="https://kybernetik.com.au/animancer/docs/samples/jobs/hit-impacts">
  26. /// Hit Impacts</see>
  27. /// </remarks>
  28. ///
  29. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.Jobs/SimpleLean
  30. ///
  31. public class SimpleLean : AnimancerJob<SimpleLean.Job>, IDisposable
  32. {
  33. /************************************************************************************************************************/
  34. #region Initialization
  35. /************************************************************************************************************************/
  36. public SimpleLean(
  37. AnimancerGraph animancer,
  38. Vector3 axis,
  39. NativeArray<TransformStreamHandle> leanBones)
  40. {
  41. Animator animator = animancer.Component.Animator;
  42. _Job = new()
  43. {
  44. root = animator.BindStreamTransform(animator.transform),
  45. bones = leanBones,
  46. axis = axis,
  47. angle = AnimancerUtilities.CreateNativeReference<float>(),
  48. };
  49. CreatePlayable(animancer);
  50. animancer.Disposables.Add(this);
  51. }
  52. /************************************************************************************************************************/
  53. #endregion
  54. /************************************************************************************************************************/
  55. #region Control
  56. /************************************************************************************************************************/
  57. // The Axis probably won't change often so the setter can just get the job data and change it.
  58. /************************************************************************************************************************/
  59. public Vector3 Axis
  60. {
  61. get => _Job.axis;
  62. set
  63. {
  64. if (_Job.axis == value)
  65. return;
  66. _Job.axis = value;
  67. _Playable.SetJobData(_Job);
  68. }
  69. }
  70. /************************************************************************************************************************/
  71. // But since the Angle could change all the time, we can exploit the fact that arrays are actualy references to avoid
  72. // copying the entire struct out of the job playable then back in every time.
  73. /************************************************************************************************************************/
  74. public float Angle
  75. {
  76. get => _Job.angle[0];
  77. set => _Job.angle[0] = value;
  78. }
  79. /************************************************************************************************************************/
  80. #endregion
  81. /************************************************************************************************************************/
  82. #region Clean Up
  83. /************************************************************************************************************************/
  84. void IDisposable.Dispose() => Dispose();
  85. /// <summary>Cleans up the <see cref="NativeArray{T}"/>s.</summary>
  86. /// <remarks>Called by <see cref="AnimancerGraph.OnPlayableDestroy"/>.</remarks>
  87. private void Dispose()
  88. {
  89. if (_Job.angle.IsCreated)
  90. _Job.angle.Dispose();
  91. if (_Job.bones.IsCreated)
  92. _Job.bones.Dispose();
  93. }
  94. /// <summary>Destroys the <see cref="_Playable"/> and restores the graph connection it was intercepting.</summary>
  95. public override void Destroy()
  96. {
  97. Dispose();
  98. base.Destroy();
  99. }
  100. /************************************************************************************************************************/
  101. #endregion
  102. /************************************************************************************************************************/
  103. #region Job
  104. /************************************************************************************************************************/
  105. /// <summary>An <see cref="IAnimationJob"/> that applies a lean effect to an <see cref="AnimationStream"/>.</summary>
  106. ///
  107. /// <remarks>
  108. /// <strong>Sample:</strong>
  109. /// <see href="https://kybernetik.com.au/animancer/docs/samples/jobs/hit-impacts">
  110. /// Hit Impacts</see>
  111. /// </remarks>
  112. ///
  113. /// https://kybernetik.com.au/animancer/api/Animancer.Samples.Jobs/Job
  114. ///
  115. public struct Job : IAnimationJob
  116. {
  117. /************************************************************************************************************************/
  118. public TransformStreamHandle root;
  119. public NativeArray<TransformStreamHandle> bones;
  120. public Vector3 axis;
  121. public NativeArray<float> angle;
  122. /************************************************************************************************************************/
  123. public readonly void ProcessRootMotion(AnimationStream stream) { }
  124. /************************************************************************************************************************/
  125. public void ProcessAnimation(AnimationStream stream)
  126. {
  127. float angle = this.angle[0] / bones.Length;
  128. Vector3 worldAxis = root.GetRotation(stream) * axis;
  129. Quaternion offset = Quaternion.AngleAxis(angle, worldAxis);
  130. for (int i = bones.Length - 1; i >= 0; i--)
  131. {
  132. TransformStreamHandle bone = bones[i];
  133. bone.SetRotation(stream, offset * bone.GetRotation(stream));
  134. }
  135. }
  136. /************************************************************************************************************************/
  137. }
  138. /************************************************************************************************************************/
  139. #endregion
  140. /************************************************************************************************************************/
  141. }
  142. }