hexx 0.3.0

Hexagonal utilities
Documentation

Hexx

workflow License unsafe forbidden Crates.io Docs.rs dependency status

Hexagonal tools lib in rust.

Inspired by this RedBlobGames article.

This lib allows you to:

  • Manipulate hexagon coordinates
  • Generate hexagonal maps with custom layouts and orientation
  • Generate hexagon meshes (planes or columns)

I made the choice to use Axial Coordinates for performance and utility reasons, but the [Hex] type has conversion utilities with cubic, doubled and offset coordinates.

See the hexagonal coordinate systems

Features

hexx provides the [Hex] coordinates with:

  • Distances
  • Neighbors and directions
  • Lines
  • Ranges
  • Rings
  • Spirals
  • Rotation
  • Symmetry
  • Vector operations
  • Conversions to other coordinate systems

And the [HexMap] utility, for wraparound (seamless) hexagonal maps

Usage in bevy

If you want to generate 3D hexagonal mesh and use it in bevy you may do it this way:

 use bevy::prelude::Mesh;
 use bevy::render::{mesh::Indices, render_resource::PrimitiveTopology};
 use hexx::{HexLayout, Hex, MeshInfo};

pub fn hexagonal_plane(hex_layout: &HexLayout) -> Mesh {
    // Compute hex plane data for at the origin
    let mesh_info = MeshInfo::hexagonal_plane(hex_layout, Hex::ZERO);
    // Compute the bevy mesh
    let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
    mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, mesh_info.vertices.to_vec());
    mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, mesh_info.normals.to_vec());
    mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, mesh_info.uvs.to_vec());
    mesh.set_indices(Some(Indices::U16(mesh_info.indices)));
    mesh
}

The [MeshInfo] type provides the following mesh generations:

  • [MeshInfo::hexagonal_plane] (7 vertices) useful for 2D games
  • [MeshInfo::cheap_hexagonal_column] (13 vertices) with merged vertices and useful only for unlit games
  • [MeshInfo::partial_hexagonal_column] (31 vertices) without the bottom face
  • [MeshInfo::hexagonal_column] (38 vertices) with the bottom face

See the examples for bevy usage

Example

example

cargo run --example hex_grid

example

cargo run --example 3d_columns