我正在尝试制作3D XNA游戏,但是当我尝试加载我的模型时,它无法呈现纹理和光线剂量确实起作用,看起来大多只是黑色,但模型形状是正确的。
在游戏中寻找:
应该看:
代码:
class Floor
{
private int floorWidth;
private int floorHeight;
private VertexBuffer floorBuffer;
private IndexBuffer indexBuffer;
private GraphicsDevice device;
public Floor(GraphicsDevice device, int width, int height)
{
this.device = device;
this.floorWidth = width;
this.floorHeight = height;
BuildFloorBuffer();
}
private void BuildFloorBuffer()
{
List<short> indexList = new List<short>();
List<VertexPositionNormalTexture> vertexList = new List<VertexPositionNormalTexture>();
foreach (ModelMesh mesh in Game1.myModel.Meshes)
{
foreach (ModelMeshPart part in mesh.MeshParts)
{
VertexPositionNormalTexture[] vertexArray = new VertexPositionNormalTexture[part.VertexBuffer.VertexCount];
part.VertexBuffer.GetData<VertexPositionNormalTexture>(vertexArray);
vertexList.AddRange(vertexArray.ToList());
short[] indexArray = new short[part.IndexBuffer.IndexCount];
part.IndexBuffer.GetData<short>(indexArray);
indexList.AddRange(indexArray);
}
}
floorBuffer = new VertexBuffer(device, VertexPositionNormalTexture.VertexDeclaration, vertexList.Count, BufferUsage.None);
floorBuffer.SetData<VertexPositionNormalTexture>(vertexList.ToArray());
indexBuffer = new IndexBuffer(device, IndexElementSize.SixteenBits, sizeof(short) * indexList.Count, BufferUsage.None);
indexBuffer.SetData<short>(indexList.ToArray());
}
public void Draw(Camera camera, BasicEffect effect)
{
effect.VertexColorEnabled = false;
effect.TextureEnabled = true;
effect.LightingEnabled = true;
effect.View = camera.View;
effect.Projection = camera.Projection;
effect.World = Matrix.Identity;
effect.EnableDefaultLighting();
foreach(EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
device.Indices = indexBuffer;
device.SetVertexBuffer(floorBuffer);
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, floorBuffer.VertexCount, 0, floorBuffer.VertexCount);
}
}
}