我一直在尝试在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();
}
}
}
}
答案 0 :(得分:1)
FBX没有在模型文件中嵌入纹理,只是将存储到纹理文件中。此路径通常相对于导出位置(但它取决于工具)。
尝试将纹理文件放在与导出位置相同的相对路径中,但放在可执行文件的输出目录中。否则,FBX文件是人类可读的IIRC,因此您应该能够确定它在哪里寻找纹理,并把它放在那里。