No description
  • Fennel 65.8%
  • Lua 31%
  • Nix 1.9%
  • Makefile 1.3%
Find a file
2026-01-08 02:11:27 -03:00
.build.yml feat: sr.ht ci 2026-01-08 01:59:01 -03:00
.gitignore chore: init 2026-01-08 00:45:17 -03:00
flake.lock chore: init 2026-01-08 00:45:17 -03:00
flake.nix chore: init 2026-01-08 00:45:17 -03:00
LICENCE chore: init 2026-01-08 00:45:17 -03:00
Makefile chore: init 2026-01-08 00:45:17 -03:00
matrices.fnl chore: init 2026-01-08 00:45:17 -03:00
NOTICE chore: init 2026-01-08 00:45:17 -03:00
README.md feat: build badge on readme 2026-01-08 02:11:27 -03:00
simplex.fnl chore: init 2026-01-08 00:45:17 -03:00
simplex.lua chore: init 2026-01-08 00:45:17 -03:00
tables.lua chore: init 2026-01-08 00:45:17 -03:00
test.fnl chore: init 2026-01-08 00:45:17 -03:00

Markdown

Simplex Noise for LuaJIT

builds.sr.ht status

Simplex noise implementation in Fennel, optimized for LuaJIT performance. Provides 2D, 3D, and 4D noise functions with fractal variants and Gaussian blur utilities.

Performance optimizations

The Fennel version avoids performance pitfalls from the reference Lua implementation:

  • No table allocation in hot loops: Dimension-specific functions eliminate varargs overhead
  • Compile-time code generation: Macros expand gradient lookups and kernel contributions at compile time
  • Direct function calls: Replaces unpack() calls with direct argument passing
  • Cache-aware: Optional internal caching for repeated coordinate lookups

The reference simplex.lua creates temporary tables and uses unpack() in loops, which hurts LuaJIT performance. This implementation uses Fennel macros to generate dimension-specific variants that avoid these allocations.

Usage

(local simplex (require :simplex))

;; Basic noise
(simplex.Noise2D 0.5 0.3)
(simplex.Noise3D 1.0 2.0 3.0)
(simplex.Noise4D 1.0 2.0 3.0 4.0)

;; Fractal sums
(simplex.FractalSum simplex.Noise2D 4 0.5 0.3)
(simplex.FractalSumAbs simplex.Noise3D 3 1.0 2.0 3.0)

;; Turbulence (applies sin to a direction component)
(simplex.Turbulence simplex.Noise2D 0 4 0.5 0.3)  ; direction 0 = x
(simplex.Turbulence simplex.Noise3D 1 3 1.0 2.0 3.0)  ; direction 1 = y

;; Gaussian blur
(simplex.GBlur1D 0.5 1.0)
(simplex.GBlur2D 0.5 0.3 1.0)

;; Enable internal caching
(set simplex.internal-cache true)

Implementation details

  • Macros: matrices.fnl generates gradient tables and permutation arrays at compile time
  • Kernel contributions: The contribution macro inlines distance calculations and dot products
  • Dimension dispatch: FractalSumAbs and Turbulence use separate 2D/3D/4D implementations to avoid varargs overhead

Testing

Run the test suite to verify correctness against the reference implementation:

fennel test.fnl

Tests validate:

  • Generated tables match reference data
  • Noise outputs match reference implementation
  • Fractal functions produce expected results
  • Edge cases and boundary conditions

License

AGPL-3.0

* Original Implementations Licenced under MIT