2011-11-26 78 views
4

我有一個模型在攪拌機(2.6+)與裝配動畫。我將它導出到FBX並將其導入到XNA。我知道要在屏幕上繪製它,但我如何運行動畫(例如稱爲「運行」)?如何在XNA 4.0中使用攪拌機動畫?

謝謝!

+2

見http://create.msdn.com/en-US/education/catalo g/sample/custom_model_rigid_and_skinned和http://blog.diabolicalgame.co.uk/2011/07/exporting-animated-models-from-blender.html –

回答

7

您可以使用微軟的SkinnedModelSample。請確保您設置的FBX文件SkinnedModelProcessor在屬性框的ContentProcessor屬性,然後你就可以做(需要優化):

主要遊戲類:

AnimationPlayer player;// This calculates the Matrices of the animation 
AnimationClip clip;// This contains the keyframes of the animation 
SkinningData skin;// This contains all the skinning data 
Model model;// The actual model 

LoadContent方法:

model = Content.Load<Model>("path_to_model"); 
skin = model.Tag as SkinningData;// The SkinnedModelProcessor puts skinning data in the Tag property 

player = new AnimationPlayer(skin); 
clip = skin.AnimationClips["run"];// The name of the animation 

player.StartClip(clip); 

繪製方法:

Matrix[] bones = player.GetSkinTransforms(); 

// Compute camera matrices. 
Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, -30), // Change the last number according to the size of your model 
              new Vector3(0, 0, 0), Vector3.Up); 

Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 
                 device.Viewport.AspectRatio, 
                 1, 
                 10000); 

// Render the skinned mesh. 
foreach (ModelMesh mesh in model.Meshes) 
{ 
    foreach (SkinnedEffect effect in mesh.Effects) 
    { 
     effect.SetBoneTransforms(bones); 

     effect.View = view; 
     effect.Projection = projection; 

     effect.EnableDefaultLighting(); 

     effect.SpecularColor = new Vector3(0.25f); 
     effect.SpecularPower = 16; 
    } 

    mesh.Draw(); 
}