FBX Import Settings on Mesh Assets
From the Project window, clicking on an FBX file brings up the Import Settings in the Inspector panel. In particular, I want to use Optimize Mesh, Weld Vertices and Calculate Normals, which all make my meshes more efficient.
Meshes can also exist in the filesystem as .asset files, but clicking on these brings up a mostly blank Inspector panel with no import options.
So, how do I use the Optimize Mesh / Weld Vertices / Calculate Normals utils on meshes saved as .asset files?
Are those utilities accessible in the Unity API?
Can asset meshes be repackaged back to FBX?
Does anyone have source code for those functions?
Answer by sarahnorthway · May 24, 2017 at 04:21 AM
Optimize Mesh is: UnityEditor.MeshUtility.Optimize()
Calculate Normals is: Mesh.RecalculateNormals(). It doesn't include smoothing angle option and doesn't change the number of vertices, but Unwrapping.GenerateSecondaryUVSet() does. Though I think it only adds vertices and won't remove them. So first you need:
Weld Vertices I tweaked from a couple sources:
public static void WeldVertices(Mesh mesh, float threshold = float.Epsilon) {
Vector3[] oldVertices = mesh.vertices;
Color[] oldColors = mesh.colors;
Vector3[] newVertices = new Vector3[oldVertices.Length];
Color[] newColors = new Color[oldVertices.Length];
int[] old2new = new int[oldVertices.Length];
int newSize = 0;
// make new vertices with no duplicates
for (int i = 0; i < oldVertices.Length; i++) {
bool foundMatch = false;
for (int j = 0; j < newSize; j++) {
if (Vector3.SqrMagnitude(newVertices[j] - oldVertices[i]) < threshold) {
if (oldColors.Length >= i + 1 && newColors[j] == oldColors[i]) {
old2new[i] = j;
foundMatch = true;
}
}
}
if (!foundMatch) {
// Add new vertex
newVertices[newSize] = oldVertices[i];
if (oldColors.Length >= i + 1) {
newColors[newSize] = oldColors[i];
}
old2new[i] = newSize;
newSize++;
}
}
// make new triangles
int[] oldTris = mesh.triangles;
int[] newTris = new int[oldTris.Length];
for (int i = 0; i < oldTris.Length; i++) {
newTris[i] = old2new[oldTris[i]];
}
Vector3[] finalVertices = new Vector3[newSize];
Color[] finalColors = new Color[newSize];
for (int i = 0; i < newSize; i++) {
finalVertices[i] = newVertices[i];
finalColors[i] = newColors[i];
}
mesh.Clear();
mesh.vertices = finalVertices;
mesh.colors = finalColors;
mesh.triangles = newTris;
mesh.uv = null;
mesh.uv2 = null;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612121904im_/https://answers.unity.com/themes/thub/images/avi.jpg)