简体   繁体   English

有没有更简便的方法在XNA中对3D模型进行动画处理?

[英]Is there an easier way to animate 3D models in XNA?

Currently, I'm struggling to understand and do 3D animation in XNA. 目前,我正在努力理解和使用XNA进行3D动画。

My model originally looks like: 我的模型最初看起来像:

I don't want it with the arms out to the side like this. 我不希望这样把手臂伸到一边。 Using XNA, I tried to put the arms at the side and it ended up like: 使用XNA,我试图将手臂放在一边,结果如下:

The code I'm using is Microsoft's XNA Animation Sample: 我使用的代码是Microsoft的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);
    }
}

The bones in the model are: 模型中的骨骼为: 在此处输入图片说明

and I want it to be smooth like this: 我希望它像这样平滑:

在此处输入图片说明

How can I do this using XNA? 如何使用XNA做到这一点? Is there an easier way to animate the body parts? 有没有更简单的方法来对身体部位进行动画处理? I spent the last 3 hours just doing the left arm and using trial-error to figure out how much to tranform each part.. it looks ok but it's not "great". 我花了最后3个小时来做​​左臂,并尝试错误地找出要转换每个部分的大小。

Animation of complicated 3D geometry is exclusively done using visual animation tools. 复杂的3D几何图形的动画专门使用视觉动画工具完成。 And even with the right tools it will take many hours of practice to get it right. 即使使用了正确的工具,也需要花费大量的时间来实践它。

There's a variety of free or commercial 3D modelling and animation software packages. 有各种各样的免费或商业3D建模和动画软件包。 To get started I would recommend taking a look at Blender . 首先,我建议您看一下Blender

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM