Last active
September 8, 2020 20:04
-
-
Save marcusx2/cebcc5a62284a5b7cd05e91aac91982e to your computer and use it in GitHub Desktop.
Add this to any mesh to flip the normals. Useful to create 360 spheres, etc. Run once and remove the script.
This file contains 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class FlipNormals : MonoBehaviour { | |
// Use this for initialization | |
void Start () { | |
Mesh mesh = this.GetComponent<MeshFilter>().sharedMesh; | |
Vector3[] normals = mesh.normals; | |
for(int i=0;i<normals.Length;i++) | |
{ | |
normals[i] = -1 * normals[i]; | |
} | |
mesh.normals = normals; | |
for(int i=0;i<mesh.subMeshCount;i++) | |
{ | |
int[] tris = mesh.GetTriangles(i); | |
for(int j =0;j<tris.Length;j+=3) | |
{ | |
//swap order of tris vertices | |
int temp = tris[j]; | |
tris[j] = tris[j+1]; | |
tris[j + 1] = temp; | |
} | |
mesh.SetTriangles(tris, i); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment