Generating Tiles

This section covers how to generate tiles for a sphere.

Synchronous Generation

BlueprintsC++

A list of tiles can be generated with a call to FHexaSphereLib::GenerateHexaSphere().

// Includes the library.
#include "Generation/HexaSphereLib.h"
// ...
// The parameters to generate the tiles.
FHexaSphereParameters Parameters;
Parameters.Divisions = 12;
Parameters.Radius = 400.f;
Parameters.Percent = 1.f; 
Parameters.bGenerateNeighborData = true;
// Generates the tiles.
TArray<FHexaTile> Tiles = FHexaSphereLib::GenerateHexaSphere(Parameters);

A tile struct contains a few attributes defining the tiles:

  1. CenterPoint: The center point of the hexagon representing the tile.
  2. Boundaries: The boundaries of the tile. For an hexagon, it contains the 6 location of the edges relative to the center of the sphere.
  3. Offset: The tile offset from the surface. If noise is applied to the tile, the noise vector is stored in this property. Can be modified to define an arbitrary offset to the tile.
  4. Color: The color of the tile, that is passed to the material. Can be changed to modify a tile's color.
  5. Neighbors: The index of neighbors of the tiles in the Tiles array. Can be empty if bGenerateNeighborData is set to false when generating the tiles.
// Gets a tile at an arbitrary index. Make sure to use a ref (&)
// if you want to edit its properties.
FHexaTile& FirstTile = Tiles[0];
// Can access the tile's properties with Tile.X
// Tile.CenterPoint;
// Tile.Boundaries;
// Tile.Offset;
// Tile.Color;
// Tile.Neighbors;
// For example, we can set a tile's color:
Tile.Color = FLinearColor::Red;
// And do the same for its neighbors
for (const int32 NeighborIndex : Tile.Neighbors)
{
    Tiles[NeighborIndex].Color = FLinearColor::Blue;
}

A HexaSphere can be generated with a call to the Generate HexaSphere node.

The resulting UHexaSphere object consists of tiles and a few helper methods to manipulate those tiles. It is possible to get the tiles by calling the Get Tiles pure node.

A tile struct contains a few attributes defining the tiles:

  1. Center Point: The center point of the hexagon representing the tile.
  2. Boundaries: The boundaries of the tile. For an hexagon, it contains the 6 location of the edges relative to the center of the sphere.
  3. Offset: The tile offset from the surface. If noise is applied to the tile, the noise vector is stored in this property. Can be modified to define an arbitrary offset to the tile.
  4. Color: The color of the tile, that is passed to the material. Can be changed to modify a tile's color.
  5. Neighbors: The index of neighbors of the tiles in the UHexaSphere's Tiles array. Can be empty if bGenerateNeighborData is set to false when generating the tiles.

Asynchronous generation

As the number of divisions increases, the time it takes to generate the tiles increases. If you generate huge spheres and start to see a freeze, you might want to switch to asynchronous generation. Asnchronous generations performs the sphere generation in a background thread to not block the Game Thread.

Switching thread comes with a delay. You can expect up to nearly 2 * 1 / FPS seconds (= 33ms at 60 FPS) of idle delay to leave and come back to the Game Thread.

BlueprintsC++
// Includes the library.
#include "Generation/HexaSphereLib.h"
// ...
// The parameters to generate the tiles.
FHexaSphereParameters Parameters;
Parameters.Divisions = 12;
Parameters.Radius = 400.f;
Parameters.Percent = 1.f; 
Parameters.bGenerateNeighborData = true;
// Generates the tiles asynchronously.
FHexaSphereLib::GenerateHexaSphereAsync(Parameters, FGenerateHexaSphereCallback::CreateLambda([](TArray<FHexaTile> Tiles) -> void
{
    // The HexaSphere is represented by the Tiles array.
}))

A HexaSphere can be generated asynchronously with a call to the Generate HexaSphere node.

Common Mistake: Make sure to connect any code that uses the HexaSphere to the On Generated pin. The HexaSphere object is null on the default pin.