Exemplo n.º 1
0
        /// <summary>
        /// Creates a new instance of ModelAnimator.
        /// </summary>
        /// <param name="game">The game to which this component will belong.</param>
        /// <param name="model">The model to be animated.</param>
        public ModelAnimator(Game game, Model model)
            : base(game)
        {
            this.model = model;

            animations = AnimationInfoCollection.FromModel(model);
            bonePoses = BonePoseCollection.FromModelBoneCollection(
                model.Bones);
            numMeshes = model.Meshes.Count;
            // Find total number of effects used by the model
            numEffects = 0;
            foreach (ModelMesh mesh in model.Meshes)
                foreach (Effect effect in mesh.Effects)
                    numEffects++;


            // Initialize the arrays that store effect parameters
            modelEffects = new Effect[numEffects];
            worldParams = new EffectParameter[numEffects];
            matrixPaletteParams = new EffectParameter[numEffects];
            InitializeEffectParams();

            pose = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(pose);
            // Get all the skinning info for the model
            Dictionary<string, object> modelTagInfo = (Dictionary<string, object>)model.Tag;
            if (modelTagInfo == null)
                throw new Exception("Model Processor must subclass AnimatedModelProcessor.");
            skinInfo = (SkinInfoCollection[])modelTagInfo["SkinInfo"];
            if (skinInfo == null)
                throw new Exception("Model processor must pass skinning info through the tag.");

            palette = new Matrix[model.Meshes.Count][];
            for (int i = 0; i < skinInfo.Length; i++)
            {
                if (Util.IsSkinned(model.Meshes[i]))
                    palette[i] = new Matrix[skinInfo[i].Count];
                else
                    palette[i] = null;
            }
            // Update after AnimationController by default
            base.UpdateOrder = 1;
            game.Components.Add(this);

            // Test to see if model has too many bones
            for (int i = 0; i < model.Meshes.Count; i++)
            {
                if (palette[i] != null && matrixPaletteParams[i] != null)
                {
                    Matrix[] meshPalette = palette[i];
                    try
                    {
                        matrixPaletteParams[i].SetValue(meshPalette);
                    }
                    catch
                    {
                        throw new Exception("Model has too many skinned bones for the matrix palette.");
                    }
                }
            }
        }
