Search Unity

Prebake Collision Data not doing anything

Discussion in 'Physics' started by DroidifyDevs, Aug 2, 2017.

  1. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Hello!

    So I'm creating an infinite world. To do this, I'm spawning "tiles", and each tile spawns objects on it (such as trees, rocks etc...). However, when playing you can feel a hiccup as a tile gets spawned. A quick look in the Profiler points to Mesh.Bake PhysX CollisionData being the culprit:
    upload_2017-8-1_18-59-1.png

    After some looking around, I found a setting in the Player Settings called "Prebake Collision Meshes". I thought "Great, I'm saved!". In the Editor, it makes no difference, so I thought OK, let's build it for Android. I run it with the Profiler connected and "Prebake Collision Meshes" enabled, and there are still hickups from Mesh.Bake (see screenshot above from the Android device WITH Prebake enabled).

    What am I supposed to do if that button doesn't seem to work?

    Also, that brings me to another point: I'm spawning only 1 type of tree right now. ALL trees are identical. So why is it calculating a mesh for each tree when all of them are the same? Is that an optimization oversight by UT? If so, can I get around this via scripting?

    Thanks for any ideas!

    EDIT: I unchecked "Inflate Mesh" and now what used to take 120ms now takes 10ms. A huge improvement! So I guess Inflate Mesh isn't baked in when "Prebake Collision Meshes" is enabled? Is this normal or a bug?

    Some insight from UT would be great here.
     
    Last edited: Aug 2, 2017
    Edy likes this.
  2. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    596
    Yes, you're right. The mesh inflation forces the shared mesh to be rebuilt. Can you still proceed without the inflation?
     
  3. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    I think so. If I do need mesh inflation later on, what are my options to avoid the performance hit?
     
  4. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    596
    Probably none at this point, it's a sort of a design flaw. Once the inflation is set, the mesh is never taken from the shared data.
     
  5. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    I guess if I was good in 3D modeling programs, I could take a mesh, inflate it there, then re-import it and have a mesh collider use the pre-inflated mesh (basically avoid using Unity's mesh inflator). Very hairy but in theory that would work. I don't see why inflated meshes can't be baked.

    Perhaps it's because the inflate mesh setting can be set from code?
    https://docs.unity3d.com/ScriptReference/MeshCollider-inflateMesh.html

    That would slightly explain things, as if the Skin Width was changed in-game, the pre-baked data would be useless. However, that could be fixed by having a "Prevent runtime changes to mesh collider" checkbox that would prevent you from changing the skin width while the game is running. So I hope the Mesh Collider gets revisited and that extra feature is added.
     
  6. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    Has this been re-visited? The inflate feature is excellent for generating low poly collision geometry for meshes in our game. We don't expect to be able to inflate and deflate at runtime.
     
  7. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    Maybe even some API for inflating meshes ourselves so I can save them to the project and assign them to the mesh collider with inflation turned off?

    There doesn't appear to be a way to 'get' the inflated mesh from the MeshCollider.
     
  8. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    596
    I think that we're actually deprecating this flag with the PhysX 3.4 upgrade because we switch to the Quickhull algorithm for building convexes. There will be a spin-off build soon, once 2018.2 goes beta.
     
  9. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    I see. It would be a shame to loose the inflate functionality, even if it was just a static helper method on the Mesh class. var inflatedMesh = Mesh.Inflate(myMesh, inflateAmount);

    The inflation was a nice and quick way to programmatically generate convex low-poly mesh colliders.

    Though I think the inflate functionality is a part of PhysX and not Unity, so it may not be possible to 'expose' this function to us?
     
  10. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    596
    Not sure this is a good use for it really. Inflation increases volume, and you get the described effect of lower faces in a convex only with the extreme values I believe. Though a possible case, I'd think this was never the design intention.
     
  11. JohnnyFactor

    JohnnyFactor

    Joined:
    May 18, 2018
    Posts:
    343
    I encountered this problem when enabling 'Prebake Collision Meshes' from the Player settings. PhysX ignores the inflated mesh and tries to use the gameObject mesh. In my case, that resulted in a lot of failures due to more than 255 faces. I was forced to create simpler versions in Maya.

    A search on the asset store shows a few people who have tried to tackle this issue. One example:
    https://assetstore.unity.com/packages/tools/physics/concave-collider-4596

    Another:
    https://assetstore.unity.com/packages/tools/physics/non-convex-mesh-collider-84867

    What would be really amazing is if Unity dropped dependancy on the PhysX engine and looked at other options, like Bullet or Havok.
     
  12. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    596
    Could you try 2018.3 beta? The mesh cooking there is much more reliable.