FastNoiseLite Documentation

repository·master·Indexed 25 days ago

https://github.com/auburn/fastnoiselite

A highly portable, high-performance noise generation library supporting multiple algorithms including Perlin, Simplex, and Cellular noise. Designed for easy integration across various languages and platforms, it provides 2D and 3D sampling and is available for C, C++, C#, Fortran, GLSL, Go, Haxe, HLSL, Java, JavaScript/TypeScript, Odin, Pascal, and PowerShell.

Tokens
7.6K
Snippets
22
Records
47
Agent score
85%

What's inside FastNoiseLite

  1. Overview of FastNoise Lite

    master
    FastNoise Lite is a highly portable, open-source noise generation library designed for high performance across various platforms and languages. It provides a wide selection of noise algorithms and supports both 2D and 3D sampling. It is an evolution of the original FastNoise library, optimized for easy integration and modern noise generation requirements.
  2. Integrate FastNoiseLite into an Odin project

    master
    To use FastNoiseLite in an Odin project, copy the FastNoiseLite.odin file into your project structure. You can place it in either a local fast_noise_lite directory within your project or in the global odin/shared directory.
  3. Import FastNoiseLite

    master

    Depending on your module system, import FastNoiseLite as follows:

    ESM: import FastNoiseLite from "fastnoise-lite";

    CommonJS: const FastNoiseLite = require("fastnoise-lite").default;

    // using ESM
    import FastNoiseLite from "fastnoise-lite";
    // using CJS
    const FastNoiseLite = require("fastnoise-lite").default;
    
    let noise = new FastNoiseLite();
  4. Generate noise in GLSL using FastNoiseLite

    master

    To generate noise in a GLSL shader, you must first create and configure a noise state using fnlCreateState(). You can then sample noise at specific coordinates using fnlGetNoise2D().

    Common configuration involves setting the noise_type property (e.g., FNL_NOISE_OPENSIMPLEX2) on the state object.

    // Create and configure noise state
    fnl_state noise = fnlCreateState();
    noise.noise_type = FNL_NOISE_OPENSIMPLEX2;
    
    // Gather noise data
    float noiseData[128 * 128];
    int index = 0;
    
    for (int y = 0; y < 128; y++)
    {
        for (int x = 0; x < 128; x++) 
        {
            noiseData[index++] = fnlGetNoise2D(noise, x, y);
        }
    }
    
    // Do something with this data...
  5. Generate noise with FastNoiseLite

    master

    To generate noise, follow these steps:

    1. Initialize: Create a FastNoiseLite instance using FastNoiseLite::new() or FastNoiseLite::with_seed(seed).
    2. Configure: Use setter functions to adjust noise settings (e.g., set_noise_type).
    3. Domain Warp (Optional): Use domain_warp_2d(x, y) or domain_warp_3d(x, y, z) to transform coordinates before sampling.
    4. Sample: Call get_noise_2d(x, y) or get_noise_3d(x, y, z) to retrieve the noise value.

    Note: Noise values are returned in the range -1..1. You may need to remap this to a different range (e.g., 0..1).

    use fastnoise_lite::*;
    
    // Create and configure the FastNoise object
    let mut noise = FastNoiseLite::new();
    noise.set_noise_type(Some(NoiseType::OpenSimplex2));
    
    const WIDTH: usize = 128;
    const HEIGHT: usize = 128;
    let mut noise_data = [[0.; HEIGHT]; WIDTH];
    
    // Sample noise pixels
    for x in 0..WIDTH {
        for y in 0..HEIGHT {
            // Domain warp can optionally be employed to transform the coordinates before sampling:
            // let (x, y) = noise.domain_warp_2d(x as f32, y as f32);
            
            let negative_1_to_1 = noise.get_noise_2d(x as f32, y as f32);
            // You may want to remap the -1..1 range data to the 0..1 range:
            noise_data[x][y] = (negative_1_to_1 + 1.) / 2.;
            
            // (Uses of `as f32` above should become `as f64` if you're using FNL with the "f64" feature flag)
        }
    }
  6. Generate 2D noise in Go

    master

    To generate 2D noise in Go, import the fastnoise package. You can create a noise state using generics to specify either float32 or float64. Once initialized, configure the NoiseType (e.g., fastnoise.OpenSimplex2) and use the Noise2D(x, y) method to sample noise values at specific coordinates.

    import "fastnoise"
    
    // Create and configure noise state (either float32 or float64)
    var noise = fastnoise.New[float32]()
    noise.NoiseType(fastnoise.OpenSimplex2)
    
    // Gather noise data
    var noiseData [128][128]float32
    
    for x := 0; x < 128; x++ {
    	for y := 0; y < 128; y++ {
    		noiseData[x][y] = noise.Noise2D(x, y)
    	}
    }
  7. Integrate FastNoiseLite in C++

    master
    To use FastNoiseLite in a C++ project, you must include the header file. Crucially, you must add #define FNL_IMPL before including FastNoiseLite.h in exactly one source file to generate the implementation. Failure to do this will result in linker errors.