} // ========================================================================================================================== bool CountVertices(SingleMeshes sortedMesh) { totalVertices = 0; totalTriangles = 0; startIndex = splitIndex; bool split = false; for (int i = splitIndex; i < sortedMesh.meshes.Count; ++i) { MeshInfo meshInfo = sortedMesh.meshes[i]; // meshInfo.RemoveGeometry(); Mesh mesh = meshInfo.mesh; if (totalVertices + mesh.vertexCount > _vertexOutputLimit) { splitIndex = i; split = true; break; } totalVertices += mesh.vertexCount; totalTriangles += (int)mesh.GetIndexCount(0); } if (!split) { splitIndex = sortedMesh.meshes.Count; } // if (totalTriangles != totalTriangleCount) { newTriangles = new int[totalTriangles]; } // Debug.Log (totalVertices); return(split); } // ==========================================================================================================================
} // ========================================================================================================================== public void CombineMeshes(SingleMeshes sortedMesh, Vector3 center) { totalCombined += sortedMesh.meshes.Count; splitIndex = 0; totalVertexCount = 0; totalTriangleCount = 0; bool split; int count = 0; do { split = CountVertices(sortedMesh); CombineMesh(sortedMesh, center); CreateMesh(null, combinedGO.transform, sortedMesh, center, 0, false, count++); }while (split); splitIndex = 0; } // ==========================================================================================================================
} // ========================================================================================================================== MeshRenderer CreateMesh(Transform t, Transform parent, SingleMeshes sortedMesh, Vector3 center, int matIndex, bool rotate, int meshIndex) { string name; if (t != null) { name = (t.name); } else { name = (sortedMesh.mat.name); } GameObject go = new GameObject(name); Transform newT = go.transform; newT.parent = parent; newT.position = center; if (rotate) { newT.rotation = t.rotation; } MeshFilter mf = go.AddComponent <MeshFilter>() as MeshFilter; Mesh mesh = new Mesh(); mesh.name = name + "_" + meshIndex; MeshRenderer mr = go.AddComponent <MeshRenderer>(); // Material[] sharedMats = t.GetComponent<MeshRenderer>().sharedMaterials; //if (matIndex > sharedMats.Length - 1) //{ // UnityEditor.Selection.activeTransform = t; // Debug.Log(matIndex + " " + sharedMats.Length); //} if (t != null) { mr.sharedMaterial = t.GetComponent <MeshRenderer>().sharedMaterials[matIndex]; } else { mr.sharedMaterial = sortedMesh.mat; } // mr.motionVectorGenerationMode = MotionVectorGenerationMode.Camera; mesh.SetVertices(newVertices); mesh.SetTriangles(newTriangles, 0); mesh.SetTangents(newTangents); mesh.SetNormals(newNormals); mesh.SetUVs(0, newUvs1); if (hasUv2) { mesh.SetUVs(1, newUvs2); } if (hasUv3) { mesh.SetUVs(2, newUvs3); } if (hasUv4) { mesh.SetUVs(3, newUvs4); } if (hasColors) { mesh.SetColors(newColors); } mf.sharedMesh = mesh; if (addMeshColliders) { go.AddComponent <MeshCollider>(); } go.AddComponent <GarbageCollectMesh>(); if (rotate) { combinedMeshList.Add(new CachedGameObject(go, newT, mr, mf)); } return(mr); } // ==========================================================================================================================
void CombineMesh(SingleMeshes sortedMesh, Vector3 center) { totalVertexCount = 0; totalTriangleCount = 0; ClearNewLists(); for (int count = startIndex; count < splitIndex; ++count) { MeshInfo meshInfo = sortedMesh.meshes[count]; Transform t = meshInfo.t; Vector3 pos = t.position; Quaternion rotation = t.rotation; Vector3 scale = t.lossyScale; Vector3 newPos = pos - center; Mesh mesh = meshInfo.mesh; mesh.GetVertices(vertices); mesh.GetTriangles(triangles, 0); mesh.GetNormals(normals); mesh.GetTangents(tangents); hasUv2 = hasUv3 = hasUv4 = hasColors = false; vertexCount = vertices.Count; triangleCount = triangles.Count; mesh.GetUVs(0, uvs1); mesh.GetUVs(1, uvs2); mesh.GetUVs(2, uvs3); mesh.GetUVs(3, uvs4); mesh.GetColors(colors); if (uvs2.Count > 0) { hasUv2 = true; } if (uvs3.Count > 0) { hasUv3 = true; } if (uvs4.Count > 0) { hasUv4 = true; } if (colors.Count > 0) { hasColors = true; } for (int i = 0; i < vertexCount; ++i) { Vector3 vertexPos = t.TransformPoint(vertices[i]) - pos; newVertices.Add(vertexPos + newPos); newNormals.Add(rotation * normals[i]); Vector4 newTangent = rotation * tangents[i]; newTangent.w = tangents[i].w; newTangents.Add(newTangent); newUvs1.Add(uvs1[i]); if (hasUv2) { newUvs2.Add(uvs2[i]); } if (hasUv3) { newUvs3.Add(uvs3[i]); } if (hasUv4) { newUvs4.Add(uvs4[i]); } if (hasColors) { newColors.Add(colors[i]); } } for (int i = 0; i < triangleCount; ++i) { newTriangles.Add(triangles[i] + totalVertexCount); } totalVertexCount += vertexCount; totalTriangleCount += triangleCount; } } // ==========================================================================================================================