4 using System.Collections.Generic;
6 using SiliconStudio.Core;
7 using SiliconStudio.Core.Collections;
9 using SiliconStudio.Core.Diagnostics;
10 using SiliconStudio.Paradox.DataModel;
11 using SiliconStudio.Core.Extensions;
12 using SiliconStudio.Core.Mathematics;
13 using SiliconStudio.Paradox.Effects;
15 using SiliconStudio.Paradox.EntityModel;
16 using SiliconStudio.Paradox.Games;
18 namespace SiliconStudio.
Paradox.Engine
22 private FastList<AnimationOperation> animationOperations =
new FastList<AnimationOperation>(2);
26 : base(new
PropertyKey[] { ModelComponent.Key, AnimationComponent.Key })
37 base.OnEntityAdding(entity, associatedData);
44 base.OnEntityRemoved(entity, data);
47 foreach (var playingAnimation
in data.AnimationComponent.PlayingAnimations)
49 var evaluator = playingAnimation.Evaluator;
50 if (evaluator != null)
52 data.AnimationComponent.Blender.ReleaseEvaluator(evaluator);
53 playingAnimation.Evaluator = null;
58 if (data.AnimationClipResult != null)
59 data.AnimationComponent.Blender.FreeIntermediateResult(data.AnimationClipResult);
64 foreach (var entity
in enabledEntities)
66 var associatedData = entity.Value;
67 var meshAnimation = associatedData.MeshAnimationUpdater;
68 var animationComponent = associatedData.AnimationComponent;
71 foreach (var playingAnimation
in animationComponent.PlayingAnimations)
73 if (playingAnimation.IsPlaying)
75 switch (playingAnimation.RepeatMode)
77 case AnimationRepeatMode.PlayOnce:
78 playingAnimation.CurrentTime = TimeSpan.FromTicks(playingAnimation.CurrentTime.Ticks + (long)(time.Elapsed.Ticks * (
double)playingAnimation.TimeFactor));
79 if (playingAnimation.CurrentTime > playingAnimation.Clip.Duration)
80 playingAnimation.CurrentTime = playingAnimation.Clip.Duration;
82 case AnimationRepeatMode.LoopInfinite:
83 playingAnimation.CurrentTime = TimeSpan.FromTicks((playingAnimation.CurrentTime.Ticks + (long)(time.Elapsed.Ticks * (
double)playingAnimation.TimeFactor)) % playingAnimation.Clip.Duration.Ticks);
86 throw new ArgumentOutOfRangeException();
92 animationOperations.Clear();
94 float totalWeight = 0.0f;
96 for (
int index = 0; index < animationComponent.PlayingAnimations.Count; index++)
98 var playingAnimation = animationComponent.PlayingAnimations[index];
99 var animationWeight = playingAnimation.Weight;
102 if (animationWeight == 0.0f)
106 totalWeight += animationWeight;
107 float currentBlend = animationWeight / totalWeight;
113 currentBlend = animationWeight;
114 totalWeight = animationWeight;
118 var evaluator = playingAnimation.Evaluator;
119 if (evaluator == null)
121 evaluator = animationComponent.Blender.CreateEvaluator(playingAnimation.Clip);
122 playingAnimation.Evaluator = evaluator;
125 animationOperations.Add(CreatePushOperation(playingAnimation));
127 if (animationOperations.Count >= 2)
128 animationOperations.Add(AnimationOperation.NewBlend(playingAnimation.BlendOperation, currentBlend));
131 if (animationOperations.Count > 0)
134 animationComponent.Blender.Compute(animationOperations, ref associatedData.AnimationClipResult);
137 meshAnimation.Update(associatedData.ModelComponent.ModelViewHierarchy, associatedData.AnimationClipResult);
142 associatedData.ModelComponent.ModelViewHierarchy.ResetInitialValues();
146 for (
int index = 0; index < animationComponent.PlayingAnimations.Count; index++)
148 var playingAnimation = animationComponent.PlayingAnimations[index];
149 if (playingAnimation.RemainingTime > TimeSpan.Zero)
151 playingAnimation.Weight += (playingAnimation.WeightTarget - playingAnimation.Weight)*
152 ((
float)time.Elapsed.Ticks/
153 (float)playingAnimation.RemainingTime.Ticks);
154 playingAnimation.RemainingTime -= time.Elapsed;
155 if (playingAnimation.RemainingTime <= TimeSpan.Zero)
157 playingAnimation.Weight = playingAnimation.WeightTarget;
160 else if (playingAnimation.Weight / totalWeight <= 0.01f)
162 animationComponent.PlayingAnimations.RemoveAt(index--);
164 var evaluator = playingAnimation.Evaluator;
165 if (evaluator != null)
167 animationComponent.Blender.ReleaseEvaluator(evaluator);
168 playingAnimation.Evaluator = null;
177 return AnimationOperation.NewPush(playingAnimation.Evaluator, playingAnimation.CurrentTime);
Game entity. It usually aggregates multiple EntityComponent
ModelComponent ModelComponent
AnimationClipResult AnimationClipResult
AnimationBlendOperation
Describes the type of animation blend operation.
override void Draw(GameTime time)
A single animation operation (push or blend).
MeshAnimationUpdater MeshAnimationUpdater
Applies animation from a AnimationClip to a ModelViewHierarchyUpdater.
override void OnEntityRemoved(Entity entity, AnimationProcessor.AssociatedData data)
Entity processor, triggered on various EntitySystem events such as Entity and Component additions and...
Add animation capabilities to an Entity. It will usually apply to ModelComponent.ModelViewHierarchy ...
Current timing used for variable-step (real time) or fixed-step (game time) games.
AnimationComponent AnimationComponent
override void OnEntityAdding(Entity entity, AssociatedData associatedData)
Add a Model to an Entity, that will be used during rendering.
static PropertyKey< AnimationComponent > Key
override AssociatedData GenerateAssociatedData(Entity entity)
A class that represents a tag propety.