Noises
Cubiomes.Noises
— ModuleWorking over raw noise functions is very low-level and should only be used as a last resort or for performance reasons.
Noises is a module to generate and sample various types of noise functions used in the procedural generation of Minecraft worlds. The result are always floating, but the input can be any type of number.
A noise object can be quite big in memory, so we can create an undefined noise object and initialize it without copying it with the setrng!🎲
function, saving time and memory.
The main uses are with the functions:
Noise
: create an undefined noise object.setrng!🎲
: initialize the noise object.Noise🎲
: create and initialize the noise object in one step.sample_noise
: sample the noise at a given point.
The noises implemented are:
Perlin
: a Perlin noise.Octaves
: a sum ofN
Perlin noises.DoublePerlin
: a sum of two independent and identically distributed Octaves noises.
Index
Cubiomes.Noises.DoublePerlin
Cubiomes.Noises.Noise
Cubiomes.Noises.Noise
Cubiomes.Noises.Octaves
Cubiomes.Noises.Perlin
Cubiomes.Noises.Noise🎲
Cubiomes.Noises.indexed_lerp
Cubiomes.Noises.init_coord_values
Cubiomes.Noises.interpolate_perlin
Cubiomes.Noises.next_perlin🎲
Cubiomes.Noises.sample_noise
Cubiomes.Noises.sample_simplex
Cubiomes.Noises.setrng!🎲
Cubiomes.Noises.shuffle!🎲
Cubiomes.Noises.simplex_gradient
Cubiomes.Noises.smoothstep_perlin_unsafe
Cubiomes.Noises.unsafe_setrng!🎲
API
Cubiomes.Noises.DoublePerlin
— TypeDoublePerlin{N} <: Noise
A double Perlin noise implementation. It's a sum of two independent and identically distributed (iid) Octaves{N} noise.
Cubiomes.Noises.Noise
— TypeNoise
The abstract type for a Noise sampler.
Methods
sample_noise
setrng!🎲
Noise(::Type{Noise}, ::UndefInitializer, ...)
Noise🎲
See also: Perlin
, Octaves
, DoublePerlin
Cubiomes.Noises.Noise
— MethodNoise(::Type{T}, ::UndefInitializer) where {T<:Noise}
Noise(::Type{DoublePerlin}; ::UndefInitializer, amplitudes)
Create a noise of type T
with an undefined state, i.e., it is not initialized yet. Use setrng!🎲
or unsafe_setrng!🎲
to initialize it.
See also: Noise🎲
, setrng!🎲
, unsafe_setrng!🎲
Cubiomes.Noises.Octaves
— TypeOctaves{N} <: Noise
An ordered collection of N
Perlin objects representing the octaves of a noise.
See also: Noise
, [sample_noise
], Perlin
, DoublePerlin
Cubiomes.Noises.Perlin
— TypePerlin <: Noise
The type for the perlin noise. See https://en.wikipedia.org/Perlin_Noise to know how it works.
See also: Noise
, sample_noise
, sample_simplex
Cubiomes.Noises.Noise🎲
— MethodNoise🎲(::Type{T}, rng::AbstractJavaRNG, args...) where {N, T<:Noise}
Create a noise of type T
and initialize it with the given random number generator rng
. Other arguments are used to initialize the noise. They depend on the noise type and they are the same as the arguments of the setrng!🎲
function.
Strictly equivalent to
julia> noise = Noise(T, undef) # or Noise(T, undef, args[1]) for DoublePerlin
T(...)
julia> setrng!🎲(noise, rng, args...)`.
Cubiomes.Noises.sample_noise
— Functionsample_noise(noise::Perlin, x, z, y=missing, yamp=0, ymin=0)
sample_noise(noise::Octaves, x, z, y=missing, yamp=missing, ymin=missing)
sample_noise(noise::DoublePerlin, x, z, y=missing, [move_factor,])
Sample the given noise at the specified coordinates.
See also: sample_simplex
, Noise
, Noise🎲
Cubiomes.Noises.sample_simplex
— Functionsample_simplex(noise::Perlin, x, y)
Sample the given noise at the given coordinate using the simplex noise algorithm instead of the perlin one. See https://en.wikipedia.org/wiki/Simplex_noise
See also: sample_noise
, Perlin
Cubiomes.Noises.setrng!🎲
— Functionsetrng!🎲(noise::Perlin, rng)
setrng!🎲(noise::Octaves{N}, rng::JavaRandom, octave_min) where N
setrng!🎲(noise::Octaves{N}, rng::JavaXoroshiro128PlusPlus, amplitudes, octave_min) where N
setrng!🎲(noise::DoublePerlin{N}, rng, octave_min) where N
setrng!🎲(noise::DoublePerlin{N}, rng, amplitudes, octave_min) where N
` Initialize the noise in place with the given random number generator (of type AbstractJavaRNG).
N
represents the number of octaves, each associated with a non-zero amplitude. Therefore, N
MUST be equal to the number of non-zero values in amplitudes. This number can be obtained with Cubiomes.length_filter(!iszero, amplitudes)
. For performance reasons, it is possible to lower N
and completely ignore the last amplitudes using unsafe_setrng!🎲
.
Since the last amplitudes are ignored if they are set to zero, replace the tuple of amplitudes with the trimmed version without the last zeros can save a very small amount of memory / time. However, only do this if the trimmed amplitudes are already known. Computing them only for this function call will not save any time.
See also: unsafe_setrng!🎲
, Noise
, Noise🎲
Cubiomes.Noises.unsafe_setrng!🎲
— Functionunsafe_setrng!🎲(noise, rng::JavaXoroshiro128PlusPlus, amplitudes, octave_min)
Same as setrng!🎲
but allows to skip some octaves for performance reasons, i.e. N
can be less than the number of non-zero values in amplitudes
, and the last octaves are completely ignored. If instead N
is greater, the behavior is undefined.
Private API
Click to see
Cubiomes.Noises.indexed_lerp
— Methodindexed_lerp(idx::Integer, x, y, z)
Use the lower 4 bits of idx
as a simple hash to combine the x
, y
, and z
values into a single number (a new index), to be used in the Perlin noise interpolation.
Cubiomes.Noises.init_coord_values
— Methodinit_coord_values(coord)
Initialize one coordinate for the Perlin noise sampling.
Returns:
- the fractional part of
coord
- the integer part of
coord
, modulo UInt8 - the smoothstep value of the fractional part of
coord
See also: smoothstep_perlin_unsafe
, sample_noise
, Perlin
Cubiomes.Noises.interpolate_perlin
— Methodinterpolate_perlin(
idx::PermsType,
d1, d2, d3,
h1, h2, h3,
t1, t2, t3
) -> Real
Interpolate the Perlin noise at the given coordinates.
Arguments
- The
idx
parameter is the permutations array. - The
d1
,d2
, andd3
parameters are the fractional parts of thex
,y
, andz
coordinates.
- The
h1
,h2
, andh3
parameters are the integer parts of thex
,y
, andz
coordinates. They MUST be between 0 and 255.
- The
t1
,t2
, andt3
parameters are the smoothstep values of the fractional parts
of the x
, y
, and z
coordinates.
See also: init_coord_values
, sample_noise
, Perlin
Cubiomes.Noises.next_perlin🎲
— Functionnext_perlin🎲(rng::JavaRandom, ::Type{Int32}; start=0, stop) -> Int32
next_perlin🎲(rng::JavaXoroshiro128PlusPlus, ::Type{Int32}; start=0, stop) -> Int3
Same as next🎲
but with a different implementation specific for the perlin noise. Don't ask why this is different, it's just how Minecraft does it.
See also: next🎲
Cubiomes.Noises.shuffle!🎲
— Methodshuffle!🎲(rng::AbstractRNG_MC, perms::PermsType)
Shuffle the permutations array using the given random number generator.
Cubiomes.Noises.simplex_gradient
— Methodsimplex_gradient(idx, x, y, z, d)
Compute the gradient of the simplex noise at the given coordinates.
Arguments
idx
: Index used for interpolation.x
,y
,z
: Coordinates in the simplex grid.d
: Constant used to determine the influence of the point in the grid.
See also: sample_simplex
Cubiomes.Noises.smoothstep_perlin_unsafe
— Methodsmoothstep_perlin_unsafe(x)
Compute $6x^5 - 15x^4 + 10x^3$, the smoothstep function used in Perlin noise. See https://en.wikipedia.org/wiki/Smoothstep#Variations for more details.
This function is unsafe because it is assuming that $0 \leq x \leq 1$ (it does not clamp the input).