Calls To Add Mesh Collider Components Forces Physx ConvexHullLib CleanUpVertices, Which Hangs Main Thread

Hi,

I have a model that has 50 different meshes, all of which I want to have convex colliders. On calling a gameObject.AddComponent().convex = true on each mesh, I get a very long hang on my Polyspatial App. What XCode instruments picks up, is that no matter if I use “Generate Colliders” or prebaking, physx::ConvexHullLib::cleanupVertices(unsigned int, physx::PxVec3 const*, unsigned int, unsigned int&, physx::PxVec3*, physx::PxVec3&, physx::PxVec3&) always hangs the app.

I decided to move the prebaking to Burst Jobs with Physics.BakeMesh, and after the jobs are successful, ANYTIME I call the GameObject().AddComponent<MeshCollider>().convex = true on the main thread, that physx::ConvexHullLib::cleanupVertices is called.

Has anyone else noticed that PhysX calls inescapably hang the app? Or, have any developers found approaches around this? My app involves the ability to grab a submesh of a larger model from a pinch interaction, which in my mind involves convex colliders to be most accurate on the initial grab interaction.

Heya!

This sounds kinda odd / bug inside physx itself. What flags are you using for your mesh colliders collision meshes? Have you tried disabling Weld Colocated Vertices and Enable mesh cleaning?

Would be good to file a bug ^^.

Cheers,
AlexRvn.

Is this using RealityKit mode? I’m wondering if this call is coming from Unity itself or from RealityKit (which I believe also uses PhysX). A repro project would definitely be helpful.

How long is the hang, and is it only at startup, or are you updating/creating new meshes frequently and thus causing hangs each time? We do have an upcoming change that will switch to an asynchronous API for some of the mesh calls (including non-convex shapes), though there’s also an asynchronous API for convex shapes, which we’re not using at the moment but could potentially switch to.

Hi,
For @alexrvn I’ll make a bug report. I am using the default import flags for my dense model:


I can try to try out those extra options right now.

@AndrzejUnity I am using RealityKit, and latest polyspatial. The hang only occurs when instantiating the object with mesh colliders added, or by adding the mesh collider component on runtime. The meshes aren’t modified on runtime and come from a prefab. I have a skeleton that has an isolated example with a miscellaneous unoptimized model:
[LINK TO PROJECT REMOVED. Message this account if you want to investigate]

NOTE: These are my polyspatial settings. This mimics the real project as close as possible. I have ‘everything’ selected on the collider objects mask:

On instruments, the full hang printout stack in a 1 second hang on pressing the button to spawn any kind of convex collider prefab (in the attached project):

