Wrong result when trying to recreate mesh using MeshDataTool and ArrayMesh

Godot Version

3.5

Question

Hello everyone! I’m trying to recreate/clone imported 3D model using MeshDataTool, and ArrayMesh. My actual goal is to be able to merge multiple meshes. I’ve been using SurfaceTool.append_from(), but it randomly starts working super slow. The documentation also suggests to not use it.
“Note: Using append_from() on a Thread is much slower as the GPU must communicate data back to the CPU, while also causing the main thread to stall (as OpenGL is not thread-safe). Consider requesting a copy of the mesh, converting it to an ArrayMesh and adding vertices manually instead.”

So I’m trying to read all the mesh vertices, and recreate the model.
Here’s how my code looks like.

var aMeshArray = []
	aMeshArray.resize(Mesh.ARRAY_MAX)
	
	var aVerts = PoolVector3Array()
	var aNormals = PoolVector3Array()
	var aUV1 = PoolVector2Array()
	var aUV2 = PoolVector2Array()
	var aIndices = PoolIntArray()
	
	var mMaterial : Material
	
	for node in parent.get_children() :
		
		print(node.mesh)
		if mMaterial == null:
			mMaterial = node.material_override
		
		var mdt = MeshDataTool.new()
		mdt.create_from_surface(node.mesh, 0)
		
		for i in range(mdt.get_vertex_count()):
			var vert = mdt.get_vertex(i)
			var norm = mdt.get_vertex_normal(i)
			var uv1 = mdt.get_vertex_uv(i)
			var uv2 = mdt.get_vertex_uv2(i)
			
			aVerts.push_back(vert)
			aNormals.push_back(norm)
			aUV1.push_back(uv1)
			aUV2.push_back(uv2)
			aIndices.push_back(i)
	

	aMeshArray[Mesh.ARRAY_VERTEX] = aVerts
	aMeshArray[Mesh.ARRAY_TEX_UV] = aUV1
	aMeshArray[Mesh.ARRAY_TEX_UV2] = aUV2
	aMeshArray[Mesh.ARRAY_NORMAL] = aNormals
	aMeshArray[Mesh.ARRAY_INDEX] = aIndices

	var hMesh = Mesh.new()
	hBatched.mesh = hMesh
	hBatched.mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, aMeshArray)

	hBatched.material_override = mMaterial

but the end result looks like this.

Wanted to ask if anyone knows what could be the reason :frowning:
Thanks!

You know what the Vertex array and the Index array do, right? I seems you are assuming your indices are a linear monotonic progression. That is just wrong. You’re going back to mesh school. =P

you’re right, I know absolutely nothing about how meshes work hehe… but can’t really find any example to follow :sob:

Indices are like pointers to vertices. They can repeat, for example, to complete a triangle using four points (1 2 3 1). You put the index of your “for” loop in there, instead of the index of the vertex, thus making something with no relation to the thing you wanted.

1 Like

Got it! Thank you very much! I’ve managed to get the indices from faces! :slight_smile:
This is how it looks like, in case someone like me needs it )

for i in mdt.get_face_count():
	var vID1 = mdt.get_face_vertex(i , 0)
	var vID2 = mdt.get_face_vertex(i , 1)
	var vID3 = mdt.get_face_vertex(i , 2)
	
	aIndices.push_back(vID1)
	aIndices.push_back(vID2)
	aIndices.push_back(vID3)
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.