C# 模型在XNA中显示为黑色

C# 模型在XNA中显示为黑色,c#,xna,monogame,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 Ga

我一直在尝试用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();
        }
    }
}
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用制度;
命名空间游戏1
{
公共类游戏1:游戏
{
图形管理器图形;
SpriteBatch SpriteBatch;
基本效应;
纹理2D地板;
模型;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
受保护的覆盖无效初始化()
{
效果=新的基本效果(graphics.GraphicsDevice);
base.Initialize();
}
受保护的覆盖void LoadContent()
{
spriteBatch=新spriteBatch(图形设备);
楼层=内容物荷载(“楼层”);
模型=Content.Load(“多维数据集”);
}
受保护的覆盖无效UnloadContent()
{
}
受保护覆盖无效更新(游戏时间游戏时间)
{
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed | | | Keyboard.GetState().IsKeyDown(Keys.Escape))
退出();
更新(游戏时间);
}
受保护覆盖无效绘制(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
var cameraPosition=new Vector3((float)Math.Cos((double)gameTime.TotalGameTime.totalmicrosides/1000)*20,40,(float)Math.Cos((double)gameTime.TotalGameTime.totalmicrosides/1000)*20);
var cameraLookAtVector=Vector3.0;
var cameraUpVector=Vector3.UnitZ;
effect.View=Matrix.CreateLookAt(
cameraPosition、cameraLookAtVector、cameraUpVector);
浮体=
graphics.PreferredBackBufferWidth/(float)graphics.PreferredBackBufferHeight;
float fieldOfView=Microsoft.Xna.Framework.MathHelper.PiOver4;
浮动近Clipplane=1;
浮动平面=200;
effect.Projection=Matrix.CreatePerspectiveFieldOfView(
视野、纵横比、近克利普兰、远克利普兰);
effect.TextureEnabled=真;
效果:纹理=地板;
drawModel(模型、效果、世界、效果、视图、效果、投影);
foreach(有效的var传递.currentTechnology.passs)
{
pass.Apply();
drawQuad(新矢量3[]{
新矢量3(20,-20,0),
新矢量3(-20,-20,0),
新矢量3(-20,20,0),
新矢量3(20,20,0),
},2f);
}
基础。抽签(游戏时间);
}
公共虚空绘图四边形(矢量3[]p,浮动瓷砖)
{
VertexPositionNormalTexture[]verts=新的VertexPositionNormalTexture[6];
顶点[0]。位置=p[0];
顶点[1]。位置=p[1];
顶点[2]。位置=p[3];
顶点[3]。位置=p[1];
顶点[4]。位置=p[2];
顶点[5]。位置=p[3];
顶点[0]。法线=p[0];
顶点[1]。法线=p[1];
顶点[2]。法线=p[3];
顶点[3]。法线=p[1];
顶点[4]。法线=p[2];
顶点[5]。正常值=p[3];
顶点[0]。纹理坐标=新向量2(0,0);
顶点[1]。纹理坐标=新矢量2(0,平铺);
顶点[2]。纹理坐标=新矢量2(平铺,0);
顶点[3]。纹理坐标=顶点[1]。纹理坐标;
顶点[4]。纹理坐标=新矢量2(平铺,平铺);
顶点[5]。纹理坐标=顶点[2]。纹理坐标;
graphics.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangelList,verts,0,2);
}
公共模型(模型模型、矩阵世界、矩阵视图、矩阵投影)
{
foreach(model.mesh中的var网格)
{
foreach(网格中的基本效果。效果)
{
effect.EnableDefaultLighting();
effect.DiffuseColor=Color.White.ToVector3();
效果:世界=世界;
效果。视图=视图;
效果。投影=投影;
}
mesh.Draw();
}
}
}

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


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

好的,我以前根本没有使用纹理文件。这是必须的吗?无论如何我创建了同一个立方体并对其应用了纹理。纹理绝对位于正确的路径中。当我在管道中构建内容时,如果其路径不正确,它将返回一个错误。因此,内容构建是成功的,但它仍然完全是黑色的。这真的很奇怪。此外,monogame似乎不支持acii fbx文件,它们需要是二进制的。@ceoepts是的,XNA支持很少的“材质”。您需要包括纹理文件。纹理是否也编译成XNB?你把它包括进去了吗?是的,我把所有的东西都包括在monogame的管道里了。我可以看到bin文件夹中的所有xnb。我真的不知道还有什么需要测试:/@ceoepts您也可以尝试不手动设置
效果
对象的纹理,但很难说是什么问题:(啊,我刚想出来。Blender似乎没有导出工作的xna文件。它说它在加载项注册表上有。我必须从autodesk下载一个fbx转换器并进行转换。我还注意到我可以使用dae文件。你知道使用它们有什么缺点吗?总之,在我转换fbx文件后,它工作得很好。谢谢F