Class OpenSimplex2

java.lang.Object
com.github.yellowstonegames.grid.OpenSimplex2
All Implemented Interfaces:
INoise, Externalizable, Serializable

public class OpenSimplex2 extends Object implements INoise
K.jpg's OpenSimplex 2, faster variant. This is a variant on the Simplex Noise algorithm in 2D, and a subtly different algorithm in 3D and 4D. This does not support 5D or 6D noise. This is very similar to OpenSimplex2Smooth, but has more separate "spots" or "surflets" with noticeable gaps between areas of similar results. It is faster than OpenSimplex2Smooth, though.
OpenSimplex2.java was originally by KdotJPG, who released it into the public domain. Original license.
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface INoise

    INoise.Serializer
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    OpenSimplex2(long seed)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates a copy of this INoise, which should be a deep copy for any mutable state but can be shallow for immutable types such as functions.
    boolean
     
    int
    Gets the maximum dimension supported by this generator, which is 4 here.
    int
    Gets the minimum dimension supported by this generator, which is 2 here.
    float
    getNoise(float x, float y)
    Gets 2D noise with a default or pre-set seed.
    float
    getNoise(float x, float y, float z)
    Gets 3D noise with a default or pre-set seed.
    float
    getNoise(float x, float y, float z, float w)
    Gets 4D noise with a default or pre-set seed.
    float
    getNoise(float x, float y, float z, float w, float u)
    Gets 5D noise with a default or pre-set seed.
    float
    getNoise(float x, float y, float z, float w, float u, float v)
    Gets 6D noise with a default or pre-set seed.
    float
    getNoiseWithSeed(float x, float y, float z, float w, float u, float v, long seed)
    Gets 6D noise with a specific seed.
    float
    getNoiseWithSeed(float x, float y, float z, float w, float u, long seed)
    Gets 5D noise with a specific seed.
    float
    getNoiseWithSeed(float x, float y, float z, float w, long seed)
    Gets 4D noise with a specific seed.
    float
    getNoiseWithSeed(float x, float y, float z, long seed)
    Gets 3D noise with a specific seed.
    float
    getNoiseWithSeed(float x, float y, long seed)
    Gets 2D noise with a specific seed.
    long
    Gets the current seed of the generator, as a long even if the seed is stored internally as an int.
    Returns a typically-four-character String constant that should uniquely identify this INoise as well as possible.
    boolean
    Returns true if this generator can be seeded with setSeed(long) and retrieved with getSeed(), which this can be.
    int
     
    static float
    noise2(long seed, float x, float y)
    2D Simplex noise, standard lattice orientation.
    static float
    noise2_ImproveX(long seed, float x, float y)
    2D Simplex noise, with Y pointing down the main diagonal.
    static float
    noise3_Fallback(long seed, float x, float y, float z)
    3D OpenSimplex2 noise, fallback rotation option Use noise3_ImproveXY or noise3_ImproveXZ instead, wherever appropriate.
    static float
    noise3_ImproveXY(long seed, float x, float y, float z)
    3D OpenSimplex2 noise, with better visual isotropy in (X, Y).
    static float
    noise3_ImproveXZ(long seed, float x, float y, float z)
    3D OpenSimplex2 noise, with better visual isotropy in (X, Z).
    static float
    noise4_Fallback(long seed, float x, float y, float z, float w)
    4D OpenSimplex2 noise, fallback lattice orientation.
    static float
    noise4_ImproveXY_ImproveZW(long seed, float x, float y, float z, float w)
    4D OpenSimplex2 noise, with XY and ZW forming orthogonal triangular-based planes.
    static float
    noise4_ImproveXYZ(long seed, float x, float y, float z, float w)
    4D OpenSimplex2 noise, with XYZ oriented like noise3_Fallback and W for an extra degree of freedom.
    static float
    noise4_ImproveXYZ_ImproveXY(long seed, float x, float y, float z, float w)
    4D OpenSimplex2 noise, with XYZ oriented like noise3_ImproveXY and W for an extra degree of freedom.
    static float
    noise4_ImproveXYZ_ImproveXZ(long seed, float x, float y, float z, float w)
    4D OpenSimplex2 noise, with XYZ oriented like noise3_ImproveXZ and W for an extra degree of freedom.
     
    void
    setSeed(long seed)
    Sets the seed to the given long, if long seeds are supported, or (int)seed if only int seeds are supported.
    Given a serialized String produced by INoise.stringSerialize(), reassigns this INoise to have the described state from the given String.
    Produces a String that describes everything needed to recreate this INoise in full.
     

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface INoise

    getNoise, getNoiseWithSeed, readExternal, writeExternal
  • Field Details

    • seed

      public long seed
  • Constructor Details

    • OpenSimplex2

      public OpenSimplex2()
    • OpenSimplex2

      public OpenSimplex2(long seed)
  • Method Details

    • getMinDimension

      public int getMinDimension()
      Gets the minimum dimension supported by this generator, which is 2 here. This generator can produce 2D, 3D, or 4D noise.
      Specified by:
      getMinDimension in interface INoise
      Returns:
      the minimum supported dimension, which is 2 here
    • getMaxDimension

      public int getMaxDimension()
      Gets the maximum dimension supported by this generator, which is 4 here. This generator can produce 2D, 3D, or 4D noise.
      Specified by:
      getMaxDimension in interface INoise
      Returns:
      the maximum supported dimension, which is 4 here
    • hasEfficientSetSeed

      public boolean hasEfficientSetSeed()
      Returns true if this generator can be seeded with setSeed(long) and retrieved with getSeed(), which this can be.
      Specified by:
      hasEfficientSetSeed in interface INoise
      Returns:
      true, because setSeed(long) and getSeed() are supported
    • getSeed

      public long getSeed()
      Description copied from interface: INoise
      Gets the current seed of the generator, as a long even if the seed is stored internally as an int. This must be implemented, but if the generator doesn't have a seed that can be expressed as a long (potentially using BitConversion.floatToIntBits(float)), this can just return 0.
      Specified by:
      getSeed in interface INoise
      Returns:
      the current seed, as a long
    • setSeed

      public void setSeed(long seed)
      Description copied from interface: INoise
      Sets the seed to the given long, if long seeds are supported, or (int)seed if only int seeds are supported. If this generator cannot be seeded, this should do nothing, and should not throw an exception. If this operation allocates or is time-intensive, then INoise.hasEfficientSetSeed() should return false. That method is checked in INoise.getNoiseWithSeed(float, float, long), and if it returns false, the noise call will avoid calling setSeed(). You can always at least try to set the seed, even if it does nothing or is heavy on performance, and doing it a few times each frame should typically be fine for any generator. In the case this is called thousands of times each frame, check INoise.hasEfficientSetSeed().
      Specified by:
      setSeed in interface INoise
      Parameters:
      seed - a long or int seed, with no restrictions unless otherwise documented
    • getNoise

      public float getNoise(float x, float y)
      Gets 2D noise with a default or pre-set seed.
      Specified by:
      getNoise in interface INoise
      Parameters:
      x - x position; can be any finite float
      y - y position; can be any finite float
      Returns:
      a noise value between -1.0f and 1.0f, both inclusive
      Throws:
      UnsupportedOperationException - if 2D noise cannot be produced by this generator
    • getNoise

      public float getNoise(float x, float y, float z)
      Gets 3D noise with a default or pre-set seed.
      Specified by:
      getNoise in interface INoise
      Parameters:
      x - x position; can be any finite float
      y - y position; can be any finite float
      z - z position; can be any finite float
      Returns:
      a noise value between -1.0f and 1.0f, both inclusive
      Throws:
      UnsupportedOperationException - if 3D noise cannot be produced by this generator
    • getNoise

      public float getNoise(float x, float y, float z, float w)
      Gets 4D noise with a default or pre-set seed.
      Specified by:
      getNoise in interface INoise
      Parameters:
      x - x position; can be any finite float
      y - y position; can be any finite float
      z - z position; can be any finite float
      w - w position; can be any finite float
      Returns:
      a noise value between -1.0f and 1.0f, both inclusive
      Throws:
      UnsupportedOperationException - if 4D noise cannot be produced by this generator
    • getNoise

      public float getNoise(float x, float y, float z, float w, float u)
      Gets 5D noise with a default or pre-set seed.
      Specified by:
      getNoise in interface INoise
      Parameters:
      x - x position; can be any finite float
      y - y position; can be any finite float
      z - z position; can be any finite float
      w - w position; can be any finite float
      u - u position; can be any finite float
      Returns:
      a noise value between -1.0f and 1.0f, both inclusive
      Throws:
      UnsupportedOperationException - if 5D noise cannot be produced by this generator
    • getNoise

      public float getNoise(float x, float y, float z, float w, float u, float v)
      Gets 6D noise with a default or pre-set seed.
      Specified by:
      getNoise in interface INoise
      Parameters:
      x - x position; can be any finite float
      y - y position; can be any finite float
      z - z position; can be any finite float
      w - w position; can be any finite float
      u - u position; can be any finite float
      v - v position; can be any finite float
      Returns:
      a noise value between -1.0f and 1.0f, both inclusive
      Throws:
      UnsupportedOperationException - if 6D noise cannot be produced by this generator
    • getNoiseWithSeed

      public float getNoiseWithSeed(float x, float y, long seed)
      Gets 2D noise with a specific seed. If the seed cannot be retrieved or changed per-call, then this falls back to getNoise(float, float); you can check if this will happen with hasEfficientSetSeed().
      Specified by:
      getNoiseWithSeed in interface INoise
      Parameters:
      x - x position; can be any finite float
      y - y position; can be any finite float
      seed -
      Returns:
      a noise value between -1.0f and 1.0f, both inclusive
      Throws:
      UnsupportedOperationException - if 2D noise cannot be produced by this generator
    • getNoiseWithSeed

      public float getNoiseWithSeed(float x, float y, float z, long seed)
      Gets 3D noise with a specific seed. If the seed cannot be retrieved or changed per-call, then this falls back to getNoise(float, float); you can check if this will happen with hasEfficientSetSeed().
      Specified by:
      getNoiseWithSeed in interface INoise
      Parameters:
      x - x position; can be any finite float
      y - y position; can be any finite float
      z - z position; can be any finite float
      seed -
      Returns:
      a noise value between -1.0f and 1.0f, both inclusive
      Throws:
      UnsupportedOperationException - if 3D noise cannot be produced by this generator
    • getNoiseWithSeed

      public float getNoiseWithSeed(float x, float y, float z, float w, long seed)
      Gets 4D noise with a specific seed. If the seed cannot be retrieved or changed per-call, then this falls back to getNoise(float, float); you can check if this will happen with hasEfficientSetSeed().
      Specified by:
      getNoiseWithSeed in interface INoise
      Parameters:
      x - x position; can be any finite float
      y - y position; can be any finite float
      z - z position; can be any finite float
      w - w position; can be any finite float
      seed -
      Returns:
      a noise value between -1.0f and 1.0f, both inclusive
      Throws:
      UnsupportedOperationException - if 4D noise cannot be produced by this generator
    • getNoiseWithSeed

      public float getNoiseWithSeed(float x, float y, float z, float w, float u, long seed)
      Gets 5D noise with a specific seed. If the seed cannot be retrieved or changed per-call, then this falls back to getNoise(float, float); you can check if this will happen with hasEfficientSetSeed().
      Specified by:
      getNoiseWithSeed in interface INoise
      Parameters:
      x - x position; can be any finite float
      y - y position; can be any finite float
      z - z position; can be any finite float
      w - w position; can be any finite float
      u - u position; can be any finite float
      seed -
      Returns:
      a noise value between -1.0f and 1.0f, both inclusive
      Throws:
      UnsupportedOperationException - if 5D noise cannot be produced by this generator
    • getNoiseWithSeed

      public float getNoiseWithSeed(float x, float y, float z, float w, float u, float v, long seed)
      Gets 6D noise with a specific seed. If the seed cannot be retrieved or changed per-call, then this falls back to getNoise(float, float); you can check if this will happen with hasEfficientSetSeed().
      Specified by:
      getNoiseWithSeed in interface INoise
      Parameters:
      x - x position; can be any finite float
      y - y position; can be any finite float
      z - z position; can be any finite float
      w - w position; can be any finite float
      u - u position; can be any finite float
      v - v position; can be any finite float
      seed -
      Returns:
      a noise value between -1.0f and 1.0f, both inclusive
      Throws:
      UnsupportedOperationException - if 6D noise cannot be produced by this generator
    • copy

      public OpenSimplex2 copy()
      Creates a copy of this INoise, which should be a deep copy for any mutable state but can be shallow for immutable types such as functions. This almost always just calls a copy constructor.
      The default implementation throws an UnsupportedOperationException only. Implementors are strongly encouraged to implement this in general, and that is required to use an INoise class with INoise.Serializer.
      Specified by:
      copy in interface INoise
      Returns:
      a copy of this INoise
    • stringSerialize

      public String stringSerialize()
      Description copied from interface: INoise
      Produces a String that describes everything needed to recreate this INoise in full. This String can be read back in by INoise.stringDeserialize(String) to reassign the described state to another INoise. The syntax here should always start and end with the ` character, which is used by INoise.stringDeserialize(String) to identify the portion of a String that can be read back. The ` character should not be otherwise used unless to serialize another INoise that this uses.
      If you use Base to produce String representations for numeric fields in an INoise, Base.BASE10 is strongly recommended; in most cases, you can just use string concatenation with the fields separated by the tilde character, "~". For printing a float field with Base to a StringBuilder sb, use Base.BASE10.appendGeneral(sb, field).
      The default implementation throws an UnsupportedOperationException only. INoise classes do not have to implement any serialization methods, but they aren't serializable by the methods in this class or in INoise.Serializer unless they do implement this, INoise.getTag(), INoise.stringDeserialize(String), and INoise.copy().
      Specified by:
      stringSerialize in interface INoise
      Returns:
      a String that describes this INoise for serialization
    • stringDeserialize

      public OpenSimplex2 stringDeserialize(String data)
      Description copied from interface: INoise
      Given a serialized String produced by INoise.stringSerialize(), reassigns this INoise to have the described state from the given String. The serialized String must have been produced by the same class as this object is.
      Any class that implements INoise.stringSerialize() should also implement this method in a compatible way. Using base-10 is expected for most uses. Using Base.BASE10.readFloat(data, start, end) may be useful to parse only the part of data between start and end.
      The default implementation throws an UnsupportedOperationException only. INoise classes do not have to implement any serialization methods, but they aren't serializable by the methods in this class or in INoise.Serializer unless they do implement this, INoise.getTag(), INoise.stringSerialize(), and INoise.copy().
      Specified by:
      stringDeserialize in interface INoise
      Parameters:
      data - a serialized String, typically produced by INoise.stringSerialize()
      Returns:
      this INoise, after being modified (if possible)
    • recreateFromString

      public static OpenSimplex2 recreateFromString(String data)
    • getTag

      public String getTag()
      Returns a typically-four-character String constant that should uniquely identify this INoise as well as possible. If a duplicate tag is already registered and INoise.Serializer.register(INoise) attempts to register the same tag again, a message is printed to System.err. Implementing this is required for any usage of Serializer.
      Specified by:
      getTag in interface INoise
      Returns:
      a short String constant that identifies this INoise type
    • noise2

      public static float noise2(long seed, float x, float y)
      2D Simplex noise, standard lattice orientation.
    • noise2_ImproveX

      public static float noise2_ImproveX(long seed, float x, float y)
      2D Simplex noise, with Y pointing down the main diagonal. Might be better for a 2D sandbox style game, where Y is vertical. Probably slightly less optimal for heightmaps or continent maps, unless your map is centered around an equator. It's a subtle difference, but the option is here to make it an easy choice.
    • noise3_ImproveXY

      public static float noise3_ImproveXY(long seed, float x, float y, float z)
      3D OpenSimplex2 noise, with better visual isotropy in (X, Y). Recommended for 3D terrain and time-varied animations. The Z coordinate should always be the "different" coordinate in whatever your use case is. If Y is vertical in world coordinates, call noise3_ImproveXZ(x, z, Y) or use noise3_XZBeforeY. If Z is vertical in world coordinates, call noise3_ImproveXZ(x, y, Z). For a time varied animation, call noise3_ImproveXY(x, y, T).
    • noise3_ImproveXZ

      public static float noise3_ImproveXZ(long seed, float x, float y, float z)
      3D OpenSimplex2 noise, with better visual isotropy in (X, Z). Recommended for 3D terrain and time-varied animations. The Y coordinate should always be the "different" coordinate in whatever your use case is. If Y is vertical in world coordinates, call noise3_ImproveXZ(x, Y, z). If Z is vertical in world coordinates, call noise3_ImproveXZ(x, Z, y) or use noise3_ImproveXY. For a time varied animation, call noise3_ImproveXZ(x, T, y) or use noise3_ImproveXY.
    • noise3_Fallback

      public static float noise3_Fallback(long seed, float x, float y, float z)
      3D OpenSimplex2 noise, fallback rotation option Use noise3_ImproveXY or noise3_ImproveXZ instead, wherever appropriate. They have less diagonal bias. This function's best use is as a fallback.
    • noise4_ImproveXYZ_ImproveXY

      public static float noise4_ImproveXYZ_ImproveXY(long seed, float x, float y, float z, float w)
      4D OpenSimplex2 noise, with XYZ oriented like noise3_ImproveXY and W for an extra degree of freedom. W repeats eventually. Recommended for time-varied animations which texture a 3D object (W=time) in a space where Z is vertical
    • noise4_ImproveXYZ_ImproveXZ

      public static float noise4_ImproveXYZ_ImproveXZ(long seed, float x, float y, float z, float w)
      4D OpenSimplex2 noise, with XYZ oriented like noise3_ImproveXZ and W for an extra degree of freedom. W repeats eventually. Recommended for time-varied animations which texture a 3D object (W=time) in a space where Y is vertical
    • noise4_ImproveXYZ

      public static float noise4_ImproveXYZ(long seed, float x, float y, float z, float w)
      4D OpenSimplex2 noise, with XYZ oriented like noise3_Fallback and W for an extra degree of freedom. W repeats eventually. Recommended for time-varied animations which texture a 3D object (W=time) where there isn't a clear distinction between horizontal and vertical
    • noise4_ImproveXY_ImproveZW

      public static float noise4_ImproveXY_ImproveZW(long seed, float x, float y, float z, float w)
      4D OpenSimplex2 noise, with XY and ZW forming orthogonal triangular-based planes. Recommended for 3D terrain, where X and Y (or Z and W) are horizontal. Recommended for noise(x, y, sin(time), cos(time)) trick.
    • noise4_Fallback

      public static float noise4_Fallback(long seed, float x, float y, float z, float w)
      4D OpenSimplex2 noise, fallback lattice orientation.
    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface INoise
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object