Advertisement
Jummit

GDScript Mesh Utilities

Jun 7th, 2020
1,342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.62 KB | None | 0 0
  1. # returns a 2d array of all faces of the `mesh` with the transform `mesh_transform` that are in `radius` of the given `position`
  2. # each array in the returned array corresponds to a surface of the mesh
  3. static func get_faces_near_radius(mesh : Mesh, mesh_transform : Transform, position : Vector3, radius := 1.0) -> Array:
  4.     var surface_faces := []
  5.     for surface in mesh.get_surface_count():
  6.         var faces := PoolIntArray()
  7.         var mesh_tool := MeshDataTool.new()
  8.         mesh_tool.create_from_surface(mesh, 0)
  9.         for face in mesh_tool.get_face_count():
  10.             for vertex in 3:
  11.                 var vertex_position := mesh_tool.get_vertex(mesh_tool.get_face_vertex(face, vertex))
  12.                 if mesh_transform.xform(vertex_position).distance_to(position) < radius:
  13.                     faces.append(face)
  14.                     break
  15.         surface_faces.append(faces)
  16.     return surface_faces
  17.  
  18.  
  19. # returns a copy of the given `mesh` with `surface_faces` removed
  20. static func remove_faces_from_mesh(mesh : Mesh, surface_faces : Array) -> ArrayMesh:
  21.     var new_mesh := ArrayMesh.new()
  22.     for surface in mesh.get_surface_count():
  23.         var surface_tool := SurfaceTool.new()
  24.         surface_tool.begin(Mesh.PRIMITIVE_TRIANGLES)
  25.         var mesh_tool := MeshDataTool.new()
  26.         mesh_tool.create_from_surface(mesh, surface)
  27.         for face in mesh_tool.get_face_count():
  28.             if not Array(surface_faces[surface]).has(face):
  29.                 for face_vertex in 3:
  30.                     var vertex := mesh_tool.get_face_vertex(face, face_vertex)
  31.                     surface_tool.add_uv(mesh_tool.get_vertex_uv(vertex))
  32.                     surface_tool.add_normal(mesh_tool.get_vertex_normal(vertex))
  33.                     surface_tool.add_vertex(mesh_tool.get_vertex(vertex))
  34.         surface_tool.commit(new_mesh)
  35.     return new_mesh
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement