我试图将InstancedModelSample代码实现到我的游戏中,除了一个问题之外它完美地工作 - 我的icospheres上的边缘被绘制在错误的顶点之间而且它只是画一个乱码。
当我以前使用单独的Draw()调用绘制300个左右的模型时,它渲染得很好,但不是这个新代码。我对这些东西不熟悉 - 当这似乎是一个可用于任何东西的通用方法时,如何让方法填充正确的面孔?
我的代码只是从原始样本中略微调整,我可以看到的部分对我来说是不必要的(如骨转换)。
private void DrawSeriesOfModels(Model model, Matrix[] matrices, Matrix view, Matrix Projection)
{
// Set up vertex declaration.
VertexDeclaration vertexDeclaration = new VertexDeclaration
(
new VertexElement(0, VertexElementFormat.Vector4, VertexElementUsage.BlendWeight, 0),
new VertexElement(16, VertexElementFormat.Vector4, VertexElementUsage.BlendWeight, 1),
new VertexElement(32, VertexElementFormat.Vector4, VertexElementUsage.BlendWeight, 2),
new VertexElement(48, VertexElementFormat.Vector4, VertexElementUsage.BlendWeight, 3)
);
// Set up dynamic vertic buffer.
DynamicVertexBuffer vertexBuffer = new DynamicVertexBuffer(GraphicsDevice, vertexDeclaration, matrices.Length, BufferUsage.WriteOnly);
// Transfer matrices into buffer.
vertexBuffer.SetData(matrices, 0, matrices.Length, SetDataOptions.Discard);
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
GraphicsDevice.SetVertexBuffers(new VertexBufferBinding(meshPart.VertexBuffer, meshPart.VertexOffset, 0),
new VertexBufferBinding(vertexBuffer, 0, 1));
// Set up instance rendering effect.
Effect effect = meshPart.Effect;
effect.CurrentTechnique = effect.Techniques["HardwareInstancing"];
effect.Parameters["World"].SetValue(Matrix.Identity);
effect.Parameters["View"].SetValue(view);
effect.Parameters["Projection"].SetValue(Projection);
effect.Parameters["AmbientLight"].SetValue(1); // Make model solid white.
// Draw everything in a single call.
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0, meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount, matrices.Length);
}
}
}
amountOfDraws++;
}
在此先感谢任何可以帮助我的人:)
答案 0 :(得分:0)
好吧,我担心这是由于我的疏忽,但是由于其他人有类似问题的轻微机会,问题是我没有包括这条线
GraphicsDevice.Indices = meshPart.IndexBuffer;
在行
之后 GraphicsDevice.SetVertexBuffers(new VertexBufferBinding(meshPart.VertexBuffer, meshPart.VertexOffset, 0),
new VertexBufferBinding(vertexBuffer, 0, 1));
......我的坏。