+1 vote

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:
http://docs.godotengine.org/en/stable/tutorials/2d/custom_drawing_in_2d.html

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.

in Engine by (13 points)

1 Answer

+2 votes

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.

by (27,898 points)

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

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

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

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

Thanks again, Zylann

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.