Class MerlinNoise

java.lang.Object
squidpony.squidmath.MerlinNoise
All Implemented Interfaces:
Serializable, Noise.Noise2D, Noise.Noise3D

public class MerlinNoise
extends Object
implements Noise.Noise2D, Noise.Noise3D, Serializable
Really strange noise functions that typically produce curving black and white shapes when rendered. This technique uses no floating-point math, surprisingly, which helps its performance a little. The shapes this produces look like this in 2D and look like this in 3D. MerlinNoise implements 2D and 3D noise interfaces, allowing it to be used with the various support code in Noise like Noise.Layered2D.
MerlinNoise can be a good fit for some kinds of procedural generation that need smoothly-curving patterns that don't look altogether organic, like paint jobs on a space ship. It does tend to produce a somewhat-noticeable grid.
This is called Merlin noise because it has a roughly-similar implementation to "classic" Perlin Noise (with hashes per grid point used to blend values), and because working with noise functions makes me feel like a wizard. This was a completely unrelated noise algorithm that also avoided floating-point math, but was really pretty awful.
See Also:
Serialized Form
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected int bits  
    static MerlinNoise instance  
    protected int resolution  
    long seed  
  • Constructor Summary

    Constructors 
    Constructor Description
    MerlinNoise()
    Constructor for a default MerlinNoise instance with 8-bit output and resolution 3 (yielding 8x8-cell zones that share their corners).
    MerlinNoise​(long seed, int bits, int resolution)
    Constructor for a MerlinNoise instance that allows specification of all parameters.
  • Method Summary

    Modifier and Type Method Description
    int getBits()  
    double getNoise​(double x, double y)  
    double getNoise​(double x, double y, double z)  
    double getNoiseWithSeed​(double x, double y, double z, long seed)  
    double getNoiseWithSeed​(double x, double y, long seed)  
    int getResolution()  
    long getSeed()  
    long noise2D​(long x, long y)
    2D Merlin noise; black and white much of the time but curving instead of angular.
    long noise2D​(long x, long y, long seed)
    2D Merlin noise; black and white much of the time but curving instead of angular.
    static long noise2D​(long x, long y, long state, int resolution, int bits)
    2D Merlin noise; black and white much of the time but curving instead of angular.
    long noise3D​(long x, long y, long z)
    3D Merlin noise; black and white much of the time but curving instead of angular.
    long noise3D​(long x, long y, long z, long seed)
    3D Merlin noise; black and white much of the time but curving instead of angular.
    static long noise3D​(long x, long y, long z, long state, int resolution, int bits)
    3D merlin noise.
    static int[][] preCalcNoise2D​(int width, int height, long seed)
    Generates higher-quality continuous-style noise than the other methods, but requires pre-calculating a grid.
    void setBits​(int bits)
    Sets the number of bits this will output; 8 is common to produce byte-sized values between 0 and 255.
    void setResolution​(int resolution)
    Sets the resolution, which is an exponent that determines the width/height of each zone that shares the same four corners (where only the corners have their own hashed values).
    void setSeed​(long seed)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • MerlinNoise

      public MerlinNoise()
      Constructor for a default MerlinNoise instance with 8-bit output and resolution 3 (yielding 8x8-cell zones that share their corners). The seed can be set at any point, but it will start at 1.
    • MerlinNoise

      public MerlinNoise​(long seed, int bits, int resolution)
      Constructor for a MerlinNoise instance that allows specification of all parameters.
      Parameters:
      seed - the seed to use to alter the generated noise in noise2D(long, long) and noise3D(long, long, long)
      bits - the number of bits to output; typically 8 to produce byte-sized values
      resolution - an exponent that determines the size of a "zone" of cells that blend between the values at the zone's corners; commonly 1-6
  • Method Details

    • getSeed

      public long getSeed()
    • setSeed

      public void setSeed​(long seed)
    • getBits

      public int getBits()
    • setBits

      public void setBits​(int bits)
      Sets the number of bits this will output; 8 is common to produce byte-sized values between 0 and 255. This value can be between 1 and 64. If bits is 8, then this should produce values of 255 or 0, plus or minus 1. If bits is some other value, then it may produce more than two values, or only produce one.
      Parameters:
      bits - the number of bits of output each call should generate.
    • getResolution

      public int getResolution()
    • setResolution

      public void setResolution​(int resolution)
      Sets the resolution, which is an exponent that determines the width/height of each zone that shares the same four corners (where only the corners have their own hashed values). If resolution is 1, the size of a zone is 2x2, if it is 2, then the size of a zone is 4x4, if it is 3, then 8x8, and so on by powers of 2. The resolution can be as low as 0 (which won't blend corners' hashes at all) or as high as 31, but cannot easily be increased above that (10 is a really large size for a cell at 1024x1024, and 31 is over 2 billion by 2 billion). This doesn't slow down significantly (or at all) if resolution is particularly high or low, but this is often between 1 and 6.
      Parameters:
      resolution - an int between 0 and 31
    • noise2D

      public long noise2D​(long x, long y)
      2D Merlin noise; black and white much of the time but curving instead of angular.
      Parameters:
      x - x input
      y - y input
    • noise2D

      public long noise2D​(long x, long y, long seed)
      2D Merlin noise; black and white much of the time but curving instead of angular.
      Parameters:
      x - x input
      y - y input
      seed - the seed to use to alter the generated noise
    • noise3D

      public long noise3D​(long x, long y, long z)
      3D Merlin noise; black and white much of the time but curving instead of angular.
      Parameters:
      x - x input
      y - y input
      z - z input
    • noise3D

      public long noise3D​(long x, long y, long z, long seed)
      3D Merlin noise; black and white much of the time but curving instead of angular.
      Parameters:
      x - x input
      y - y input
      z - z input
      seed - the seed to use to alter the generated noise
    • noise2D

      public static long noise2D​(long x, long y, long state, int resolution, int bits)
      2D Merlin noise; black and white much of the time but curving instead of angular.
      Parameters:
      x - x input
      y - y input
      state - state to adjust the output
      resolution - the number of cells between "vertices" where one hashed value is used fully
      bits - how many bits should be used for each (signed long) output; often this is 8 to output a byte
      Returns:
      noise from -(1L << bits) to (1L << bits) - 1L, both inclusive
    • noise3D

      public static long noise3D​(long x, long y, long z, long state, int resolution, int bits)
      3D merlin noise.
      Parameters:
      x - x input
      y - y input
      z - z input
      state - state to adjust the output
      resolution - the number of cells between "vertices" where one hashed value is used fully
      bits - how many bits should be used for each (signed long) output; often this is 8 to output a byte
      Returns:
      noise from -(1L << bits) to (1L << bits) - 1L, both inclusive
    • preCalcNoise2D

      public static int[][] preCalcNoise2D​(int width, int height, long seed)
      Generates higher-quality continuous-style noise than the other methods, but requires pre-calculating a grid. Does allow taking a seed because internally it uses a DiverRNG to quickly generate initial white noise before processing it into more continuous noise. This generates a lot of random numbers (at least 1 + 14 * height, or more if width is greater than 64), so DiverRNG's high speed and fairly good period are both assets here.
      The 2D int array this produces has ints ranging from 1 to 255, with extreme values very unlikely. Because 0 is impossible for this to generate, it should be fine to use values from this as denominators in division.
      Parameters:
      width - the width of the 2D int array to generate
      height - the height of the 2D int array to generate
      seed - the RNG seed to use when pseudo-randomly generating the initial white noise this then processes
      Returns:
      a 2D int array where each int should be between 1 and 255, inclusive
    • getNoise

      public double getNoise​(double x, double y)
      Specified by:
      getNoise in interface Noise.Noise2D
    • getNoiseWithSeed

      public double getNoiseWithSeed​(double x, double y, long seed)
      Specified by:
      getNoiseWithSeed in interface Noise.Noise2D
    • getNoise

      public double getNoise​(double x, double y, double z)
      Specified by:
      getNoise in interface Noise.Noise3D
    • getNoiseWithSeed

      public double getNoiseWithSeed​(double x, double y, double z, long seed)
      Specified by:
      getNoiseWithSeed in interface Noise.Noise3D