MotionUpdateJob.cs
Runtime/Jobs/MotionUpdateJob.cs
1 using MeatMotion.Collections;
2 using Unity.Burst;
3 using Unity.Collections;
4 using Unity.Jobs;
5
6 namespace MeatMotion
7 {
8 /// <summary>Samples an array of independent motions. The caller owns all buffers and curves.</summary>
9 [BurstCompile]
10 public struct MotionUpdateJob<TValue, TOptions, TAdapter> : IJobParallelFor
11 where TValue : unmanaged
12 where TOptions : unmanaged, IMotionOptions
13 where TAdapter : unmanaged, IMotionAdapter<TValue, TOptions>
14 {
15 public enum Status { Scheduled, Delayed, Playing, Completed, Canceled, Disposed }
16
17 public struct Motion : IMotionEase
18 {
19 public TValue Start, End;
20 public TOptions Options;
21 public float Duration, Delay, PlaybackSpeed;
22 public int Loops, CompletedLoops;
23 public DelayType DelayType;
24 public LoopType LoopType;
25 public MotionTimeKind TimeKind;
26 public Ease Ease;
27 /// <summary>A borrowed curve used with Ease.CustomAnimationCurve. Keep it alive until the job completes.</summary>
28 public UnsafeAnimationCurve Curve;
29 public double Time;
30 public Status State;
31 int flags;
32 public bool Preserved
33 {
34 readonly get => (flags & 1) != 0;
35 set => flags = value ? flags | 1 : flags & ~1;
36 }
37 public bool InSequence
38 {
39 readonly get => (flags & 2) != 0;
40 set => flags = value ? flags | 2 : flags & ~2;
41 }
42
43 public Motion(TValue from, TValue to, float duration) : this()
44 {
45 Start = from;
46 End = to;
47 Duration = duration;
48 PlaybackSpeed = 1;
49 Loops = 1;
50 }
51
52 readonly float IMotionEase.EaseAt(float t) => Ease == Ease.CustomAnimationCurve ? Curve.Evaluate(t) : EaseUtility.Evaluate(t, Ease);
53 }
54
55 public NativeArray<Motion> Motions;
56 [ReadOnly] public double DeltaTime, UnscaledDeltaTime, RealDeltaTime;
57 [WriteOnly] public NativeArray<TValue> Output;
58 /// <summary>Reserve capacity for each potentially retired motion before scheduling.</summary>
59 [WriteOnly] public NativeList<int>.ParallelWriter CompletedIndexList;
60
61 public void Execute(int index)
62 {
63 ref var motion = ref Motions.AsSpan()[index];
64 if (motion.State == Status.Disposed) return;
65 if (motion.State == Status.Canceled || (motion.State == Status.Completed && !motion.Preserved))
66 {
67 CompletedIndexList.AddNoResize(index);
68 motion.State = Status.Disposed;
69 return;
70 }
71 if (motion.InSequence) return;
72
73 double delta = motion.TimeKind switch
74 {
75 MotionTimeKind.Time => DeltaTime,
76 MotionTimeKind.UnscaledTime => UnscaledDeltaTime,
77 MotionTimeKind.Realtime => RealDeltaTime,
78 _ => 0
79 };
80 motion.Time += delta * motion.PlaybackSpeed;
81 float progress = TimelineMath.Sample(motion.Time, false, motion.Duration, motion.Delay, motion.Loops,
82 motion.DelayType, motion.LoopType, ref motion, out int loops, out bool delayed, out bool ended);
83 motion.CompletedLoops = loops;
84 motion.State = ended ? Status.Completed : delayed ? Status.Delayed : Status.Playing;
85 var context = new MotionEvaluationContext { Progress = progress, Time = motion.Time };
86 Output[index] = default(TAdapter).Evaluate(ref motion.Start, ref motion.End, ref motion.Options, in context);
87 }
88 }
89 }