绘制图形的方法比绘制单独的线条更快捷的方法是什么?

use*_*322 3 c# xna drawing graph

到目前为止,我正在绘制我的调试性能图,其中1px矩形被拉伸到必要的高度,但是以这种方式绘制大量数据会导致显着的性能损失.

目前的逻辑是:收集当前帧的所有时序,将它们放入Queue<float>s中,并通过绘制300个拉伸的1px精灵来绘制每个队列的图形.有4个图,因此仅在调试覆盖中有1200个精灵,这是资源消耗.

有没有更好的方法来绘制图形,至少不需要绘制这么多精灵?

nee*_*eKo 11

行列表

您可以使用VertexPositionColor数组存储单个图形值,然后GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>与定义的线列表(索引)一起使用以使用正交投影绘制它们.

在此输入图像描述
由于文件大小,.gif的大小调整为50%.

我正在以60fps绘制这些样本(4个图表,每个值为300个值点/像素).

在此输入图像描述


三角带

如果需要填充线下方的图形,则可以绘制三角形条(在图形底部有点).

在此输入图像描述


行列表代码

以下是上面呈现的第一张图的相关代码:

Matrix worldMatrix;
Matrix viewMatrix;
Matrix projectionMatrix;
BasicEffect basicEffect;

VertexPositionColor[] pointList;
short[] lineListIndices;

protected override void Initialize()
{
    int n = 300;
    //GeneratePoints generates a random graph, implementation irrelevant
    pointList = new VertexPositionColor[n];
    for (int i = 0; i < n; i++)
        pointList[i] = new VertexPositionColor() { Position = new Vector3(i, (float)(Math.Sin((i / 15.0)) * height / 2.0 + height / 2.0 + minY), 0), Color = Color.Blue };

    //links the points into a list
    lineListIndices = new short[(n * 2) - 2];
    for (int i = 0; i < n - 1; i++)
    {
        lineListIndices[i * 2] = (short)(i);
        lineListIndices[(i * 2) + 1] = (short)(i + 1);
    }

    worldMatrix = Matrix.Identity;
    viewMatrix = Matrix.CreateLookAt(new Vector3(0.0f, 0.0f, 1.0f), Vector3.Zero, Vector3.Up);
    projectionMatrix = Matrix.CreateOrthographicOffCenter(0, (float)GraphicsDevice.Viewport.Width, (float)GraphicsDevice.Viewport.Height, 0, 1.0f, 1000.0f);

    basicEffect = new BasicEffect(graphics.GraphicsDevice);
    basicEffect.World = worldMatrix;
    basicEffect.View = viewMatrix;
    basicEffect.Projection = projectionMatrix;

    basicEffect.VertexColorEnabled = true; //important for color

    base.Initialize();
}
Run Code Online (Sandbox Code Playgroud)

绘制它:

foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
    pass.Apply();
    GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(
        PrimitiveType.LineList,
        pointList,
        0,
        pointList.Length,
        lineListIndices,
        0,
        pointList.Length - 1
    );
}
Run Code Online (Sandbox Code Playgroud)

对于三角形条形图,修改代码以显示三角形条带,并且对于图形曲线中的每个点,将一个放在图形的底部.