Weight	Self Weight	Symbol Names
1.26 s  87.5%	0 s	  Thread  0x727692
1.26 s  87.5%	0 s	   start
1.26 s  87.5%	0 s	    start_sim
1.26 s  87.5%	0 s	     main
1.26 s  87.5%	0 s	      static UnityPolySpatialApp.$main()
1.26 s  87.5%	0 s	       static App.main()
1.26 s  87.5%	0 s	        runApp<A>(_:)
1.26 s  87.5%	0 s	         closure #1 in KitRendererCommon(_:)
1.26 s  87.5%	0 s	          UIApplicationMain
1.26 s  87.5%	0 s	           -[UIApplication _run]
1.26 s  87.5%	0 s	            GSEventRunModal
1.26 s  87.5%	0 s	             CFRunLoopRunSpecific
1.26 s  87.5%	100.00 µs	              __CFRunLoopRun
1.26 s  87.3%	0 s	               __CFRunLoopDoSources0
1.26 s  87.3%	0 s	                __CFRunLoopDoSource0
1.26 s  87.3%	0 s	                 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
1.26 s  87.3%	0 s	                  runloopSourceCallback
1.25 s  87.2%	0 s	                   schedulerStepScheduledMainSection
1.25 s  87.1%	400.00 µs	                    _UIUpdateSequenceRun
1.14 s  79.4%	0 s	                     CA::Display::DisplayLink::dispatch_deferred_display_links(unsigned int)
1.14 s  79.3%	0 s	                      CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long)
1.14 s  79.3%	0 s	                       CA::Display::DisplayLinkItem::dispatch_(CA::SignPost::Interval<(CA::SignPost::CAEventCode)835322056>&)
1.13 s  78.8%	0 s	                        UnityRepaint
1.13 s  78.8%	0 s	                         UnityPlayerLoopImpl(bool)
1.13 s  78.5%	0 s	                          PlayerLoop()
1.13 s  78.4%	400.00 µs	                           ExecutePlayerLoop(NativePlayerLoopSystem*)
1.03 s  71.9%	0 s	                            ScriptingInvocation::Invoke(ScriptingExceptionPtr*, bool)
1.03 s  71.9%	0 s	                             scripting_method_invoke(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingArguments&, ScriptingExceptionPtr*, bool)
1.03 s  71.9%	0 s	                              il2cpp::vm::Runtime::Invoke(MethodInfo const*, void*, void**, Il2CppException**)
1.03 s  71.9%	0 s	                               il2cpp::vm::Runtime::InvokeWithThrow(MethodInfo const*, void*, void**)
1.03 s  71.8%	100.00 µs	                                PolySpatialCore_PolySpatialAfterLateUpdate_m7EC5B99661C30542E56C69109A612167F09B768C
1.03 s  71.7%	0 s	                                 PolySpatialUnitySimulation_UpdateInternal_mA9C08F55473429BA3C679E2746EF5A067A65BDFB
918.50 ms  63.8%	100.00 µs	                                  PolySpatialUnityTracker_Process_m78434E152D88663BA40B7E5832E6260C55323594
905.70 ms  63.0%	0 s	                                   PolySpatialUnityTracker_ProcessNewAndModifiedObjects_m9A5758704BFC56B57F35E89A3294DFD57C4085C1
905.60 ms  63.0%	0 s	                                    InterfaceActionInvoker0::Invoke(unsigned short, Il2CppClass*, Il2CppObject*)
868.40 ms  60.4%	0 s	                                     UnityObjectTracker_4_ProcessNewAndModifiedObjects_m58615861A50B6EDA613F94D05112DA16B7CC847A_gshared
868.40 ms  60.4%	0 s	                                      UnityObjectTracker_4_ProcessAllNewAndModifiedObjects_m5AF65F8982CD7BA59410D7C62874D5160B612046(UnityObjectTracker_4_t126C1F8A3D80E9DBCD8A7281AAD9AB675EC7B467*, MethodInfo const*)
868.40 ms  60.4%	0 s	                                       UnityObjectTracker_4_ProcessAllNewAndModifiedObjects_m5AF65F8982CD7BA59410D7C62874D5160B612046_gshared
868.40 ms  60.4%	0 s	                                        VirtualActionInvoker2<PolySpatialUnitySimulation_t534909DD7A251F36F5473F74CD49AEC27D12B25C*, Il2CppObject*>::Invoke(unsigned short, Il2CppObject*, PolySpatialUnitySimulation_t534909DD7A251F36F5473F74CD49AEC27D12B25C*, Il2CppObject*)
868.40 ms  60.4%	0 s	                                         PolySpatialNativeExtensions_SerializedChangeListCommand_TisPolySpatialColliderData_tFA66DE6E7574A7C6DE9A6E4679C0BA8F7A5A9146_m8A8CC4832B0392A2966B8A756A09494928F12508_gshared
868.40 ms  60.4%	0 s	                                          PolySpatialNativeExtensions_Command_TisByte_t94D9231AC217BE4D2E004C4CD32DF6D099EA41A3_mBE415AC9F818B3916EADF464E0DBEDF3C86F0E0E(Il2CppObject*, PolySpatialCommandHeader_t4C08AB2248229376EE2A1AB84F30B514B08EAD9B, ReadOnlySpan_1_tA850A6C0E88ABBA37646A078ACBC24D6D5FD9B4D, MethodInfo const*)
868.40 ms  60.4%	0 s	                                           PolySpatialNativeExtensions_Command_TisByte_t94D9231AC217BE4D2E004C4CD32DF6D099EA41A3_mBE415AC9F818B3916EADF464E0DBEDF3C86F0E0E_gshared
868.40 ms  60.4%	0 s	                                            InterfaceActionInvoker4<PolySpatialCommandHeader_t4C08AB2248229376EE2A1AB84F30B514B08EAD9B, int, void**, int*>::Invoke(unsigned short, Il2CppClass*, Il2CppObject*, PolySpatialCommandHeader_t4C08AB2248229376EE2A1AB84F30B514B08EAD9B, int, void**, int*)
868.40 ms  60.4%	0 s	                                             SessionSceneGraphHandler_HandleCommand_m0279725F2DE7C243DC788F21BBB4B8044ED58263
868.00 ms  60.3%	0 s	                                              SessionEntity_CreateOrUpdateComponent_mC82C107E71AED6AF864909DAC30520E3982AAA45
867.60 ms  60.3%	100.00 µs	                                               ViewEntity_IssueCommandToNextHandlerWithStruct_TisPolySpatialColliderData_tFA66DE6E7574A7C6DE9A6E4679C0BA8F7A5A9146_mD1B1E1C651CF85239B9AA5C181541C201B46805A_gshared
867.50 ms  60.3%	0 s	                                                PolySpatialNativeExtensions_Command_TisByte_t94D9231AC217BE4D2E004C4CD32DF6D099EA41A3_mBE415AC9F818B3916EADF464E0DBEDF3C86F0E0E(Il2CppObject*, PolySpatialCommandHeader_t4C08AB2248229376EE2A1AB84F30B514B08EAD9B, ReadOnlySpan_1_tA850A6C0E88ABBA37646A078ACBC24D6D5FD9B4D, MethodInfo const*)
867.50 ms  60.3%	0 s	                                                 PolySpatialNativeExtensions_Command_TisByte_t94D9231AC217BE4D2E004C4CD32DF6D099EA41A3_mBE415AC9F818B3916EADF464E0DBEDF3C86F0E0E_gshared
867.50 ms  60.3%	0 s	                                                  InterfaceActionInvoker4<PolySpatialCommandHeader_t4C08AB2248229376EE2A1AB84F30B514B08EAD9B, int, void**, int*>::Invoke(unsigned short, Il2CppClass*, Il2CppObject*, PolySpatialCommandHeader_t4C08AB2248229376EE2A1AB84F30B514B08EAD9B, int, void**, int*)
867.50 ms  60.3%	0 s	                                                   RealityKitBackend_HandleCommand_mDDF59ED88DCC336EF3CAEAC1EF5440B9AFC1A48E
867.50 ms  60.3%	0 s	                                                    SendClientCommandCallback_Invoke_m070CD1B34785553533C4634FEB2190104A16092B_inline(SendClientCommandCallback_t5D0BBD9E6240CD41CEA460757F2C2F04050378ED*, int, int, void**, int*, MethodInfo const*)
867.50 ms  60.3%	0 s	                                                     @objc closure #1 in static PolySpatialRealityKit.overrideApi(_:_:)
867.50 ms  60.3%	0 s	                                                      closure #1 in static PolySpatialRealityKit.overrideApi(_:_:)
867.50 ms  60.3%	0 s	                                                       PolySpatialRealityKit.OnSendClientCommandOverrides(_:_:_:_:)
867.50 ms  60.3%	0 s	                                                        specialized PolySpatialRealityKit.OnSendClientCommandOverrides(_:_:_:_:)
867.50 ms  60.3%	0 s	                                                         PolySpatialRealityKit.OnSendClientCommand(_:_:_:_:)
867.50 ms  60.3%	0 s	                                                          specialized PolySpatialRealityKit.OnSendClientCommand(_:_:_:_:)
867.40 ms  60.3%	0 s	                                                           implicit closure #11 in PolySpatialRealityKit.OnSendClientCommand(_:_:_:_:)
867.40 ms  60.3%	0 s	                                                            specialized PolySpatialRealityKit.HandleChangeListArg<A>(_:_:_:entryCallback:)
867.40 ms  60.3%	0 s	                                                             specialized PolySpatialRealityKit.HandleChangeListArg<A>(_:_:_:entryCallback:)
867.40 ms  60.3%	0 s	                                                              implicit closure #12 in implicit closure #11 in PolySpatialRealityKit.OnSendClientCommand(_:_:_:_:)
867.40 ms  60.3%	0 s	                                                               specialized implicit closure #12 in implicit closure #11 in PolySpatialRealityKit.OnSendClientCommand(_:_:_:_:)
867.40 ms  60.3%	0 s	                                                                PolySpatialRealityKit.createOrUpdateCollider(_:_:_:)
867.40 ms  60.3%	0 s	                                                                 PolySpatialEntity.createOrUpdateCollision(info:trackingFlags:)
867.40 ms  60.3%	0 s	                                                                  PolySpatialEntity.applyCollisionShapeData(info:)
865.50 ms  60.2%	0 s	                                                                   static PolySpatialRealityKit.generateConvexShape(_:)
865.50 ms  60.2%	0 s	                                                                    specialized static PolySpatialRealityKit.generateConvexShape(_:)
811.70 ms  56.4%	0 s	                                                                     static ShapeResource.generateConvex(from:)
805.70 ms  56.0%	0 s	                                                                      REAssetManagerCollisionShapeAssetCreateConvexPolyhedron
803.80 ms  55.9%	0 s	                                                                       re::PhysXCollisionFactory::createConvexPolyhedronShape(re::Allocator&, re::Vector3<float> const*, unsigned long, unsigned long)
803.70 ms  55.9%	0 s	                                                                        (anonymous namespace)::createConvexMesh(physx::PxCooking*, physx::PxPhysics*, void const*, unsigned int, unsigned int)
803.40 ms  55.8%	0 s	                                                                         physx::Cooking::cookConvexMesh(physx::PxConvexMeshDesc const&, physx::PxOutputStream&, physx::PxConvexMeshCookingResult::Enum*) const
793.70 ms  55.2%	0 s	                                                                          physx::Cooking::cookConvexMeshInternal(physx::PxConvexMeshDesc const&, physx::ConvexMeshBuilder&, physx::ConvexHullLib*, physx::PxConvexMeshCookingResult::Enum*) const
793.20 ms  55.1%	1.60 ms	                                                                           physx::QuickHullConvexHullLib::createConvexHull()
785.40 ms  54.6%	0 s	                                                                            physx::ConvexHullLib::cleanupVertices(unsigned int, physx::PxVec3 const*, unsigned int, unsigned int&, physx::PxVec3*, physx::PxVec3&, physx::PxVec3&)
784.80 ms  54.6%	784.80 ms	                                                                             physx::ConvexHullLib::cleanupVertices(unsigned int, physx::PxVec3 const*, unsigned int, unsigned int&, physx::PxVec3*, physx::PxVec3&, physx::PxVec3&)

