public void BuildRegions()
        {
            AstarProfiler.StartProfile("Build Regions");

            int w = voxelArea.width;
            int d = voxelArea.depth;

            int wd = w * d;

            int expandIterations = 8;

            int spanCount = voxelArea.compactSpanCount;


            //new List<int>(1024);
            //List<int> visited = new List<int>(1024);



#if ASTAR_RECAST_BFS
            ushort[] srcReg = voxelArea.tmpUShortArr;
            if (srcReg.Length < spanCount)
            {
                srcReg = voxelArea.tmpUShortArr = new ushort[spanCount];
            }
            Pathfinding.Util.Memory.MemSet <ushort> (srcReg, 0, sizeof(ushort));
#else
            List <int> stack = Pathfinding.Util.ListPool <int> .Claim(1024);

            ushort[] srcReg  = new ushort[spanCount];
            ushort[] srcDist = new ushort[spanCount];
            ushort[] dstReg  = new ushort[spanCount];
            ushort[] dstDist = new ushort[spanCount];
#endif
            ushort regionId = 2;
            MarkRectWithRegion(0, borderSize, 0, d, (ushort)(regionId | BorderReg), srcReg);        regionId++;
            MarkRectWithRegion(w - borderSize, w, 0, d, (ushort)(regionId | BorderReg), srcReg);        regionId++;
            MarkRectWithRegion(0, w, 0, borderSize, (ushort)(regionId | BorderReg), srcReg);        regionId++;
            MarkRectWithRegion(0, w, d - borderSize, d, (ushort)(regionId | BorderReg), srcReg);        regionId++;



#if ASTAR_RECAST_BFS
            uint level = 0;

            List <Int3> basins = Pathfinding.Util.ListPool <Int3> .Claim(100);          //new List<Int3>();

            // Find "basins"
            DebugReplay.BeginGroup("Basins");
            for (int z = 0, pz = 0; z < wd; z += w, pz++)
            {
                for (int x = 0; x < voxelArea.width; x++)
                {
                    CompactVoxelCell c = voxelArea.compactCells[z + x];

                    for (int i = (int)c.index, ni = (int)(c.index + c.count); i < ni; i++)
                    {
                        CompactVoxelSpan s        = voxelArea.compactSpans[i];
                        bool             anyBelow = false;

                        if (voxelArea.areaTypes[i] == UnwalkableArea || srcReg[i] != 0)
                        {
                            continue;
                        }

                        for (int dir = 0; dir < 4; dir++)
                        {
                            if (s.GetConnection(dir) != NotConnected)
                            {
                                int nx = x + voxelArea.DirectionX[dir];
                                int nz = z + voxelArea.DirectionZ[dir];

                                int ni2 = (int)(voxelArea.compactCells[nx + nz].index + s.GetConnection(dir));

                                if (voxelArea.dist[i] < voxelArea.dist[ni2])
                                {
                                    anyBelow = true;
                                    break;
                                }


                                //CompactVoxelSpan ns = voxelArea.compactSpans[ni];
                            }
                        }
                        if (!anyBelow)
                        {
                            //Debug.DrawRay (this.ConvertPosition(x,z,i),Vector3.down,Color.red);
                            DebugReplay.DrawCube(this.ConvertPosition(x, z, i), cellScale, Color.red);
                            basins.Add(new Int3(x, i, z));
                            //System.Console.WriteLine ("Basin at " + voxelArea.dist[i]);
                            level = System.Math.Max(level, voxelArea.dist[i]);
                        }
                    }
                }
            }

            //Start at maximum possible distance. & ~1 is rounding down to an even value
            level = (uint)((level + 1) & ~1);

            DebugReplay.EndGroup();
            DebugReplay.BeginGroup("BFS");

            List <Int3> st1 = Pathfinding.Util.ListPool <Int3> .Claim(300);

            List <Int3> st2 = Pathfinding.Util.ListPool <Int3> .Claim(300);

            //bool visited = new bool[voxelArea.compactSpanCount];

            for (;; level -= 2)
            {
                DebugReplay.BeginGroup("BFS " + level);
                //System.Console.WriteLine ("Starting level " + level + " with st1.Count = " + st1.Count);

                int ocount      = st1.Count;
                int expandCount = 0;

                if (ocount == 0)
                {
                    //int c = 0;
                    for (int q = 0; q < basins.Count; q++)
                    {
                        if (voxelArea.dist[basins[q].y] >= level)
                        {
                            DebugReplay.DrawCube(this.ConvertPosition(basins[q].x, basins[q].z, basins[q].y) + Vector3.up, cellScale, new Color(0, 1, 0, 0.5f));
                        }

                        if (srcReg[basins[q].y] == 0 && voxelArea.dist[basins[q].y] >= level)
                        {
                            srcReg[basins[q].y] = 1;
                            st1.Add(basins[q]);
                            //c++;
                            //visited[basins[i].y] = true;
                        }
                    }
                }

                for (int j = 0; j < st1.Count; j++)
                {
                    int x = st1[j].x;
                    int i = st1[j].y;
                    int z = st1[j].z;

                    ushort r = srcReg[i];

                    CompactVoxelSpan s = voxelArea.compactSpans[i];
                    int area           = voxelArea.areaTypes[i];

                    DebugReplay.DrawCube(this.ConvertPosition(x, z, i), cellScale, Mathfx.IntToColor(srcReg[i], 0.7f));

                    bool anyAbove = false;
                    for (int dir = 0; dir < 4; dir++)
                    {
                        if (s.GetConnection(dir) == NotConnected)
                        {
                            continue;
                        }

                        int nx = x + voxelArea.DirectionX[dir];
                        int nz = z + voxelArea.DirectionZ[dir];

                        int ni = (int)voxelArea.compactCells[nx + nz].index + s.GetConnection(dir);

                        if (area != voxelArea.areaTypes[ni])
                        {
                            continue;
                        }

                        if (voxelArea.dist[ni] < level)
                        {
                            anyAbove = true;
                            continue;
                        }


                        if (srcReg[ni] == 0)
                        {
                            bool same = false;
                            for (int v = (int)voxelArea.compactCells[nx + nz].index, vt = (int)voxelArea.compactCells[nx + nz].index + (int)voxelArea.compactCells[nx + nz].count; v < vt; v++)
                            {
                                if (srcReg[v] == srcReg[i])
                                {
                                    same = true;
                                    break;
                                }
                            }

                            if (!same)
                            {
                                //if ((int)srcDist[ni]+2 < (int)d2)
                                //{
                                //visited[i] = true;
                                srcReg[ni] = r;
                                //Debug.DrawRay (ConvertPosition(x,z,i),Vector3.up,Mathfx.IntToColor((int)level,0.6f));

                                //if (dstReg[ni] == 0)
                                st1.Add(new Int3(nx, ni, nz));
                                //d2 = (ushort)(srcDist[ni]+2);
                                //}
                            }
                        }
                    }

                    //Still on the edge
                    if (anyAbove)
                    {
                        st2.Add(st1[j]);
                    }

                    if (j == ocount - 1)
                    {
                        expandCount++;
                        ocount = st1.Count;

                        if (expandCount == 8 || j == st1.Count - 1)
                        {
                            //int c = 0;
                            for (int q = 0; q < basins.Count; q++)
                            {
                                if (voxelArea.dist[basins[q].y] >= level)
                                {
                                    DebugReplay.DrawCube(this.ConvertPosition(basins[q].x, basins[q].z, basins[q].y) + Vector3.up, cellScale, new Color(0, 1, 0, 0.5f));
                                }

                                if (srcReg[basins[q].y] == 0 && voxelArea.dist[basins[q].y] >= level)
                                {
                                    srcReg[basins[q].y] = 1;
                                    st1.Add(basins[q]);
                                    //c++;
                                    //visited[basins[i].y] = true;
                                }
                            }
                        }
                    }
                }



                List <Int3> tmpList = st1;
                st1 = st2;
                st2 = tmpList;
                st2.Clear();

                //System.Console.WriteLine ("Flooding basins");

                for (int i = 0; i < basins.Count; i++)
                {
                    if (srcReg[basins[i].y] == 1)
                    {
                        st2.Add(basins[i]);
                        FloodOnes(st2, srcReg, level, regionId); regionId++;
                        st2.Clear();
                    }
                }


                //System.Console.WriteLine ("Added " + c + " basins");
                DebugReplay.EndGroup();
                if (level == 0)
                {
                    break;
                }
            }

            DebugReplay.EndGroup();

            Pathfinding.Util.ListPool <Int3> .Release(st1);

            Pathfinding.Util.ListPool <Int3> .Release(st2);

            Pathfinding.Util.ListPool <Int3> .Release(basins);

            // Filter out small regions.
            voxelArea.maxRegions = regionId;

            FilterSmallRegions(srcReg, minRegionSize, voxelArea.maxRegions);


            // Write the result out.
            for (int i = 0; i < voxelArea.compactSpanCount; i++)
            {
                voxelArea.compactSpans[i].reg = srcReg[i];
            }
#else       /// ====== Use original recast code ====== //
            //Start at maximum possible distance. & ~1 is rounding down to an even value
            uint level = (uint)((voxelArea.maxDistance + 1) & ~1);

            int count = 0;



            while (level > 0)
            {
                level = level >= 2 ? level - 2 : 0;

                AstarProfiler.StartProfile("--Expand Regions");
                if (ExpandRegions(expandIterations, level, srcReg, srcDist, dstReg, dstDist, stack) != srcReg)
                {
                    ushort[] tmp = srcReg;
                    srcReg = dstReg;
                    dstReg = tmp;

                    tmp     = srcDist;
                    srcDist = dstDist;
                    dstDist = tmp;
                }

                AstarProfiler.EndProfile("--Expand Regions");

                AstarProfiler.StartProfile("--Mark Regions");


                // Mark new regions with IDs.
                // Find "basins"
                for (int z = 0, pz = 0; z < wd; z += w, pz++)
                {
                    for (int x = 0; x < voxelArea.width; x++)
                    {
                        CompactVoxelCell c = voxelArea.compactCells[z + x];

                        for (int i = (int)c.index, ni = (int)(c.index + c.count); i < ni; i++)
                        {
                            if (voxelArea.dist[i] < level || srcReg[i] != 0 || voxelArea.areaTypes[i] == UnwalkableArea)
                            {
                                continue;
                            }

                            if (FloodRegion(x, z, i, level, regionId, srcReg, srcDist, stack))
                            {
                                regionId++;
                            }
                        }
                    }
                }


                AstarProfiler.EndProfile("--Mark Regions");

                count++;

                //if (count == 10) {
                //	return;
                //}
            }

            if (ExpandRegions(expandIterations * 8, 0, srcReg, srcDist, dstReg, dstDist, stack) != srcReg)
            {
                ushort[] tmp = srcReg;
                srcReg = dstReg;
                dstReg = tmp;

                tmp     = srcDist;
                srcDist = dstDist;
                dstDist = tmp;
            }

            // Filter out small regions.
            voxelArea.maxRegions = regionId;

            FilterSmallRegions(srcReg, minRegionSize, voxelArea.maxRegions);

            // Write the result out.
            for (int i = 0; i < voxelArea.compactSpanCount; i++)
            {
                voxelArea.compactSpans[i].reg = srcReg[i];
            }

            Pathfinding.Util.ListPool <int> .Release(stack);


            /*
             * int sCount = voxelArea.GetSpanCount ();
             * Vector3[] debugPointsTop = new Vector3[sCount];
             * Vector3[] debugPointsBottom = new Vector3[sCount];
             * Color[] debugColors = new Color[sCount];
             *
             * int debugPointsCount = 0;
             * //int wd = voxelArea.width*voxelArea.depth;
             *
             * for (int z=0, pz = 0;z < wd;z += voxelArea.width, pz++) {
             *      for (int x=0;x < voxelArea.width;x++) {
             *
             *              Vector3 p = new Vector3(x,0,pz)*cellSize+forcedBounds.min;
             *
             *              //CompactVoxelCell c = voxelArea.compactCells[x+z];
             *              CompactVoxelCell c = voxelArea.compactCells[x+z];
             *              //if (c.count == 0) {
             *              //	Debug.DrawRay (p,Vector3.up,Color.red);
             *              //}
             *
             *              //for (int i=(int)c.index, ni = (int)(c.index+c.count);i<ni;i++)
             *
             *              for (int i = (int)c.index; i < c.index+c.count; i++) {
             *                      CompactVoxelSpan s = voxelArea.compactSpans[i];
             *                      //CompactVoxelSpan s = voxelArea.compactSpans[i];
             *
             *                      p.y = ((float)(s.y+0.1F))*cellHeight+forcedBounds.min.y;
             *
             *                      debugPointsTop[debugPointsCount] = p;
             *
             *                      p.y = ((float)s.y)*cellHeight+forcedBounds.min.y;
             *                      debugPointsBottom[debugPointsCount] = p;
             *
             *                      debugColors[debugPointsCount] = Pathfinding.Mathfx.IntToColor(s.reg,0.7f);//s.reg == 1 ? Color.green : (s.reg == 2 ? Color.yellow : Color.red);
             *                      debugPointsCount++;
             *
             *                      //Debug.DrawRay (p,Vector3.up*0.5F,Color.green);
             *              }
             *      }
             * }
             *
             * DebugUtility.DrawCubes (debugPointsTop,debugPointsBottom,debugColors, cellSize);*/
#endif
            AstarProfiler.EndProfile("Build Regions");
        }
        public ushort CalculateDistanceField(ushort[] src)
        {
            int wd = voxelArea.width * voxelArea.depth;

            //Mark boundary cells
            for (int z = 0; z < wd; z += voxelArea.width)
            {
                for (int x = 0; x < voxelArea.width; x++)
                {
                    CompactVoxelCell c = voxelArea.compactCells[x + z];

                    for (int i = (int)c.index, ci = (int)(c.index + c.count); i < ci; i++)
                    {
                        CompactVoxelSpan s = voxelArea.compactSpans[i];

                        int nc = 0;
                        for (int d = 0; d < 4; d++)
                        {
                            if (s.GetConnection(d) != NotConnected)
                            {
                                //This function (CalculateDistanceField) is used for both ErodeWalkableArea and by itself.
                                //The C++ recast source uses different code for those two cases, but I have found it works with one function
                                //the voxelArea.areaTypes[ni] will actually only be one of two cases when used from ErodeWalkableArea
                                //so it will have the same effect as
                                // if (area != UnwalkableArea) {
                                //This line is the one where the differ most

                                nc++;
#if FALSE
                                if (area == voxelArea.areaTypes[ni])
                                {
                                    nc++;
                                }
                                else
                                {
                                    //No way we can reach 4
                                    break;
                                }
#endif
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (nc != 4)
                        {
                            src[i] = 0;
                        }
                    }
                }
            }

            //Pass 1

            for (int z = 0; z < wd; z += voxelArea.width)
            {
                for (int x = 0; x < voxelArea.width; x++)
                {
                    CompactVoxelCell c = voxelArea.compactCells[x + z];

                    for (int i = (int)c.index, ci = (int)(c.index + c.count); i < ci; i++)
                    {
                        CompactVoxelSpan s = voxelArea.compactSpans[i];

                        if (s.GetConnection(0) != NotConnected)
                        {
                            // (-1,0)
                            int nx = x + voxelArea.DirectionX[0];
                            int nz = z + voxelArea.DirectionZ[0];

                            int ni = (int)(voxelArea.compactCells[nx + nz].index + s.GetConnection(0));

                            if (src[ni] + 2 < src[i])
                            {
                                src[i] = (ushort)(src[ni] + 2);
                            }

                            CompactVoxelSpan ns = voxelArea.compactSpans[ni];

                            if (ns.GetConnection(3) != NotConnected)
                            {
                                // (-1,0) + (0,-1) = (-1,-1)
                                int nnx = nx + voxelArea.DirectionX[3];
                                int nnz = nz + voxelArea.DirectionZ[3];

                                int nni = (int)(voxelArea.compactCells[nnx + nnz].index + ns.GetConnection(3));

                                if (src[nni] + 3 < src[i])
                                {
                                    src[i] = (ushort)(src[nni] + 3);
                                }
                            }
                        }

                        if (s.GetConnection(3) != NotConnected)
                        {
                            // (0,-1)
                            int nx = x + voxelArea.DirectionX[3];
                            int nz = z + voxelArea.DirectionZ[3];

                            int ni = (int)(voxelArea.compactCells[nx + nz].index + s.GetConnection(3));

                            if (src[ni] + 2 < src[i])
                            {
                                src[i] = (ushort)(src[ni] + 2);
                            }

                            CompactVoxelSpan ns = voxelArea.compactSpans[ni];

                            if (ns.GetConnection(2) != NotConnected)
                            {
                                // (0,-1) + (1,0) = (1,-1)
                                int nnx = nx + voxelArea.DirectionX[2];
                                int nnz = nz + voxelArea.DirectionZ[2];

                                int nni = (int)(voxelArea.compactCells[nnx + nnz].index + ns.GetConnection(2));

                                if (src[nni] + 3 < src[i])
                                {
                                    src[i] = (ushort)(src[nni] + 3);
                                }
                            }
                        }
                    }
                }
            }

            //Pass 2

            for (int z = wd - voxelArea.width; z >= 0; z -= voxelArea.width)
            {
                for (int x = voxelArea.width - 1; x >= 0; x--)
                {
                    CompactVoxelCell c = voxelArea.compactCells[x + z];

                    for (int i = (int)c.index, ci = (int)(c.index + c.count); i < ci; i++)
                    {
                        CompactVoxelSpan s = voxelArea.compactSpans[i];

                        if (s.GetConnection(2) != NotConnected)
                        {
                            // (-1,0)
                            int nx = x + voxelArea.DirectionX[2];
                            int nz = z + voxelArea.DirectionZ[2];

                            int ni = (int)(voxelArea.compactCells[nx + nz].index + s.GetConnection(2));

                            if (src[ni] + 2 < src[i])
                            {
                                src[i] = (ushort)(src[ni] + 2);
                            }

                            CompactVoxelSpan ns = voxelArea.compactSpans[ni];

                            if (ns.GetConnection(1) != NotConnected)
                            {
                                // (-1,0) + (0,-1) = (-1,-1)
                                int nnx = nx + voxelArea.DirectionX[1];
                                int nnz = nz + voxelArea.DirectionZ[1];

                                int nni = (int)(voxelArea.compactCells[nnx + nnz].index + ns.GetConnection(1));

                                if (src[nni] + 3 < src[i])
                                {
                                    src[i] = (ushort)(src[nni] + 3);
                                }
                            }
                        }

                        if (s.GetConnection(1) != NotConnected)
                        {
                            // (0,-1)
                            int nx = x + voxelArea.DirectionX[1];
                            int nz = z + voxelArea.DirectionZ[1];

                            int ni = (int)(voxelArea.compactCells[nx + nz].index + s.GetConnection(1));

                            if (src[ni] + 2 < src[i])
                            {
                                src[i] = (ushort)(src[ni] + 2);
                            }

                            CompactVoxelSpan ns = voxelArea.compactSpans[ni];

                            if (ns.GetConnection(0) != NotConnected)
                            {
                                // (0,-1) + (1,0) = (1,-1)
                                int nnx = nx + voxelArea.DirectionX[0];
                                int nnz = nz + voxelArea.DirectionZ[0];

                                int nni = (int)(voxelArea.compactCells[nnx + nnz].index + ns.GetConnection(0));

                                if (src[nni] + 3 < src[i])
                                {
                                    src[i] = (ushort)(src[nni] + 3);
                                }
                            }
                        }
                    }
                }
            }

            ushort maxDist = 0;

            for (int i = 0; i < voxelArea.compactSpanCount; i++)
            {
                maxDist = System.Math.Max(src[i], maxDist);
            }

#if ASTAR_DEBUGREPLAY && FALSE
            DebugReplay.BeginGroup("Distance Field");
            for (int z = wd - voxelArea.width; z >= 0; z -= voxelArea.width)
            {
                for (int x = voxelArea.width - 1; x >= 0; x--)
                {
                    CompactVoxelCell c = voxelArea.compactCells[x + z];

                    for (int i = (int)c.index, ci = (int)(c.index + c.count); i < ci; i++)
                    {
                        DebugReplay.DrawCube(CompactSpanToVector(x, z / voxelArea.width, i), Vector3.one * cellSize, new Color((float)src[i] / maxDist, (float)src[i] / maxDist, (float)src[i] / maxDist));
                    }
                }
            }
            DebugReplay.EndGroup();
#endif

            return(maxDist);
        }