2
目前,我很努力地在XNA中理解和做3D動畫。有沒有一種更簡單的方法來在XNA中動畫3D模型?
我的模式最初的樣子:
我不與雙臂像這樣一邊想它。使用XNA,我試圖把雙臂放在一邊,它弄成:
我正在使用的代碼是微軟的XNA動畫樣品:
public class SkinningSampleGame : Microsoft.Xna.Framework.Game
{
/* Other stuff here */
private Matrix rotate(float X, float Y, float Z)
{
return Matrix.CreateRotationX(MathHelper.ToRadians(X)) * Matrix.CreateRotationY(MathHelper.ToRadians(Y)) * Matrix.CreateRotationZ(MathHelper.ToRadians(Z));
}
protected override void Update(GameTime gameTime)
{
float time = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
//transform the model's left arm.
int boneId = currentModel.Bones["swat:LeftShoulder"].Index - 3;
animationPlayer.boneTransforms[boneId] = rotate(-2.593f, 1.84f, -14.782f) * Matrix.CreateTranslation(new Vector3(2, -3, 5)) * _originalBonesMatrix[boneId];
boneId = currentModel.Bones["swat:LeftShoulder"].Index - 2;
animationPlayer.boneTransforms[boneId] = rotate(0, -30, -56f) * Matrix.CreateTranslation(new Vector3(-25, 2, -1)) * _originalBonesMatrix[boneId];
boneId = currentModel.Bones["swat:LeftShoulder"].Index - 1;
animationPlayer.boneTransforms[boneId] = rotate(0, -2, -5f) * Matrix.CreateTranslation(new Vector3(0, 0, 0)) * _originalBonesMatrix[boneId];
//Transform model's right arm (the messed up arm)..
boneId = currentModel.Bones["swat:RightShoulder"].Index - 8;
animationPlayer.boneTransforms[boneId] = rotate(-2.593f, 1.84f, 14.782f) * Matrix.CreateTranslation(new Vector3(5, 3, 5)) * _originalBonesMatrix[boneId];
boneId = currentModel.Bones["swat:RightShoulder"].Index - 7;
animationPlayer.boneTransforms[boneId] = rotate(0, 7, 56f) * Matrix.CreateTranslation(new Vector3(-15, -2, 1)) * _originalBonesMatrix[boneId];
boneId = currentModel.Bones["swat:RightShoulder"].Index - 1;
animationPlayer.boneTransforms[boneId] = rotate(0, 2, 5f) * Matrix.CreateTranslation(new Vector3(0, 0, 0)) * _originalBonesMatrix[boneId];
animationPlayer.UpdateWorldTransforms(Matrix.Identity);
animationPlayer.UpdateSkinTransforms();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice device = graphics.GraphicsDevice;
device.Clear(Color.CornflowerBlue);
Matrix[] bones = animationPlayer.GetSkinTransforms();
// Compute camera matrices.
Matrix view = Matrix.CreateScale(0.005f) * Matrix.CreateTranslation(0, 0, 0) * Matrix.CreateRotationY(MathHelper.ToRadians(0)) *
Matrix.CreateLookAt(new Vector3(0, 1, 1), Vector3.Forward, Vector3.Up);
Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 0.1f, 1000f);
// Render the skinned mesh.
foreach (ModelMesh mesh in currentModel.Meshes)
{
foreach (SkinnedEffect effect in mesh.Effects)
{
effect.SetBoneTransforms(bones);
effect.View = view;
effect.Projection = projection;
effect.EnableDefaultLighting();
effect.SpecularColor = new Vector3(0.35f);
effect.SpecularPower = 16;
}
mesh.Draw();
}
base.Draw(gameTime);
}
}
在骨頭型號有:
,我希望它是光滑這樣的:
我該如何使用XNA來做到這一點?有沒有更簡單的方法來動畫身體部位?我花了最後3個小時做了左手臂,並使用試錯法計算了每個部分需要轉換多少。它看起來不錯,但並不是很好。