Skip to content

Instantly share code, notes, and snippets.

@SixWays
Created November 13, 2016 20:20
Show Gist options
  • Save SixWays/203154eea230904a13b8dc1cd6adb637 to your computer and use it in GitHub Desktop.
Save SixWays/203154eea230904a13b8dc1cd6adb637 to your computer and use it in GitHub Desktop.
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ImportFlatShadedMesh : AssetPostprocessor {
void OnPostprocessModel(GameObject g){
Mesh m = g.GetComponentInChildren<MeshFilter>().sharedMesh;
Vector3[] oldVertices = m.vertices;
Vector3[] newVertices = new Vector3[m.triangles.Length];
Vector2[] newUvs1 = new Vector2[newVertices.Length];
Vector2[] newUvs2 = new Vector2[newVertices.Length];
int uv1l = m.uv.Length;
int uv2l = m.uv2.Length;
// Loop over submeshes
int offset = 0;
int[][] submeshes = new int[m.subMeshCount][];
for (int s=0; s<m.subMeshCount; ++s){
submeshes[s] = m.GetIndices(s);
for(int i = 0; i < submeshes[s].Length; i++) {
newVertices[i+offset] = oldVertices[submeshes[s][i]];
if (uv1l<=submeshes[s][i]){
newUvs1[i+offset] = new Vector2(0,0);
} else {
newUvs1[i+offset] = m.uv[submeshes[s][i]];
}
if (uv2l<=submeshes[s][i]){
newUvs2[i+offset] = new Vector2(0,0);
} else {
newUvs2[i+offset] = m.uv2[submeshes[s][i]];
}
submeshes[s][i] = i+offset;
}
offset += submeshes[s].Length;
}
// re-assign to mesh
m.vertices = newVertices;
for (int i=0; i<submeshes.Length; ++i){
m.SetTriangles(submeshes[i],i);
}
m.uv = newUvs1;
m.uv2 = newUvs2;
m.RecalculateNormals();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment