How solve RenderingServer::MAX_MESH_SURFACES assertion Error upgrading my Godot 3.5.1 project to Godot 4?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tximikel

I’m migrating my Godot 3.5.1 project to Godot 4.1, using a debug_draw module to draw a 2D grid in a plane of 3D scene, is running ok with 100 x 100 grid lines, but when i extend grid size to 200 x 200 only first 256 lines are shown, and then i’ve a runtine error in debugger window that shows:

  • E 0:00:01:0151 debug_draw_3d.gd:292 @ process(): Condition “mesh->surface_count == RenderingServer::MAX_MESH_SURFACES” is true.
  • <C++ Source> drivers/gles3/storage/mesh_storage.cpp:110 @ mesh_add_surface()
  • debug_draw_3d.gd:292 @ process() MeshInstance.gd:503 @ _physics_process()

This occurs when i run mi.mesh.surface_end() instruction, and the mesh object is configured in _init() as:

  • mi = MeshInstance3D.new()
  • mi.mesh = ImmediateMesh.new()

How can i increase MAX_MESH_SURFACES value of RenderingServer constant? I can’t find a parameter to change in Project - Settings - General - Rendering…

Thanks & hugs! Mikel

It looks like this happens because your mesh contains too many surfaces. This is a constant because there is no practical reason to want so many, so it was fixed to some high maximum to make the code simpler I guess.

Is there a reason you use so many? It sounds like a misuse. Surfaces are needed only when you want more than one material in your mesh. Usually to draw a grid you should only need one.

Zylann | 2023-07-08 12:47

Yes i’ve solved the issue! thanks Zylann for your clarification :slight_smile:

The question is that when i migrate this debug_draw addon script from GD3 to GD4, i’ve adapted from ImmediateGeometry to MeshInstance3D+ImmediateMesh, and then translated code from ig.begin(Mesh.PRIMITIVE_LINES) to mi.mesh.surface_begin(Mesh.PRIMITIVE_LINES), that is located in a for loop to draw lines added to items_to_draw Array, then i’ve MAXMESHSURFACES assertion error in GD4 that before don’t occurs with ImmediateGeometry in GD3.

The solution for now is to move mi.mesh.surface_begin(Mesh.PRIMITIVE_LINES) out and before the for loop where array items are drawn, and mi.mesh.surface_end() after it, so don’t increase the surfaces avoiding the mistake of exceeding the limit of meshes, drawing all grid lines with the same surface ok.

tximikel | 2023-07-08 15:13