- Home /
Any script to 'fix' normals/winding order?
I was wondering if anyone knows of an editor script which would go through a mesh and compare the normal direction(s) to the triangle vertex winding order, and fix them to match? Reason is, I got some crazy models from sketchup and I know the verts and tris are there, but some face the wrong way. So I wanna run like a sanity-check on them, either flip the order, or flip the normals. Probably flip the order, cuz I think the normals are correct.
Answer by DaveA · Jun 08, 2013 at 07:59 PM
This will flip the triangles. I'll leave it to you to pick one and compare the normals and fix them they way you want.
@MenuItem("GameObject/Flip Mesh", false, 4)
static function FlipMesh()
{
Undo.RegisterSceneUndo ("Flip Mesh");
var trs = Selection.GetTransforms (SelectionMode.Deep);
for (var tr in trs)
{
var r : MeshFilter = tr.gameObject.GetComponent.<MeshFilter>();
if (r)
{
var m : Mesh = r.sharedMesh;
if (m)
{
var tris : int[] = m.triangles;
for (var i : int = 0; i< tris.Length; i+= 3)
{
var t : int = tris[i];
tris[i] = tris[i+1];
tris[i+1] = t;
}
m.triangles = tris;
}
}
}
}
Answer by ScroodgeM · Jul 14, 2012 at 01:24 PM
it's not easy for script to detect right face direct automatically
'calculate normals' in import settings for model can fix problem with incorrect normals.