正确旋转皮肤模型的骨骼? XNA

时间:2012-01-16 05:48:19

标签: c# xna

如何围绕自身旋转蒙皮模型的骨骼而不是模型的原点?

SkinningSample中,当我旋转花花公子的前臂时,它围绕看似模型的原点旋转。如果可能的话,我想围绕自己的起源旋转骨骼。

GetSkinTransforms()的说明:

  

“获取相对于蒙皮的当前骨骼变换矩阵   绑定姿势。“

所以我怀疑这可能是问题所在。有谁知道如何将这些转换转换成他们需要的东西?

以下是SkinningSample的一部分。

  float rotation = 0;
    protected override void Update(GameTime gameTime)
    {
        HandleInput();

        UpdateCamera(gameTime);

        animationPlayer.UpdateWorldTransforms(Matrix.Identity);
        animationPlayer.UpdateSkinTransforms();

        Matrix RotationTransform = Matrix.CreateFromYawPitchRoll(rotation, 0, 0) ;
        animationPlayer.GetSkinTransforms().SetValue(RotationTransform, 34); 

        rotation = rotation + .1f;
        base.Update(gameTime);
    }


    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice device = graphics.GraphicsDevice;

        device.Clear(Color.CornflowerBlue);

        Matrix[] bones = animationPlayer.GetSkinTransforms();

        // Compute camera matrices.
        Matrix view = Matrix.CreateTranslation(0, -40, 0) * 
                      Matrix.CreateRotationY(MathHelper.ToRadians(cameraRotation)) *
                      Matrix.CreateRotationX(MathHelper.ToRadians(cameraArc)) *
                      Matrix.CreateLookAt(new Vector3(0, 0, -cameraDistance), 
                                          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 currentModel.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();
        }

        base.Draw(gameTime);
    }

1 个答案:

答案 0 :(得分:0)

您必须转换boneTransform而不是skinTransform。我刚刚在AnimationPlayer类中创建了另一个方法来允许这种操作:

public void TransformBone(Matrix BoneAlteration, int BoneID)
{
    boneTransform[BoneID] = BoneAlteration * bindPose[BoneID];
    UpdateWorldTransforms(Matrix.Identity);
    UpdateSkinTransforms();
}

因此,如果我想旋转骨骼,我会调用此方法来创建正确的skinTransformation矩阵。这假设其他boneTransforms设置在正确的位置。我还认为如果你想围绕已经旋转的骨骼旋转,你可以用boneTransform替换bindPose。

相关问题