3D cubes slowly rendering

2019-08-23 03:53发布

问题:

My previous problem was fixed after I rewrote my block.cs class, however, I have one final problem:

I have a list of 256 blocks each with 12 triangles and 36 vertices. When I am rendering these blocks using:

blocks.ForEach(block => block.Draw(spriteBatch, camera, effect));

They do render, but the game speed drops intensely, to about 0.25 FPS. Each block has a different texture. Should 3072 triangles and 9216 vertices cause this much of a problem?

My render code:

    public void Render(GraphicsDevice graphicsDevice)
    {
        if (!isConstructed) ConstructBlock();

        using (var buffer = new VertexBuffer(graphicsDevice, VertexPositionNormalTexture.VertexDeclaration, NUMVERTICES, BufferUsage.WriteOnly))
        {
            buffer.SetData(vertices);
            graphicsDevice.SetVertexBuffer(buffer);
        }

        graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, NUMTRIANGLES);
    }

My cube's Draw code:

    public void Draw(SpriteBatch spriteBatch, Camera camera, BasicEffect effect)
    {


        effect = new BasicEffect(spriteBatch.GraphicsDevice)
        {
            TextureEnabled = true,
            Texture = Texture,
            View = camera.View,
            Projection = camera.Projection,
            World = Matrix.Identity
        };


        effect.EnableDefaultLighting();
        foreach (var pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();
            Render(spriteBatch.GraphicsDevice);
        }
    }