-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCraPlaybackManager.cs
More file actions
655 lines (530 loc) · 20.2 KB
/
CraPlaybackManager.cs
File metadata and controls
655 lines (530 loc) · 20.2 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
using System;
using System.Collections.Generic;
using UnityEngine;
using Unity.Collections;
using Unity.Mathematics;
using Unity.Burst;
using Unity.Jobs;
using UnityEngine.Jobs;
public class CraPlaybackManager
{
public static CraPlaybackManager Instance { get; private set; }
struct CraPlayerData
{
// setable
public int4 ClipIndex;
public bool4 Looping;
public bool4 IsPlaying;
public float4 PlaybackSpeed;
public float4 Playback;
public float4 Transition;
// get only
public bool4 Finished;
public float4 Duration;
public int4 FrameIndex;
// For Debug reasons only
public const ulong SIZE =
sizeof(int) * 4 +
sizeof(bool) * 4 +
sizeof(bool) * 4 +
sizeof(float) * 4 +
sizeof(float) * 4 +
sizeof(float) * 4 +
sizeof(bool) * 4 +
sizeof(float) * 4 +
sizeof(int) * 4;
}
// Describes a clip (baked transfrom data) within the BakedClipTransforms NativeArray
struct CraClipData
{
public float FPS;
// Offset into BakedClipTransforms memory
public int FrameOffset;
public int FrameCount;
// For Debug reasons only
public const ulong SIZE =
sizeof(float) +
sizeof(int) +
sizeof(int);
}
// Describes what player and clip this particular bone is assigned to
struct CraBoneData
{
// Offset into PlayerData memory
public int PlayerIndex;
// When baking a clip, each bone transform curve ends
// up with exactly the same number of frames, i.e.
// FrameCount. Using this information, we can index
// into BakedClipTransforms not only per Clip, but
// also per Bone within a Clip
public int ClipBoneIndex;
// For Debug reasons only
public const ulong SIZE =
sizeof(int) +
sizeof(int);
}
[BurstCompile]
struct CraPlayJob : IJobParallelFor
{
[ReadOnly]
public float DeltaTime;
[ReadOnly]
public NativeArray<CraClipData> ClipData;
// Read + Write
public NativeArray<CraPlayerData> PlayerData;
// Transition time in seconds
const float TransitionSpeed = 0.3f;
public void Execute(int index)
{
CraPlayerData player = PlayerData[index];
//player.Playback += DeltaTime * player.PlaybackSpeed;
//bool4 end = player.Playback >= player.Duration;
player.Transition = math.clamp(player.Transition + DeltaTime / TransitionSpeed, float4.zero, new float4(1f, 1f, 1f, 1f));
// TODO: This is BAAAAAAAAAAD
for (int i = 0; i < 4; ++i)
{
if (!player.IsPlaying[i])
{
continue;
}
CraClipData clip = ClipData[player.ClipIndex[i]];
player.Playback[i] += DeltaTime * player.PlaybackSpeed[i];
if (player.PlaybackSpeed[i] > 0f)
{
if (player.Playback[i] >= player.Duration[i])
{
if (!player.Looping[i])
{
player.Playback[i] = player.Duration[i] - 0.001f;
player.IsPlaying[i] = false;
player.Finished[i] = true;
}
else
{
player.Playback[i] = 0;
player.FrameIndex[i] = 0;
player.Finished[i] = false;
}
}
else
{
player.FrameIndex[i] = (int)math.floor(clip.FPS * player.Playback[i]);
}
}
else
{
if (player.Playback[i] <= 0f)
{
if (!player.Looping[i])
{
player.Playback[i] = 0.001f;
player.IsPlaying[i] = false;
player.Finished[i] = true;
}
else
{
player.Playback[i] = player.Duration[i];
player.FrameIndex[i] = (int)math.floor(clip.FPS * player.Duration[i]);
player.Finished[i] = false;
}
}
else
{
player.FrameIndex[i] = (int)math.floor(clip.FPS * player.Playback[i]);
}
}
}
PlayerData[index] = player;
}
}
[BurstCompile]
struct CraBoneEvalJob : IJobParallelForTransform
{
[ReadOnly]
public NativeArray<CraPlayerData> PlayerData;
[ReadOnly]
public NativeArray<CraBoneData> BoneData;
[ReadOnly]
public NativeArray<CraClipData> ClipData;
[ReadOnly]
public NativeArray<CraTransform> BakedClipTransforms;
public void Execute(int index, TransformAccess transform)
{
int playerIdx = BoneData[index].PlayerIndex;
int boneIndex = BoneData[index].ClipBoneIndex;
int playerMemIdx = playerIdx / 4;
int playerSubIdx = playerIdx % 4;
CraPlayerData player = PlayerData[playerMemIdx];
if (!player.IsPlaying[playerSubIdx])
{
return;
}
int clipIdx = player.ClipIndex[playerSubIdx];
int clipFrameCount = ClipData[clipIdx].FrameCount;
int clipFrameOffset = ClipData[clipIdx].FrameOffset;
int localFrameIndex = player.FrameIndex[playerSubIdx];
float transition = player.Transition[playerSubIdx];
int frameIndex = clipFrameOffset + (boneIndex * clipFrameCount) + localFrameIndex;
CraTransform frameTransform = BakedClipTransforms[frameIndex];
transform.localPosition = math.lerp(
transform.localPosition,
frameTransform.Position,
transition
);
transform.localRotation = math.slerp(
transform.localRotation,
frameTransform.Rotation,
transition
);
}
}
#if UNITY_EDITOR
public CraStatistics Statistics { get; private set; } = new CraStatistics();
#endif
// Player data memory
CraDataContainer<CraPlayerData> PlayerData;
int PlayerCounter;
// Bone data memory. These two arrays have the same length!
CraDataContainer<CraBoneData> BoneData;
TransformAccessArray Bones;
// Clip data memory
CraDataContainer<CraClipData> ClipData;
CraDataContainer<CraTransform> BakedClipTransforms;
// Jobs
CraPlayJob PlayerJob;
CraBoneEvalJob BoneJob;
// int is an index pointing into ClipData
Dictionary<CraClip, int> KnownClipIndices = new Dictionary<CraClip, int>();
Dictionary<int, CraClip> KnownClips = new Dictionary<int, CraClip>();
// Map from a given Unity Transform into BoneData & Bones
Dictionary<Transform, int> KnownBoneIndices = new Dictionary<Transform, int>();
// for each bone in our memory buffer, provides a map to retrieve the bone's local
// bone index within a clip, i.e. ClipIndex -> LocalBoneIndex
List<Dictionary<int, int>> BonePlayerClipIndices = new List<Dictionary<int, int>>();
// For each player, provide a list of bone indices into BoneData & Bones
List<List<int>> PlayerAssignedBones = new List<List<int>>();
CraPlaybackManager()
{
Instance = this;
PlayerData = new CraDataContainer<CraPlayerData>(CraSettings.MAX_PLAYER_DATA);
ClipData = new CraDataContainer<CraClipData>(CraSettings.MAX_CLIP_DATA);
BakedClipTransforms = new CraDataContainer<CraTransform>(CraSettings.MAX_BAKED_CLIP_TRANSFORMS);
BoneData = new CraDataContainer<CraBoneData>(CraSettings.MAX_BONE_DATA);
Bones = new TransformAccessArray(CraSettings.MAX_BONES);
#if UNITY_EDITOR
Statistics.PlayerData.MaxElements = CraSettings.MAX_PLAYER_DATA;
Statistics.PlayerData.MaxBytes = CraPlayerData.SIZE * CraSettings.MAX_PLAYER_DATA;
Statistics.ClipData.MaxElements = CraSettings.MAX_CLIP_DATA;
Statistics.ClipData.MaxBytes = CraClipData.SIZE * CraSettings.MAX_CLIP_DATA;
Statistics.BakedClipTransforms.MaxElements = CraSettings.MAX_BAKED_CLIP_TRANSFORMS;
Statistics.BakedClipTransforms.MaxBytes = CraTransform.SIZE * CraSettings.MAX_BAKED_CLIP_TRANSFORMS;
Statistics.BoneData.MaxElements = CraSettings.MAX_BONE_DATA;
Statistics.BoneData.MaxBytes = CraBoneData.SIZE * CraSettings.MAX_BONE_DATA;
Statistics.Bones.MaxElements = CraSettings.MAX_BONES;
Statistics.Bones.MaxBytes = (sizeof(bool) + sizeof(int) * 2) * CraSettings.MAX_BONES;
#endif
PlayerJob = new CraPlayJob()
{
PlayerData = PlayerData.GetMemoryBuffer(),
ClipData = ClipData.GetMemoryBuffer()
};
BoneJob = new CraBoneEvalJob()
{
PlayerData = PlayerData.GetMemoryBuffer(),
BoneData = BoneData.GetMemoryBuffer(),
ClipData = ClipData.GetMemoryBuffer(),
BakedClipTransforms = BakedClipTransforms.GetMemoryBuffer()
};
}
public static CraPlaybackManager Get()
{
if (Instance != null)
{
return Instance;
}
Instance = new CraPlaybackManager();
return Instance;
}
public CraHandle PlayerNew()
{
if (PlayerCounter + 1 >= (PlayerData.GetCapacity() * 4))
{
Debug.LogError($"Limit of {CraSettings.MAX_PLAYERS} Animation Players ({PlayerData.GetCapacity()} Player Data) reached!");
return new CraHandle(-1);
}
int dataIdx = PlayerCounter / 4;
int subIdex = PlayerCounter % 4;
if (dataIdx >= PlayerData.GetNumAllocated())
{
int newIdx = PlayerData.Alloc();
Debug.Assert(newIdx == dataIdx);
}
CraPlayerData data = PlayerData.Get(dataIdx);
data.ClipIndex[subIdex] = -1;
data.PlaybackSpeed[subIdex] = 1f;
data.Transition[subIdex] = 1f;
PlayerData.Set(dataIdx, in data);
Debug.Assert(PlayerAssignedBones.Count == PlayerCounter);
PlayerAssignedBones.Add(new List<int>());
return new CraHandle(PlayerCounter++);
}
public void PlayerSetClip(CraHandle player, CraClip clip)
{
Debug.Assert(clip != null);
(CraPlayerData data, int subIdex) = PlayerGet(player);
int clipIdx = data.ClipIndex[subIdex];
if (clipIdx >= 0)
{
Debug.LogWarning($"Player {player.Handle} already has clip {clipIdx} assigned!");
return;
}
if (!KnownClipIndices.TryGetValue(clip, out clipIdx))
{
clipIdx = CopyClip(clip);
if (clipIdx < 0)
{
Debug.LogError($"Setting clip {clip.Name} to Player {player.Handle} failed!");
return;
}
}
data.ClipIndex[subIdex] = clipIdx;
data.Duration[subIdex] = clip.FrameCount / clip.Fps;
data.Transition[subIdex] = 1f;
PlayerSet(player, ref data);
}
public void PlayerCaptureBones(CraHandle player)
{
List<int> boneIndices = PlayerAssignedBones[player.Handle];
for (int i = 0; i < boneIndices.Count; ++i)
{
int boneIdx = boneIndices[i];
(CraPlayerData playerData, int subIdex) = PlayerGet(player);
int clipIdx = playerData.ClipIndex[subIdex];
// Let the bone point to our Player and Clip
CraBoneData boneData = BoneData.Get(boneIdx);
boneData.PlayerIndex = player.Handle;
boneData.ClipBoneIndex = BonePlayerClipIndices[boneIdx][clipIdx];
BoneData.Set(boneIdx, in boneData);
}
}
public void PlayerReset(CraHandle player)
{
(CraPlayerData data, int subIdex) = PlayerGet(player);
data.Finished[subIdex] = false;
data.Playback[subIdex] = 0f;
data.IsPlaying[subIdex] = false;
PlayerSet(player, ref data);
}
public bool PlayerIsPlaying(CraHandle player)
{
(CraPlayerData data, int subIdex) = PlayerGet(player);
return data.IsPlaying[subIdex];
}
public float PlayerGetDuration(CraHandle player)
{
(CraPlayerData data, int subIdex) = PlayerGet(player);
return data.Duration[subIdex];
}
public float PlayerGetPlayback(CraHandle player)
{
(CraPlayerData data, int subIdex) = PlayerGet(player);
return data.Playback[subIdex];
}
public void PlayerPlay(CraHandle player, bool transit = false)
{
(CraPlayerData data, int subIdex) = PlayerGet(player);
if (data.PlaybackSpeed[subIdex] > 0f)
{
data.Playback[subIdex] = .001f;
}
else
{
data.Playback[subIdex] = data.Duration[subIdex] - .001f;
}
data.IsPlaying[subIdex] = true;
data.Transition[subIdex] = transit ? 0f : 1f;
PlayerSet(player, ref data);
}
public float PlayerGetPlaybackSpeed(CraHandle player)
{
(CraPlayerData data, int subIdex) = PlayerGet(player);
return data.PlaybackSpeed[subIdex];
}
public void PlayerSetPlaybackSpeed(CraHandle player, float speed)
{
(CraPlayerData data, int subIdex) = PlayerGet(player);
data.PlaybackSpeed[subIdex] = speed;//Mathf.Max(speed, 0.01f);
PlayerSet(player, ref data);
}
public void PlayerResetTransition(CraHandle player)
{
(CraPlayerData data, int subIdex) = PlayerGet(player);
data.Transition[subIdex] = 0f;
PlayerSet(player, ref data);
}
public bool PlayerIsLooping(CraHandle player)
{
(CraPlayerData data, int subIdex) = PlayerGet(player);
return data.Looping[subIdex];
}
public void PlayerSetLooping(CraHandle player, bool loop)
{
(CraPlayerData data, int subIdex) = PlayerGet(player);
data.Looping[subIdex] = loop;
PlayerSet(player, ref data);
}
public bool PlayerIsFinished(CraHandle player)
{
(CraPlayerData data, int subIdex) = PlayerGet(player);
return data.Finished[subIdex];
}
public void PlayerAssign(CraHandle player, Transform root, CraMask? mask = null)
{
Debug.Assert(root != null);
if (CraSettings.BoneHashFunction == null)
{
throw new Exception("CraSettings.BoneHashFunction is not assigned! You need to assign a custom hash function!");
}
List<int> assignedBones = PlayerAssignedBones[player.Handle];
if (assignedBones.Count > 0)
{
Debug.LogWarning($"Player {player.Handle} already has bones assigned!");
return;
}
(CraPlayerData data, int subIdex) = PlayerGet(player);
int clipIdx = data.ClipIndex[subIdex];
if (clipIdx < 0)
{
Debug.LogError($"Cannot assign Transform '{root.name}' to CraPlayer! No clip(s) set!");
return;
}
CraClip clip = KnownClips[clipIdx];
PlayerAssignInternal(assignedBones, clip, clipIdx, root, mask);
data.Transition[subIdex] = 1f;
PlayerSet(player, ref data);
PlayerCaptureBones(player);
}
void PlayerAssignInternal(List<int> assignedBones, CraClip clip, int clipIdx, Transform current, CraMask? mask = null, bool maskedChild = false)
{
void AddBone(int clipBoneIdx)
{
int allocIdx;
if (!KnownBoneIndices.TryGetValue(current, out allocIdx))
{
allocIdx = BoneData.Alloc();
KnownBoneIndices.Add(current, allocIdx);
BonePlayerClipIndices.Add(new Dictionary<int, int>());
Bones.Add(current);
Debug.Assert(KnownBoneIndices.Count == BoneData.GetNumAllocated());
Debug.Assert(BonePlayerClipIndices.Count == BoneData.GetNumAllocated());
Debug.Assert(Bones.length == BoneData.GetNumAllocated());
}
BonePlayerClipIndices[allocIdx].Add(clipIdx, clipBoneIdx);
assignedBones.Add(allocIdx);
}
int boneHash = CraSettings.BoneHashFunction(current.name);
bool isMasked = false;
if (clip.BoneHashToIdx.TryGetValue(boneHash, out int clipBoneIdx))
{
if (mask.HasValue)
{
if (maskedChild || mask.Value.BoneHashes.Contains(boneHash))
{
AddBone(clipBoneIdx);
isMasked = mask.Value.MaskChildren;
}
}
else
{
AddBone(clipBoneIdx);
}
}
for (int i = 0; i < current.childCount; ++i)
{
PlayerAssignInternal(assignedBones, clip, clipIdx, current.GetChild(i), mask, isMasked);
}
}
(CraPlayerData, int) PlayerGet(CraHandle player)
{
int dataIdx = player.Handle / 4;
int subIdex = player.Handle % 4;
return (PlayerData.Get(dataIdx), subIdex);
}
void PlayerSet(CraHandle player, ref CraPlayerData data)
{
int dataIdx = player.Handle / 4;
PlayerData.Set(dataIdx, in data);
}
int CopyClip(CraClip clip)
{
int clipIdx = ClipData.Alloc();
if (clipIdx < 0)
{
return -1;
}
CraClipData data = ClipData.Get(clipIdx);
data.FPS = clip.Fps;
data.FrameCount = clip.FrameCount;
data.FrameOffset = BakedClipTransforms.GetNumAllocated();
ClipData.Set(clipIdx, in data);
KnownClipIndices.Add(clip, clipIdx);
KnownClips.Add(clipIdx, clip);
for (int i = 0; i < clip.Bones.Length; ++i)
{
if (clip.Bones[i].Curve.BakedFrames == null)
{
Debug.LogError($"Given clip '{clip.Name}' is not fully baked!");
return -1;
}
if (!BakedClipTransforms.AllocFrom(clip.Bones[i].Curve.BakedFrames))
{
return -1;
}
}
return clipIdx;
}
public void Clear()
{
PlayerData.Clear();
PlayerCounter = 0;
BoneData.Clear();
Bones.SetTransforms(new Transform[] { });
ClipData.Clear();
BakedClipTransforms.Clear();
KnownClipIndices.Clear();
KnownClips.Clear();
KnownBoneIndices.Clear();
BonePlayerClipIndices.Clear();
PlayerAssignedBones.Clear();
}
public void Destroy()
{
Debug.Log("Deleting all animator manager data");
PlayerData.Destroy();
ClipData.Destroy();
BoneData.Destroy();
Bones.Dispose();
BakedClipTransforms.Destroy();
Instance = null;
}
public void Tick()
{
PlayerJob.DeltaTime = Time.deltaTime;
JobHandle playerJob = PlayerJob.Schedule(PlayerData.GetNumAllocated(), 8);
// Update ALL players FIRST before evaluating ALL bones!
JobHandle boneJob = BoneJob.Schedule(Bones, playerJob);
boneJob.Complete();
#if UNITY_EDITOR
Statistics.PlayerData.CurrentElements = PlayerData.GetNumAllocated();
Statistics.PlayerData.CurrentBytes = CraPlayerData.SIZE * (ulong)PlayerData.GetNumAllocated();
Statistics.ClipData.CurrentElements = ClipData.GetNumAllocated();
Statistics.ClipData.CurrentBytes = CraClipData.SIZE * (ulong)ClipData.GetNumAllocated();
Statistics.BakedClipTransforms.CurrentElements = BakedClipTransforms.GetNumAllocated();
Statistics.BakedClipTransforms.CurrentBytes = CraTransform.SIZE * (ulong)BakedClipTransforms.GetNumAllocated();
Statistics.BoneData.CurrentElements = BoneData.GetNumAllocated();
Statistics.BoneData.CurrentBytes = CraBoneData.SIZE * (ulong)BoneData.GetNumAllocated();
Statistics.Bones.CurrentElements = Bones.length;
Statistics.Bones.CurrentBytes = (sizeof(bool) + sizeof(int) * 2) * (ulong)Bones.length;
#endif
}
}