MotionUpdateJob.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using Unity.Burst;
  2. using Unity.Burst.CompilerServices;
  3. using Unity.Collections;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. using Unity.Jobs;
  6. using Unity.Mathematics;
  7. namespace LitMotion
  8. {
  9. /// <summary>
  10. /// A job that updates the status of the motion data and outputs the current value.
  11. /// </summary>
  12. /// <typeparam name="TValue">The type of value to animate</typeparam>
  13. /// <typeparam name="TOptions">The type of special parameters given to the motion data</typeparam>
  14. /// <typeparam name="TAdapter">The type of adapter that support value animation</typeparam>
  15. [BurstCompile]
  16. public unsafe struct MotionUpdateJob<TValue, TOptions, TAdapter> : IJobParallelFor
  17. where TValue : unmanaged
  18. where TOptions : unmanaged, IMotionOptions
  19. where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
  20. {
  21. [NativeDisableUnsafePtrRestriction] public MotionData<TValue, TOptions>* DataPtr;
  22. [ReadOnly] public double DeltaTime;
  23. [ReadOnly] public double UnscaledDeltaTime;
  24. [ReadOnly] public double RealDeltaTime;
  25. [WriteOnly] public NativeList<int>.ParallelWriter CompletedIndexList;
  26. [WriteOnly] public NativeArray<TValue> Output;
  27. public void Execute([AssumeRange(0, int.MaxValue)] int index)
  28. {
  29. var ptr = DataPtr + index;
  30. var corePtr = (MotionDataCore*)ptr;
  31. if (Hint.Likely(corePtr->Status is MotionStatus.Scheduled or MotionStatus.Delayed or MotionStatus.Playing))
  32. {
  33. var deltaTime = corePtr->TimeKind switch
  34. {
  35. MotionTimeKind.Time => DeltaTime,
  36. MotionTimeKind.UnscaledTime => UnscaledDeltaTime,
  37. MotionTimeKind.Realtime => RealDeltaTime,
  38. _ => default
  39. };
  40. corePtr->Time = math.max(corePtr->Time + deltaTime * corePtr->PlaybackSpeed, 0.0);
  41. var motionTime = corePtr->Time;
  42. double t;
  43. bool isCompleted;
  44. bool isDelayed;
  45. int completedLoops;
  46. int clampedCompletedLoops;
  47. if (Hint.Unlikely(corePtr->Duration <= 0f))
  48. {
  49. if (corePtr->DelayType == DelayType.FirstLoop || corePtr->Delay == 0f)
  50. {
  51. var time = motionTime - corePtr->Delay;
  52. isCompleted = corePtr->Loops >= 0 && time > 0f;
  53. if (isCompleted)
  54. {
  55. t = 1f;
  56. completedLoops = corePtr->Loops;
  57. }
  58. else
  59. {
  60. t = 0f;
  61. completedLoops = time < 0f ? -1 : 0;
  62. }
  63. clampedCompletedLoops = corePtr->Loops < 0 ? math.max(0, completedLoops) : math.clamp(completedLoops, 0, corePtr->Loops);
  64. isDelayed = time < 0;
  65. }
  66. else
  67. {
  68. completedLoops = (int)math.floor(motionTime / corePtr->Delay);
  69. clampedCompletedLoops = corePtr->Loops < 0 ? math.max(0, completedLoops) : math.clamp(completedLoops, 0, corePtr->Loops);
  70. isCompleted = corePtr->Loops >= 0 && clampedCompletedLoops > corePtr->Loops - 1;
  71. isDelayed = !isCompleted;
  72. t = isCompleted ? 1f : 0f;
  73. }
  74. }
  75. else
  76. {
  77. if (corePtr->DelayType == DelayType.FirstLoop)
  78. {
  79. var time = motionTime - corePtr->Delay;
  80. completedLoops = (int)math.floor(time / corePtr->Duration);
  81. clampedCompletedLoops = corePtr->Loops < 0 ? math.max(0, completedLoops) : math.clamp(completedLoops, 0, corePtr->Loops);
  82. isCompleted = corePtr->Loops >= 0 && clampedCompletedLoops > corePtr->Loops - 1;
  83. isDelayed = time < 0f;
  84. if (isCompleted)
  85. {
  86. t = 1f;
  87. }
  88. else
  89. {
  90. var currentLoopTime = time - corePtr->Duration * clampedCompletedLoops;
  91. t = math.clamp(currentLoopTime / corePtr->Duration, 0f, 1f);
  92. }
  93. }
  94. else
  95. {
  96. var currentLoopTime = math.fmod(motionTime, corePtr->Duration + corePtr->Delay) - corePtr->Delay;
  97. completedLoops = (int)math.floor(motionTime / (corePtr->Duration + corePtr->Delay));
  98. clampedCompletedLoops = corePtr->Loops < 0 ? math.max(0, completedLoops) : math.clamp(completedLoops, 0, corePtr->Loops);
  99. isCompleted = corePtr->Loops >= 0 && clampedCompletedLoops > corePtr->Loops - 1;
  100. isDelayed = currentLoopTime < 0;
  101. if (isCompleted)
  102. {
  103. t = 1f;
  104. }
  105. else
  106. {
  107. t = math.clamp(currentLoopTime / corePtr->Duration, 0f, 1f);
  108. }
  109. }
  110. }
  111. float progress;
  112. switch (corePtr->LoopType)
  113. {
  114. default:
  115. case LoopType.Restart:
  116. progress = GetEasedValue(corePtr, (float)t);
  117. break;
  118. case LoopType.Yoyo:
  119. progress = GetEasedValue(corePtr, (float)t);
  120. if ((clampedCompletedLoops + (int)t) % 2 == 1) progress = 1f - progress;
  121. break;
  122. case LoopType.Incremental:
  123. progress = GetEasedValue(corePtr, 1f) * clampedCompletedLoops + GetEasedValue(corePtr, (float)math.fmod(t, 1f));
  124. break;
  125. }
  126. var totalDuration = corePtr->DelayType == DelayType.FirstLoop
  127. ? corePtr->Delay + corePtr->Duration * corePtr->Loops
  128. : (corePtr->Delay + corePtr->Duration) * corePtr->Loops;
  129. if (corePtr->Loops > 0 && motionTime >= totalDuration)
  130. {
  131. corePtr->Status = MotionStatus.Completed;
  132. }
  133. else if (isDelayed)
  134. {
  135. corePtr->Status = MotionStatus.Delayed;
  136. }
  137. else
  138. {
  139. corePtr->Status = MotionStatus.Playing;
  140. }
  141. var context = new MotionEvaluationContext()
  142. {
  143. Progress = progress
  144. };
  145. Output[index] = default(TAdapter).Evaluate(ref ptr->StartValue, ref ptr->EndValue, ref ptr->Options, context);
  146. }
  147. else if (corePtr->Status is MotionStatus.Completed or MotionStatus.Canceled)
  148. {
  149. CompletedIndexList.AddNoResize(index);
  150. corePtr->Status = MotionStatus.Disposed;
  151. }
  152. }
  153. static float GetEasedValue(MotionDataCore* data, float value)
  154. {
  155. return data->Ease switch
  156. {
  157. Ease.CustomAnimationCurve => data->AnimationCurve.Evaluate(value),
  158. _ => EaseUtility.Evaluate(value, data->Ease)
  159. };
  160. }
  161. }
  162. }