Class NoiseAdjustment

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

public class NoiseAdjustment extends Object implements INoise, com.github.yellowstonegames.core.ISerializersNeeded
Wraps another INoise and alters its output by running it through an Interpolations.Interpolator. The [-1,1] range for noise is mapped to the [0,1] range that Interpolator uses, and mapped back afterward. This uses Interpolator rather than the slightly-simpler Interpolations.InterpolationFunction because an Interpolator can be serialized just by storing its Interpolations.Interpolator.getTag(), and loaded by looking up its tag.
Example usage:
    Interpolator spline_06_03 = new Interpolator("spline_06_03", (f) -> MathTools.barronSpline(f, 0.6f, 0.3f));
    NoiseAdjustment centralizeLow = new NoiseAdjustment(new SimplexNoise(12345), spline_06_03);
The Interpolator is created separately so that it can be created (and thus registered) before any saving or loading step might happen. You may want to create Interpolators in their own file, much like how the Interpolations class defines many Interpolators for later usage. The NoiseAdjustment can use any INoise, such as a Noise or here a SimplexNoise.
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface INoise

    INoise.Serializer
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    com.github.tommyettinger.digital.Interpolations.Interpolator
     
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    The same as calling new NoiseAdjustment(new SimplexNoise(123), Interpolations.smooth).
    NoiseAdjustment(INoise toWrap, com.github.tommyettinger.digital.Interpolations.Interpolator adjust)
    Creates a NoiseAdjustment wrapping the given INoise and modifying its output with the given Interpolator.
    Copies the given NoiseAdjustment, using INoise.copy() to deep-copy the wrapped INoise, and just making a shallow copy of the adjustment because it is immutable.
  • 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
     
    com.github.tommyettinger.digital.Interpolations.Interpolator
     
    int
    Gets the maximum dimension supported by this generator, such as 2 for a generator that only is defined for flat surfaces, or 7 for one that is defined up to the highest dimension this interface knows about (7D).
    int
    Gets the minimum dimension supported by this generator, such as 2 for a generator that only is defined for flat surfaces, or 3 for one that is only defined for 3D or higher-dimensional spaces.
    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 INoise.setSeed(long) during each call to obtain noise, or false if calling setSeed() is slow enough or allocates enough that alternative approaches should be used.
    int
     
    setAdjustment(com.github.tommyettinger.digital.Interpolations.Interpolator adjustment)
     
    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.
    setWrapped(INoise wrapped)
     
    Given a serialized String produced by 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, toString, wait, wait, wait

    Methods inherited from interface INoise

    getNoise, getNoiseWithSeed, readExternal, writeExternal
  • Field Details

    • wrapped

      public INoise wrapped
    • adjustment

      public com.github.tommyettinger.digital.Interpolations.Interpolator adjustment
  • Constructor Details

    • NoiseAdjustment

      public NoiseAdjustment()
      The same as calling new NoiseAdjustment(new SimplexNoise(123), Interpolations.smooth).
    • NoiseAdjustment

      public NoiseAdjustment(INoise toWrap, com.github.tommyettinger.digital.Interpolations.Interpolator adjust)
      Creates a NoiseAdjustment wrapping the given INoise and modifying its output with the given Interpolator.
      Parameters:
      toWrap - an INoise, such as a Noise or SimplexNoise
      adjust - an Interpolator, such as Interpolations.exp5, to apply to the output of the INoise
    • NoiseAdjustment

      public NoiseAdjustment(NoiseAdjustment other)
      Copies the given NoiseAdjustment, using INoise.copy() to deep-copy the wrapped INoise, and just making a shallow copy of the adjustment because it is immutable.
      Parameters:
      other - another NoiseAdjustment to copy
  • Method Details

    • getWrapped

      public INoise getWrapped()
    • setWrapped

      public NoiseAdjustment setWrapped(INoise wrapped)
    • getAdjustment

      public com.github.tommyettinger.digital.Interpolations.Interpolator getAdjustment()
    • setAdjustment

      public NoiseAdjustment setAdjustment(com.github.tommyettinger.digital.Interpolations.Interpolator adjustment)
    • getMinDimension

      public int getMinDimension()
      Description copied from interface: INoise
      Gets the minimum dimension supported by this generator, such as 2 for a generator that only is defined for flat surfaces, or 3 for one that is only defined for 3D or higher-dimensional spaces.
      Specified by:
      getMinDimension in interface INoise
      Returns:
      the minimum supported dimension, from 2 to 7 inclusive
    • getMaxDimension

      public int getMaxDimension()
      Description copied from interface: INoise
      Gets the maximum dimension supported by this generator, such as 2 for a generator that only is defined for flat surfaces, or 7 for one that is defined up to the highest dimension this interface knows about (7D).
      Specified by:
      getMaxDimension in interface INoise
      Returns:
      the maximum supported dimension, from 2 to 7 inclusive
    • hasEfficientSetSeed

      public boolean hasEfficientSetSeed()
      Description copied from interface: INoise
      Returns true if this generator can be seeded with INoise.setSeed(long) during each call to obtain noise, or false if calling setSeed() is slow enough or allocates enough that alternative approaches should be used. You can always call setSeed() on your own, but generators that don't have any seed won't do anything. Generators that return false for this method will generally behave differently when comparing how INoise.getNoiseWithSeed(float, float, long) changes the seed and how setSeed() does.
      Specified by:
      hasEfficientSetSeed in interface INoise
      Returns:
      whether INoise.setSeed(long) should be efficient to call in every INoise.getNoiseWithSeed(float, float, long) call
    • copy

      public NoiseAdjustment copy()
      Description copied from interface: INoise
      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
    • getNoise

      public float getNoise(float x, float y)
      Description copied from interface: INoise
      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
    • getNoise

      public float getNoise(float x, float y, float z)
      Description copied from interface: INoise
      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
    • getNoise

      public float getNoise(float x, float y, float z, float w)
      Description copied from interface: INoise
      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
    • getNoise

      public float getNoise(float x, float y, float z, float w, float u)
      Description copied from interface: INoise
      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
    • getNoise

      public float getNoise(float x, float y, float z, float w, float u, float v)
      Description copied from interface: INoise
      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
    • 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
    • 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
    • getNoiseWithSeed

      public float getNoiseWithSeed(float x, float y, long seed)
      Description copied from interface: INoise
      Gets 2D noise with a specific seed. If the seed cannot be retrieved or changed per-call, then this falls back to changing the position instead of the seed; you can check if this will happen with INoise.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 - can be any long
      Returns:
      a noise value between -1.0f and 1.0f, both inclusive
    • getNoiseWithSeed

      public float getNoiseWithSeed(float x, float y, float z, long seed)
      Description copied from interface: INoise
      Gets 3D noise with a specific seed. If the seed cannot be retrieved or changed per-call, then this falls back to changing the position instead of the seed; you can check if this will happen with INoise.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 - can be any long
      Returns:
      a noise value between -1.0f and 1.0f, both inclusive
    • getNoiseWithSeed

      public float getNoiseWithSeed(float x, float y, float z, float w, long seed)
      Description copied from interface: INoise
      Gets 4D noise with a specific seed. If the seed cannot be retrieved or changed per-call, then this falls back to changing the position instead of the seed; you can check if this will happen with INoise.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 - can be any long
      Returns:
      a noise value between -1.0f and 1.0f, both inclusive
    • getNoiseWithSeed

      public float getNoiseWithSeed(float x, float y, float z, float w, float u, long seed)
      Description copied from interface: INoise
      Gets 5D noise with a specific seed. If the seed cannot be retrieved or changed per-call, then this falls back to changing the position instead of the seed; you can check if this will happen with INoise.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 - can be any long
      Returns:
      a noise value between -1.0f and 1.0f, both inclusive
    • getNoiseWithSeed

      public float getNoiseWithSeed(float x, float y, float z, float w, float u, float v, long seed)
      Description copied from interface: INoise
      Gets 6D noise with a specific seed. If the seed cannot be retrieved or changed per-call, then this falls back to changing the position instead of the seed; you can check if this will happen with INoise.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 - can be any long
      Returns:
      a noise value between -1.0f and 1.0f, both inclusive
    • getTag

      public String getTag()
      Description copied from interface: INoise
      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. The default implementation returns the String (NO), which is essentially an invalid tag, meant to indicate that this was not fully implemented. Implementing this is required for any usage of Serializer.
      Specified by:
      getTag in interface INoise
      Returns:
      "NAdj"
    • stringSerialize

      public String stringSerialize()
      Produces a String that describes everything needed to recreate this INoise in full. This String can be read back in by 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.Serializer.deserialize(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.
      Specified by:
      stringSerialize in interface INoise
      Returns:
      a String that describes this INoise for serialization
    • stringDeserialize

      public NoiseAdjustment stringDeserialize(String data)
      Given a serialized String produced by 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.
      Specified by:
      stringDeserialize in interface INoise
      Parameters:
      data - a serialized String, typically produced by stringSerialize()
      Returns:
      this INoise, after being modified (if possible)
    • 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
    • getSerializersNeeded

      public List<Class<?>> getSerializersNeeded()
      Specified by:
      getSerializersNeeded in interface com.github.yellowstonegames.core.ISerializersNeeded