View Source on Github
The collision calculations are being performed by a Unity Compute Shader. A Unity Command Buffer is used to dispatch the compute shader and the subsequent render commands. In my implementation, no per voxel data is transferred between the GPU and the CPU at runtime, and no C# code is executed at runtime. Voxel forces are modeled by 8 particles per cube - 1 per corner. Every frame, the Compute Shader computes and sums the particle forces to calculate every cubes' position and velocity. Performance will likely be improved through research and optimization of the per particle Kernels within the Compute Shader. The compute shader's implementation is based off of Takahiro Harada's Chapter in GPU Gems 3: Real-Time Rigid Body Simulation on the GPU.
After the physics step, rendering takes place without copying the position and rotation data onto the CPU by using Unity's Command Buffer version of DrawMeshInstancedIndirect. At the time of writing this API is brand new and seems to have some bugs. For example, I cannot seem to render anything using this method on a Mac. Nothing renders when the Camera's clear mode is set to Solid Color (the image above is Skybox). Some of these bugs can be resolved by excluding rendering from the command buffer and rendering using Graphics version of DrawMeshInstancedIndirect instead. Despite these bugs, I am very thankful to Unity for providing these APIs and empowering my creative development.
I'm hoping that by open sourcing this code, it can be used as an example of cutting edge, efficient GPU use in Unity.
There are likely errors in the application of the damping and tangential forces in the collision resolution step of the compute shader, and in the integration of angular momentum, and also perhaps in the worldToObject matrix calculation in the Surface Shader. I have a limited understanding of all of these areas, and cannot confirm their correctness. If you find errors in those places, please let me know.