Search code examples
unity-game-enginespatialmeshmrtk

MRTK | HoloLens - Get all Triangles of a Mesh within an Object


- Question -
I want to interact with the spatial mesh that I can access via MRTK. I want to instantiate a sphere at the cursor position and get every triangle of the spatial mesh that is inside of the sphere, so I can cut that out and save it for me.

I know how to

  • instantiate and position the sphere
  • get the spatial mesh via the scene understanding sdk

But I don't know how to approach this. To better understand what I want to try, I have drawn a small sketch:

enter image description here

- Solution -

  1. Get collided objects via TriggerEnter & -Exit
  2. My sphere has the object manipulation script from MRTK, that has an EndOfManipulation-Event that triggers, after placing the sphere.
  3. Getting all MeshFilter and Meshes
  4. Check via collider.bounds.contains if point is inside my collider and save all those points to create a new mesh via the code from @Pluto.
  5. Combining meshes into one mesh and placing it somewhere

Solution

    • Get the indices of vertices from spatialMesh that are inside the sphere -> indicesList
    • From the triangle array of spatialMesh get the triangles that have all the vertex indices inside indicesList -> triangleList

    And you have all you need to construct a vertex array and triangle array for the mesh inside the sphere.

    Just as an example:

    for i = 0 to triangleList.Count
        newVertices[i] = spatialMesh.vertices[triangleList[i]];
        newTriangles[i] = i;