//将高度区域转换为平面方格
        public static RecastGridData HeightfieldToPlaneGrid(Heightfield heightfield, float planeY, float walkHeigh)
        {
            RecastGridData gridData = new RecastGridData
            {
                OriginPoint = new Vector2(heightfield.Offset.x, heightfield.Offset.z),
                Width       = heightfield.Size.x,
                Length      = heightfield.Size.y,
                CellSize    = heightfield.CellSize,
            };
            int zero = (int)((planeY - heightfield.Offset.y) / heightfield.CellSize);

            heightfield.Combin(walkHeigh);
            gridData.Mask = new BitArray(gridData.Width * gridData.Length);
            for (int i = 0; i < gridData.Width; ++i)
            {
                for (int j = 0; j < gridData.Length; ++j)
                {
                    Cell cell = heightfield.Get(i, j);
                    if (cell != null)
                    {
                        foreach (var span in cell.Spans)
                        {
                            if (Mathf.Abs(span.Max - zero) <= 1)
                            {
                                gridData.Mask.Set(i + j * gridData.Width, true);
                            }
                        }
                    }
                }
            }
            return(gridData);
        }
        //体素转换为高度区域
        public static Heightfield VoxelToHeightfield(Voxelization voxel)
        {
            Heightfield heightfield = new Heightfield(voxel.Bounds.min, voxel.Size.x, voxel.Size.z, voxel.CellSize);
            Cell        cell        = new Cell(voxel.Size.y);

            for (int x = 0; x < voxel.Size.x; ++x)
            {
                for (int z = 0; z < voxel.Size.z; ++z)
                {
                    int min   = -1;
                    int count = 0;
                    for (int y = 0; y < voxel.Size.y; ++y)
                    {
                        bool mask = voxel.Voxel.Get(x, y, z);
                        if (mask)
                        {
                            count++;
                            if (min == -1)
                            {
                                min   = y;
                                count = 1;
                            }
                        }
                        else
                        {
                            if (min != -1)
                            {
                                cell.Spans.Add(new Cell.Span {
                                    Min = min, Max = min + count
                                });
                                min   = -1;
                                count = 0;
                            }
                        }
                    }
                    if (cell.Spans.Count > 0)
                    {
                        int cellIndex = z * voxel.Size.x + x;
                        heightfield.Cells[cellIndex] = cell;
                        cell = new Cell(voxel.Size.y);
                    }
                }
            }
            return(heightfield);
        }
Exemple #3
0
 public static void DrawHeightfield(Heightfield heightfield)
 {
     //Color color = Gizmos.color;
     //Gizmos.color = Color.green;
     for (int x = 0; x < heightfield.Size.x; ++x)
     {
         for (int z = 0; z < heightfield.Size.y; ++z)
         {
             Cell cell = heightfield.Get(x, z);
             if (cell == null)
             {
                 continue;
             }
             for (int i = 0; i < cell.Spans.Count; ++i)
             {
                 var     span = cell.Spans[i];
                 Vector3 size = new Vector3(heightfield.CellSize, heightfield.CellSize * (span.Max - span.Min), heightfield.CellSize);
                 Vector3 pos  = new Vector3(heightfield.CellSize * x, heightfield.CellSize * span.Min, heightfield.CellSize * z) + heightfield.Offset + size * 0.5f;
                 Gizmos.DrawCube(pos, size);
             }
         }
     }
     //Gizmos.color = color;
 }