- Home /
Merging meshes at runtime - upside down normals?
Hello,
I am creating a side scrolling game that will "randomly" generate the terrain as it goes, and then the user will kick a ball and it will bounce along until it comes to a stop. I put randomly in quotation marks as it isn't completely random, it chooses from a select group of meshes which to copy and place at the end.
Each of my meshes ends on a slight flat section, to allow smooth joining. When I align the planes together, sometimes when the ball lands at the join it created unexpected results, like the ball bouncing backwards. (i tried this using meshes of 3d objects, and changed to a flat plane as i thought it may fix it, but it doesn't but it cut the verts down to around 10% so i kept them!)
Mesh Merging
Next I merged the meshes on the fly (the 5 closest MeshFilters to the ball are combined and set to a MeshCollider of a seperate game object) this all works fine but again I was still getting the unexpected results. SOOOOOO... I then tried to strip out the overlapping vertices from the mesh after the combine, and then ran Mesh.RecalculateBounds to update it.
List<Vector3> newPoints = new List<Vector3>();
for( int i = 0; i < meshVerticeCount; i++)
{
Vector3 point = meshToSort.vertices[i];
bool AddPoint = true;
for( int j = 0; j < newPoints.Count; j++)
{
if( point == newPoints[j])
{
AddPoint = false;
}
}
if(AddPoint) newPoints.Add(point);
}
Vector3[] verticeArray = new Vector3[ newPoints.Count ];
for( int k = 0; k < newPoints.Count; k++)
{
verticeArray[k] = newPoints[k];
}
meshToSort.Clear();
meshToSort.vertices = verticeArray;
meshToSort.RecalculateNormals();
meshToSort.RecalculateBounds();
When I Debug.Log the count before and after the this method is called, It is removing the correct number of vertices, (2 overlaping per plane, exept the last) and the mesh all appears fine, but im still getting these odd bounces.
Just to throw another spanner in the works, I noticed that now it doesnt just happen where the meshes join, but anywhere!! If I bounce a ball at a plane, 9/10 it'll act as expected but 10% of the time it'll bounce backwards.
Is it possible that the normals of one or more of the triangles could be upside down to cause this? is my method of removing over lapping vetrices too easy to be true, should i be linking up the trianlges to the missing points or is all of that calculated automatically?
Any help on this would be greatly appreciated as this has been 2 days of hair pulling development and it's driving me mad!
Cheers in advance!
Well recalculating the normals will use the winding order (which shouldn't have changed) and then work out the normal based on the differences between the planes on the two sides - you can check if this is working a you expect by shining a light on it and turning it around - I expect it will be fine. I'm wondering if it's the physics and collision detection - how do you have that configured?
All the physics settings are as unity standards the $$anonymous$$aterials i've used all values are set to 0 except Dynamic frication 0.7 and bounciness 0.65 Thank you for your time!