Esempio n. 1
0
        public static HacdConvexHull[] DecomposeToConvexHulls(ulong meshHash, bool useCache, HacdPreset preset, float[] verts, int[] indicies)
        {
            if (verts.Length % 3 != 0)
            {
                throw new InvalidOperationException("Number of verticies must be divisble by 3");
            }

            if (indicies.Length % 3 != 0)
            {
                throw new InvalidOperationException("Number of indicies must be divisble by 3");
            }


            if (IsCacheCandidate(useCache, verts.Length))
            {
                //try cache
                try
                {
                    HacdConvexHull[] cachedHulls;
                    if (MeshingStage.HullCache.TryGetHulls(meshHash, out cachedHulls))
                    {
                        return(cachedHulls);
                    }
                }
                catch (Exception e)
                {
                    m_log.ErrorFormat("[InWorldz.PhysX.HACD] Failure retrieving HACD hulls from cache: {0}: {1}", e, e.Message);
                }
            }

            IntPtr session = Decompose(verts, indicies, verts.Length, indicies.Length, preset.DEFAULT_CC_CONNECT_DIST,
                                       preset.MIN_HULL_COUNT, preset.CONCAVITY, preset.TARGET_TRIANGLES_IN_FULL_MESH, preset.MAX_VERTS_PER_HULL,
                                       true, true, preset.VOLUME_WEIGHT, preset.SMALL_CLUSTER_THRESHOLD);

            if (session == IntPtr.Zero)
            {
                return(null);
            }

            HacdConvexHull[] retHulls;

            try
            {
                int hullCount = GetConvexHullCount(session);

                retHulls = new HacdConvexHull[hullCount];

                for (int hullNum = 0; hullNum < hullCount; ++hullNum)
                {
                    int vertexCount = GetVertexCount(session, hullNum);
                    int indexCount  = GetIndexCount(session, hullNum);

                    float[] hullVerts   = new float[vertexCount];
                    int[]   hullIndexes = new int[indexCount];

                    if (!GetConvexVertsAndIndexes(session, hullNum, hullVerts, hullIndexes))
                    {
                        return(null);
                    }

                    HacdConvexHull hull = new HacdConvexHull
                    {
                        Vertices  = PhysUtil.FloatArrayToVectorArray(hullVerts),
                        _rawVerts = IsCacheCandidate(useCache, verts.Length) ? hullVerts : null,
                        Indicies  = hullIndexes
                    };

                    retHulls[hullNum] = hull;
                }

                //store in cache for later
                if (IsCacheCandidate(useCache, verts.Length))
                {
                    try
                    {
                        MeshingStage.HullCache.CacheHulls(meshHash, retHulls);
                    }
                    catch (Exception e)
                    {
                        m_log.ErrorFormat("[InWorldz.PhysX.HACD] Failure storing HACD results in cache: {0}: {1}", e, e.Message);
                    }
                }

                return(retHulls);
            }
            finally
            {
                FreeSession(session);
            }
        }
Esempio n. 2
0
        public static HacdConvexHull[] DecomposeToConvexHulls(ulong meshHash, bool useCache, HacdPreset preset, float[] verts, int[] indicies)
        {
            if (verts.Length % 3 != 0)
                throw new InvalidOperationException("Number of verticies must be divisble by 3");

            if (indicies.Length % 3 != 0)
                throw new InvalidOperationException("Number of indicies must be divisble by 3");


            if (IsCacheCandidate(useCache, verts.Length))
            {
                //try cache
                try
                {
                    HacdConvexHull[] cachedHulls;
                    if (MeshingStage.HullCache.TryGetHulls(meshHash, out cachedHulls))
                    {
                        return cachedHulls;
                    }
                }
                catch (Exception e)
                {
                    m_log.ErrorFormat("[InWorldz.PhysX.HACD] Failure retrieving HACD hulls from cache: {0}: {1}", e, e.Message);
                }
            }

            IntPtr session = Decompose(verts, indicies, verts.Length, indicies.Length, preset.DEFAULT_CC_CONNECT_DIST,
                preset.MIN_HULL_COUNT, preset.CONCAVITY, preset.TARGET_TRIANGLES_IN_FULL_MESH, preset.MAX_VERTS_PER_HULL,
                true, true, preset.VOLUME_WEIGHT, preset.SMALL_CLUSTER_THRESHOLD);

            if (session == IntPtr.Zero)
            {
                return null;
            }

            HacdConvexHull[] retHulls;

            try
            {
                int hullCount = GetConvexHullCount(session);

                retHulls = new HacdConvexHull[hullCount];

                for (int hullNum = 0; hullNum < hullCount; ++hullNum)
                {
                    int vertexCount = GetVertexCount(session, hullNum);
                    int indexCount = GetIndexCount(session, hullNum);

                    float[] hullVerts = new float[vertexCount];
                    int[] hullIndexes = new int[indexCount];

                    if (!GetConvexVertsAndIndexes(session, hullNum, hullVerts, hullIndexes))
                    {
                        return null;
                    }

                    HacdConvexHull hull = new HacdConvexHull
                    {
                        Vertices = PhysUtil.FloatArrayToVectorArray(hullVerts),
                        _rawVerts = IsCacheCandidate(useCache, verts.Length) ? hullVerts : null,
                        Indicies = hullIndexes
                    };

                    retHulls[hullNum] = hull;
                }

                //store in cache for later
                if (IsCacheCandidate(useCache, verts.Length))
                {
                    try
                    {
                        MeshingStage.HullCache.CacheHulls(meshHash, retHulls);
                    }
                    catch (Exception e)
                    {
                        m_log.ErrorFormat("[InWorldz.PhysX.HACD] Failure storing HACD results in cache: {0}: {1}", e, e.Message);
                    }
                }

                return retHulls;
            }
            finally
            {
                FreeSession(session);
            }
        }