Thanks for the stack trace! That does confirm that the hang is coming from RealityKit, and that our switching to the async API might alleviate the hang. If you do submit a bug report (and let us know the incident number, IN-#####, so that we can find it easily), I can test with the async API to see if it helps, and if it does, we can probably make that change in the next release.

Edit: Sorry, I didn’t see the repro project at first. I’ll try the async API with that shortly.

Thanks for looking into this.
I made a bug report, attaching assets and project settings in case you are having issues reproducing it with that google drive link or would like someone else at Unity to take a look.
IN-100649
Let me know if you would like any other information. For reference, I will be removing that folder link when this is resolved.

Thanks for the repro project! I was able to confirm the issue, and we have a couple of fixes that we should be able to include with the next release (which we’re actively working on). The first is switching to the asynchronous API for convex shapes (in addition to non-convex/“static” meshes, which only have an asynchronous API), and the second is caching the shapes along with the meshes so that we don’t create them anew for each entity. Together, that made a big difference in how long it takes to update.

This should be improved as of the latest release, 2.3.1.

I can confirm that the stutter decreased massively, such that instead of a 6 second stutter with complex 12 assets, PLUS 5 object pool counts per complex asset, there is NOW a decrease to about 3, 200ms stutters. I will make a new thread if other issues arise. Thank you for being active on this so quickly.