-
-
Notifications
You must be signed in to change notification settings - Fork 911
/
AnimationCurveEvaluatorOptimizedGroup.cs
247 lines (202 loc) · 9.62 KB
/
AnimationCurveEvaluatorOptimizedGroup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
#pragma warning disable SA1402 // File may only contain a single class
using System;
using Stride.Core;
using Stride.Core.Collections;
using Stride.Core.Mathematics;
using Stride.Updater;
namespace Stride.Animations
{
public abstract class AnimationCurveEvaluatorOptimizedGroup : AnimationCurveEvaluatorGroup
{
public abstract void Initialize(AnimationData animationData);
public abstract void SetChannelOffset(string name, int offset);
public static AnimationCurveEvaluatorOptimizedGroup Create<T>()
{
// Those types require interpolators
// TODO: Simple enough for now, but at some point we might want a mechanism to register them externally?
if (typeof(T) == typeof(float))
return new AnimationCurveEvaluatorOptimizedFloatGroup();
// TODO: Reintroduces explicit int path for now, since generic path does not work on iOS
if (typeof(T) == typeof(int))
return new AnimationCurveEvaluatorOptimizedIntGroup();
if (typeof(T) == typeof(Quaternion))
return new AnimationCurveEvaluatorOptimizedQuaternionGroup();
if (typeof(T) == typeof(Vector3))
return new AnimationCurveEvaluatorOptimizedVector3Group();
if (typeof(T) == typeof(Vector4))
return new AnimationCurveEvaluatorOptimizedVector4Group();
// Blittable
if (BlittableHelper.IsBlittable(typeof(T)))
return new AnimationCurveEvaluatorOptimizedBlittableGroup<T>();
// Objects
return new AnimationCurveEvaluatorOptimizedObjectGroup<T>();
}
}
public abstract class AnimationCurveEvaluatorOptimizedGroup<T> : AnimationCurveEvaluatorOptimizedGroup
{
private int animationSortedIndex;
protected AnimationData<T> animationData;
private CompressedTimeSpan currentTime;
protected FastListStruct<Channel> channels = new FastListStruct<Channel>(8);
public override Type ElementType => typeof(T);
public override void Initialize(AnimationData animationData)
{
Initialize((AnimationData<T>)animationData);
}
public void Initialize(AnimationData<T> animationData)
{
this.animationData = animationData;
foreach (var channel in animationData.AnimationInitialValues)
{
channels.Add(new Channel { InterpolationType = channel.InterpolationType, Offset = -1 });
}
// Setting infinite time means next time a rewind will be performed and initial values will be populated properly
currentTime = CompressedTimeSpan.MaxValue;
}
public override void Cleanup()
{
animationData = null;
channels.Clear();
}
public override void SetChannelOffset(string name, int offset)
{
var targetKeys = animationData.TargetKeys;
for (int i = 0; i < targetKeys.Length; ++i)
{
if (targetKeys[i] == name)
{
var channel = channels.Items[i];
channel.Offset = offset;
channels.Items[i] = channel;
break;
}
}
}
protected void SetTime(CompressedTimeSpan timeSpan)
{
// TODO: Add jump frames to do faster seeking.
// If user seek back, need to start from beginning
if (timeSpan < currentTime)
{
// Always start from beginning after a reset
animationSortedIndex = 0;
for (int channelIndex = 0; channelIndex < animationData.AnimationInitialValues.Length; ++channelIndex)
{
InitializeAnimation(ref channels.Items[channelIndex], ref animationData.AnimationInitialValues[channelIndex]);
}
}
currentTime = timeSpan;
var animationSortedValueCount = animationData.AnimationSortedValueCount;
var animationSortedValues = animationData.AnimationSortedValues;
if (animationSortedValueCount == 0)
return;
// Advance until requested time is reached
while (animationSortedIndex < animationSortedValueCount
&& animationSortedValues[animationSortedIndex / AnimationData.AnimationSortedValueBlock][animationSortedIndex % AnimationData.AnimationSortedValueBlock].RequiredTime <= currentTime)
{
//int channelIndex = animationSortedValues[animationSortedIndex / animationSortedValueBlock][animationSortedIndex % animationSortedValueBlock].ChannelIndex;
UpdateAnimation(ref animationSortedValues[animationSortedIndex / AnimationData.AnimationSortedValueBlock][animationSortedIndex % AnimationData.AnimationSortedValueBlock]);
animationSortedIndex++;
}
currentTime = timeSpan;
}
private static void InitializeAnimation(ref Channel animationChannel, ref AnimationInitialValues<T> animationValue)
{
animationChannel.ValuePrev = animationValue.Value1;
animationChannel.ValueStart = animationValue.Value1;
animationChannel.ValueEnd = animationValue.Value1;
animationChannel.ValueNext = animationValue.Value2;
}
private void UpdateAnimation(ref AnimationKeyValuePair<T> animationValue)
{
UpdateAnimation(ref channels.Items[animationValue.ChannelIndex], ref animationValue.Value);
}
private static void UpdateAnimation(ref Channel animationChannel, ref KeyFrameData<T> animationValue)
{
animationChannel.ValuePrev = animationChannel.ValueStart;
animationChannel.ValueStart = animationChannel.ValueEnd;
animationChannel.ValueEnd = animationChannel.ValueNext;
animationChannel.ValueNext = animationValue;
}
protected struct Channel
{
public int Offset;
public AnimationCurveInterpolationType InterpolationType;
public KeyFrameData<T> ValuePrev;
public KeyFrameData<T> ValueStart;
public KeyFrameData<T> ValueEnd;
public KeyFrameData<T> ValueNext;
}
}
public abstract class AnimationCurveEvaluatorOptimizedBlittableGroupBase<T> : AnimationCurveEvaluatorOptimizedGroup<T>
{
public override void Evaluate(CompressedTimeSpan newTime, IntPtr data, UpdateObjectData[] objects)
{
if (animationData == null)
return;
SetTime(newTime);
var channelCount = channels.Count;
var channelItems = channels.Items;
for (int i = 0; i < channelCount; ++i)
{
ProcessChannel(ref channelItems[i], newTime, data);
}
}
protected void ProcessChannel(ref Channel channel, CompressedTimeSpan currentTime, IntPtr data)
{
if (channel.Offset == -1)
return;
var startTime = channel.ValueStart.Time;
// TODO: Should we really do that?
// Sampling before start (should not really happen because we add a keyframe at TimeSpan.Zero, but let's keep it in case it changes later.
if (currentTime <= startTime)
{
Utilities.UnsafeWrite(data + channel.Offset, ref channel.ValueStart.Value);
return;
}
var endTime = channel.ValueEnd.Time;
// TODO: Should we really do that?
// Sampling after end
if (currentTime >= endTime)
{
Utilities.UnsafeWrite(data + channel.Offset, ref channel.ValueEnd.Value);
return;
}
float factor = (float)(currentTime.Ticks - startTime.Ticks) / (float)(endTime.Ticks - startTime.Ticks);
ProcessChannel(ref channel, currentTime, data, factor);
}
protected abstract void ProcessChannel(ref Channel channel, CompressedTimeSpan currentTime, IntPtr data, float factor);
}
public class AnimationCurveEvaluatorOptimizedObjectGroup<T> : AnimationCurveEvaluatorOptimizedGroup<T>
{
public override void Evaluate(CompressedTimeSpan newTime, IntPtr data, UpdateObjectData[] objects)
{
if (animationData == null)
return;
SetTime(newTime);
var channelCount = channels.Count;
var channelItems = channels.Items;
for (int i = 0; i < channelCount; ++i)
{
ProcessChannel(ref channelItems[i], newTime, objects);
}
}
protected void ProcessChannel(ref Channel channel, CompressedTimeSpan currentTime, UpdateObjectData[] objects)
{
if (channel.Offset == -1)
return;
var startTime = channel.ValueStart.Time;
// TODO: Should we really do that?
// Sampling before start (should not really happen because we add a keyframe at TimeSpan.Zero, but let's keep it in case it changes later.
if (currentTime <= startTime)
{
objects[channel.Offset].Value = channel.ValueStart.Value;
return;
}
// (This including sampling after end)
objects[channel.Offset].Value = channel.ValueStart.Value;
}
}
}