How to use Mesh.add_surface?

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

I want to generate a mesh not using SurfaceTool because its API is impractical to use in my case.

So I looked at Mesh.add_surface, but the documentation of this method looks completely off from what the function prototype says: Mesh — Godot Engine (latest) documentation in English

It doesn’t says what arrays should contain, and doesn’t describe morph_arrays or alphasort.

So I don’t understand how to use this function at all :s

:bust_in_silhouette: Reply From: davidoc

Looking at the git repo I learned this:

Arrays is an array of arrays, you can pass null arrays for those you don’t use, the first array is for vertex information (ARRAY_VERTEX = 0), next are normals, tanget, etc.

I didn’t look at what morphs do (animation maybe?) but it looks like you can left it with the default value, same for alphasort (if your mesh has transparency this helps to fix rendering artifacts).

I cannot test it at this time, but I hope this may guide you.

Is there an array for indices? Or should all vertices be specified redundantly for all triangles/lines?

Oh… that’s it, am I right? https://github.com/godotengine/godot/blob/master/drivers/gles2/rasterizer_gles2.cpp#L1934

Zylann | 2016-12-07 18:15

It would be the 9th array (ARRAY_INDEX = 8), or you could specify the type as PRIMITIVE_TRIANGLES and every 3 vertices a triangle is created, as I mentioned I only looked at the repo and have not tested it.

Yes, that’s the code I looked at.

davidoc | 2016-12-07 18:32

I think I won’t use PRIMITIVE_TRIANGLES because I need to make quads, so with indexes I can specify 4 vertices instead of 6 (or even less if it’s a strip).
We’ll see if 3.0 changes this API, thanks for the answer!

Not tested though, will do that once I get back home :slight_smile:

Zylann | 2016-12-07 18:42

I just wanted to know whether one can resize the arrays and reposition the vertices on the fly, without creating a new one? Thanks.

rraallvv | 2017-11-01 18:35

You probably can in raw OpenGL, but not to my knowledge in Godot API. You have to regenerate the whole surface (not the whole mesh though if you use multiple materials).

Zylann | 2017-11-01 19:51

@Zylann I see, I’ll have to create a custom C++ class and expose it to GDScript, thanks.

rraallvv | 2017-11-01 20:43

Note that in Godot, raw OpenGL is located deep in the rendering engine, you would have to add the feature in there.

Zylann | 2017-11-01 20:45

@Zylann I’ll keep that in mind, thanks.

rraallvv | 2017-11-01 20:53