Last active
August 29, 2015 14:17
-
-
Save twobob/637fd92aac7e1d578396 to your computer and use it in GitHub Desktop.
FlipNormals on mesh (FlipNormals.cs in Editor Folder)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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