在XNA 3D中创建模型更改形状

时间:2012-10-26 18:00:32

标签: c# xna-4.0

我想创建一个移动的对象(就像人在移动或弹性弹簧一样),移动时,对象的形状会发生变化。我不知道如何在XNA 4.0中创建3D模型形状更改。你能帮助我吗??谢谢!

1 个答案:

答案 0 :(得分:1)

我或许可以给你一些从初学者到初学者的建议。

我刚刚学会了如何从this example创建一个模型,并根据你的问题我将一个额外的比例变换应用到其中一个骨骼上,看看我是否可以像它的位置一样操纵它的大小,并且它确实有效。

所以我暗示你的问题的答案可能是,当模型的顶点数据保持不变时,你可以使用Scale变换使其改变形状。

这是我的模特:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace SimpleAnimation
{
    public class Body
    {
        Model bodyModel;
        ModelBone headBone;
        ModelBone bodyBone;
        Matrix headTransform;
        Matrix bodyTransform;
        Matrix[] boneTransforms;

        public Body()
        {
            HeadScale = 1;
        }

        public void Load(ContentManager content)
        {
            // Load the tank model from the ContentManager.
            bodyModel = content.Load<Model>("body");

            // Look up shortcut references to the bones we are going to animate.
            headBone = bodyModel.Bones["head"];
            bodyBone = bodyModel.Bones["body"];

            // Store the original transform matrix for each animating bone.
            headTransform = headBone.Transform;
            bodyTransform = bodyBone.Transform;

            // Allocate the transform matrix array.
            boneTransforms = new Matrix[bodyModel.Bones.Count];
        }

        public void Draw(Matrix world, Matrix view, Matrix projection)
        {
            // Set the world matrix as the root transform of the model.
            bodyModel.Root.Transform = world;

            // Calculate matrices based on the current animation position.
            Matrix headRotation = Matrix.CreateRotationX(HeadRotation);
            Matrix headScale = Matrix.CreateScale(HeadScale);
            Matrix bodyRotation = Matrix.CreateRotationX(BodyRotation);

            // Apply matrices to the relevant bones.
            headBone.Transform = headScale * headRotation * headTransform;
            bodyBone.Transform = bodyRotation * bodyTransform;

            // Look up combined bone matrices for the entire model.
            bodyModel.CopyAbsoluteBoneTransformsTo(boneTransforms);

            // Draw the model.
            foreach (ModelMesh mesh in bodyModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World = boneTransforms[mesh.ParentBone.Index];
                    effect.View = view;
                    effect.Projection = projection;

                    effect.EnableDefaultLighting();
                }

                mesh.Draw();
            }
        }

        public float HeadRotation { get; set; }

        public float HeadScale { get; set; }

        public float BodyRotation { get; set; }
    }
}