/// <summary> /// Compile a UnityEngine.Mesh from a ProBuilderMesh. /// </summary> /// <param name="probuilderMesh">The mesh source.</param> /// <param name="targetMesh">Destination UnityEngine.Mesh.</param> /// <param name="preferredTopology">If specified, the function will try to create topology matching the reqested format (and falling back on triangles where necessary).</param> /// <returns>The resulting material array from the compiled faces array. This is suitable to assign to the MeshRenderer.sharedMaterials property.</returns> public static void Compile(ProBuilderMesh probuilderMesh, Mesh targetMesh, MeshTopology preferredTopology = MeshTopology.Triangles) { if (probuilderMesh == null) { throw new ArgumentNullException("probuilderMesh"); } if (targetMesh == null) { throw new ArgumentNullException("targetMesh"); } targetMesh.Clear(); targetMesh.vertices = probuilderMesh.positionsInternal; targetMesh.uv = probuilderMesh.texturesInternal; if (probuilderMesh.HasArrays(MeshArrays.Texture2)) { List <Vector4> uvChannel = new List <Vector4>(); probuilderMesh.GetUVs(2, uvChannel); targetMesh.SetUVs(2, uvChannel); } if (probuilderMesh.HasArrays(MeshArrays.Texture3)) { List <Vector4> uvChannel = new List <Vector4>(); probuilderMesh.GetUVs(3, uvChannel); targetMesh.SetUVs(3, uvChannel); } targetMesh.normals = probuilderMesh.GetNormals(); targetMesh.tangents = probuilderMesh.GetTangents(); if (probuilderMesh.HasArrays(MeshArrays.Color)) { targetMesh.colors = probuilderMesh.colorsInternal; } var materialCount = probuilderMesh.GetComponent <Renderer>().sharedMaterials.Length; var submeshes = Submesh.GetSubmeshes(probuilderMesh.facesInternal, materialCount, preferredTopology); targetMesh.subMeshCount = submeshes.Length; for (int i = 0; i < targetMesh.subMeshCount; i++) { targetMesh.SetIndices(submeshes[i].m_Indexes, submeshes[i].m_Topology, i, false); } targetMesh.name = string.Format("pb_Mesh{0}", probuilderMesh.id); }