private void openToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); if (dialog.ShowDialog() == DialogResult.OK) { var path = dialog.FileName; var builder = new g3.DMesh3Builder(); var reader = new g3.StandardMeshReader() { MeshBuilder = builder }; g3.IOReadResult result = reader.Read(path, g3.ReadOptions.Defaults); if (result.code == g3.IOCode.Ok) { var meshes = builder.Meshes; _mesh = meshes[0]; DrawMesh(); } } }
public static unsafe void retopoMesh(Mesh mesh, int maxVerts = -1, int maxFaces = -1) { IntPtr?libPtr = null; libPtr = NativeLib.LoadLibrary(ClayxelsPrefs.retopoLib); if (libPtr == IntPtr.Zero) { Debug.Log("Clayxels failed to find " + ClayxelsPrefs.retopoLib + ", please set the correct path to this lib in your ClayxelsPrefs.cs file."); return; } try{ MeshUtils.weldVertices(mesh); IntPtr retopoMeshFuncPtr = NativeLib.GetProcAddress(libPtr.Value, "retopoMesh"); RetopoMeshDelegate retopoMesh = (RetopoMeshDelegate)Marshal.GetDelegateForFunctionPointer(retopoMeshFuncPtr, typeof(RetopoMeshDelegate)); IntPtr getRetopoMeshVertsCountFuncPtr = NativeLib.GetProcAddress(libPtr.Value, "getRetopoMeshVertsCount"); GetRetopoMeshVertsCountDelegate getRetopoMeshVertsCount = (GetRetopoMeshVertsCountDelegate)Marshal.GetDelegateForFunctionPointer(getRetopoMeshVertsCountFuncPtr, typeof(GetRetopoMeshVertsCountDelegate)); IntPtr getRetopoMeshTrisCountFuncPtr = NativeLib.GetProcAddress(libPtr.Value, "getRetopoMeshTrisCount"); GetRetopoMeshTrisCountDelegate getRetopoMeshTrisCount = (GetRetopoMeshTrisCountDelegate)Marshal.GetDelegateForFunctionPointer(getRetopoMeshTrisCountFuncPtr, typeof(GetRetopoMeshTrisCountDelegate)); IntPtr getRetopoMeshFuncPtr = NativeLib.GetProcAddress(libPtr.Value, "getRetopoMesh"); GetRetopoMeshDelegate getRetopoMesh = (GetRetopoMeshDelegate)Marshal.GetDelegateForFunctionPointer(getRetopoMeshFuncPtr, typeof(GetRetopoMeshDelegate)); Vector3[] vertsArray = mesh.vertices; int[] indices = mesh.triangles; Color[] colors = mesh.colors; fixed(Vector3 *vertsPtr = vertsArray) { fixed(int *indicesPtr = indices) { fixed(Color *colorsPtr = colors) { retopoMesh((IntPtr)vertsPtr, mesh.vertices.Length, (IntPtr)indicesPtr, indices.Length, (IntPtr)colorsPtr, maxVerts, maxFaces); } } } int newVertsCount = getRetopoMeshVertsCount(); int newTrisCount = getRetopoMeshTrisCount(); Vector3[] newVerts = new Vector3[newVertsCount]; int[] newIndices = new int[newTrisCount]; Vector3[] normals = new Vector3[newVertsCount]; Color[] newColors = new Color[newVertsCount]; fixed(Vector3 *vertsPtr = newVerts) { fixed(int *indicesPtr = newIndices) { fixed(Vector3 *normalsPtr = normals) { fixed(Color *newColorsPtr = newColors) { getRetopoMesh((IntPtr)vertsPtr, (IntPtr)indicesPtr, (IntPtr)normalsPtr, (IntPtr)newColorsPtr); } } } } mesh.Clear(); mesh.vertices = newVerts; mesh.triangles = newIndices; mesh.normals = normals; mesh.colors = newColors; g3.DMesh3 d3mesh = g3UnityUtils.UnityMeshToDMesh(mesh); g3.MeshBoundaryLoops loopsFinder = new g3.MeshBoundaryLoops(d3mesh); for (int i = 0; i < loopsFinder.Count; ++i) { gs.MinimalHoleFill filler = new gs.MinimalHoleFill(d3mesh, loopsFinder.Loops[i]); filler.Apply(); } Mesh newMesh = g3UnityUtils.DMeshToUnityMesh(d3mesh); mesh.Clear(); mesh.vertices = newMesh.vertices; mesh.normals = newMesh.normals; mesh.triangles = newMesh.triangles; mesh.colors = newMesh.colors; mesh.Optimize(); mesh.RecalculateNormals(); } catch { Debug.Log("Clayxels: failed to perform retopology"); } NativeLib.FreeLibrary(libPtr.Value); libPtr = null; }