Skip to content

Instantly share code, notes, and snippets.

@marcusx2
Last active September 8, 2020 20:04
Show Gist options
  • Save marcusx2/cebcc5a62284a5b7cd05e91aac91982e to your computer and use it in GitHub Desktop.
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.
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