gpt4 book ai didi

c# - 无法让模型骨骼在 XNA 中独立设置动画

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:51 24 4
gpt4 key购买 nike

我正在尝试以编程方式在 XNA 中为我的角色模型制作动画。我首先查看此处的代码示例: http://create.msdn.com/en-US/education/catalog/sample/skinned_model通过编辑本例中的代码,我能够让角色随心所欲地移动。然后我尝试用基本代码制作一个单独的程序来做到这一点。 (我从示例中借用了“花花公子”模型)。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame1
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDevice device;
GraphicsDeviceManager graphics;
Model myModel;
float aspectRatio;

public Matrix[] boneTransforms;
public Matrix[] originalBoneTransforms;
Matrix[] worldTransforms;

float pos = 0;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
base.Initialize();
}

protected override void LoadContent()
{
device = graphics.GraphicsDevice;
myModel = Content.Load<Model>("dude");

worldTransforms = new Matrix[myModel.Bones.Count];
boneTransforms = new Matrix[myModel.Bones.Count];
originalBoneTransforms = new Matrix[myModel.Bones.Count];
myModel.CopyAbsoluteBoneTransformsTo(boneTransforms);
myModel.CopyAbsoluteBoneTransformsTo(originalBoneTransforms);


aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
}

protected override void Update(GameTime gameTime)
{
UpdateBoneTransforms();
UpdateWorldTransforms();

base.Update(gameTime);
}

public void UpdateBoneTransforms()
{
pos += .1f;
int boneId = 32; //Right Arm
boneTransforms[boneId] = Matrix.CreateTranslation(new Vector3(0, pos, 0)) * originalBoneTransforms[boneId];
}

public void UpdateWorldTransforms()
{
// Root bone.
worldTransforms[0] = boneTransforms[0];

// Child bones.
for (int bone = 1; bone < worldTransforms.Length; bone++)
{
int parentBone = myModel.Bones[bone].Parent.Index;

worldTransforms[bone] = boneTransforms[bone] *
worldTransforms[parentBone];
}
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

Matrix view = Matrix.CreateLookAt(new Vector3(0, 150, 125),
Vector3.Zero, Vector3.Up);
Matrix projection = Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f), aspectRatio,
1.0f, 10000.0f);

foreach (ModelMesh mesh in myModel.Meshes)
{
foreach (BasicEffect meshEffect in mesh.Effects)
{
meshEffect.EnableDefaultLighting();
meshEffect.World = worldTransforms[mesh.ParentBone.Index];
meshEffect.View = view;
meshEffect.Projection = projection;

meshEffect.SpecularColor = new Vector3(0.25f);
meshEffect.SpecularPower = 16;
}
mesh.Draw();
}

base.Draw(gameTime);
}
}
}

我不明白为什么这不起作用。如果我将 boneId(在 UpdateBoneTransforms 中)设置为 0,整个模型会按预期四处移动,但如果我将 boneId 更改为其他任何值,他只会站在原来的位置。即使我没有更新 child ,我也希望单个关节可以移动,但我什至无法做到这一点。我是不是忘记了什么重要的事情?

最佳答案

您的代码似乎是正确的,但我敢打赌您不会通过 SkinnedModelProcessor 加载模型。如果不是,则您的模型是使用 BasicEffect 着色器绘制的,该着色器不支持在您的模型上保留蒙皮信息。

所以:

1) 确认 dude.fbx 使用 SkinnedModelProcessor 作为内容处理器。为此,您必须在内容项目中包含对 SkinnedModelPipeline 的引用。使用此处理器将使您的模型使用正确的着色器 (SkinnedModel.fx)。

2) 您必须使用 AnimationPlayer 来获取模型姿势(而不是使用 CopyAbsoluteBoneTransformsTo)。如果不需要动画,请不要 Update() AnimationPlayer。

3) 对生成的姿势(矩阵[])做任何你想要的变换。

4) 使用姿势(骨骼)以及 View 和投影矩阵配置 SkinnedModelEffect:

effect.Parameters["Bones"].SetValue(bones);
effect.Parameters["View"].SetValue(view);
effect.Parameters["Projection"].SetValue(projection);

5) 绘制。

所有这些东西都在蒙皮模型示例代码中,您需要它来使用蒙皮数据。

关于c# - 无法让模型骨骼在 XNA 中独立设置动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6825568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com