Need suggestion for multiple lines

Avatar
  • updated
  • Answered

I try to create a line chart. I use a camera and a render texture. I show that chart on UI using raw image. When I disable chart generator script I get 120 fps. When I enable it I have 30 fps. I have i9-9900K and 2080 Ti. There are 200 disc and 200 lines on my chart. I tried enable and disable GPU Instancing.


How can I optimize performance?

Image 672

Image 673

Reporting a bug? please specify Unity version:
2021.3
Reporting a bug? please specify Shapes version:
4.1.1
Reporting a bug? please specify Render Pipeline:
URP
Pinned replies
Avatar
Freya Holmér creator
  • Answer
  • Answered

this is slow because you're alternating between lines and discs (ie, each iteration draws a line, and then a disc, which prevents GPU instancing). To save performance, you should draw all lines first, and then all discs.

another optimization would be to use a polyline instead of individual lines, and only recalculate the polyline when the lines change shape, instead of on every draw. There's more information on performance optimization in the shapes docs

Avatar
Freya Holmér creator

how are you generating/drawing the charts right now?

Avatar
atmuc

public override void DrawShapes(Camera cam)

{

using (Draw.Command(cam))

{

Draw.LineGeometry = LineGeometry.Flat2D;
Draw.ThicknessSpace = ThicknessSpace.Pixels;
Draw.Thickness = LineThicknessMajor;
Draw.Matrix = transform.localToWorldMatrix;
Draw.Color = Color.blue;

for (int i = 0; i < lineCountMajorY; i++)
{
var y = -halfHeight + i * StepMajorY * pixelPerUnitY;
var start = new Vector3(-halfWidth, y, 0);
var end = new Vector3(halfWidth, y, 0);
Draw.Line(start, end, Color.black);
}

}

Avatar
Freya Holmér creator

I'm a little but confused, these look like horizontal lines, not a line graph, and I don't sere the discs in this code

Avatar
atmuc

Draw.Polyline performance is better but the result is not good.

Image 675

Avatar
atmuc
Quote from Freya Holmér

I'm a little but confused, these look like horizontal lines, not a line graph, and I don't sere the discs in this code

I did not share whole code.

Draw.Thickness = LineThicknessData;

var start = Vector3.zero;
var end = Vector3.zero;

start.x = (Data1[0].x * pixelPerUnitX) - halfWidth;
start.y = (Data1[0].y * pixelPerUnitY) - halfHeight;

for (int i = 1; i < Data1.Count; i++)
{
end.x = (Data1[i].x * pixelPerUnitX) - halfWidth;
end.y = (Data1[i].y * pixelPerUnitY) - halfHeight;

Draw.Line(start, end, Color1);

if (DrawPoints)
{
Draw.Disc(start);
}

start = end;
}

Avatar
Freya Holmér creator
  • Answer
  • Answered

this is slow because you're alternating between lines and discs (ie, each iteration draws a line, and then a disc, which prevents GPU instancing). To save performance, you should draw all lines first, and then all discs.

another optimization would be to use a polyline instead of individual lines, and only recalculate the polyline when the lines change shape, instead of on every draw. There's more information on performance optimization in the shapes docs