NativeAnimationCurve.cs
Runtime/Collections/NativeAnimationCurve.cs
1 using System;
2 using Unity.Collections;
3 using Unity.Collections.LowLevel.Unsafe;
4 using UnityEngine;
5
6 namespace MeatMotion.Collections
7 {
8 public unsafe struct NativeAnimationCurve : IDisposable
9 {
10 NativeList<CurveSegment> segments;
11 WrapMode before, after;
12 public readonly bool IsCreated => segments.IsCreated;
13
14 public NativeAnimationCurve(AllocatorManager.AllocatorHandle allocator)
15 {
16 segments = new NativeList<CurveSegment>(0, allocator);
17 before = after = default;
18 }
19
20 public NativeAnimationCurve(AnimationCurve animationCurve, AllocatorManager.AllocatorHandle allocator)
21 {
22 if (animationCurve == null) throw new ArgumentNullException(nameof(animationCurve));
23 segments = new NativeList<CurveSegment>(animationCurve.length, allocator);
24 before = after = default;
25 CopyFrom(animationCurve);
26 }
27
28 public void CopyFrom(AnimationCurve animationCurve)
29 {
30 if (animationCurve == null) throw new ArgumentNullException(nameof(animationCurve));
31 int count = animationCurve.length;
32 segments.Resize(count, NativeArrayOptions.UninitializedMemory);
33 CurveSegment.Bake(animationCurve, (CurveSegment*)segments.GetUnsafePtr(), count);
34 before = animationCurve.preWrapMode;
35 after = animationCurve.postWrapMode;
36 }
37
38 public void CopyFrom(in NativeAnimationCurve animationCurve)
39 {
40 if (segments.GetUnsafeList() != animationCurve.segments.GetUnsafeList()) segments.CopyFrom(animationCurve.segments);
41 before = animationCurve.before;
42 after = animationCurve.after;
43 }
44
45 public float Evaluate(float time) => CurveLookup.Evaluate((CurveSegment*)segments.GetUnsafeReadOnlyPtr(), segments.Length, before, after, time);
46 public void Dispose() => segments.Dispose();
47 }
48
49 public unsafe struct UnsafeAnimationCurve : IDisposable
50 {
51 UnsafeList<CurveSegment> segments;
52 WrapMode before, after;
53 public readonly bool IsCreated => segments.IsCreated;
54
55 public UnsafeAnimationCurve(AllocatorManager.AllocatorHandle allocator)
56 {
57 segments = new UnsafeList<CurveSegment>(0, allocator);
58 before = after = default;
59 }
60
61 public UnsafeAnimationCurve(AnimationCurve animationCurve, AllocatorManager.AllocatorHandle allocator)
62 {
63 if (animationCurve == null) throw new ArgumentNullException(nameof(animationCurve));
64 segments = new UnsafeList<CurveSegment>(animationCurve.length, allocator);
65 before = after = default;
66 CopyFrom(animationCurve);
67 }
68
69 public void CopyFrom(AnimationCurve animationCurve)
70 {
71 if (animationCurve == null) throw new ArgumentNullException(nameof(animationCurve));
72 int count = animationCurve.length;
73 segments.Resize(count, NativeArrayOptions.UninitializedMemory);
74 CurveSegment.Bake(animationCurve, segments.Ptr, count);
75 before = animationCurve.preWrapMode;
76 after = animationCurve.postWrapMode;
77 }
78
79 public void CopyFrom(in UnsafeAnimationCurve animationCurve)
80 {
81 if (segments.Ptr != animationCurve.segments.Ptr) segments.CopyFrom(animationCurve.segments);
82 before = animationCurve.before;
83 after = animationCurve.after;
84 }
85
86 public float Evaluate(float time) => CurveLookup.Evaluate(segments.Ptr, segments.Length, before, after, time);
87 public void Dispose() => segments.Dispose();
88 }
89 }