Пример #1
0
        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;
        }
        public static unsafe void retopoMesh(Mesh mesh, int maxVerts = -1, int maxFaces = -1)
        {
            IntPtr?libPtr = null;

                        #if CLAYXELS_RETOPO_LIB
            libPtr = NativeLib.LoadLibrary(CLAYXELS_RETOPO_LIB);
                        #else
            libPtr = NativeLib.LoadLibrary("Assets\\Clayxels\\meshUtils\\RetopoLib.dll");
                        #endif

            if (libPtr == null)
            {
                Debug.Log("failed to find Assets\\Clayxels\\meshUtils\\RetopoLib.dll, please define CLAYXELS_RETOPO_LIB with the actual path to this lib in your project");

                return;
            }

            try{
                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;
            }
            catch {
                Debug.Log("error during retopo");
            }

            NativeLib.FreeLibrary(libPtr.Value);
            libPtr = null;
        }