92 lines
3.3 KiB
C#
92 lines
3.3 KiB
C#
//original code by Bunny83 from https://answers.unity.com/questions/1213025/separating-submeshes-into-unique-meshes.html
|
|
namespace NOT_Lonely
|
|
{
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
|
|
public static class MeshExtension
|
|
{
|
|
private class Vertices
|
|
{
|
|
List<Vector3> verts = null;
|
|
List<Vector2> uv1 = null;
|
|
List<Vector3> normals = null;
|
|
List<Vector4> tangents = null;
|
|
List<Color32> colors = null;
|
|
|
|
public Vertices()
|
|
{
|
|
verts = new List<Vector3>();
|
|
}
|
|
public Vertices(Mesh aMesh)
|
|
{
|
|
verts = CreateList(aMesh.vertices);
|
|
uv1 = CreateList(aMesh.uv);
|
|
normals = CreateList(aMesh.normals);
|
|
tangents = CreateList(aMesh.tangents);
|
|
colors = CreateList(aMesh.colors32);
|
|
}
|
|
|
|
private List<T> CreateList<T>(T[] aSource)
|
|
{
|
|
if (aSource == null || aSource.Length == 0)
|
|
return null;
|
|
return new List<T>(aSource);
|
|
}
|
|
private void Copy<T>(ref List<T> aDest, List<T> aSource, int aIndex)
|
|
{
|
|
if (aSource == null)
|
|
return;
|
|
if (aDest == null)
|
|
aDest = new List<T>();
|
|
aDest.Add(aSource[aIndex]);
|
|
}
|
|
public int Add(Vertices aOther, int aIndex)
|
|
{
|
|
int i = verts.Count;
|
|
Copy(ref verts, aOther.verts, aIndex);
|
|
Copy(ref uv1, aOther.uv1, aIndex);
|
|
Copy(ref normals, aOther.normals, aIndex);
|
|
Copy(ref tangents, aOther.tangents, aIndex);
|
|
Copy(ref colors, aOther.colors, aIndex);
|
|
return i;
|
|
}
|
|
public void AssignTo(Mesh aTarget)
|
|
{
|
|
if (verts.Count > 65535)
|
|
aTarget.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
|
|
aTarget.SetVertices(verts);
|
|
if (uv1 != null) aTarget.SetUVs(0, uv1);
|
|
if (normals != null) aTarget.SetNormals(normals);
|
|
if (tangents != null) aTarget.SetTangents(tangents);
|
|
if (colors != null) aTarget.SetColors(colors);
|
|
}
|
|
}
|
|
|
|
public static Mesh GetSubmesh(this Mesh aMesh, int aSubMeshIndex)
|
|
{
|
|
if (aSubMeshIndex < 0 || aSubMeshIndex >= aMesh.subMeshCount)
|
|
return null;
|
|
int[] indices = aMesh.GetTriangles(aSubMeshIndex);
|
|
Vertices source = new Vertices(aMesh);
|
|
Vertices dest = new Vertices();
|
|
Dictionary<int, int> map = new Dictionary<int, int>();
|
|
int[] newIndices = new int[indices.Length];
|
|
for (int i = 0; i < indices.Length; i++)
|
|
{
|
|
int o = indices[i];
|
|
int n;
|
|
if (!map.TryGetValue(o, out n))
|
|
{
|
|
n = dest.Add(source, o);
|
|
map.Add(o, n);
|
|
}
|
|
newIndices[i] = n;
|
|
}
|
|
Mesh m = new Mesh();
|
|
dest.AssignTo(m);
|
|
m.triangles = newIndices;
|
|
return m;
|
|
}
|
|
}
|
|
} |