Class TaffyNoise
java.lang.Object
com.github.yellowstonegames.grid.TaffyNoise
- All Implemented Interfaces:
INoise, Externalizable, Serializable
A variant on
Consider using
PhantomNoise that also produces arbitrary-dimensional continuous noise, but that is optimized
for higher-dimensional output (4 and up, in particular). TaffyNoise doesn't slow down as rapidly as other forms of
noise do when dimensions are added. Whereas a call to n-dimensional value noise requires pow(2, n) points to
be hashed, each one taking O(n) operations, TaffyNoise only requires O(n) operations for its
comparable-to-value-noise primitive step. Phantom uses n+1 calls to n-dimensional value noise per point, and Taffy
uses n+1 of its faster primitive steps per call. Noise types like Simplex only need to hash n+1 points (each
O(n)), but Simplex loses quality in dimensions past 4 or so. Taffy actually gains quality in higher
dimensions, and even though it doesn't look best in 2 or 3 dimensions, it looks pretty good with just one octave.
Consider using
Noise with its Noise.TAFFY or Noise.TAFFY_FRACTAL noise type if you only need
noise in 3-7 dimensions (these are produced when requesting 2-6-dimensional noise, with the extra dimension editable
via Noise.setMutation(float)). Noise is faster than this class because it isn't as generalized to operate in
arbitrarily-high dimensions.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface INoise
INoise.Serializer -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionTaffyNoise(long seed, int dimension) TaffyNoise(long seed, int dimension, float sharpness) -
Method Summary
Modifier and TypeMethodDescriptioncopy()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.booleanintGets 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).intGets 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.floatgetNoise(float... args) floatgetNoise(float x, float y) Gets 2D noise with a default or pre-set seed.floatgetNoise(float x, float y, float z) Gets 3D noise with a default or pre-set seed.floatgetNoise(float x, float y, float z, float w) Gets 4D noise with a default or pre-set seed.floatgetNoise(float x, float y, float z, float w, float u) Gets 5D noise with a default or pre-set seed.floatgetNoise(float x, float y, float z, float w, float u, float v) Gets 6D noise with a default or pre-set seed.floatgetNoise(float x, float y, float z, float w, float u, float v, float m) Gets 7D noise with a default or pre-set seed.floatgetNoise2D(float x, float y) longgetSeed()Gets the current seed of the generator, as a long even if the seed is stored internally as an int.getTag()Returns a typically-four-character String constant that should uniquely identify this INoise as well as possible.booleanReturns true if this generator can be seeded withINoise.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.floatnoise(int dim, float... args) reassign(long seed, int dimension, float sharpness) static TaffyNoiserecreateFromString(String data) voidsetSeed(long seed) Sets the seed, BUT ALSO resets the vertices this has, using the seed to randomize them.stringDeserialize(String data) Given a serialized String produced byINoise.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.protected floatprotected floatvalueNoise(int dim) protected floatMethods inherited from class Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface INoise
getNoiseWithSeed, getNoiseWithSeed, getNoiseWithSeed, getNoiseWithSeed, getNoiseWithSeed, getNoiseWithSeed, readExternal, writeExternal
-
Field Details
-
seed
public long seed -
dim
public int dim -
sharpness
public float sharpness -
inverse
protected float inverse -
lesserInverse
protected float lesserInverse -
working
protected transient float[] working -
points
protected transient float[] points -
input
protected transient float[] input -
vertices
protected transient float[][] vertices
-
-
Constructor Details
-
TaffyNoise
public TaffyNoise() -
TaffyNoise
public TaffyNoise(long seed, int dimension) -
TaffyNoise
public TaffyNoise(long seed, int dimension, float sharpness)
-
-
Method Details
-
reassign
-
getSeed
public long getSeed()Description copied from interface:INoiseGets 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 usingBitConversion.floatToIntBits(float)), this can just return0. -
setSeed
public void setSeed(long seed) Sets the seed, BUT ALSO resets the vertices this has, using the seed to randomize them. This can be significantly more computationally expensive than a plain setter, but still shouldn't slow down most usage unless you call this method many times per frame. -
getTag
Description copied from interface:INoiseReturns a typically-four-character String constant that should uniquely identify this INoise as well as possible. If a duplicate tag is already registered andINoise.Serializer.register(INoise)attempts to register the same tag again, a message is printed toSystem.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. -
stringSerialize
Description copied from interface:INoiseProduces a String that describes everything needed to recreate this INoise in full. This String can be read back in byINoise.stringDeserialize(String)to reassign the described state to another INoise. The syntax here should always start and end with the`character, which is used byINoise.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 useBaseto produce String representations for numeric fields in an INoise,Base.BASE10is strongly recommended; in most cases, you can just use string concatenation with the fields separated by the tilde character,"~". For printing a floatfieldwith Base to a StringBuildersb, useBase.BASE10.appendGeneral(sb, field).
The default implementation throws anUnsupportedOperationExceptiononly. INoise classes do not have to implement any serialization methods, but they aren't serializable by the methods in this class or inINoise.Serializerunless they do implement this,INoise.getTag(),INoise.stringDeserialize(String), andINoise.copy().- Specified by:
stringSerializein interfaceINoise- Returns:
- a String that describes this INoise for serialization
-
stringDeserialize
Description copied from interface:INoiseGiven a serialized String produced byINoise.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 implementsINoise.stringSerialize()should also implement this method in a compatible way. Using base-10 is expected for most uses. UsingBase.BASE10.readFloat(data, start, end)may be useful to parse only the part of data between start and end.
The default implementation throws anUnsupportedOperationExceptiononly. INoise classes do not have to implement any serialization methods, but they aren't serializable by the methods in this class or inINoise.Serializerunless they do implement this,INoise.getTag(),INoise.stringSerialize(), andINoise.copy().- Specified by:
stringDeserializein interfaceINoise- Parameters:
data- a serialized String, typically produced byINoise.stringSerialize()- Returns:
- this INoise, after being modified (if possible)
-
recreateFromString
-
copy
Description copied from interface:INoiseCreates 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 anUnsupportedOperationExceptiononly. Implementors are strongly encouraged to implement this in general, and that is required to use an INoise class withINoise.Serializer. -
valueNoise
protected float valueNoise() -
valueNoise
protected float valueNoise(int dim) -
valueNoise2D
protected float valueNoise2D() -
getNoise
public float getNoise(float... args) -
noise
public float noise(int dim, float... args) -
getNoise2D
public float getNoise2D(float x, float y) -
equals
-
getMinDimension
public int getMinDimension()Description copied from interface:INoiseGets 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:
getMinDimensionin interfaceINoise- Returns:
- the minimum supported dimension, from 2 to 7 inclusive
-
getMaxDimension
public int getMaxDimension()Description copied from interface:INoiseGets 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:
getMaxDimensionin interfaceINoise- Returns:
- the maximum supported dimension, from 2 to 7 inclusive
-
hasEfficientSetSeed
public boolean hasEfficientSetSeed()Description copied from interface:INoiseReturns true if this generator can be seeded withINoise.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 howINoise.getNoiseWithSeed(float, float, long)changes the seed and how setSeed() does.- Specified by:
hasEfficientSetSeedin interfaceINoise- Returns:
- whether
INoise.setSeed(long)should be efficient to call in everyINoise.getNoiseWithSeed(float, float, long)call
-
getNoise
public float getNoise(float x, float y) Description copied from interface:INoiseGets 2D noise with a default or pre-set seed. -
getNoise
public float getNoise(float x, float y, float z) Description copied from interface:INoiseGets 3D noise with a default or pre-set seed. -
getNoise
public float getNoise(float x, float y, float z, float w) Description copied from interface:INoiseGets 4D noise with a default or pre-set seed. -
getNoise
public float getNoise(float x, float y, float z, float w, float u) Description copied from interface:INoiseGets 5D noise with a default or pre-set seed.- Specified by:
getNoisein interfaceINoise- Parameters:
x- x position; can be any finite floaty- y position; can be any finite floatz- z position; can be any finite floatw- w position; can be any finite floatu- 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:INoiseGets 6D noise with a default or pre-set seed.- Specified by:
getNoisein interfaceINoise- Parameters:
x- x position; can be any finite floaty- y position; can be any finite floatz- z position; can be any finite floatw- w position; can be any finite floatu- u position; can be any finite floatv- v 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, float m) Description copied from interface:INoiseGets 7D noise with a default or pre-set seed.- Specified by:
getNoisein interfaceINoise- Parameters:
x- x position; can be any finite floaty- y position; can be any finite floatz- z position; can be any finite floatw- w position; can be any finite floatu- u position; can be any finite floatv- v position; can be any finite floatm- m position; can be any finite float- Returns:
- a noise value between -1.0f and 1.0f, both inclusive
-