- Home /
Mesh.CombineMeshes flips normals
I am trying to use the following example script to combine small parts of a mesh into one, so I am able to make a dynamic terrain: http://unity3d.com/support/documentation/ScriptReference/Mesh.CombineMeshes.html.
The problem is that it flips the normals of some of the meshes. How come and how to solve this?
The following image shows that the normals of the edges of my model are flipped, while some of the edges are still ok. These blocks all exist out of a center plane, corners and edges.
What it should be:
private static void CombineMesh(GameObject iceBerg)
{
MeshFilter[] meshes = iceBerg.GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshes.Length];
for (int i = 0; i < meshes.Length; i++)
{
combine[i].mesh = meshes[i].sharedMesh;
combine[i].transform = meshes[i].transform.localToWorldMatrix;
meshes[i].gameObject.active = false;
}
MeshFilter meshFilter = iceBerg.AddComponent<MeshFilter>();
meshFilter.mesh = new Mesh();
meshFilter.mesh.CombineMeshes(combine);
MeshRenderer renderer = iceBerg.AddComponent<MeshRenderer>();
renderer.material = (Material)Resources.Load("Materials/GridIce");
MeshCollider col = iceBerg.AddComponent<MeshCollider>();
col.smoothSphereCollisions = true;
}
Hmm it's hard to tell what's going wrong here. Can you add a screenshot of the same view before you combine the meshes so you have a reference how it should look like?
By "flipped normals" do you refer to front / back sides are flipped? Or do you talk about the vertex normals (used for lighting)?
I added another picture. As you can see, the front and back is flipped. Which means that the normals are flipped. The top and sideskirts are not attached to one another in the original model. So both top and side have their own vertex normals. I used an edge split modifier in blender to achieve that.
There is nothing flipped. It looks like there are some triangles missing. What's the vertex and triangle count of the meshes? Unity use just a 16 bit index buffer, so there can be only 65536 vertices in a single mesh.
Also what shader do you use?
$$anonymous$$aybe I should update my images. I hoped that it would be clear from these images. But I am certain that the normals are flipped. Looking the this object from the other side shows exactly the missing faces. I will try to make a better image later this week.
I've had issues in the past with n-gon geometry not getting triangulated properly, and subsequently not rendering. Is your mesh triangulated pre-unity import, or are you allowing unity to triangulate it?
Answer by Marnix · Apr 18, 2012 at 07:21 PM
I have found the problem! It is a bug in Unity and I will probably contact them about it.
The objects that are flipped in my scene are flipped because of a negative scale that I use to reuse the model multiple times. I avoided this by making a mirrored version of the object in Blender.
Uhmm in which way is this a bug? If you use negative scaling values the faces have to flip. A mesh always have only one front side, if you use a negative scale all points are projected to the opposite side but the renderorder will stay the same so the faces are flipped.
Answer by kaz2057 · Jan 23, 2013 at 11:51 AM
Any news about any fix? Anybody can help me to insert a check inverted scale on game object and its parent to have correct normals on triangle strip?
Please contact me becausa I have thousand combined geometry and it not possible mirror them!
Thanks
Just avoid scaling inside Unity in general. negative scaling (especially on only one axis) will cause a lot more problems than that. For example rotating the object might flip the faces on certain angles.
I had to instruct our artists several times to reset all transformations before they export the model.
The models are exported in 0,0,0 in world but the problem is I create big shapes welding thounsands of little modulared objects. To save memory I use 4 singular fbx mesh duplicated and scaled on -1 to create custom bigger shape. Any solution?