c# - 模型在 XNA 中显示为黑色

标签 c# xna monogame

我一直试图在 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,因此您应该能够确定它在哪里寻找纹理,并将其放在那里。

关于c# - 模型在 XNA 中显示为黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32959280/

相关文章:

c# - 为什么更新列表中的实例会更新另一个列表中的相同实例?

c# - 如何知道流已经结束C#

c# - 声音未添加到字典容器

c# - 向上和向下缩放Sprite,以产生跳跃感

c# - 收集DAG的所有路径

c# - 我的第一个 COM 导入

C# XNA 矩阵 - 找出矩阵乘法后的当前位置

windows-phone-7 - 如何在 xna 中为 wp7 生成平滑的运动?

c# - Monogame 模棱两可的 Vector2

c# - MonoGame 只是 XNA 吗?