0

下面的代码是一个完整的 XNA 3.1 程序,几乎没有改变 Visual Studio 在创建新项目时创建的代码骨架。

我唯一改变的是

  • 将 .x 模型导入 VS 解决方案的内容文件夹。

    (模型是一个简单的正方形,上面有一个纹理 - 使用 Google Sketchup 制作并使用多个 .x 导出器导出)

  • 在 Load() 方法中,我将 .x 模型加载到游戏中。

  • Draw() 方法使用 BasicEffect 来渲染模型。

除了这三件事我没有添加任何代码。

为什么模型不显示纹理?我该怎么做才能使纹理可见?

这是纹理文件(调色板中的标准 SketchUp 纹理):

在此处输入图像描述

这就是我的程序的样子——你可以看到:没有纹理!

在此处输入图像描述

这是程序的完整源代码:

namespace WindowsGame1 {
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game {
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

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

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize() {
        // TODO: Add your initialization logic here
        base.Initialize();
    }

    Model newModel;

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent() {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: usse this.Content to load your game content here

        newModel = Content.Load<Model>(@"aau3d");

        foreach (ModelMesh mesh in newModel.Meshes) {

            foreach (ModelMeshPart meshPart in mesh.MeshParts) {

                meshPart.Effect = new BasicEffect(this.GraphicsDevice, null);
            }
        }

    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent() {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime) {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime) {

        if (newModel != null) {

            GraphicsDevice.Clear(Color.CornflowerBlue);

            Matrix[] transforms = new Matrix[newModel.Bones.Count];
            newModel.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in newModel.Meshes) {
                foreach (BasicEffect effect in mesh.Effects) {
                    effect.EnableDefaultLighting();
                    effect.TextureEnabled = true;

                    effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(0)
                * Matrix.CreateTranslation(new Vector3(0, 0, 0));
                    effect.View = Matrix.CreateLookAt(new Vector3(200, 1000, 200), Vector3.Zero, Vector3.Up);
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
                        0.75f, 1.0f, 10000.0f);
                }
                mesh.Draw();
            }
        }

        base.Draw(gameTime);
    }
}

}

4

2 回答 2

4

我认为你试图做的太多了。这将取决于“.x”文件中的内容,但这些可以包含为模型着色和纹理所需的所有着色器。

我认为问题在于加载模型后,您使用 BasicEffect 覆盖模型的效果,它不知道您要用于模型的纹理。

我认为最简单的做法是注释掉与 LoadContent 中的 BasicEffect 相关的代码。“EnableDefaultLighting()”和/或“TextureEnabled”的设置也可能是不必要的。

于 2012-10-12T02:00:29.840 回答
1

过去我在使用 .x 模型时遇到了一些问题。尝试使用 .fbx 导出文件。从您的 3D 应用程序导出后,将模型资产及其纹理放在项目中的同一地图中。不要重命名它,因为纹理的名称在 .FBX 中可能不可读。

于 2012-10-12T01:33:58.667 回答