-
-
Notifications
You must be signed in to change notification settings - Fork 911
/
AnimationProcessor.cs
211 lines (179 loc) · 9.92 KB
/
AnimationProcessor.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
// 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.
using System;
using System.Collections.Generic;
using Stride.Core.Collections;
using Stride.Core.Threading;
using Stride.Engine;
using Stride.Games;
using Stride.Rendering;
namespace Stride.Animations
{
public class AnimationProcessor : EntityProcessor<AnimationComponent, AnimationProcessor.AssociatedData>
{
private readonly ConcurrentPool<FastList<AnimationOperation>> animationOperationPool = new ConcurrentPool<FastList<AnimationOperation>>(() => new FastList<AnimationOperation>());
public AnimationProcessor()
{
Order = -500;
}
protected override AssociatedData GenerateComponentData(Entity entity, AnimationComponent component)
{
return new AssociatedData
{
AnimationComponent = component,
};
}
protected override bool IsAssociatedDataValid(Entity entity, AnimationComponent component, AssociatedData associatedData)
{
return component == associatedData.AnimationComponent;
}
protected override void OnEntityComponentAdding(Entity entity, AnimationComponent component, AssociatedData data)
{
data.AnimationUpdater = new AnimationUpdater();
}
protected override void OnEntityComponentRemoved(Entity entity, AnimationComponent component, AssociatedData data)
{
// Return AnimationClipEvaluators to pool
foreach (var playingAnimation in data.AnimationComponent.PlayingAnimations)
{
var evaluator = playingAnimation.Evaluator;
if (evaluator != null)
{
data.AnimationComponent.Blender.ReleaseEvaluator(evaluator);
playingAnimation.Evaluator = null;
}
}
// Return AnimationClipResult to pool
if (data.AnimationClipResult != null)
data.AnimationComponent.Blender.FreeIntermediateResult(data.AnimationClipResult);
}
public override void Draw(RenderContext context)
{
var time = context.Time;
//foreach (var entity in ComponentDatas.Values)
Dispatcher.ForEach(ComponentDatas, () => animationOperationPool.Acquire(), (entity, animationOperations) =>
{
var associatedData = entity.Value;
var animationUpdater = associatedData.AnimationUpdater;
var animationComponent = associatedData.AnimationComponent;
if (animationComponent.BlendTreeBuilder != null)
{
animationComponent.BlendTreeBuilder.BuildBlendTree(animationOperations);
}
else
{
// Advance time for all playing animations with AutoPlay set to on
foreach (var playingAnimation in animationComponent.PlayingAnimations)
{
if (!playingAnimation.Enabled || playingAnimation.Clip == null)
continue;
switch (playingAnimation.RepeatMode)
{
case AnimationRepeatMode.PlayOnceHold:
case AnimationRepeatMode.PlayOnce:
playingAnimation.CurrentTime = TimeSpan.FromTicks(playingAnimation.CurrentTime.Ticks + (long)(time.WarpElapsed.Ticks * (double)playingAnimation.TimeFactor));
if (playingAnimation.CurrentTime > playingAnimation.Clip.Duration)
playingAnimation.CurrentTime = playingAnimation.Clip.Duration;
else if (playingAnimation.CurrentTime < TimeSpan.Zero)
playingAnimation.CurrentTime = TimeSpan.Zero;
break;
case AnimationRepeatMode.LoopInfinite:
playingAnimation.CurrentTime = playingAnimation.Clip.Duration == TimeSpan.Zero
? TimeSpan.Zero
: TimeSpan.FromTicks((playingAnimation.CurrentTime.Ticks + playingAnimation.Clip.Duration.Ticks
+ (long)(time.WarpElapsed.Ticks * (double)playingAnimation.TimeFactor)) % playingAnimation.Clip.Duration.Ticks);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
// Regenerate animation operations
float totalWeight = 0.0f;
for (int index = 0; index < animationComponent.PlayingAnimations.Count; index++)
{
var playingAnimation = animationComponent.PlayingAnimations[index];
var animationWeight = playingAnimation.Weight;
// Skip animation with 0.0f weight
if (animationWeight == 0.0f || playingAnimation.Clip == null)
continue;
// Default behavior for linea blending (it will properly accumulate multiple blending with their cumulative weight)
totalWeight += animationWeight;
float currentBlend = animationWeight / totalWeight;
if (playingAnimation.BlendOperation == AnimationBlendOperation.Add)
{
// Additive or substractive blending will use the weight as is (and reset total weight with it)
currentBlend = animationWeight;
totalWeight = animationWeight;
}
// Create evaluator
var evaluator = playingAnimation.Evaluator;
if (evaluator == null)
{
evaluator = animationComponent.Blender.CreateEvaluator(playingAnimation.Clip);
playingAnimation.Evaluator = evaluator;
}
animationOperations.Add(CreatePushOperation(playingAnimation));
if (animationOperations.Count >= 2)
animationOperations.Add(AnimationOperation.NewBlend((CoreAnimationOperation)playingAnimation.BlendOperation, currentBlend));
}
}
if (animationOperations.Count > 0)
{
// Animation blending
animationComponent.Blender.Compute(animationOperations, ref associatedData.AnimationClipResult);
// Update animation data if we have a model component
animationUpdater.Update(animationComponent.Entity, associatedData.AnimationClipResult);
}
if (animationComponent.BlendTreeBuilder == null)
{
// Update weight animation
for (int index = 0; index < animationComponent.PlayingAnimations.Count; index++)
{
var playingAnimation = animationComponent.PlayingAnimations[index];
bool removeAnimation = false;
if (playingAnimation.CrossfadeRemainingTime > TimeSpan.Zero)
{
playingAnimation.Weight += (playingAnimation.WeightTarget - playingAnimation.Weight)
* ((float)time.WarpElapsed.Ticks / playingAnimation.CrossfadeRemainingTime.Ticks);
playingAnimation.CrossfadeRemainingTime -= time.WarpElapsed;
if (playingAnimation.CrossfadeRemainingTime <= TimeSpan.Zero)
{
playingAnimation.Weight = playingAnimation.WeightTarget;
// If weight target was 0, removes the animation
if (playingAnimation.Weight <= 0.0f)
removeAnimation = true;
}
}
if (playingAnimation.RepeatMode == AnimationRepeatMode.PlayOnce)
{
if ((playingAnimation.TimeFactor > 0 && playingAnimation.CurrentTime == playingAnimation.Clip.Duration) ||
(playingAnimation.TimeFactor < 0 && playingAnimation.CurrentTime == TimeSpan.Zero))
removeAnimation = true;
}
if (removeAnimation)
{
animationComponent.PlayingAnimations.RemoveAt(index--); // Will also release its evaluator
}
}
}
animationOperations.Clear();
}, animationOperations => animationOperationPool.Release(animationOperations));
}
private AnimationOperation CreatePushOperation(PlayingAnimation playingAnimation)
{
return AnimationOperation.NewPush(playingAnimation.Evaluator, playingAnimation.CurrentTime);
}
public AnimationClipResult GetAnimationClipResult(AnimationComponent animationComponent)
{
if (!ComponentDatas.ContainsKey(animationComponent))
return null;
return ComponentDatas[animationComponent].AnimationClipResult;
}
public class AssociatedData
{
public AnimationUpdater AnimationUpdater;
public AnimationComponent AnimationComponent;
public AnimationClipResult AnimationClipResult;
}
}
}