Sign in

    SimplexNoise

    实现基础的 Simplex Noise 算法

    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    last year
    Downloads
    48

    #🎨 SimplexNoise: 2D / 3D / 4D Procedural Noise & fBm Library for MoonBit

    Build Status License

    SimplexNoise is a lightweight, modular MoonBit library supporting high-performance 2D / 3D / 4D Simplex noise and multi-octave fractal Brownian motion (fBm). It features domain warping, tileable output, grayscale and color PNG rendering, and batch slicing. Suitable for procedural terrain, texture generation, cloud rendering, simulation, and research.


    #🚀 Features

    • ✅ 2D / 3D / 4D Simplex Noise with seed & gradient control
    • ✅ Multi-octave fractal Brownian motion (fBm)
    • ✅ Domain Warping support (strength~)
    • ✅ Tileable noise and fBm via periodic mapping (pi~)
    • ✅ PNG image output: grayscale & RGB
    • ✅ Batch slice output for 3D / 4D volumes
    • ✅ Python-style gradient compatibility
    • ✅ Full test suite including value ranges, comparisons, slice validation


    #📦 Installation

    moon add ZSeanYves/SimplexNoise

    Or manually in moon.mod.json:

    "import": ["ZSeanYves/SimplexNoise"]


    #🧑‍💻 Basic Usage

    #2D Color fBm with Domain Warping

    let config = @ZSeanYves/SimplexNoise.new_NoiseConfig(6, 0.5, 2.0, 0.03)
    let (_, grads) = @ZSeanYves/SimplexNoise.create2d(42)
    @ZSeanYves/SimplexNoise.fbm2d_image("./fbm2d.png", 256, 256, 42, grads, config, true, false, strength~ = 2.5)

    #Tileable 3D Grayscale fBm

    let config = new_NoiseConfig(5, 0.5, 2.0, 0.03)
    let (_, grads) = create3d(99)
    @ZSeanYves/SimplexNoise.tfbm3d_image("./tile3d.png", 256, 256, 64, 99, 0.0, grads, config, pi~ = 3.14, true)

    #4D Slice Output (Batch)

    let config = new_NoiseConfig(4, 0.5, 2.0, 0.03)
    let (_, grads) = create4d(123)
    @ZSeanYves/SimplexNoise.fbm4d_slices("./slices", 128, 128, 64, 123, grads, config, false, true, 10)


    #📘 API Overview

    FunctionDimTypeTileableWarpSliceOutput
    noise2d_image2Dbasic❌✅❌PNG
    fbm2d_image2DfBm❌✅❌PNG
    tnoise2d_image2Dbasic✅❌❌PNG
    tfbm2d_image2DfBm✅❌❌PNG
    noise3d_image3Dbasic❌✅❌PNG
    fbm3d_image3DfBm❌✅❌PNG
    tnoise3d_image3Dbasic✅❌❌PNG
    tfbm3d_image3DfBm✅❌❌PNG
    noise3d_slices3Dbasic❌✅✅PNG
    fbm3d_slices3DfBm❌✅✅PNG
    noise4d_image4Dbasic❌✅❌PNG
    fbm4d_image4DfBm❌✅❌PNG
    tnoise4d_image4Dbasic✅❌❌PNG
    tfbm4d_image4DfBm✅❌❌PNG
    noise4d_slices4Dbasic❌✅✅PNG
    fbm4d_slices4DfBm❌✅✅PNG


    #🎛️ Parameters

    ParameterDescription
    width, heightOutput image size in pixels
    depth, timeFor 3D/4D tileable output
    scaleControls frequency of noise
    seedDetermines permutation table
    gradsGradient vector table
    z, wSlice positions in 3D/4D
    pi~Controls tileability (default 3.14)
    strength~Warp intensity (default 1.0)
    useWarpEnable domain warping
    isGrayScaleUse grayscale or RGB


    #📂 Project Structure

    SimplexNoise/ ├── src/ │ ├── noise2d.mbt / noise3d.mbt / noise4d.mbt # Core simplex noise │ ├── fbm.mbt / fbm3d.mbt / fbm4d.mbt # fBm logic │ ├── image_gen.mbt / slice.mbt # PNG output & slicing │ ├── simplex_noise.mbt / SimplexNoise.mbti # Public interface │ ├── hash.mbt # build hash │ └── simplex_noise_test.mbt # Full test suite ├── examples/ # Output images ├── moon.pkg.json / LICENSE / README.md


    #✅ Testing

    moon test -p ZSeanYves/SimplexNoise

    #🔬 Test Coverage Includes

    • ✔️ Noise value range checks (2D/3D/4D)
    • ✔️ fBm vs basic comparison
    • ✔️ Warp & Tileable effects
    • ✔️ Grayscale vs RGB rendering
    • ✔️ Batch slice tests
    • ✔️ Custom gradient support
    • ✔️ Python noise parity (snoise2/3/4, fbm)

    Test images are saved under src/examples/ and grouped into folders by type.


    #🧠 Roadmap

    • 🎨 Normal map / analytic derivative output


    #📜 License

    MIT License. See LICENSE for details.

    NoiseConfig

    pub struct NoiseConfig {
    octaves : Int
    persistence : Float
    lacunarity : Float
    scale : Float
    }

    create2d

    fn create2d(seed : Int) -> (Array[Int], Array[Array[Float]])

    create3d

    fn create3d(seed : Int) -> (Array[Int], Array[Array[Float]])

    create4d

    fn create4d(seed : Int) -> (Array[Int], Array[Array[Float]])

    fbm2d

    fn fbm2d(p : Array[Float], octaves : Int, persistence : Float, lacunarity : Float, perm_table : Array[Int], grads : Array[Array[Float]], base_scale : Float) -> Float

    fbm2d_image

    fn fbm2d_image(path : String, width : Int, height : Int, seed : Int, grads : Array[Array[Float]], config : NoiseConfig, useWarp : Bool, isGrayScale : Bool, strength~ : Float = ..) -> Bool raise Error

    fbm3d

    fn fbm3d(p : Array[Float], octaves : Int, persistence : Float, lacunarity : Float, perm_table : Array[Int], grads : Array[Array[Float]], base_scale : Float) -> Float

    fbm3d_image

    fn fbm3d_image(path : String, width : Int, height : Int, z : Float, seed : Int, grads : Array[Array[Float]], config : NoiseConfig, useWarp : Bool, isGrayScale : Bool, strength~ : Float = ..) -> Bool raise Error

    fbm3d_slices

    fn fbm3d_slices(base_path : String, width : Int, height : Int, seed : Int, grads : Array[Array[Float]], config : NoiseConfig, useWarp : Bool, isGrayScale : Bool, strength~ : Float = .., slice_count : Int) -> Unit raise Error

    fbm4d

    fn fbm4d(p : Array[Float], octaves : Int, persistence : Float, lacunarity : Float, perm_table : Array[Int], grads : Array[Array[Float]], base_scale : Float) -> Float

    fbm4d_image

    fn fbm4d_image(path : String, width : Int, height : Int, z : Float, w : Float, seed : Int, grads : Array[Array[Float]], config : NoiseConfig, useWarp : Bool, isGrayScale : Bool, strength~ : Float = ..) -> Bool raise Error

    fbm4d_slices

    fn fbm4d_slices(base_path : String, width : Int, height : Int, z : Float, seed : Int, grads : Array[Array[Float]], config : NoiseConfig, useWarp : Bool, isGrayScale : Bool, strength~ : Float = .., slice_count : Int) -> Unit raise Error

    grads2d

    let grads2d : Array[Array[Float]]

    grads3d

    let grads3d : Array[Array[Float]]

    grads4d

    let grads4d : Array[Array[Float]]

    new_NoiseConfig

    fn new_NoiseConfig(octaves : Int, persistence : Float, lacunarity : Float, scale : Float) -> NoiseConfig

    new_maxwarp

    fn new_maxwarp(a : Float) -> Float

    noise2d

    fn noise2d(p : Array[Float], perm_table : Array[Int], grads : Array[Array[Float]], scale : Float) -> Float

    noise2d_image

    fn noise2d_image(path : String, width : Int, height : Int, scale : Float, seed : Int, grads : Array[Array[Float]], useWarp : Bool, isGrayScale : Bool, strength~ : Float = ..) -> Bool raise Error

    noise3d

    fn noise3d(p : Array[Float], perm_table : Array[Int], grads : Array[Array[Float]], scale : Float) -> Float

    noise3d_image

    fn noise3d_image(path : String, width : Int, height : Int, scale : Float, z : Float, seed : Int, grads : Array[Array[Float]], useWarp : Bool, isGrayScale : Bool, strength~ : Float = ..) -> Bool raise Error

    noise3d_slices

    fn noise3d_slices(base_path : String, width : Int, height : Int, scale : Float, seed : Int, grads : Array[Array[Float]], useWarp : Bool, isGrayScale : Bool, strength~ : Float = .., slice_count : Int) -> Unit raise Error

    noise4d

    fn noise4d(p : Array[Float], perm_table : Array[Int], grads : Array[Array[Float]], scale : Float) -> Float

    noise4d_image

    fn noise4d_image(path : String, width : Int, height : Int, scale : Float, z : Float, w : Float, seed : Int, grads : Array[Array[Float]], useWarp : Bool, isGrayScale : Bool, strength~ : Float = ..) -> Bool raise Error

    noise4d_slices

    fn noise4d_slices(base_path : String, width : Int, height : Int, scale : Float, z : Float, seed : Int, grads : Array[Array[Float]], useWarp : Bool, isGrayScale : Bool, strength~ : Float = .., slice_count : Int) -> Unit raise Error

    tfbm2d_image

    fn tfbm2d_image(path : String, width : Int, height : Int, seed : Int, grads : Array[Array[Float]], config : NoiseConfig, pi~ : Double = .., isGrayScale : Bool) -> Bool raise Error

    tfbm3d_image

    fn tfbm3d_image(path : String, width : Int, height : Int, depth : Int, seed : Int, z : Float, grads : Array[Array[Float]], config : NoiseConfig, pi~ : Double = .., isGrayScale : Bool) -> Bool raise Error

    tfbm4d_image

    fn tfbm4d_image(path : String, width : Int, height : Int, depth : Int, time : Int, z : Float, w : Float, seed : Int, grads : Array[Array[Float]], config : NoiseConfig, pi~ : Double = .., isGrayScale : Bool) -> Bool raise Error

    tnoise2d_image

    fn tnoise2d_image(path : String, width : Int, height : Int, scale : Float, seed : Int, grads : Array[Array[Float]], pi~ : Double = .., isGrayScale : Bool) -> Bool raise Error

    tnoise3d_image

    fn tnoise3d_image(path : String, width : Int, height : Int, depth : Int, scale : Float, seed : Int, z : Float, grads : Array[Array[Float]], pi~ : Double = .., isGrayScale : Bool) -> Bool raise Error

    tnoise4d_image

    fn tnoise4d_image(path : String, width : Int, height : Int, depth : Int, time : Int, z : Float, w : Float, seed : Int, grads : Array[Array[Float]], scale : Float, pi~ : Double = .., isGrayScale : Bool) -> Bool raise Error