LocalPositionTweens.cs
Runtime/Bindings/Transform/LocalPositionTweens.cs
1 using System;
2 using Unity.Burst;
3 using Unity.Collections;
4 using Unity.Collections.LowLevel.Unsafe;
5 using UnityEngine;
6
7 namespace MeatMotion
8 {
9 /// <summary>Main-thread batch. Tick once per frame and dispose when its owner is destroyed.
10 /// For overlapping tweens the last added target write wins in each tick.</summary>
11 [BurstCompile]
12 public sealed class LocalPositionTweens : IDisposable
13 {
14 // Adjacent targets with the same trajectory share a clock and an interpolated position.
15 // Keeping runs in insertion order also preserves parent/child and duplicate-target writes.
16 struct Run
17 {
18 public int Count;
19 public Vector3 From, To;
20 public double Elapsed, Duration;
21 public Easing Ease;
22 public bool World;
23 }
24
25 NativeList<TransformHandle> targets;
26 NativeList<Run> runs;
27 public int Count => targets.IsCreated ? targets.Length : 0;
28
29 public LocalPositionTweens(int capacity = 16)
30 {
31 runs = new NativeList<Run>(capacity, Allocator.Persistent);
32 try { targets = new NativeList<TransformHandle>(capacity, Allocator.Persistent); }
33 catch { runs.Dispose(); throw; }
34 }
35
36 public void Add(Transform target, Vector3 to, float seconds, Easing ease = Easing.OutQuad)
37 => Add(target, to, seconds, ease, false);
38
39 public void AddWorldPosition(Transform target, Vector3 to, float seconds, Easing ease = Easing.OutQuad)
40 => Add(target, to, seconds, ease, true);
41
42 void Add(Transform target, Vector3 to, float seconds, Easing ease, bool world)
43 {
44 EnsureOpen();
45 if (!target) throw new ArgumentNullException(nameof(target));
46 if (!(seconds >= 0f) || float.IsInfinity(seconds)) throw new ArgumentOutOfRangeException(nameof(seconds));
47 EaseUtility.CheckBuiltin(ease);
48 if (seconds == 0f)
49 {
50 if (world) target.position = to;
51 else target.localPosition = to;
52 return;
53 }
54 var from = world ? target.position : target.localPosition;
55 var handle = target.transformHandle;
56 if (runs.Length > 0)
57 {
58 ref var last = ref runs.ElementAt(runs.Length - 1);
59 if (last.Elapsed == 0 && last.Duration == seconds && last.World == world &&
60 last.Ease == ease && last.From.Equals(from) && last.To.Equals(to))
61 {
62 targets.Add(handle);
63 last.Count++;
64 return;
65 }
66 }
67 runs.Add(new Run { Count = 1, From = from, To = to, Duration = seconds, Ease = ease, World = world });
68 try { targets.Add(handle); }
69 catch { runs.ResizeUninitialized(runs.Length - 1); throw; }
70 }
71
72 public unsafe void Tick(double deltaTime)
73 {
74 EnsureOpen();
75 if (!(deltaTime >= 0) || double.IsInfinity(deltaTime)) throw new ArgumentOutOfRangeException(nameof(deltaTime));
76 if (targets.Length == 0) return;
77 Advance((Run*)runs.GetUnsafePtr(), runs.Length, (TransformHandle*)targets.GetUnsafePtr(),
78 deltaTime, out int remainingRuns, out int remainingTargets);
79 if (remainingRuns != runs.Length) runs.ResizeUninitialized(remainingRuns);
80 if (remainingTargets != targets.Length) targets.ResizeUninitialized(remainingTargets);
81 }
82
83 [BurstCompile(CompileSynchronously = true)]
84 static unsafe void Advance(Run* runs, int count, TransformHandle* targets, double dt,
85 out int remainingRuns, out int remainingTargets)
86 {
87 int read = 0, write = 0, runCount = 0;
88 for (int i = 0; i < count; i++)
89 {
90 var run = runs[i];
91 run.Elapsed += dt;
92 bool finished = run.Elapsed >= run.Duration;
93 var position = finished ? run.To : Vector3.LerpUnclamped(run.From, run.To,
94 EaseUtility.Evaluate((float)(run.Elapsed / run.Duration), run.Ease));
95 int begin = write, end = read + run.Count;
96 for (; read < end; read++)
97 {
98 var target = targets[read];
99 if (!target.IsValid()) continue;
100 if (run.World) target.position = position;
101 else target.localPosition = position;
102 if (finished) continue;
103 if (write != read) targets[write] = target;
104 write++;
105 }
106 if (begin == write) continue;
107 run.Count = write - begin;
108 runs[runCount++] = run;
109 }
110 remainingRuns = runCount;
111 remainingTargets = write;
112 }
113
114 void EnsureOpen()
115 {
116 if (!targets.IsCreated) throw new ObjectDisposedException(nameof(LocalPositionTweens));
117 }
118
119 public void Dispose()
120 {
121 if (targets.IsCreated) targets.Dispose();
122 if (runs.IsCreated) runs.Dispose();
123 }
124 }
125 }