Exemplo n.º 2
0
        // Add this as a new method
        private void UpdateState(GameTime gameTime)
        {
            // Add this to the UpdateState method
            KeyboardState      keyState = Keyboard.GetState();
            BonePoseCollection poses    = bsdAnimator.BonePoses;

            if (bsdState == CharacterState.Idle)
            {
                currentSpeed = 0;
                // Add this to the beginning of the if (state=="idle") block in the UpdateState method
                // Remove the old if (keyState.IsKeyDown(Keys.W)) block
                if (keyState.IsKeyDown(Keys.W))
                {
                    blendFactor = 0;
                    bsdState    = CharacterState.IdleToWalk;
                }
                // Add this in the if (state=="idle") block of the UpdateState method
                //if (keyState.IsKeyDown(Keys.Space))
                //{
                //    crouch.ElapsedTime = 0;
                //    crouch.IsLooping = false;
                //    //crouch.AnimationEnded += new AnimationEventHandler(crouch_AnimationEnded);
                //    state = "crouchDown";
                //}
                //RunController(bsdAnimator, idle);
            }
            else if (bsdState == CharacterState.Walk)
            {
                currentSpeed = WALK_SPEED;
                // Add this to the beginning of the if (state=="walk") block in the UpdateState method
                // Remove the old if (keyState.IsKeyUp(Keys.W)) block
                if (keyState.IsKeyUp(Keys.W))
                {
                    blendFactor = 0;
                    bsdState    = CharacterState.WalkToIdle;
                }
                if (keyState.IsKeyDown(Keys.LeftShift) && keyState.IsKeyDown(Keys.W))
                {
                    blendFactor = 0;
                    bsdState    = CharacterState.Walk;
                    bsdAnimationControllers[(int)bsdState].SpeedFactor = 0;
                }
                //RunController(dwarfAnimator, walk);
            }
            // Add this to the UpdateState method
            else if (bsdState == CharacterState.IdleToWalk)
            {
                bsdState = CharacterState.Walk;

                //blendFactor += .1f;
                //currentSpeed = blendFactor * WALK_SPEED;
                //if (blendFactor >= 1)
                //{
                //    blendFactor = 1;
                //    bsdState = CharacterState.Walk;
                //}
                //foreach (BonePose p in poses)
                //{
                //    p.CurrentController = bsdAnimationControllers[(int)CharacterState.Idle];
                //    p.CurrentBlendController = bsdAnimationControllers[(int)CharacterState.Walk];
                //    p.BlendFactor = blendFactor;
                //}
            }
            // Add this to the UpdateState method
            else if (bsdState == CharacterState.WalkToIdle)
            {
                bsdState = CharacterState.Idle;

                //blendFactor += .1f;
                //currentSpeed = (1f - blendFactor) * WALK_SPEED;
                //if (blendFactor >= 1)
                //{
                //    blendFactor = 1;
                //    bsdState = CharacterState.Idle;
                //}
                //foreach (BonePose p in poses)
                //{
                //    p.CurrentController = bsdAnimationControllers[(int)CharacterState.Walk];
                //    p.CurrentBlendController = bsdAnimationControllers[(int)CharacterState.Idle];
                //    p.BlendFactor = blendFactor;
                //}
            }
            //// Add this in the UpdateState method
            //else if (state == "crouchDown")
            //{
            //    RunController(dwarfAnimator, crouch);
            //}
            //else if (state == "stayCrouched")
            //{
            //    // Add this to the if (state == "stayCrouched") block in the UpdateState method
            //    if (keyState.IsKeyDown(Keys.Space))
            //    {
            //        crouch.ElapsedTime = crouch.AnimationSource.Duration;
            //        crouch.SpeedFactor = 0;
            //        state = "standUp";
            //    }
            //    //RunController(dwarfAnimator, stayCrouched);
            //}
            // Add this to the UpdateState method
            //else if (state == "standUp")
            //{
            //    if (crouch.ElapsedTime - gameTime.ElapsedGameTime.Ticks <= 0)
            //    {
            //        crouch.SpeedFactor = 1;
            //        crouch.ElapsedTime = 0;
            //        idle.ElapsedTime = 0;
            //        state = "idle";
            //    }
            //    else
            //        crouch.ElapsedTime -= gameTime.ElapsedGameTime.Ticks;
            //    //RunController(dwarfAnimator, crouch);
            //}
            // Add this to the UpdateState method
            //else if (state == "walkToRun")
            //{
            //    blendFactor += .05f;
            //    if (blendFactor >= 1)
            //    {
            //        blendFactor = 1;
            //        run.SpeedFactor = 1;
            //        state = "run";
            //    }
            //    double factor = (double)walk.ElapsedTime / walk.AnimationSource.Duration;
            //    run.ElapsedTime = (long)(factor * run.AnimationSource.Duration);
            //    currentSpeed = WALK_SPEED + blendFactor * (RUN_SPEED - WALK_SPEED);
            //    foreach (BonePose p in poses)
            //    {
            //        p.CurrentController = walk;
            //        p.CurrentBlendController = run;
            //        p.BlendFactor = blendFactor;
            //    }
            //}
            //else if (state == "run")
            //{
            //    currentSpeed = RUN_SPEED;
            //    if (keyState.IsKeyUp(Keys.LeftShift))
            //    {
            //        blendFactor = 0;
            //        state = "runToWalk";
            //        walk.SpeedFactor = 0;
            //    }
            //    foreach (BonePose p in poses)
            //    {
            //        p.CurrentController = run;
            //        p.CurrentBlendController = null;
            //    }
            //}
            //else if (state == "runToWalk")
            //{
            //    blendFactor += .05f;
            //    if (blendFactor >= 1)
            //    {
            //        blendFactor = 1;

            //        //bsdA .SpeedFactor = 1;
            //        state = "walk";
            //    }
            //    double factor = (double)run.ElapsedTime / run.AnimationSource.Duration;
            //    walk.ElapsedTime = (long)(factor * walk.AnimationSource.Duration);
            //    currentSpeed = WALK_SPEED + (1f - blendFactor) * (RUN_SPEED - WALK_SPEED);
            //    foreach (BonePose p in poses)
            //    {
            //        p.CurrentController = run;
            //        p.CurrentBlendController = walk;
            //        p.BlendFactor = blendFactor;
            //    }
            //}
        }