Skip to content

andystanton/SPMOpenSimplex2

Repository files navigation

SPM Open Simplex 2

A Swift Package containing a port of KdotJPG's Open Simplex 2 for Swift with two flavours:

  1. Swift implementation that runs on the CPU
  2. Swift code for generating Metal Shading Language implementation that can be run on the GPU

The use case for the second approach is for a Metal project in Swift Playgrounds where Metal shader files can't be used directly but shader code must be provided as a string to load into a MTLLibrary.

Usage

Define a dependency from the source package to this one (Swift Package Manager Docs).

To use the CPU implementation you can run:

import SPMOpenSimplex2

...

let openSimplex2 = OpenSimplex2CPU()

let noise: Float = openSimplex2.noise3(
    seed: 42, 
    coord: SIMD3<Float>(4.3, 2.0, 1.4), 
    variant: .xy)

A Metal reference implementation is provided with the same interface as the CPU implementation. To use it, the above code can be changed to use OpenSimplex2Metal(). However it creates Metal resources which should be managed by the calling app, so direct usage is not recommended.