Skip to content

indiesoftby/defold-simplex-noise

Repository files navigation

Simplex Noise Cover

Simplex Noise for Defold

This extension for the Defold game engine provides functions to generate Simplex noise. Simplex noise is Ken Perlin's successor to classical Perlin noise that ends up doing quite a lot fewer calculations to get similar noise results.

The noise can be used as the basis for texture effects but also for animation, generating terrain heightmaps and many other things.

If you are looking for an equivalent to Unity's Mathf.PerlinNoise function, you've found it! While simplex.noise2 won't produce the exact same numerical output as the Unity function (as it uses Simplex noise, not classic Perlin noise), it behaves identically in principle for generating 2D coherent noise patterns. Furthermore, this extension offers greater flexibility through adjustable parameters like octaves, persistence, and lacunarity. And the most important thing - it's FAST! ⚡️

Setup

First add this extension as a dependency to your game.project:

https://github.com/indiesoftby/defold-simplex-noise/archive/main.zip

It makes available global Lua functions simplex.seed, simplex.noise2, simplex.noise3, and simplex.noise4.

Then you can generate noise:

function init(self)
    simplex.seed(123)

    local noise = simplex.noise2(0, 0)
    print("value at (0, 0): " .. noise)
end

Check out the API and the included example for more information. There is an online demo of the included example - here.

API

simplex.seed(seed)

Sets the seed for the noise generator.

Parameters:

  • seed (number): The integer seed value.

simplex.noise2(x, y [, octaves [, persistence [, lacunarity]]])

Computes 2D Simplex noise, optionally with fractal parameters.

Parameters:

  • x (number): The x-coordinate.
  • y (number): The y-coordinate.
  • octaves (integer, optional): Number of noise layers (must be > 0). Defaults to 1. It describes the number of loops that the code is run - making for finer and finer detail.
  • persistence (number, optional): Amplitude decrease per octave. Defaults to 0.5. Describes the maximum disturbance from the undisturbed position of a wave.
  • lacunarity (number, optional): Frequency increase per octave. Defaults to 2.0.

Returns:

  • noise (number): The calculated noise value. Output range is [0, 1].

simplex.noise3(x, y, z [, octaves [, persistence [, lacunarity]]])

Computes 3D Simplex noise, optionally with fractal parameters.

Parameters:

  • x (number): The x-coordinate.
  • y (number): The y-coordinate.
  • z (number): The z-coordinate.
  • octaves (integer, optional): Number of noise layers (must be > 0). Defaults to 1. It describes the number of loops that the code is run - making for finer and finer detail.
  • persistence (number, optional): Amplitude decrease per octave. Defaults to 0.5. Describes the maximum disturbance from the undisturbed position of a wave.
  • lacunarity (number, optional): Frequency increase per octave. Defaults to 2.0.

Returns:

  • noise (number): The calculated noise value. Output range is [0, 1].

simplex.noise4(x, y, z, w [, octaves [, persistence [, lacunarity]]])

Computes 4D Simplex noise, optionally with fractal parameters.

Parameters:

  • x (number): The x-coordinate.
  • y (number): The y-coordinate.
  • z (number): The z-coordinate.
  • w (number): The w-coordinate.
  • octaves (integer, optional): Number of noise layers (must be > 0). Defaults to 1. It describes the number of loops that the code is run - making for finer and finer detail.
  • persistence (number, optional): Amplitude decrease per octave. Defaults to 0.5. Describes the maximum disturbance from the undisturbed position of a wave.
  • lacunarity (number, optional): Frequency increase per octave. Defaults to 2.0.

Returns:

  • noise (number): The calculated noise value. Output range is [0, 1].

License

This project is licensed under the MIT License. See the LICENSE file for details.