Table of Contents

Class HexUtils

Namespace
Gamelogic.Fx.Dithering
Assembly
Gamelogic.Fx.Dithering.dll

Utility functions for hex grid coordinate conversions.

public static class HexUtils
Inheritance
HexUtils
Inherited Members

Remarks

All functions use axial coordinates: the x component is the column axis and the y component is the row axis. This matches the coordinate system used by the Gamelogic/Fx/HexPixelate and Gamelogic/Fx/HexDitherGridBias shaders.

Methods

GridPointToIndex(Vector2Int, int, int, int)

Converts a hex grid point to a fundamental region index, or -1 if the point is not in the canonical fundamental region.

public static int GridPointToIndex(Vector2Int gridPoint, int x0, int x1, int y1)

Parameters

gridPoint Vector2Int
x0 int
x1 int
y1 int

Returns

int

Remarks

Only tests membership in the canonical region (y in [0, y1), x in the corresponding column range). To test an arbitrary grid point reduced modulo the lattice, reduce it first.

GridToWorld(Vector2Int, Vector2)

Converts hex grid axial coordinates to a world position at the center of the cell.

public static Vector2 GridToWorld(Vector2Int gridPoint, Vector2 cellDimensions)

Parameters

gridPoint Vector2Int

Axial coordinates of the hex cell.

cellDimensions Vector2

The (width, height) of a single hex cell.

Returns

Vector2

IndexToGridPoint(int, int, int, int)

Converts a fundamental region index to a hex grid point.

public static Vector2Int IndexToGridPoint(int index, int x0, int x1, int y1)

Parameters

index int

Index in [0, Count) where Count = x0 * y1.

x0 int

x-component of basis vector A.

x1 int

x-component of basis vector B.

y1 int

y-component of basis vector B (must be > 0).

Returns

Vector2Int

Remarks

The fundamental region is the parallelogram with basis vectors A = (x0, 0) and B = (x1, y1). It contains Count = |x0 * y1| lattice points with indices 0 .. Count-1. Rows run from y = 0 to y = y1-1; within each row y, the x0 columns start at ⌈y · x1 / y1⌉.

Assumes x0 > 0 and y1 > 0.

ReduceToFundamentalRegion(Vector2Int, int, int, int)

Reduces an arbitrary integer grid point modulo the lattice defined by basis vectors A = (x0, 0) and B = (x1, y1), returning the equivalent point in the canonical fundamental region.

public static Vector2Int ReduceToFundamentalRegion(Vector2Int point, int x0, int x1, int y1)

Parameters

point Vector2Int

Any integer grid point.

x0 int

x-component of basis vector A (must be > 0).

x1 int

x-component of basis vector B.

y1 int

y-component of basis vector B (must be > 0).

Returns

Vector2Int

Remarks

The canonical region has rows y ∈ [0, y1) where each row y contains the x0 columns starting at ⌈y · x1 / y1⌉ — the same layout as IndexToGridPoint(int, int, int, int).

WorldToGrid(Vector2, Vector2)

Converts a world position to hex grid axial coordinates.

public static Vector2Int WorldToGrid(Vector2 worldPoint, Vector2 cellDimensions)

Parameters

worldPoint Vector2

Position in world space (pixels or UV-relative units).

cellDimensions Vector2

The (width, height) of a single hex cell in the same units.

Returns

Vector2Int