绘制部分模型滞后,整个模型工作正常

时间:2017-04-12 02:42:43

标签: c# model xna indices vertices

在XNA4.0(C#,visual studio)中绘制我的整个模型时,使用下面的方法,一切正常。问题是:当我只想绘制单个网格时,使用:

ModelMesh mm = model.Meshes[1];

而不是

foreach (ModelMesh mm in model.Meshes)

发生大规模滞后(3 fps而不是无瑕60fps)

有人会知道这是怎么回事吗?

private void getModelVerticesIndices(Model model, ref VertexPositionColorNormal[] vertices, ref short[] indices, float scale)
    {
        ModelMeshPart part = model.Meshes[0].MeshParts[0];

        VertexPositionNormalTexture[] bufferVertices = new VertexPositionNormalTexture[part.VertexBuffer.VertexCount];
        part.VertexBuffer.GetData<VertexPositionNormalTexture>(bufferVertices);

        ushort[] drawOrder = new ushort[part.IndexBuffer.IndexCount];

        int totalIndicesCount = 0;
        foreach (ModelMesh mm in model.Meshes)
        {
            foreach (ModelMeshPart mmp in mm.MeshParts)
            {
                int indicesInThisMeshPart = mmp.PrimitiveCount * 3;
                ushort[] us = new ushort[indicesInThisMeshPart];
                part.IndexBuffer.GetData<ushort>(mmp.StartIndex * 2, us, 0, indicesInThisMeshPart);

                for (int u = 0; u < us.Length; u++)
                {
                    drawOrder[u + totalIndicesCount] = (ushort)(us[u] + mmp.VertexOffset);
                }

                totalIndicesCount += us.Length;
            }
        }

        modelVertices = new VertexPositionColorNormal[bufferVertices.Length];
        modelIndices = new short[totalIndicesCount];

        for (int i = 0; i < bufferVertices.Length; i++)
        {
            modelVertices[i] = new VertexPositionColorNormal(
                Vector3.Transform(bufferVertices[i].Position,
                    Matrix.CreateRotationX(-MathHelper.PiOver2) * Matrix.CreateTranslation(0f, 1.02f, 0f) * Matrix.CreateScale(scale)),
                Color.Gray, Vector3.Zero);
        }

        for (int i = 0; i < modelIndices.Length; i++)
        {
            modelIndices[i] = (short)drawOrder[i];
        }

        indices = modelIndices;
        vertices = (VertexPositionColorNormal[])modelVertices.Clone();
    }

0 个答案:

没有答案