-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCraClip.cs
326 lines (281 loc) · 9.21 KB
/
CraClip.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
using System;
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine.Jobs;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEditor;
public enum CraInterpMethod
{
Linear,
NearestNeighbour
}
public class CraCurve
{
public List<CraKey> EditKeys = new List<CraKey>();
public float[] BakedFrames;
public int FrameCount { get; private set; }
public float Fps { get; private set; } = 30f;
// contains indices of baked frames corresponding to the original edit keys
public Queue<int> _BakedKeyIndices;
public int GetEstimatedFrameCount(float fps)
{
float endTime = EditKeys[EditKeys.Count - 1].Time;
return Mathf.Max(Mathf.CeilToInt(endTime * fps), 1);
}
public void Bake(float fps, int frameCount, CraInterpMethod method)
{
if (fps <= 0)
{
Debug.LogError($"Cannot bake curve with {fps} fps!");
return;
}
if (frameCount <= 0)
{
Debug.LogError($"Cannot bake curve with {frameCount} frames!");
return;
}
if (EditKeys.Count == 0)
{
Debug.LogError("Cannot bake empty transform curve!");
return;
}
if (BakedFrames != null)
{
Debug.LogError("Cannot bake twice!");
return;
}
Fps = fps;
FrameCount = frameCount;
BakedFrames = new float[FrameCount];
BakedFrames[0] = EditKeys[0].Value;
_BakedKeyIndices = new Queue<int>();
float endTime = EditKeys[EditKeys.Count - 1].Time;
int lastIdx = 0;
int nextIdx = 0;
float timeStep = 1f / Fps;
for (int i = 1; i < FrameCount; ++i)
{
float timeNow = timeStep * i;
// Bake Positions
if (timeNow <= endTime)
{
for (float posTimeNext = EditKeys[nextIdx].Time; posTimeNext < timeNow;)
{
lastIdx = nextIdx++;
posTimeNext = EditKeys[nextIdx].Time;
_BakedKeyIndices.Enqueue(i);
}
float duration = EditKeys[nextIdx].Time - EditKeys[lastIdx].Time;
Debug.Assert(duration > 0f);
if (method == CraInterpMethod.Linear)
{
float t = (timeNow - EditKeys[lastIdx].Time) / duration;
BakedFrames[i] = Mathf.Lerp(EditKeys[lastIdx].Value, EditKeys[nextIdx].Value, t);
}
else if (method == CraInterpMethod.NearestNeighbour)
{
float diffLast = timeNow - EditKeys[lastIdx].Time;
float diffNext = EditKeys[nextIdx].Time - timeNow;
BakedFrames[i] = diffLast < diffNext ? EditKeys[lastIdx].Value : EditKeys[nextIdx].Value;
}
}
else
{
BakedFrames[i] = BakedFrames[i - 1];
}
}
EditKeys.Clear();
}
}
public class CraTransformCurve
{
public int FrameCount { get; private set; }
public float Fps { get; private set; } = 30f;
public ulong BakeMemoryConsumption { get; private set; }
// 0 : rot X
// 1 : rot Y
// 2 : rot Z
// 3 : rot W
// 4 : pos X
// 5 : pos Y
// 6 : pos Z
public CraCurve[] Curves = new CraCurve[7];
public CraTransform[] BakedFrames;
public CraTransformCurve()
{
for (int i = 0; i < 7; ++i)
{
Curves[i] = new CraCurve();
}
}
public int GetEstimatedFrameCount(float fps)
{
int frameCount = 0;
for (int i = 0; i < 7; ++i)
{
frameCount = Mathf.Max(frameCount, Curves[i].GetEstimatedFrameCount(fps));
}
return frameCount;
}
public void Bake(float fps, int frameCount)
{
if (fps <= 0)
{
Debug.LogError($"Cannot bake curve with {fps} fps!");
return;
}
if (frameCount <= 0)
{
Debug.LogError($"Cannot bake curve with {frameCount} frames!");
return;
}
if (BakedFrames != null)
{
Debug.LogError("Cannot bake twice!");
return;
}
Fps = fps;
FrameCount = frameCount;
BakeMemoryConsumption = 0;
for (int i = 0; i < 7; ++i)
{
if (i >= 4)
{
// Bake positions with simple linear interpolation. Done.
Curves[i].Bake(fps, frameCount, CraInterpMethod.Linear);
}
else
{
// Bake rotations with nearest neighbour. This will first lead to "edgy" rotations,
// but will ensure consistent key frames accross all channels (x y z w) we can
// then use for spherical linear interpolation
Curves[i].Bake(fps, frameCount, CraInterpMethod.NearestNeighbour);
}
}
BakedFrames = new CraTransform[FrameCount];
for (int i = 0; i < FrameCount; ++i)
{
BakedFrames[i].Rotation.x = Curves[0].BakedFrames[i];
BakedFrames[i].Rotation.y = Curves[1].BakedFrames[i];
BakedFrames[i].Rotation.z = Curves[2].BakedFrames[i];
BakedFrames[i].Rotation.w = Curves[3].BakedFrames[i];
BakedFrames[i].Position.x = Curves[4].BakedFrames[i];
BakedFrames[i].Position.y = Curves[5].BakedFrames[i];
BakedFrames[i].Position.z = Curves[6].BakedFrames[i];
}
// Do proper spherical linear interpolation between rotation key frames, overriding in between frames.
// TODO: It seems there's still a faulty when dealing with either:
// - start to first key frame
// - last key frame to end
// further investigation needed!
int lastRotFrameIdx = 0;
int nextRotFrameIdx = 0;
for (int i = 0; i < FrameCount; ++i)
{
// ensure all prior key indices get dequeued
for (int ch = 0; ch < 4; ++ch)
{
while (Curves[ch]._BakedKeyIndices.Count > 0 && Curves[ch]._BakedKeyIndices.Peek() <= i)
{
Curves[ch]._BakedKeyIndices.Dequeue();
}
}
int nextCh = FrameCount - 1;
for (int ch = 0; ch < 4; ++ch)
{
if (Curves[ch]._BakedKeyIndices.Count > 0)
{
// get the next key frame
int peek = Curves[ch]._BakedKeyIndices.Peek();
if (Curves[ch]._BakedKeyIndices.Count > 0 && peek > i)
{
nextCh = Mathf.Min(nextCh, peek);
}
}
}
if (nextCh > nextRotFrameIdx)
{
lastRotFrameIdx = nextRotFrameIdx;
nextRotFrameIdx = nextCh;
}
float frameDuration = nextRotFrameIdx - lastRotFrameIdx;
if (frameDuration > 0f)
{
quaternion last = BakedFrames[lastRotFrameIdx].Rotation;
quaternion next = BakedFrames[nextRotFrameIdx].Rotation;
float t = (i - lastRotFrameIdx) / frameDuration;
BakedFrames[i].Rotation = math.slerp(last, next, t).value;
}
}
// remove redundant data
for (int i = 0; i < 7; ++i)
{
Curves[i].BakedFrames = null;
Curves[i]._BakedKeyIndices = null;
}
BakeMemoryConsumption = (ulong)BakedFrames.Length * sizeof(float) * 7;
}
}
public class CraClip
{
public string Name;
public float Fps { get; private set; } = -1f; // -1 => not yet baked
public int FrameCount { get; private set; } = 0;
public CraBone[] Bones { get; private set; }
public Dictionary<int, int> BoneHashToIdx { get; private set; } = new Dictionary<int, int>();
public void SetBones(CraBone[] bones)
{
Bones = bones;
BoneHashToIdx.Clear();
for (int i = 0; i < Bones.Length; ++i)
{
BoneHashToIdx[Bones[i].BoneHash] = i;
}
}
public int GetBoneIdx(string boneName)
{
if (BoneHashToIdx.TryGetValue(CraSettings.BoneHashFunction(boneName), out int idx))
{
return idx;
}
return -1;
}
public CraBone? GetBone(int hash)
{
if (BoneHashToIdx.TryGetValue(hash, out int idx))
{
return Bones[idx];
}
return null;
}
public void Bake(float fps)
{
if (fps <= 0)
{
Debug.LogError($"Cannot bake curve with {fps} fps!");
return;
}
if (Bones == null)
{
Debug.LogError("Cannot bake empty clip!");
return;
}
if (Fps > -1)
{
Debug.LogError("Cannot bake twice!");
return;
}
FrameCount = 0;
Fps = fps;
for (int i = 0; i < Bones.Length; ++i)
{
FrameCount = Mathf.Max(FrameCount, Bones[i].Curve.GetEstimatedFrameCount(Fps));
}
for (int i = 0; i < Bones.Length; ++i)
{
Bones[i].Curve.Bake(Fps, FrameCount);
}
}
}