Error "Condition "src_face_count % 3" is true"

Godot Version

4.4

Question

I’m trying to generate a procedural mesh and a collision shape for that mesh. but when I run the code, I get this strange error : modules/godot_physics_3d/godot_shape_3d.cpp:1619 - Condition “src_face_count % 3” is true.

here’s the code

generateMesh.gd
@tool
extends MeshInstance3D

var start = Vector2(0,0)
var dimensions = Vector2i(10,10)
var resolution = 0.5

func _ready():
#mesh
var terrain = Grounddata.Terrain()
mesh = ArrayMesh.new()
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES,terrain)

#collision
var collision_shape = CollisionShape3D.new()
var mesh_data = mesh.surface_get_arrays(0)
var vertices = mesh_data[0]
var concave_shape = ConcavePolygonShape3D.new()
concave_shape.set_faces(vertices)
collision_shape.shape = concave_shape

Grounddata.gd
@tool
extends Node

var start = Vector2(0,0)
var dimensions = Vector2i(10,10)
var resolution = 0.5

func Terrain():
var terrain =
terrain.resize(ArrayMesh.ARRAY_MAX)

var verts = PackedVector3Array()
var indices = PackedInt32Array()

for y in range(dimensions.y+1):
	for x in range(dimensions.x+1):
		verts.append(Vector3(x * resolution + start.x, randf_range(-0.2,0.2), y * resolution + start.y))


for y in range(dimensions.y):
	for x in range(dimensions.x):
		indices.append(x + y*(dimensions.x+1))
		indices.append(x + y*(dimensions.x+1)+1)
		indices.append(x + (y+1)*(dimensions.x+1)+1)
		
		indices.append(x + (y+1)*(dimensions.x+1))
		indices.append(x + y*(dimensions.x+1))
		indices.append(x + (y+1)*(dimensions.x+1)+1)


terrain[Mesh.ARRAY_VERTEX] = verts
terrain[Mesh.ARRAY_INDEX] = indices

return terrain

If you put three backticks (```) on lines before and after your code, it will format nicely in forum posts.

I’m not sure what the mesh problem is, but if I were to guess, the % 3 test looks suspiciously like a check to see if the number of vertices (or indices, since you’re indexed) is a clean multiple of three. Since each triangle pulls three off the list, (I’m assuming we’re not drawing a fan here) you’d get an incomplete triangle at the end if count % 3 is nonzero.

Maybe check the length of verts and indices and see if they make sense.