public BoneJiggleSample(Microsoft.Xna.Framework.Game game) : base(game) { var modelNode = ContentManager.Load<ModelNode>("Dude/Dude"); _meshNode = modelNode.GetSubtree().OfType<MeshNode>().First().Clone(); _meshNode.PoseLocal = new Pose(new Vector3F(0, 0, 0), Matrix33F.CreateRotationY(ConstantsF.Pi)); SampleHelper.EnablePerPixelLighting(_meshNode); GraphicsScreen.Scene.Children.Add(_meshNode); var animations = _meshNode.Mesh.Animations; var walkAnimation = new AnimationClip<SkeletonPose>(animations.Values.First()) { LoopBehavior = LoopBehavior.Cycle, Duration = TimeSpan.MaxValue, }; _walkAnimationController = AnimationService.StartAnimation(walkAnimation, (IAnimatableProperty)_meshNode.SkeletonPose); _walkAnimationController.AutoRecycle(); // Create a BoneJiggler instance for the head bone (bone index 7). _boneJiggler = new BoneJiggler(_meshNode.SkeletonPose, 7, new Vector3F(1.1f, 0, 0)) { Spring = 100, Damping = 3, }; }
public override void Update(GameTime gameTime) { base.Update(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.Back, 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. } } }
public MixingSample(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); var animations = _meshNode.Mesh.Animations; _runAnimation = new AnimationClip<SkeletonPose>(animations["Run"]) { LoopBehavior = LoopBehavior.Cycle, Duration = TimeSpan.MaxValue, }; _idleAnimation = new AnimationClip<SkeletonPose>(animations["Idle"]) { LoopBehavior = LoopBehavior.Cycle, Duration = TimeSpan.MaxValue, }; // Create a 'Shoot' animation that only affects the upper body. var shootAnimation = animations["Shoot"]; // The SkeletonKeyFrameAnimations allows to set a weight for each bone channel. // For the 'Shoot' animation, we set the weight to 0 for all bones that are // not descendants of the second spine bone (bone index 2). That means, the // animation affects only the upper body bones and is disabled on the lower // body bones. for (int i = 0; i < _meshNode.Mesh.Skeleton.NumberOfBones; i++) { if (!SkeletonHelper.IsAncestorOrSelf(_meshNode.SkeletonPose, 2, i)) shootAnimation.SetWeight(i, 0); } var loopedShootingAnimation = new AnimationClip<SkeletonPose>(shootAnimation) { LoopBehavior = LoopBehavior.Cycle, Duration = TimeSpan.MaxValue, }; // Start 'Idle' animation. _idleAnimationController = AnimationService.StartAnimation(_idleAnimation, (IAnimatableProperty)_meshNode.SkeletonPose); _idleAnimationController.AutoRecycle(); // Start looping the 'Shoot' animation. We use a Compose transition. This will add the // 'Shoot' animation to the animation composition chain and keeping all other playing // animations. // The 'Idle' animation animates the whole skeleton. The 'Shoot' animation replaces // the 'Idle' animation on the bones of the upper body. AnimationService.StartAnimation(loopedShootingAnimation, (IAnimatableProperty)_meshNode.SkeletonPose, AnimationTransitions.Compose() ).AutoRecycle(); }
public CrossFadeSample(Microsoft.Xna.Framework.Game game) : base(game) { Rectangle bounds = GraphicsService.GraphicsDevice.Viewport.TitleSafeArea; // Set the base value of the _animatedPosition. When the animations are stopped, // _animatedPosition will return to this value. _animatedPosition.Value = new Vector2(bounds.Center.X, bounds.Center.Y); // Create an oscillating horizontal animation. Vector2FromToByAnimation fromToAnimation = new Vector2FromToByAnimation { From = new Vector2(200, bounds.Center.Y), To = new Vector2(bounds.Right - 200, bounds.Center.Y), Duration = TimeSpan.FromSeconds(2), EasingFunction = new CubicEase { Mode = EasingMode.EaseInOut }, }; _horizontalAnimation = new AnimationClip<Vector2>(fromToAnimation) { LoopBehavior = LoopBehavior.Oscillate, Duration = TimeSpan.MaxValue, }; // Create an oscillating vertical animation. fromToAnimation = new Vector2FromToByAnimation { From = new Vector2(bounds.Center.X, 100), To = new Vector2(bounds.Center.X, bounds.Bottom - 100), Duration = TimeSpan.FromSeconds(2), EasingFunction = new CubicEase { Mode = EasingMode.EaseInOut }, }; _verticalAnimation = new AnimationClip<Vector2>(fromToAnimation) { LoopBehavior = LoopBehavior.Oscillate, Duration = TimeSpan.MaxValue, }; // Start the horizontal movement. AnimationService.StartAnimation() returns an // AnimationController. We keep this AnimationController because it is needed to fade-out // the animation. _state = 1; _currentAnimationController = AnimationService.StartAnimation(_horizontalAnimation, _animatedPosition); _currentAnimationController.UpdateAndApply(); // When the animation is stopped, all intermediate animation objects should be recycled. _currentAnimationController.AutoRecycle(); }
public override void Update(GameTime 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)_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)_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)_skeletonPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.2))); _idleAnimationController.AutoRecycle(); } } base.Update(gameTime); }
public DudeWalkingSample(Microsoft.Xna.Framework.Game game) : base(game) { var modelNode = ContentManager.Load<ModelNode>("Dude/Dude"); var meshNode = modelNode.GetSubtree().OfType<MeshNode>().First().Clone(); meshNode.PoseLocal = new Pose(new Vector3F(0, 0, 0), Matrix33F.CreateRotationY(ConstantsF.Pi)); SampleHelper.EnablePerPixelLighting(meshNode); GraphicsScreen.Scene.Children.Add(meshNode); // The imported animations are stored in the mesh. Dictionary<string, SkeletonKeyFrameAnimation> animations = meshNode.Mesh.Animations; // The Dude model contains only one animation, which is a SkeletonKeyFrameAnimation with // a walk cycle. SkeletonKeyFrameAnimation walkAnimation = animations.Values.First(); // Wrap the walk animation in an animation clip that loops the animation forever. AnimationClip<SkeletonPose> loopingAnimation = new AnimationClip<SkeletonPose>(walkAnimation) { LoopBehavior = LoopBehavior.Cycle, Duration = TimeSpan.MaxValue, }; // Start the animation and keep the created AnimationController. // We must cast the SkeletonPose to IAnimatableProperty because SkeletonPose implements // IAnimatableObject and IAnimatableProperty. We must tell the AnimationService if we want // to animate an animatable property of the SkeletonPose (IAnimatableObject), or if we want to // animate the whole SkeletonPose (IAnimatableProperty). _animationController = AnimationService.StartAnimation(loopingAnimation, (IAnimatableProperty)meshNode.SkeletonPose); // The animation will be applied the next time AnimationManager.ApplyAnimations() is called // in the main loop. ApplyAnimations() is called before this method is called, therefore // the model will be rendered in the bind pose in this frame and in the first animation key // frame in the next frame - this creates an annoying visual popping effect. // We can avoid this if we call AnimationController.UpdateAndApply(). This will immediately // change the model pose to the first key frame pose. _animationController.UpdateAndApply(); // (Optional) Enable Auto-Recycling: // After the animation is stopped, the animation service will recycle all // intermediate data structures. _animationController.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 (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)_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)_skeletonPose, AnimationTransitions.Replace(_runAnimationController.AnimationInstance, TimeSpan.FromSeconds(0.3))); _idleAnimationController.AutoRecycle(); } } base.Update(gameTime); }
protected override void LoadContent() { _model = Game.Content.Load<Model>("PlayerMarine"); var additionalData = (Dictionary<string, object>)_model.Tag; var skeleton = (Skeleton)additionalData["Skeleton"]; _skeletonPose = SkeletonPose.Create(skeleton); var animations = (Dictionary<string, SkeletonKeyFrameAnimation>)additionalData["Animations"]; _runAnimation = new AnimationClip<SkeletonPose>(animations["Run"]) { LoopBehavior = LoopBehavior.Cycle, Duration = TimeSpan.MaxValue, }; _idleAnimation = new AnimationClip<SkeletonPose>(animations["Idle"]) { LoopBehavior = LoopBehavior.Cycle, Duration = TimeSpan.MaxValue, }; // Create a 'Shoot' animation that only affects the upper body. var shootAnimation = animations["Shoot"]; // The SkeletonKeyFrameAnimations allows to set a weight for each bone channel. // For the 'Shoot' animation, we set the weight to 0 for all bones that are // not descendants of the second spine bone (bone index 2). That means, the // animation affects only the upper body bones and is disabled on the lower // body bones. for (int i = 0; i < skeleton.NumberOfBones; i++) { if (!SkeletonHelper.IsAncestorOrSelf(_skeletonPose, 2, i)) shootAnimation.SetWeight(i, 0); } var loopedShootingAnimation = new AnimationClip<SkeletonPose>(shootAnimation) { LoopBehavior = LoopBehavior.Cycle, Duration = TimeSpan.MaxValue, }; // Start 'Idle' animation. _idleAnimationController = AnimationService.StartAnimation(_idleAnimation, (IAnimatableProperty)_skeletonPose); _idleAnimationController.AutoRecycle(); // Start looping the 'Shoot' animation. We use a Compose transition. This will add the // 'Shoot' animation to the animation composition chain and keeping all other playing // animations. // The 'Idle' animation animates the whole skeleton. The 'Shoot' animation replaces // the 'Idle' animation on the bones of the upper body. AnimationService.StartAnimation( loopedShootingAnimation, (IAnimatableProperty)_skeletonPose, AnimationTransitions.Compose()) .AutoRecycle(); base.LoadContent(); }
protected override void LoadContent() { _model = Game.Content.Load<Model>("Soldier"); var additionalData = (Dictionary<string, object>)_model.Tag; var skeleton = (Skeleton)additionalData["Skeleton"]; _skeletonPose = SkeletonPose.Create(skeleton); // Get the animations from the additional data. var animations = (Dictionary<string, SkeletonKeyFrameAnimation>)additionalData["Animations"]; // The Dude model contains only one animation, which is a SkeletonKeyFrameAnimation with // a walk cycle. SkeletonKeyFrameAnimation walkAnimation = animations.Values.First(); // Wrap the walk animation in an animation clip that loops the animation forever. AnimationClip<SkeletonPose> loopingAnimation = new AnimationClip<SkeletonPose>(walkAnimation) { LoopBehavior = LoopBehavior.Cycle, Duration = TimeSpan.MaxValue, }; // Start the animation and keep the created AnimationController. // We must cast the SkeletonPose to IAnimatableProperty because SkeletonPose implements // IAnimatableObject and IAnimatableProperty. We must tell the AnimationService if we want // to animate an animatable property of the SkeletonPose (IAnimatableObject), or if we want to // animate the whole SkeletonPose (IAnimatableProperty). _animationController = AnimationService.StartAnimation(loopingAnimation, (IAnimatableProperty)_skeletonPose); // The animation will be applied the next time AnimationManager.ApplyAnimations() is called // in the main loop. ApplyAnimations() is called before this method is called, therefore // the model will be rendered in the bind pose in this frame and in the first animation key // frame in the next frame - this creates an annoying visual popping effect. // We can avoid this if we call AnimationController.UpdateAndApply(). This will immediately // change the model pose to the first key frame pose. _animationController.UpdateAndApply(); // (Optional) Enable Auto-Recycling: // After the animation is stopped, the animation service will recycle all // intermediate data structures. _animationController.AutoRecycle(); base.LoadContent(); }
protected override void LoadContent() { _model = Game.Content.Load<Model>("PlayerMarine"); var additionalData = (Dictionary<string, object>)_model.Tag; var skeleton = (Skeleton)additionalData["Skeleton"]; _skeletonPose = SkeletonPose.Create(skeleton); var animations = (Dictionary<string, SkeletonKeyFrameAnimation>)additionalData["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)_skeletonPose, AnimationTransitions.Replace(TimeSpan.FromSeconds(0.5))); _idleAnimationController.AutoRecycle(); base.LoadContent(); }
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(); } } }
protected override void LoadContent() { // Load model and start a looping animation. _model = Game.Content.Load<Model>("Dude"); var additionalData = (Dictionary<string, object>)_model.Tag; var skeleton = (Skeleton)additionalData["Skeleton"]; _skeletonPose = SkeletonPose.Create(skeleton); var animations = (Dictionary<string, SkeletonKeyFrameAnimation>)additionalData["Animations"]; var walkAnimation = new AnimationClip<SkeletonPose>(animations.Values.First()) { LoopBehavior = LoopBehavior.Cycle, Duration = TimeSpan.MaxValue, }; _walkAnimationController = AnimationService.StartAnimation(walkAnimation, (IAnimatableProperty)_skeletonPose); _walkAnimationController.AutoRecycle(); // Create a BoneJiggler instance for the head bone (bone index 7). _boneJiggler = new BoneJiggler(_skeletonPose, 7, new Vector3F(1.1f, 0, 0)) { Spring = 100, Damping = 3, }; base.LoadContent(); }
public void Attack(int ID) { var AnimateService = ServiceLocator.Current.GetInstance<IAnimationService>(); _animationController = AnimateService.StartAnimation(Map[ID]._animations[1], (IAnimatableProperty)Map[ID]._skeleton); _animationController.UpdateAndApply(); _animationController.AutoRecycle(); }
public void Walk(int ID) { // Map[0].animate(); var AnimateService = ServiceLocator.Current.GetInstance<IAnimationService>(); // AnimateService.StartAnimation(Map[ID]._animations[0], (IAnimatableProperty)Map[ID]._skeleton).AutoRecycle(); _animationController = AnimateService.StartAnimation(Map[ID]._animations[0], (IAnimatableProperty)Map[ID]._skeleton); _animationController.Speed += 5; //ServiceLocator.Current.GetInstance<IAnimationService>().StartAnimation(Map[ID].loopingAnimation, (IAnimatableProperty)Map[ID]._skeleton).AutoRecycle(); _animationController.UpdateAndApply(); _animationController.AutoRecycle(); }