NoiseGenerator
Package: Libraries.Compute.Noise · Inherits from: Libraries.Language.Object
The NoiseGenerator class is the parent of classes that produce coherent noise: numbers that change smoothly from place to place rather than jumping about, so that positions near each other give values near each other. A class inheriting from this one supplies GetNoise, which answers the noise at a position, and receives from here the ways of combining that noise across several scales at once. Those combinations turn a single smooth wobble into something with both broad shape and fine detail, and they are written in terms of GetNoise alone, so any noise placed under this class gains them without further work. There is one rule an implementation has to obey. GetNoise must be a pure function of its arguments: the same position gives the same answer every time it is asked, whatever was asked before it. A class that draws numbers from a random sequence while it samples breaks that rule, and with it every use that depends on two separate pieces of work agreeing about the same place, such as two terrain tiles meeting along a shared edge. Randomness belongs in Reseed, which runs once, and not in sampling, which runs everywhere. Every action here answers between -1 and 1. Use ToUnitRange to move an answer to between 0 and 1 when something downstream wants it that way.
Example Code
use Libraries.Compute.Noise.PerlinNoise
class Main
action Main
PerlinNoise noise
noise:SetSeed(1979)
noise:SetFrequency(0.01)
noise:SetOctaves(5)
number value = noise:GetAdditiveNoise(120, 340)
end
end
Actions
GetAdditiveNoise(number x, number y)
This action adds several copies of the noise together, which is often called fractal noise. Each copy is sampled at a higher frequency and a smaller height than the one before, so the first gives the broad shape of the surface and the later ones roughen it. The total is divided by the combined height of all the copies, so the answer stays between -1 and 1 however many are used.
Parameters
xnumber: The position along the first axis.ynumber: The position along the second axis.
Returns
number: The combined noise, between -1 and 1.
GetFrequency()
This action sets which noise gets generated. The same seed always gives the same noise, so a world can be thrown away and rebuilt from its seed alone.
Returns
number
GetLacunarity()
This action sets how many copies of the noise are combined. Each copy costs another sample, so the work grows in step with this number, and copies finer than the spacing of whatever is being filled in add cost without adding anything anyone can see.
Returns
number
GetMultiplicativeNoise(number x, number y)
This action multiplies several copies of the noise together instead of adding them. Each copy is moved to between 0 and 1 first, so that the product stays there, and the answer is moved back to between -1 and 1 at the end to match the rest of this class. Because numbers below one shrink when multiplied, the answer spends most of its time near the bottom of its range and rises only where every copy happens to be high at the same place. Used for terrain that reads as broad low ground broken by occasional high ground, where additive noise gives rolling country everywhere instead.
Parameters
xnumber: The position along the first axis.ynumber: The position along the second axis.
Returns
number: The combined noise, between -1 and 1.
GetNoise(number x, number y)
This action returns the noise at a position, between -1 and 1. Every class inheriting from NoiseGenerator supplies this action, and it has to be a pure function of its two arguments. The frequency is not applied here: this action answers the noise at exactly the position given, and the combining actions below scale positions before they call it.
Parameters
xnumber: The position along the first axis.ynumber: The position along the second axis.
Returns
number: The noise at that position, between -1 and 1.
GetOctaves()
This action sets how much of the noise fits into a unit of distance. A frequency of a hundredth means the noise takes about a hundred units to complete a feature. Take care not to set this to a whole number while sampling at whole positions. Perlin noise is exactly zero wherever its lattice crosses, and a frequency of one puts every whole position on a crossing, so a height map sampled that way comes out perfectly flat.
Returns
integer
GetPersistence()
This action sets how much the frequency rises from one copy of the noise to the next.
Returns
number
GetRidgeGain()
This action sets how much of its height each copy of the noise keeps compared to the one before it. Below a half the shape stays smooth, and above it the fine copies begin to dominate and the result turns rough.
Returns
number
GetRidgedMultifractalNoise(number x, number y)
This action combines octaves the way a ridged multifractal does, folding the noise about an offset, squaring what is left, and turning it upside down, so that the crease the fold puts in at the noise's zero crossings becomes the floor of a sharp valley rather than the top of a ridge. Ground between the valleys is left smooth. That is the opposite way up from GetRidgedNoise, which puts crests at the crossings instead. The two are worth thinking of as a pair: one cuts canyons into high ground, the other raises ridges out of low ground. Each octave is also held back by how high the one before it stood, bent by an exponent so that the falling off is gradual rather than abrupt, which gathers fine detail near the valley walls and leaves the ground between them smooth.
Parameters
xnumber: The position along the first axis.ynumber: The position along the second axis.
Returns
number: The combined noise, between -1 and 1.
GetRidgedNoise(number x, number y)
This action folds the noise about zero before adding its octaves together, which turns the places where the noise crossed zero into sharp crests instead of gentle slopes. Additive noise wanders smoothly through zero and gives rounded hills. Taking the distance from zero instead, and turning it upside down, leaves a value that is highest exactly where the noise changed sign. Folding a smooth curve that way puts a corner in it, and it is that corner, repeated at every scale, that reads as a ridge line. The result is squared afterwards to draw the crests finer and settle the ground on either side of them. Each octave is also held back by how high the octave before it stood, so that fine detail gathers along the crests while the low ground between them stays smooth. Without that, every scale ridges everywhere at once and the result reads as crumpled paper rather than as mountains. How sharply detail is gathered is set by SetRidgeGain.
Parameters
xnumber: The position along the first axis.ynumber: The position along the second axis.
Returns
number: The combined noise, between -1 and 1.
GetSeed()
This action moves a value from between -1 and 1 to between 0 and 1, which is what things like height maps expect. Values that stray outside the range are brought back to its edge rather than allowed through, since a height above the top of a map wraps around into a cliff.
Returns
number: The same value expressed between 0 and 1.
GetSlopeAlpha()
This action measures the slope of additive noise and wears the steep parts of it down. The slope is taken across the position rather than from it, a step either way along each axis, so that the answer does not lean toward one side. That costs five samples of the surface where an unweighted height costs one, so it is the most expensive thing in this class by some way.
Returns
number: The worn down height, between -1 and 1.
GetSlopeSpacing()
This action bends the second axis of a position. See GetWarpedX. The far apart sampling positions here and in GetWarpedX are what keep the two bends from resembling one another. Sampled at the same place they would move every position along a single diagonal instead of in a swirl.
Returns
number
GetSlopeWeightedNoise(number x, number y)
This action measures the slope of additive noise and wears the steep parts of it down. The slope is taken across the position rather than from it, a step either way along each axis, so that the answer does not lean toward one side. That costs five samples of the surface where an unweighted height costs one, so it is the most expensive thing in this class by some way.
Parameters
xnumber: The position along the first axis.ynumber: The position along the second axis.
Returns
number: The worn down height, between -1 and 1.
GetWarpFrequency()
total there too, and the last step moves it to the range the rest of this class uses.
Returns
number
GetWarpStrength()
The first octave stands on its own. Every one after it is held back by the last.
Returns
number
GetWarpedX(number x, number y)
This action bends a position sideways before the surface is sampled at it, which is what turns the even, soapy look of plain fractal noise into something with the folded, dragged quality of real ground. Nothing about the surface changes; it is asked about somewhere slightly different, and how far it is dragged is itself decided by noise. Use it by bending a position and sampling with the result, which works with any of the ways of combining octaves rather than only one: number bentX = noise:GetWarpedX(x, y) number bentY = noise:GetWarpedY(x, y) number height = noise:GetRidgedNoise(bentX, bentY) The bend is worked out from the position alone, so a bent surface is still the same everywhere it is asked about, and two terrain tiles sharing an edge still meet along it.
Parameters
xnumber: The position along the first axis.ynumber: The position along the second axis.
Returns
number: The first axis of the bent position.
GetWarpedY(number x, number y)
This action bends the second axis of a position. See GetWarpedX. The far apart sampling positions here and in GetWarpedX are what keep the two bends from resembling one another. Sampled at the same place they would move every position along a single diagonal instead of in a swirl.
Parameters
xnumberynumber
Returns
number
Reseed()
This action rebuilds whatever an implementation derives from the seed, such as a table of shuffled numbers. It runs when the seed changes, and it is the only place an implementation may draw from a random sequence.
SetFrequency(number frequency)
This action sets how much of the noise fits into a unit of distance. A frequency of a hundredth means the noise takes about a hundred units to complete a feature. Take care not to set this to a whole number while sampling at whole positions. Perlin noise is exactly zero wherever its lattice crosses, and a frequency of one puts every whole position on a crossing, so a height map sampled that way comes out perfectly flat.
Parameters
frequencynumber: How much noise fits into a unit of distance.
SetLacunarity(number lacunarity)
This action sets how much the frequency rises from one copy of the noise to the next.
Parameters
lacunaritynumber: The rise in frequency per copy, usually two.
SetOctaves(integer octaves)
This action sets how many copies of the noise are combined. Each copy costs another sample, so the work grows in step with this number, and copies finer than the spacing of whatever is being filled in add cost without adding anything anyone can see.
Parameters
octavesinteger: How many copies to combine, at least one.
SetPersistence(number persistence)
This action sets how much of its height each copy of the noise keeps compared to the one before it. Below a half the shape stays smooth, and above it the fine copies begin to dominate and the result turns rough.
Parameters
persistencenumber: The share of height kept per copy, usually a half.
SetRidgeGain(number ridgeGain)
This action sets how strongly ridged noise gathers its detail onto the crests, and has no effect on any of the other ways of combining octaves. At one, an octave passes on exactly as much allowance as it stood tall, and detail thins out gently down the flanks. Above that, allowance is granted more freely, so detail reaches further down and the whole surface roughens. At zero, every octave after the first is silenced and only the broadest ridge remains.
Parameters
ridgeGainnumber: How freely detail is granted to the crests, usually two.
SetSeed(number seed)
This action sets which noise gets generated. The same seed always gives the same noise, so a world can be thrown away and rebuilt from its seed alone.
Parameters
seednumber: The number that decides the noise.
SetSlopeAlpha(number slopeAlpha)
This action pushes a height toward the middle of its range in proportion to how steep the ground is there, so that gentle country is left alone and cliffs are worn down. The steepness is the length of the slope taken in both directions at once, and the height is multiplied by a number that falls away as that steepness grows.
Parameters
slopeAlphanumber
SetSlopeSpacing(number slopeSpacing)
This action bends a position sideways before the surface is sampled at it, which is what turns the even, soapy look of plain fractal noise into something with the folded, dragged quality of real ground. Nothing about the surface changes; it is asked about somewhere slightly different, and how far it is dragged is itself decided by noise. Use it by bending a position and sampling with the result, which works with any of the ways of combining octaves rather than only one: number bentX = noise:GetWarpedX(x, y) number bentY = noise:GetWarpedY(x, y) number height = noise:GetRidgedNoise(bentX, bentY) The bend is worked out from the position alone, so a bent surface is still the same everywhere it is asked about, and two terrain tiles sharing an edge still meet along it.
Parameters
slopeSpacingnumber
SetWarpFrequency(number warpFrequency)
after it push the total past the range the shares below are counting on.
Parameters
warpFrequencynumber
SetWarpStrength(number warpStrength)
This action folds the noise about zero before adding its octaves together, which turns the places where the noise crossed zero into sharp crests instead of gentle slopes. Additive noise wanders smoothly through zero and gives rounded hills. Taking the distance from zero instead, and turning it upside down, leaves a value that is highest exactly where the noise changed sign. Folding a smooth curve that way puts a corner in it, and it is that corner, repeated at every scale, that reads as a ridge line. The result is squared afterwards to draw the crests finer and settle the ground on either side of them. Each octave is also held back by how high the octave before it stood, so that fine detail gathers along the crests while the low ground between them stays smooth. Without that, every scale ridges everywhere at once and the result reads as crumpled paper rather than as mountains. How sharply detail is gathered is set by SetRidgeGain.
Parameters
warpStrengthnumber
ToUnitRange(number value)
This action moves a value from between -1 and 1 to between 0 and 1, which is what things like height maps expect. Values that stray outside the range are brought back to its edge rather than allowed through, since a height above the top of a map wraps around into a cliff.
Parameters
valuenumber: A value between -1 and 1.
Returns
number: The same value expressed between 0 and 1.
WeightBySlope(number height, number slopeX, number slopeY)
This action pushes a height toward the middle of its range in proportion to how steep the ground is there, so that gentle country is left alone and cliffs are worn down. The steepness is the length of the slope taken in both directions at once, and the height is multiplied by a number that falls away as that steepness grows.
Parameters
heightnumber: The height at the position.slopeXnumber: How fast the height is changing along the first axis.slopeYnumber: How fast the height is changing along the second axis.
Returns
number