// Called when the "Animate" button was clicked. private void OnButtonClicked(object sender, EventArgs eventArgs) { // Get the selected easing function from the DropDownButton. EasingFunction easingFunction = (EasingFunction)_functionDropDown.Items[_functionDropDown.SelectedIndex]; // Set the selected easing mode. easingFunction.Mode = (EasingMode)_modeDropDown.Items[_modeDropDown.SelectedIndex]; // The current slider value: float startValue = _slider.Value; // Set the slider to the target value. _slider.Value = (startValue > 0.5f) ? 0 : 1; // Create a from-animation that uses the easing function. It animates the slider // value from startValue to the new value. SingleFromToByAnimation animation = new SingleFromToByAnimation { TargetProperty = "Value", From = startValue, Duration = TimeSpan.FromSeconds(1), EasingFunction = easingFunction, FillBehavior = FillBehavior.Stop, // Stop the animation when it is finished. }; // Start the animation. // (Use the Replace transition to replace any currently running animations. // This is necessary because the user could press the Animate button while // an animation is running.) _animationService.StartAnimation(animation, _slider, AnimationTransitions.Replace()) .UpdateAndApply(); // Apply new animation value immediately. }
public override void Update(GameTime gameTime) { base.Update(gameTime); // <Space> --> Cross-fade to next animation. if (InputService.IsPressed(Keys.Space, true)) { _currentAnimationIndex++; if (_currentAnimationIndex >= _animations.Length) { _currentAnimationIndex = 0; } for (int i = 0; i < NumberOfModels; i++) { // Start a next animation using a Replace transition with a fade-in time. AnimationService.StartAnimation( _animations[_currentAnimationIndex], (IAnimatableProperty <SkeletonPose>)_meshNodes[i].SkeletonPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.2))) .AutoRecycle(); } } // Sidenote: // SkeletonPose.Update() is a method that can be called to update all internal skeleton pose // data immediately. If SkeletonPose.Update() is not called, the internal data will // be updated when needed - for example, when SkeletonPose.SkinningMatricesXna are accessed. //for (int i = 0; i < NumberOfModels; i++) // _meshNodes[i].SkeletonPose.Update(); }
public override void Update(GameTime gameTime) { if (_avatarPose == null) { if (_avatarRenderer.State == AvatarRendererState.Ready) { _avatarPose = new AvatarPose(_avatarRenderer); // Start the first animation. var wrappedAnimation = WrapAnimation(_currentAvatarAnimationPreset); AnimationService.StartAnimation(wrappedAnimation, _avatarPose).AutoRecycle(); _debugRenderer.Clear(); _debugRenderer.DrawText("\n\nCurrent Animation: " + _currentAvatarAnimationPreset); } } else { if (InputService.IsPressed(Buttons.A, false, LogicalPlayerIndex.One)) { // Switch to next preset. _currentAvatarAnimationPreset++; if (!Enum.IsDefined(typeof(AvatarAnimationPreset), _currentAvatarAnimationPreset)) { _currentAvatarAnimationPreset = 0; } // Cross-fade to new animation. var wrappedAnimation = WrapAnimation(_currentAvatarAnimationPreset); AnimationService.StartAnimation(wrappedAnimation, _avatarPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.5)) ).AutoRecycle(); _debugRenderer.Clear(); _debugRenderer.DrawText("\n\nCurrent Animation: " + _currentAvatarAnimationPreset); } if (InputService.IsPressed(Buttons.B, false, LogicalPlayerIndex.One)) { // Switch to previous preset. _currentAvatarAnimationPreset--; if (!Enum.IsDefined(typeof(AvatarAnimationPreset), _currentAvatarAnimationPreset)) { _currentAvatarAnimationPreset = (AvatarAnimationPreset)EnumHelper.GetValues(typeof(AvatarAnimationPreset)).Length - 1; } // Cross-fade to new animation. var wrappedAnimation = WrapAnimation(_currentAvatarAnimationPreset); AnimationService.StartAnimation(wrappedAnimation, _avatarPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.5)) ).AutoRecycle(); _debugRenderer.Clear(); _debugRenderer.DrawText("\n\nCurrent Animation: " + _currentAvatarAnimationPreset); } } }
public override void Update(GameTime gameTime) { if (InputService.IsPressed(Keys.Space, false)) { if (_state == 1) { // Fade-in the vertical animation, replacing the previous animations. _state = 2; _currentAnimationController = AnimationService.StartAnimation( _verticalAnimation, _animatedPosition, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.5))); // Replace all previous animations using a fade-in of 0.5 seconds. _currentAnimationController.AutoRecycle(); } else { // Fade-in the horizontal animation, replacing the previous animations. _state = 1; _currentAnimationController = AnimationService.StartAnimation( _horizontalAnimation, _animatedPosition, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.5))); _currentAnimationController.AutoRecycle(); } } if (InputService.IsPressed(Keys.Enter, false)) { if (_state == 0) { // Fade-in the horizontal animation. _state = 1; _currentAnimationController = AnimationService.StartAnimation( _horizontalAnimation, _animatedPosition, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.5))); _currentAnimationController.AutoRecycle(); } else { // Fade-out the current animation. _state = 0; _currentAnimationController.Stop(TimeSpan.FromSeconds(0.5)); // Fade-out over 0.5 seconds. } } base.Update(gameTime); }
public override void Update(GameTime gameTime) { base.Update(gameTime); // <Space> --> Cross-fade to 'Aim-and-Shoot' animation. if (InputService.IsPressed(Keys.Space, false)) { // Start the new animation using a Replace transition with a fade-in time. _aimAndShootAnimationController = AnimationService.StartAnimation( _aimAndShootAnimation, (IAnimatableProperty)_meshNode.SkeletonPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.1))); _aimAndShootAnimationController.AutoRecycle(); } // <Up> --> Cross-fade to 'Run' animation - unless the 'Aim-and-Shoot' animation is playing // or the 'Run' animation is already playing. if (_aimAndShootAnimationController.State != AnimationState.Playing && _runAnimationController.State != AnimationState.Playing && InputService.IsDown(Keys.Up)) { _runAnimationController = AnimationService.StartAnimation( _runAnimation, (IAnimatableProperty)_meshNode.SkeletonPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.2))); _runAnimationController.AutoRecycle(); } if (_aimAndShootAnimationController.State != AnimationState.Playing) { // If none of the animations are playing, or if the user releases the <Up> key, // then restart the 'Idle' animation. if (_runAnimationController.State != AnimationState.Playing && _idleAnimationController.State != AnimationState.Playing || _runAnimationController.State == AnimationState.Playing && InputService.IsUp(Keys.Up)) { _idleAnimationController = AnimationService.StartAnimation( _idleAnimation, (IAnimatableProperty)_meshNode.SkeletonPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.2))); _idleAnimationController.AutoRecycle(); } } }
public CharacterCrossFadeSample(Microsoft.Xna.Framework.Game game) : base(game) { var modelNode = ContentManager.Load <ModelNode>("Marine/PlayerMarine"); _meshNode = modelNode.GetSubtree().OfType <MeshNode>().First().Clone(); SampleHelper.EnablePerPixelLighting(_meshNode); GraphicsScreen.Scene.Children.Add(_meshNode); Dictionary <string, SkeletonKeyFrameAnimation> animations = _meshNode.Mesh.Animations; // Create a looping 'Idle' animation. _idleAnimation = new AnimationClip <SkeletonPose>(animations["Idle"]) { LoopBehavior = LoopBehavior.Cycle, Duration = TimeSpan.MaxValue, }; // Create a looping 'Run' animation. _runAnimation = new AnimationClip <SkeletonPose>(animations["Run"]) { LoopBehavior = LoopBehavior.Cycle, Duration = TimeSpan.MaxValue, }; // Combine the 'Aim' and 'Shoot' animation. The 'Aim' animation should start immediately. // The 'Shoot' animation should start after 0.3 seconds. // (Animations can be combined by creating timeline groups. All timelines/animations // in a timeline group are played simultaneously. AnimationClips can be used to // arrange animations on a timeline. The property Delay, for example, can be used to // set the begin time.) _aimAndShootAnimation = new TimelineGroup(); _aimAndShootAnimation.Add(animations["Aim"]); _aimAndShootAnimation.Add(new AnimationClip <SkeletonPose>(animations["Shoot"]) { Delay = TimeSpan.FromSeconds(0.3) }); // Start 'Idle' animation. We use a Replace transition with a fade-in. _idleAnimationController = AnimationService.StartAnimation( _idleAnimation, (IAnimatableProperty)_meshNode.SkeletonPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.5))); _idleAnimationController.AutoRecycle(); }
public override void Update(GameTime gameTime) { if (_avatarPose == null) { if (_avatarRenderer.State == AvatarRendererState.Ready) { _avatarPose = new AvatarPose(_avatarRenderer); AnimationService.StartAnimation(_waveAnimation, _avatarPose).AutoRecycle(); } } else if (InputService.IsPressed(Buttons.A, false, LogicalPlayerIndex.One)) { // Restart animation using a cross-fade of 0.5 seconds. AnimationService.StartAnimation(_waveAnimation, _avatarPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.5)) ).AutoRecycle(); } }
public void Replace() { var property = new AnimatableProperty <float> { Value = 10.0f }; var animation = new SingleFromToByAnimation { From = 100.0f, To = 200.0f, IsAdditive = true, }; var manager = new AnimationManager(); var controllerA = manager.CreateController(animation, property); Assert.AreEqual(10.0f, property.Value); controllerA.Start(AnimationTransitions.Replace()); controllerA.UpdateAndApply(); Assert.AreEqual(110.0f, property.Value); // Changing the base value has no effect. property.Value = 20.0f; manager.Update(TimeSpan.Zero); manager.ApplyAnimations(); Assert.AreEqual(120.0f, property.Value); // Start second animation using Replace. var controllerB = manager.CreateController(animation, property); Assert.AreEqual(120.0f, property.Value); controllerB.Start(AnimationTransitions.Replace(controllerA.AnimationInstance)); controllerB.UpdateAndApply(); Assert.AreEqual(120.0f, property.Value); controllerB.Stop(); controllerB.UpdateAndApply(); Assert.AreEqual(20.0f, property.Value); }
public override void Update(GameTime gameTime) { base.Update(gameTime); if (InputService.IsDown(Keys.Up)) { if (!_isRunning) { _isRunning = true; // Start 'Run' animation. We use a Replace transition and replace the 'Idle' // animation which is the first in the animation composition chain. Since we only // replace one specific animation, the 'Shoot' animation will stay in the composition // chain and keep playing. _runAnimationController = AnimationService.StartAnimation( _runAnimation, (IAnimatableProperty)_meshNode.SkeletonPose, AnimationTransitions.Replace(_idleAnimationController.AnimationInstance, TimeSpan.FromSeconds(0.3))); _runAnimationController.AutoRecycle(); } } else { if (_isRunning) { _isRunning = false; // Start 'Idle' animation and replace the 'Run' animation. _idleAnimationController = AnimationService.StartAnimation( _idleAnimation, (IAnimatableProperty)_meshNode.SkeletonPose, AnimationTransitions.Replace(_runAnimationController.AnimationInstance, TimeSpan.FromSeconds(0.3))); _idleAnimationController.AutoRecycle(); } } }
public void ReplaceAllWithFadeIn() { var property = new AnimatableProperty <float> { Value = 100.0f }; var animationA = new SingleFromToByAnimation { Duration = TimeSpan.Zero, To = 200.0f, }; var manager = new AnimationManager(); var controllerA = manager.CreateController(animationA, property); Assert.AreEqual(100.0f, property.Value); controllerA.Start(AnimationTransitions.Replace(TimeSpan.FromSeconds(1.0))); Assert.AreEqual(100.0f, property.Value); // Changing the base value has no effect. property.Value = 150.0f; manager.Update(TimeSpan.Zero); manager.ApplyAnimations(); Assert.AreEqual(150.0f, property.Value); manager.Update(TimeSpan.FromSeconds(0.5)); manager.ApplyAnimations(); Assert.AreEqual(175.0f, property.Value); manager.Update(TimeSpan.FromSeconds(0.5)); manager.ApplyAnimations(); Assert.AreEqual(200.0f, property.Value); // Start second animation using Replace. var animationB = new SingleFromToByAnimation { Duration = TimeSpan.Zero, To = 100.0f, }; var controllerB = manager.CreateController(animationB, property); Assert.AreEqual(200.0f, property.Value); controllerB.Start(AnimationTransitions.Replace(TimeSpan.FromSeconds(1.0))); Assert.AreEqual(200.0f, property.Value); manager.Update(TimeSpan.FromSeconds(0.5)); manager.ApplyAnimations(); Assert.AreEqual(150.0f, property.Value); manager.Update(TimeSpan.FromSeconds(0.5)); manager.ApplyAnimations(); Assert.AreEqual(100.0f, property.Value); manager.Update(TimeSpan.FromSeconds(0.5)); manager.ApplyAnimations(); Assert.AreEqual(100.0f, property.Value); Assert.AreEqual(AnimationState.Stopped, controllerA.State); }
public override void Update(GameTime gameTime) { if (_avatarPose == null) { if (_avatarRenderer.State == AvatarRendererState.Ready) { _avatarPose = new AvatarPose(_avatarRenderer); // Start stand animation. _standAnimationController = AnimationService.StartAnimation(_standAnimation, _avatarPose); _standAnimationController.AutoRecycle(); } } else { // When the user presses buttons, we cross-fade to the custom animations. if (InputService.IsPressed(Buttons.A, false, LogicalPlayerIndex.One)) { _actionAnimationController = AnimationService.StartAnimation( _jumpAnimation, _avatarPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.3))); _actionAnimationController.AutoRecycle(); } if (InputService.IsPressed(Buttons.B, false, LogicalPlayerIndex.One)) { _actionAnimationController = AnimationService.StartAnimation( _punchAnimation, _avatarPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.3))); _actionAnimationController.AutoRecycle(); } if (InputService.IsPressed(Buttons.X, false, LogicalPlayerIndex.One)) { _actionAnimationController = AnimationService.StartAnimation( _kickAnimation, _avatarPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.3))); _actionAnimationController.AutoRecycle(); } if (InputService.IsPressed(Buttons.Y, false, LogicalPlayerIndex.One)) { _actionAnimationController = AnimationService.StartAnimation( _faintAnimation, _avatarPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.3))); _actionAnimationController.AutoRecycle(); } // The left trigger controls the speed of the walk cycle. float leftTrigger = Math.Abs(InputService.GetGamePadState(LogicalPlayerIndex.One).Triggers.Left); _walkAnimationController.Speed = leftTrigger * 2; if (_walkAnimationController.State != AnimationState.Playing) { // The walk cycle is not playing. // --> Start walk animation if left trigger is pressed. if (leftTrigger > 0) { _walkAnimationController = AnimationService.StartAnimation( _walkAnimation, _avatarPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.3))); _walkAnimationController.AutoRecycle(); } } else { // The walk cycle is playing. // --> Cross-fade to stand animation if left trigger is not pressed. if (leftTrigger == 0) { _standAnimationController = AnimationService.StartAnimation( _standAnimation, _avatarPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.3))); _standAnimationController.AutoRecycle(); } } // If none of the animations is playing, then restart the stand animation. if (_standAnimationController.State != AnimationState.Playing && _actionAnimationController.State != AnimationState.Playing && _walkAnimationController.State != AnimationState.Playing) { _standAnimationController = AnimationService.StartAnimation( _standAnimation, _avatarPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.3))); _standAnimationController.AutoRecycle(); } } }
public void AnimationShapshots() { var property = new AnimatableProperty <float> { Value = 10.0f }; var manager = new AnimationManager(); var byAnimation = new SingleFromToByAnimation { Duration = TimeSpan.Zero, By = 25.0f, }; var byController = manager.CreateController(byAnimation, property); Assert.AreEqual(10.0f, property.Value); // Without snapshot. byController.Start(AnimationTransitions.Replace()); byController.UpdateAndApply(); Assert.AreEqual(35.0f, property.Value); property.Value = 100.0f; manager.Update(TimeSpan.Zero); manager.ApplyAnimations(); Assert.AreEqual(125.0f, property.Value); byController.Stop(); byController.UpdateAndApply(); Assert.AreEqual(100.0f, property.Value); // With snapshot. property.Value = 10.0f; byController.Start(AnimationTransitions.SnapshotAndReplace()); byController.UpdateAndApply(); Assert.AreEqual(35.0f, property.Value); property.Value = 100.0f; manager.Update(TimeSpan.Zero); manager.ApplyAnimations(); Assert.AreEqual(35.0f, property.Value); // Create another snapshot. var additiveAnimation = new SingleFromToByAnimation { Duration = TimeSpan.Zero, To = 200.0f, IsAdditive = true, }; var additiveController = manager.CreateController(additiveAnimation, property); additiveController.Start(AnimationTransitions.SnapshotAndReplace()); additiveController.UpdateAndApply(); Assert.AreEqual(235.0f, property.Value); byController.Stop(); Assert.AreEqual(235.0f, property.Value); byController.Start(AnimationTransitions.Replace()); byController.UpdateAndApply(); Assert.AreEqual(60.0f, property.Value); // 35.0f (Snapshot) + 25.0f (By Animation) }