Real-time physics baking problem

Short intro:

I’ve written a prototype of minecraft-alike terrain generator. Terrain is plain-voxel-based - no marching cubes/etc stuff. It’s divided in 32x32 chunks (every chunk contains 32x32 matrix of RLE-compressed 1-dim voxel arrays) and generated on-the-fly. Every single chunk is polygonized into one or more meshes (to keep verts number less than 64k). A chunk gets polygonized and added to the scene as soon as distance between camera and that chunk is less than ‘view distance’. I’ve tested it with distances up to 16 chunks.
Most of calculations are performed in separate threads and all heap object are kept in object pools, so there were no lags.

The problem:

The problem is in physics. Physics is based on MeshColliders attached to each game object (i.e. mesh). As those meshes are terrain - colliders have to be static. So here is the problem - adding that many static mesh colliders takes 12 ms every frame in average. Once they’re added - everything is ok and is running smoothly at 80+ fps until new pack of terrain meshes are to be added to scene. Profiler shows that this ~12 ms is spent on ‘physics baking’.

The question:

Is there a way to ‘bake physics’ outside the main thread?

Maybe some other way to minimise impact of physics baking?

The only solution that I can think of now is to write my own ‘physics’ in separate thread, but I don’t really want to.

No, there’s currently no way to do that (unless you have a source code license in which case you could modify the engine as you wish ^^ Though such a license is quite expensive probably 50k+)

The only alternative would be to roll your own physics based on your own structures. Hopefully Unity opens up more interfaces to it’s underlying subsystems in the future.

You may want to head over to feedback.unity3d.com and see if someone has already such a request and vote for it. If that request doesn’t exist yet, feel free to create one and post a link to it here in a comment so others can vote for it. If you post a feature request make sure it’s a concrete request, not just “We want more access to Unity internals”. The more concrete a request is, the easier (and more likely) it is to implement for UT.

There is physics.bakemesh…

but i’m having a problem with it, if i rebake a mesh it don’t work and still bake it while putting it in the mesh collider, so i need to make a new mesh each time, but it’s making garbage…