Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Dynamic Mesh Collider from Vertex Position

Discussion in 'Scripting' started by Cactus_on_Fire, Jan 23, 2017.

  1. Cactus_on_Fire

    Cactus_on_Fire

    Joined:
    Aug 12, 2014
    Posts:
    675
    Hello.

    I have a material with a dynamic vertex animation, although moving the vertices around do not modify the collider accordingly. How can I get the mesh collider on it to change with the vertex positions ?

    Thanks.
     
  2. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,055
    If you are moving the vertices in a shader then you will not be able to update the collider, you will need to do the same deformation in script and then reset the mesh collider, this is very very slow for anything other than a simple mesh.
     
  3. Cactus_on_Fire

    Cactus_on_Fire

    Joined:
    Aug 12, 2014
    Posts:
    675
    Is there an alternative way to modify the colliders with the vertex animations ?
     
  4. MikeUpchat

    MikeUpchat

    Joined:
    Sep 24, 2010
    Posts:
    1,055
    The only way to modify a mesh collider is build a mesh with the vertices in their position and then update the collider mesh by setting it to that mesh, so you would either have to do whatever deformation you are doing in your shader in script and update the mesh vertex positions and then set the collider to that. Which then rather defeats the purpose of having it in the shader. Only other alternative is to have a simpler proxy mesh to use as the collider and then deform that in script and update the collider with that.
     
  5. Cactus_on_Fire

    Cactus_on_Fire

    Joined:
    Aug 12, 2014
    Posts:
    675
    I see. Seems like there is no practical way to do it without compromise.
    Thanks.
     
  6. thieum

    thieum

    Joined:
    Apr 8, 2011
    Posts:
    59
    Moving the vertices of a mesh and making it the new collider mesh source isn't complicate and isn't that slow.
    Bui I also found this paper: http://beta.unity3d.com/talks/Siggraph2011_SpecialEffectsWithDepth_WithNotes.pdf
    where it states that with Z-depth a shader can determinate a "collision" (if it is the last to be drawn and if a pixel z-depth value is close enough to what was store before in z-depth buffer), so I guess that you could read this value from your shader and trigger something in response...
     
    Kokowolo and UnityLighting like this.
  7. captain_blah

    captain_blah

    Joined:
    Jul 10, 2013
    Posts:
    2
    One workaround would be to use a larger collider (box instead of polygon) which covers the entire area where the mesh could be deformed to. On the collision event you could simply check if the colliding object intersects your intended hit zone (the mesh) and otherwise dismiss the collision.