# 2018 Dynamic MeshCollider Solution For Large Meshes and Fluid Dynamic

Discussion in 'Physics' started by pan-master, Jul 8, 2018.

1. ### pan-master

Joined:
Nov 11, 2013
Posts:
120
This post is created to solve a problem of dynamic large MeshColliders with fluid collisions
Questions
1.Static MeshColiders are creatd from the shareMesh, Where are the collision calculations being executed? CPU vs GPU?( maybe it depends?, some can Use CPU as Rigidbodies, and some can be calculated on GPUs using cuda/uFlex?

2. What is the Maximum limit of tringles for a mesh collider?(255)?
3. Where do the deformatnios of SkinnedMesh take place. CPU or GPU?
4. if Deformations happen on GPU, can We Pull Deformed SharedMesh from GPU to Update MeshCollider.shardMesh?
5. If we calculate collisions on the GPU using cuda. would it be possible to copy shardMesh data within GPU to update on GPU MeshCollider Data?, without pulling back and forward data between GPU,CPU?,
6. If the limit of polygons for MeshCollider is 255, Would it be posible, to create secondary Set of Meshes,Each 255, that would fallow and deform mapped parts of main Large Mesh?
7. How woud we map secondary Meshes to fallow parts of large Main Skinned Deforming Mesh
8.Any ideas for different solutions?

2. ### Edy

Joined:
Jun 3, 2010
Posts:
1,336
I can answer a few of these:

Physics run on CPU only.

Convex mesh colliders have a limit of 255 triangles. Note that this doesn't apply to the original mesh. When you mark a mesh collider as Convex a convex-hull is computed out of the original mesh. The resulting mesh is limited to 255 triangles, no matter the number of triangles in the original mesh.

Non-convex mesh colliders have the same limits as regular meshes (65535 triangles, but objects are split automatically by Unity upon import). However, the less triangles the more efficient collisions. Also, it's highly recommended to split large mesh colliders in smaller colliders because collisions involve calculations using the bounding boxes.

Again, this is different among Convex and non-convex mesh colliders:

Convex mesh colliders are limited to 255 triangles, but you may split your collider in smaller colliders in order to resemble non-convex shapes. When you modify the sharedMesh of a Convex collider then a new convex-hull is calculated out of the new mesh data. Note that a Convex collider with less than 255 triangles may result in a convex collider with more than 255 triangles after being modified, even having modified the vertices only in the original mesh. It depends on the resulting convex shape.

Non-convex mesh colliders may be modified straight away and the collisions will result as expected.

3. ### pan-master

Joined:
Nov 11, 2013
Posts:
120

Code (CSharp):
1.  [Range(0.005f,1)]
2.     public float RateOfUpdate = 0;
3.     MeshCollider meshColider;
4.     SkinnedMeshRenderer skinnedMeshRenderer;
5.     Mesh bakedMesh;
6.     MeshFilter meshFilter;
7.     // Use this for initialization
8.     void Start () {
9.         skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();
10.         meshFilter = GetComponent<MeshFilter>();
11.         meshColider = GetComponent<MeshCollider>();
12.
13.         bakedMesh = new Mesh();
14.
15.         InvokeRepeating("BakeNewShapeForMeshCol", 1,RateOfUpdate);
17.     }
18.
20.
21.         meshColider.sharedMesh = bakedMesh;
22.         meshColider.sharedMesh.RecalculateBounds();
23.         //MF.sharedMesh = bMesh;
24.
25.     }
26.     void BakeNewShapeForMeshCol()
27.     {
28.        skinnedMeshRenderer.BakeMesh(bakedMesh);
29.
30.         meshFilter.mesh = bakedMesh;
31.         meshFilter.mesh.RecalculateBounds();
32.     }
33. }
34.
Fluid Dynamic Solution Might come from uFlex

Last edited: Jul 8, 2018
4. ### pan-master

Joined:
Nov 11, 2013
Posts:
120
Now the Question is, Why is it so slow for bigger meshes, Are these calculations being done on CPU? I thought that this sort of Physic could be done on GPU