Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Slicing object into little cubes to detect where user is touching the object

Discussion in 'Scripting' started by fraeri, Nov 21, 2018.

  1. fraeri

    fraeri

    Joined:
    Nov 8, 2018
    Posts:
    64
    Hi,
    I'm relatively new to unity, but programming itself is not new to me. I hope I'm right here.

    I need to know where the user has touched a 3D object. My idea was to slice the object into little cubes (dimensions of them should adjustable) and then detect which cube was touched. Slicing Mesh is not a new thing, you can find payed assets to make like sword slices. But I never saw this type of "organized" slicing.

    I want to know if I'm on the right path or if I miss a much simpler solution, like some kind of Grid overlay. The thing with overlaying a grid is that I want to make it possible to touch inner cubes of the object as well.

    Thank you for your help.
     
    Last edited: Aug 29, 2019
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    Hi,
    Your idea is basically to convert the model to voxels. There are voxel engines in the asset store, but I am not sure if they allow to convert meshes into voxels as you desire.

    However, a simple raycast could also give you the location on the surface of your mesh (or: mesh collider)... Is that not enough for you?
     
  3. fraeri

    fraeri

    Joined:
    Nov 8, 2018
    Posts:
    64
    Hi Hosnkobf,
    Thank you for your quick answer!!

    Voxel is a new topic for me. After a quick research, I think it is not exactly what I'm looking for, but pretty damn close! Because I need the surface of the original object to be preserved. Or is that possible without making the Voxel dimensions really small.
    But Quick Idea: When I convert the object into Voxel, place it exactly over the original object and make it transparent so that the user can't actually see the voxel-object and thinks he is touching the real object. Possible, right?

    Raycast with mesh Collider could be an option, but only for the outer surface right?

    What I want to do: The user should make Notes for on a specific part of the volume of the object(e.g. the volume of a cube I want to create).
     
  4. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    you are right. If you render it as voxels it would look "blocky".

    Yes, I would simply not render the voxels at all. Only using them as data storage (storing position, info if it is part of the object and probably your notes).

    Yes, only the surface.
    But I believe it is hard to select a location within the object anyways, so I guess the surface is the better User Experience...
    If you really want to mark spots inside the object, the rendering of voxels makes sense again because then you could "look inside".
     
  5. fraeri

    fraeri

    Joined:
    Nov 8, 2018
    Posts:
    64
    Yeah, I guess voxel are making more sense. I will try that. Thanks for the suggestion.

    Let's see how I can solve the problem of looking inside.
    I will keep you updated.
     
  6. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    Nice!
    yeah. that would be an option. You just have to check all 8 corners of the voxel if they are all inside or not (for those which have the center inside). if they are not inside of the mesh you can cull them.
    However, this is not 100% proof. edges could still be outside under certain circumstances.

    Also, it might be a problem for very detailed areas (like the seven holes in the middle) where too many voxels could be culled...
    To solve that you could go really crazy and implement a sparse voxel octree... But that is not trivial.
     
  7. MrMatthias

    MrMatthias

    Joined:
    Sep 18, 2012
    Posts:
    191
  8. fraeri

    fraeri

    Joined:
    Nov 8, 2018
    Posts:
    64
    Puh, great idea !! Any tip how I get started with this action? xD

    Hi Matthias,
    Yes, but with a Raycast it's only possible to get Positions on the Surface of the mesh. Everything inside will be hollow. "redesigning" the part with voxel give me the ability to make selections on the inside. Or do you mean something else?
     
  9. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    First you really need a proper collision detection between an axis aligned box and your mesh. Only checking one point in space is not enough anymore.

    then you have to do basically the following:
    • divide your (cubic) scene area into 8 cubes and check the intersection of each of them with the mesh.
      • If no intersection with mesh: check somehow if the cube is completely outside or completely inside the mesh and mark it accordingly. No further action required (unless you want more cubes if inside).
      • If there is an intersection with the mesh: divide the cube into 8 cubes and check intersections again. Always pass and increment an integer defining how deep you are in the octree.
    you should define a maximum depth to not go into infinity ;)
    For the last depth you can count all intersecting cubes as outside, so that the mesh can cover the cubes completely.
     
  10. fraeri

    fraeri

    Joined:
    Nov 8, 2018
    Posts:
    64
    All right, then I just need to figure out how to make the intersection tests while dividing the cubes more and more ... I guess I'm coming back in a few days to report:D Thanks so far
     
    Hosnkobf likes this.
  11. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    About the Mesh-Cube intersection:
    you should do a check between the Bounding box of the mesh with the Cube first. If they intersect you basically have to check the intersection between the cube and every triangle of the mesh (if you cannot reduce the number of triangles to check somehow).

    You don't have to re-invent the wheel. You can use existing code like this: https://github.com/mattatz/unity-intersections
    (the cube is an "Axis Aligned Bounding Box" in short "AABB")
     
    fraeri likes this.
  12. MrMatthias

    MrMatthias

    Joined:
    Sep 18, 2012
    Posts:
    191
    How do you get the depth from a 2d point though?
    I think unity has an octree implementation on github btw.
     
  13. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    yeah, as I told: you would need to manually go through all triangles of the mesh (every mesh consist of triangles only) and check each of them...
     
  14. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    yeah, also an option. nice :)
    this way you have the most accurate positions and visualization.
    It is clear that it needs some performance to slice a mesh. But once sliced the performance should be good. with the voxel octree you would face a constant performance hit instead (but probably fast enough) ...