Skip to content

Instantly share code, notes, and snippets.

@Capital-EX
Last active December 4, 2021 20:46
Show Gist options
  • Save Capital-EX/feb744efc56580d86da5273902e30d73 to your computer and use it in GitHub Desktop.
Save Capital-EX/feb744efc56580d86da5273902e30d73 to your computer and use it in GitHub Desktop.
func line_primitive_mesh(source_mesh: ArrayMesh) -> ArrayMesh:
# The first index is the vertex list of the mesh, the last index (-1) is the edges of the tris
# Edge data must exist for this function to work. Know to work fine with GLB files.
var vert_data = source_mesh.surface_get_arrays(0)
var verts = vert_data[0]
var edges = vert_data[-1]
var tris = PoolVector3Array()
# Reform the array so that PRIMITIVE_LINES correctly draws each tri of the mesh.
# The values A, B, C are the IDs of the vertext that make up some triangle
#
# A
# / \
# / \
# / \
# / \
# C---------B
#
for i in range(0, len(edges), 3):
var A = edges[i]
var B = edges[i + 1]
var C = edges[i + 2]
tris.push_back(verts[A])
tris.push_back(verts[B])
tris.push_back(verts[B])
tris.push_back(verts[C])
tris.push_back(verts[C])
tris.push_back(verts[A])
# Build the new mesh
var new_vert_data = []
new_vert_data.resize(ArrayMesh.ARRAY_MAX)
new_vert_data[ArrayMesh.ARRAY_VERTEX] = tris
var new_mesh = ArrayMesh.new()
new_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_LINES, new_vert_data)
return new_mesh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment