Custom 2d drawing is really slow for me

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By unclejack
:warning: Old Version Published before Godot 3 was released.

Hey,

I’m new to Godot. It looks great so far, except for one issue that I have (which is definitely due to the fact that I’m doing something wrong).
The thing is - my game will have a lot of custom drawn primitives, that I’ll need to redraw every frame.
I found this example:

which is exactly what I need. I got it to run, and everything was perfect, until I increased number of spinning polygons to 1000. I simply added FOR loop in the _draw function:

   for i in range(1000):
     draw_circle_arc_poly( center, radius, angle_from, angle_to, color )

and it dropped to 10-15 FPS.

How so ? What did I do wrong ?

Thank you for you help and insight.

:bust_in_silhouette: Reply From: Zylann

Every polygon you draw with draw_polygon has to be triangulated by the engine and then drawn on the fly every frame. It’s probably even slower if the polygon is concave.
On top of that, you appear to generate the points of that polygon from GDScript, and then draw it 1000 times. So it’s actually expected to be that slow, on both sides (engine and script).

You have to find another solution to draw that much, by finding what you really need. Solutions are multiple:

  • Draw in C++ with a more optimized code
  • Draw less polygons
  • Use sprites
  • Precompute things

It depends what you actually want.

A shader could be another option, but depends on what you want to do.

eons | 2017-02-27 14:20

Thanks for the answer.

Im not sure why there would be such a noticeable difference between GDScript and C++ (unless GDScript isn’t compiled, but interpreted)

I’ll try with just rectangles, see what difference would it make.

What i also find strange is that browser JS without OpenGL does the same exact task much better, than a native application with OpenGL

unclejack | 2017-02-27 23:06

Browser JS is JITted and has built-in specialized functions for drawing arcs, and is probably not limited to OpenGL ES 2.0 which makes many differences as well (will be upgraded to ES 3.0 in Godot 3.0).

GDScript is not compiled, unlike C++. It’s meant for easy development, while performance-critical parts can be ported to native code later on. If there was a function to draw arcs it would improve performance indeed. Also note that in the editor, GDScript runs with some optimizations turned off. An exported game will always run a bit faster (which is kinda equivalent to having a debugger open all the time in a browser, which slows down JS as well).

Also, just a proof of concept: https://www.youtube.com/watch?v=KOl4_O0CQrE

Zylann | 2017-02-28 00:12

Got it. Didn’t know GDScript isn’t compiled. Thanks again for the answer. Gonna try triangles with C++ at least for curiosity sake :slight_smile:

Thanks again, Zylann

unclejack | 2017-02-28 00:29