Skip to content

Instantly share code, notes, and snippets.

@twobob
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twobob/637fd92aac7e1d578396 to your computer and use it in GitHub Desktop.
Save twobob/637fd92aac7e1d578396 to your computer and use it in GitHub Desktop.
FlipNormals on mesh (FlipNormals.cs in Editor Folder)
using UnityEngine;
using System.Collections;
using UnityEditor;
public class FlipNormals
{
[MenuItem("Tools/Flip Normals")]
static void FlipMeshNormals()
{ // Flip Mesh Normals on selected mesh(es)
FlipInit();
}
private static void FlipInit()
{
foreach (GameObject go in Selection.gameObjects)
{
MeshFilter filter = go.GetComponent(typeof(MeshFilter)) as MeshFilter;
FlipNormalMethod(ref filter);
}
}
private static void FlipNormalMethod(ref MeshFilter filter)
{
if (filter != null)
{
Mesh mesh = filter.sharedMesh;
Vector3[] normals = mesh.normals;
for (int i = 0; i < normals.Length; i++)
normals[i] = -normals[i];
mesh.normals = normals;
for (int m = 0; m < mesh.subMeshCount; m++)
{
int[] triangles = mesh.GetTriangles(m);
for (int i = 0; i < triangles.Length; i += 3)
{
int temp = triangles[i + 0];
triangles[i + 0] = triangles[i + 1];
triangles[i + 1] = temp;
}
mesh.SetTriangles(triangles, m);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment