模型在 XNA 中显示为黑色

Models appear black in XNA

我一直试图在 xna 中绘制一个简单的立方体,但它完全显示为黑色。我尝试了多种不同的 FBX 模型。试玩管道中模型的设置。我还以各种可能的方式应用基本闪电。还是黑的。

我的代码:

 using Microsoft.Xna.Framework;
 using Microsoft.Xna.Framework.Graphics;
 using Microsoft.Xna.Framework.Input;
 using System;

namespace Game1
{

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    BasicEffect effect;
    Texture2D floor;
    Model model;

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

    protected override void Initialize()
    {
        effect = new BasicEffect(graphics.GraphicsDevice);

        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        floor = Content.Load<Texture2D>("floor");
        model = Content.Load<Model>("cube");
    }

    protected override void UnloadContent()
    {
    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();


        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        var cameraPosition = new Vector3((float)Math.Cos((double)gameTime.TotalGameTime.TotalMilliseconds/1000)*20, 40, (float)Math.Cos((double)gameTime.TotalGameTime.TotalMilliseconds / 1000) * 20);
        var cameraLookAtVector = Vector3.Zero;
        var cameraUpVector = Vector3.UnitZ;

        effect.View = Matrix.CreateLookAt(
            cameraPosition, cameraLookAtVector, cameraUpVector);

        float aspectRatio =
            graphics.PreferredBackBufferWidth / (float)graphics.PreferredBackBufferHeight;
        float fieldOfView = Microsoft.Xna.Framework.MathHelper.PiOver4;
        float nearClipPlane = 1;
        float farClipPlane = 200;

        effect.Projection = Matrix.CreatePerspectiveFieldOfView(
            fieldOfView, aspectRatio, nearClipPlane, farClipPlane);

        effect.TextureEnabled = true;
        effect.Texture = floor;

        drawModel(model, effect.World, effect.View, effect.Projection);

        foreach (var pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();

            drawQuad(new Vector3[] {
                new Vector3(20,-20,0),
                new Vector3(-20,-20,0),
                new Vector3(-20,20,0),
                new Vector3(20,20,0),
            },2f);
        }


        base.Draw(gameTime);
    }

    public void drawQuad(Vector3[] p, float tiling)
    {
        VertexPositionNormalTexture[] verts = new VertexPositionNormalTexture[6];

        verts[0].Position = p[0];
        verts[1].Position = p[1];
        verts[2].Position = p[3];

        verts[3].Position = p[1];
        verts[4].Position = p[2];
        verts[5].Position = p[3];

        verts[0].Normal = p[0];
        verts[1].Normal = p[1];
        verts[2].Normal = p[3];

        verts[3].Normal = p[1];
        verts[4].Normal = p[2];
        verts[5].Normal = p[3];

        verts[0].TextureCoordinate = new Vector2(0, 0);
        verts[1].TextureCoordinate = new Vector2(0, tiling);
        verts[2].TextureCoordinate = new Vector2(tiling, 0);

        verts[3].TextureCoordinate = verts[1].TextureCoordinate;
        verts[4].TextureCoordinate = new Vector2(tiling, tiling);
        verts[5].TextureCoordinate = verts[2].TextureCoordinate;

        graphics.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, verts, 0, 2);
    }

    public void drawModel(Model model, Matrix world, Matrix view, Matrix projection)
    {
        foreach (var mesh in model.Meshes)
        {
            foreach (BasicEffect effect in mesh.Effects)
            {
                    effect.EnableDefaultLighting();
                    effect.DiffuseColor = Color.White.ToVector3(); 
                    effect.World = world;
                    effect.View = view;
                    effect.Projection = projection;
            }

            mesh.Draw();
        }
    }
}

}

FBX 本身不在模型文件中嵌入纹理,它只是存储一个路径 纹理文件。此路径通常是相对于导出位置的(但取决于工具)。

尝试将纹理文件放在与导出位置相同的相对路径中,但放在可执行文件的输出目录中。否则,FBX 文件是人类可读的 IIRC,因此您应该能够确定它在哪里寻找纹理,并将其放在那里。