示例#1
0
        //将高度区域转换为平面方格
        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);
        }
示例#2
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;
 }