3

- 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
Perazim
  • 1,501
  • 3
  • 19
  • 42

1 Answers1

1
  • 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;
Pluto
  • 3,911
  • 13
  • 21
  • 1
    ok, thanks for responding. The problem is that I didnt get how to check if an vertices is inside my sphere. How do I determine whether a point or triangle is inside an object. The only thing I found was [this post](https://answers.unity.com/questions/611947/am-i-inside-a-volume-without-colliders.html) about creating planes and checking if the point is on the plane, but I guess this is not the right way. – Perazim Aug 18 '20 at 08:58
  • 1
    @Perazim It's really easy to check if a point is inside a sphere. It's just `bool isInsideSphere = Vector3.Distance(point, sphereCenter) < sphereRadius`. For the triangles you only need to check if the indices are in the `indicesList`, like I said in the answer. – Pluto Aug 18 '20 at 10:01
  • oh of course, you are right. Do you know how to achiev that if it would not be an sphere, but for example a cuboid or a cube? The distance would then not be a possible way. – Perazim Aug 18 '20 at 10:18
  • 1
    @Perazim Again it's fairly easy if its axis aligned. Search for _axis aligned bounding box_ or _AABB_. There should be resources on the internet/SO on this topic, and if you don't find what you're looking for, you can post a separate question on SO. – Pluto Aug 18 '20 at 10:39
  • I added my solution to my question. Thanks again! – Perazim Aug 25 '20 at 20:08