Ejemplo n.º 1
0
        /// <summary>
        /// Gets a collection of animations stored in the model.
        /// </summary>
        /// <param name="model">The model that contains the animations.</param>
        /// <returns>The animations stored in the model.</returns>
        public static AnimationInfoCollection FromModel(Model model)
        {
            // Grab the tag that was set in the processor; this is a dictionary so that users can extend
            // the processor and pass their own data into the program without messing up the animation data
            Dictionary <string, object> modelTagData = (Dictionary <string, object>)model.Tag;

            if (modelTagData == null || !modelTagData.ContainsKey("Animations"))
            {
                return(new AnimationInfoCollection());
            }
            else
            {
                AnimationInfoCollection animations = (AnimationInfoCollection)modelTagData["Animations"];
                return(animations);
            }
        }
Ejemplo n.º 2
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;

            // The pose gives us the fully accumulated matrices for the entire model
            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.");
            }

            // Now we will build an array of arrays - one array for each mesh containing all the matrices animating in that mesh
            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;
            if (game != null)
            {
                game.Components.Add(this);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates a new instance of ModelAnimator.
        /// </summary>
        /// <param name="model">The model to be animated.</param>
        public ModelAnimator(Model model)
        {
            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++;
                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    numEffects++;
                }
            }

            //foreach(ModelMesh mesh in model.Meshes)
            //{
            //    System.Console.WriteLine("numeffects:"+ numEffects);
            //    System.Console.WriteLine("effects:" +mesh.Effects.Count);
            //    foreach(Effect effect in mesh.Effects) System.Console.WriteLine(effect.GetHashCode());
            //}

            // 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;
                }
            }

            // 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.");
                    }
                }
            }
        }