ArrayMesh.add_surface_from_arrays() triangulation issue

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

Hello everyone,

Given an array of vertices I am trying to do triangulation using add_surface_from_arrays()

When I want to triangulate monotonous polygons, there is no issue. But when trying with non-monotonous ones, the mesh shape is not what i expect. I will put an example; vertices are added to the array in the order shown below:

What i want it to be:
Edges goes from 0-1, 1-2, 2-3, 3-0

 0--------------------1
   \                 /
        \           /        
             3     /
               \  /
                 2

What it is:
Edges goes from: 0-1, 1-2, 2-3

 0--------------------1
   \                 /
      \             /        
         \    3    /
             \    /
                 2

I can see how the method draws another edge from 2 to 0. How can I avoid that?
Is it possible for me to define what the edges will be? Am I using the wrong method or procedure?

If i correctly understand, the method im using only knows the vertices i am passing, but not the shape i want. How to achieve that?

This is my piece of code:

func _triangulator(points_: Array) -> void:
	
	var vertices = PoolVector3Array()
	var array_mesh = ArrayMesh.new()
	var arrays = []
	var plane = MeshInstance.new()
	
	# Add all Vertex in order
	for v in points_:
		vertices.push_back(v)
	
	
	arrays.resize(Mesh.ARRAY_MAX)
	arrays[Mesh.ARRAY_VERTEX] = vertices
	
	
	# Create the mesh
	array_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLE_FAN, arrays)
	
	plane.mesh = array_mesh

	FenceWallsNd.add_child(plane)

Thanks so much.