//Return a unity mesh in a left-handed coordinate system public UnityMesh ToUnityMesh() { UnityMesh result = new UnityMesh(this.Name, this.UniqueIdentifier); Vector3[] vertices = new Vector3[VertexList.Length]; //Flip z component of all Vector3 in vertex list for (int i = 0; i < VertexList.Length; i++) { vertices[i] = new Vector3(VertexList[i].x, VertexList[i].y, -VertexList[i].z); } result.VertexList = vertices; Vector3[] normals = new Vector3[NormalList.Length]; //Flip z component of all Vector3 in normal list for (int i = 0; i < NormalList.Length; i++) { normals[i] = new Vector3(NormalList[i].x, NormalList[i].y, -NormalList[i].z); } result.NormalList = normals; int[] triangles = new int[TriangleList.Length]; //Flip the order of triangle vertices. v0,v1,v2 -> v0,v2,v1. for (int i = 0; i < TriangleList.Length; i += 3) { triangles[i] = TriangleList[i]; triangles[i + 1] = TriangleList[i + 2]; triangles[i + 2] = TriangleList[i + 1]; } result.TriangleList = triangles; return(result); }
//Creates a list of lists with BlenderMeshes. The inner list contains multipe BlenderMeshes for one object with max. 2^16 vertices, because of Unitys limitation. public static List <List <UnityMesh> > createSubmeshesForUnity(List <BlenderMesh> blenderMesh) { List <List <UnityMesh> > result = new List <List <UnityMesh> >(); //Iterate all complete meshes found in file foreach (BlenderMesh completeBlenderMesh in blenderMesh) { UnityMesh completeMesh = completeBlenderMesh.ToUnityMesh(); List <UnityMesh> outterListElement = new List <UnityMesh>(); result.Add(outterListElement); int maxVerts = 6000;//65534; //TODO ? magic number, performs well int positionTriangleList = 0; //Go over the complete triangle list of mesh while (positionTriangleList < completeMesh.TriangleList.Length) { UnityMesh newMesh = new UnityMesh(completeMesh.Name, completeMesh.UniqueIdentifier); //Create submesh outterListElement.Add(newMesh); List <Vector3> vertList = new List <Vector3>(); //Create vertices list for submesh List <Vector3> normList = new List <Vector3>(); //Create normal list for submesh List <int> triangles = new List <int>(); //Create triangle list for submesh int[] findVertexIndex = new int[completeMesh.VertexList.Length]; //for optimization, key: vertex index of complete mesh, value: vertex index of new mesh for (int i = 0; i < findVertexIndex.Length; i++) //memset() faster? { findVertexIndex[i] = -1; } //Write vertex, normal and triangle infos in variables until maxVert is reached or end of TriangleList is reached while (vertList.Count < maxVerts - 3 && positionTriangleList < completeMesh.TriangleList.Length) { for (int i = 0; i < 3; i++) { int indexCurrentVertex = completeMesh.TriangleList[positionTriangleList]; int newIndex = findVertexIndex[indexCurrentVertex]; if (newIndex == -1) { vertList.Add(completeMesh.VertexList[indexCurrentVertex]); normList.Add(completeMesh.NormalList[indexCurrentVertex]); findVertexIndex[indexCurrentVertex] = vertList.Count - 1; triangles.Add(vertList.Count - 1); } else { triangles.Add(newIndex); } positionTriangleList++; } } newMesh.VertexList = vertList.ToArray(); newMesh.NormalList = normList.ToArray(); newMesh.TriangleList = triangles.ToArray(); } } return(result); }