Skip to content

Instantly share code, notes, and snippets.

@yoeven
Created January 30, 2018 15:53
Show Gist options
  • Save yoeven/9ff65c9edbe74b904c1a829edbe24215 to your computer and use it in GitHub Desktop.
Save yoeven/9ff65c9edbe74b904c1a829edbe24215 to your computer and use it in GitHub Desktop.
/*
* The following code is based of this thread: https://answers.unity.com/questions/798510/flat-shading.html
*
* This function flat shades any mesh.
*
*/
using UnityEngine;
public static class FlatShader
{
public static void flatShade(this Mesh mesh)
{
Vector3[] oldVerts = mesh.vertices;
int[] triangles = mesh.triangles;
Vector3[] vertices = new Vector3[triangles.Length];
for (int i = 0; i < triangles.Length; i++)
{
vertices[i] = oldVerts[triangles[i]];
triangles[i] = i;
}
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment