Skip to content

zilmarinen/Deltille

Repository files navigation

Platforms Swift 5.1 Swift Package Manager License

Deltille

Deltille is a Swift library for working with hexagonal and triangular grid systems. It provides robust data structures and utilities to make grid based development simple, accurate and efficient.

Hexagonal and triangular grids are deeply related through duality:

  • Every hexagonal grid can be subdivided into equilateral triangles forming a triangular grid.
  • Conversely; The dual graph of a triangular grid is a hexagonal grid and vice versa.
  • This makes them complementary representations of the same underlying space. You can often solve problems easily by switching between them.

By supporting both grid types in a unified API, Deltille lets you take advantage of this relationship making it easier to build flexible systems for geometry, navigation, or procedural generation.

Features

  • Hexagonal & Triangular Grids: Supports both coordinate systems.
  • Coordinate Conversion: Easily convert between 2D Cartesian coordinates and 3D hexagonal grid coordinates.
  • Neighbour & Vertex Navigation: Convenient methods for traversing edge neighbours and corner vertices.
  • Scaling & Transformations: Built-in support for scaling grid space and handling grid math such as subdivision and rotation.
  • Composable API: Designed to integrate cleanly into games, simulations and visualization tools.

Installation

To install using Swift Package Manager, add this to the dependencies: section in your Package.swift file:

.package(url: "https://github.com/zilmarinen/Deltille.git", branch: "main"),

Dependencies

Euclid is a Swift library for creating and manipulating 3D geometry and is used extensively within this project for mesh generation and vector operations.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Implementation

Deltille defines a Tile archetype of which both Triangle and Hexagon conform to provide high level utility methods for grid based operations.

Triangles & Hexagons

The basic building blocks of Deltille are both the Triangle and Hexagon Tile types which are used together to model vertex positions along the xz plane.

// MARK: Triangle
let triangle = Grid.Triangle(.zero)
    
//generate triangle vertices for the desired scale
let vertices = triangle.vertices.map { $0.position(.tile) }

// MARK: Hexagon
let hexagon = Grid.Hexagon(.zero)

//generate hexagon vertices for the desired scale
let vertices = hexagon.vertices.map { $0.position(.tile) }

Stencils & Sieves

Both a Stencil or Sieve can be used to subdivide a triangle into individual sub-triangles mapped to a specific grid scale.

  • Stencil defines a discrete set of subdivided triangles and their vertices in world space.
  • Sieve calculates a dynamic range of subdivided triangles and their vertices in grid space.
// MARK: Stencil
let stencil = triangle.stencil(.tile)

let triangles = stencil.triangles

//MARK: Sieve
let sieve = triangle.sieve(for: .chunk)

let triangles = sieve.triangles

Footprints

A Footprint defines a collection of Tile types centered around a given origin.

//MARK: Footprint
let coordinates: [Grid.Coordinate] = [.zero,
                                      -.unitX,
                                      -.unitY,
                                      -.unitZ]

let footprint = Grid.Triangle.Footprint(.zero,
                                        coordinates)

//rotate footprint around its origin
let rotated = footprint.rotate(.clockwise)

Examples

Regolith makes use of the concepts introduced by Deltille to generate meshes for predefined tessellations of a triangle interior using Ortho-Tiling.

Verdure implements additional mesh generation on top of Deltille to create stylised foliage canopies constrained to a triangular grid.

Credits

The Deltille framework is primarily the work of Zack Brown.

Special thanks go to;

  • Boris the Brave for his extensive articles on grid systems, dual contouring, marching cubes, ortho-tiling and so much more.
  • Oskar Stalberg for inspiring posts on procedural generation, wave function collapse and dual grid systems.
  • Amit Patel for stimulating deep dives into hexagonal grids, coordinate systems, grid edge classifications and graph theory.

About

Swift library for working with hexagonal and triangular grid systems.

Topics

Resources

License

Stars

Watchers

Forks

Languages