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 ![]()
Thanks!
