Search Unity

Question Make new mesh from section of other mesh

Discussion in 'General Graphics' started by DevDunk, May 9, 2022.

  1. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,060
    I want to copy a part of a mesh based on collision.
    If I collide with a mesh I want to copy all triangles within a set range and generate a new mesh from that data. I have looked around but am unable to find a proper solution to solve this.

    If you need any more info, please let me know!

    I am using Unity 2021.3.2f1 with URP
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    What does this mean exactly?
     
  3. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,060
    Let's say all triangles within 0.5 units from a certain contact point
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Well, here is a good tutorial that you can use to get familiar with Unity's mesh data structure.
    https://catlikecoding.com/unity/tutorials/procedural-meshes/creating-a-mesh/

    When a collision occurs, the collision will have a list of contact points. I think they might be in world-space, so you'll probably need to convert them into model space. Loop through all of the vertices in the mesh and compare the distance to the contact point and store the indexes of the vertices. Then loop through the triangles and find triangle that contain those indexes.

    Edit: Also, I noticed that Unity's RaycastHit object has a triangleIndex property that might make it easier to identify which vertices are close to the collision if you are using a raycast. You can start with the triangle and work backwards from there.
     
    DevDunk likes this.
  5. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,060
    Thanks!
    I'll check it out tomorrow
     
  6. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,060
    I checked it out, but still am not sure how to go the other way around. I understand the generating meshes part, but not idea on how to copy over a part of a mesh. So it's mostly the detection and checking what's around it that bothers me.
    Right now I am going to hard-code everything to compare each tri or vert to the hit position, but I doubt performance will like that
     
  7. mabulous

    mabulous

    Joined:
    Jan 4, 2013
    Posts:
    198
    If performance is an issue, better fracture your object at build-time into a couple of junks instead of doing it at run-time. If you really need to do it at run-time and have an accurate result and don't want to spend O(n) time on it, you'll have to precompute an acceleration structure and do a range-search. Ususally a Kd-Tree is used for that.
     
  8. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,060
    Sadly I use realtime meshing from Lightship ARDK
     
  9. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,060
    I found a working solution here (and rewrote it to work in OnCollision):

    It does give a lag spike of about 400ms because of garbage collection, which I want to solve using Native Lists later in the process (right now we want to demo that it can in fact work)
     
  10. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,060