No description
- Fennel 65.8%
- Lua 31%
- Nix 1.9%
- Makefile 1.3%
| .build.yml | ||
| .gitignore | ||
| flake.lock | ||
| flake.nix | ||
| LICENCE | ||
| Makefile | ||
| matrices.fnl | ||
| NOTICE | ||
| README.md | ||
| simplex.fnl | ||
| simplex.lua | ||
| tables.lua | ||
| test.fnl | ||
Markdown
Simplex Noise for LuaJIT
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.fnlgenerates gradient tables and permutation arrays at compile time - Kernel contributions: The
contributionmacro inlines distance calculations and dot products - Dimension dispatch:
FractalSumAbsandTurbulenceuse 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