TerrainGenerator
Package: Libraries.Game.Graphics.Worlds · Inherits from: Libraries.Language.Object
A TerrainGenerator makes and changes terrain in bulk, for building test worlds quickly. It has four kinds of tool: 1. **Presets** fill a rectangle of samples with ground from noise: rolling hills, mountains, plateaus, canyons, or flat ground. The same seed always gives the same ground, and the noise is laid out by world position, so a preset looks the same at any vertex spacing. 2. **Stamps** press a shape into the ground in one go, like Ghost Recon Wildlands' "stamp a mountain in one click": a hill, a mountain, a mesa, a crater or a ridge. A stamp can be any size and turned to any angle, and any square of numbers can be used as a shape. 3. **Erosion** wears the ground the way weather does. Rain erosion sends drops of water downhill, each picking up ground where it speeds up and dropping it where it slows down, which carves channels and fills hollows. Slope settling lets ground slide off slopes steeper than a set angle. Both move ground around rather than adding or removing it. 4. **Auto paint** sets the splat weights by rules: rock on steep slopes, sand on low ground, dirt in hollows, and grass everywhere else, using the red (grass), green (rock), blue (dirt) and alpha (sand) layers. Everything works on samples that already have regions; it never adds regions. Changes go into the TerrainData's edited areas, so a WorldTerrain's Rebuild redraws them. To make them undoable, wrap them in a TerrainStroke with BeginOperation, CopyArea and End.
Example Code
use Libraries.Game.Graphics.Worlds.TerrainData
use Libraries.Game.Graphics.Worlds.TerrainGenerator
TerrainData data
data:SetRegionSize(64)
data:AddRegion(0, 0)
TerrainGenerator generator
generator:GeneratePreset(data, generator:PRESET_MOUNTAINS, 0, 0, 64, 64, 40)
generator:Stamp(data, generator:MakeStampShape(generator:STAMP_CRATER, 65), 65, 16, 16, 10, 0, 6, generator:STAMP_ADD)
generator:Erode(data, 0, 0, 63, 63, 1000)
generator:AutoPaint(data, 0, 0, 63, 63)
Variables
integer STAMP_MESAinteger STAMP_HILLinteger PRESET_CANYONSinteger STAMP_CRATERinteger PRESET_ROLLING_HILLSinteger PRESET_FLATinteger STAMP_RAISEinteger STAMP_ADD: to it (the ground stays where it is already higher).integer STAMP_MOUNTAINinteger STAMP_RIDGE: to it (the ground stays where it is already higher).integer PRESET_PLATEAUSinteger STAMP_SUBTRACTinteger PRESET_MOUNTAINS
Actions
AutoPaint(Libraries.Game.Graphics.Worlds.TerrainData data, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ)
This action sets the splat weights of a rectangle of samples by rules: rock (green) where the ground is steep, fading in between two slopes; sand (alpha) below a height; dirt (blue) in hollows, where the ground is lower than the average around it; and grass (red) everywhere else. Rock comes first, then sand, then dirt, each taking its share of what is left. Samples with no region are skipped, and samples whose weights already match are left alone, so painting the same area twice changes nothing the second time.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain.minimumXinteger: The rectangle's smallest sample along x.minimumZinteger: The rectangle's smallest sample along z.maximumXinteger: The rectangle's largest sample along x.maximumZinteger: The rectangle's largest sample along z.
Erode(Libraries.Game.Graphics.Worlds.TerrainData data, integer startMinimumX, integer startMinimumZ, integer startMaximumX, integer startMaximumZ, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ, integer drops)
This action wears ground as Erode does, but the drops of rain start in one rectangle and may run on through a larger one around it. Ground picked up under a brush is then carried downhill and away, rather than all settling back inside the brush, so eroding one spot again and again keeps carving instead of leveling it. Slope settling covers the larger rectangle.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain.startMinimumXinteger: The smallest sample along x where drops start.startMinimumZinteger: The smallest sample along z where drops start.startMaximumXinteger: The largest sample along x where drops start.startMaximumZinteger: The largest sample along z where drops start.minimumXinteger: The smallest sample along x drops may reach. Ground moves within this rectangle and one sample around it.minimumZinteger: The smallest sample along z drops may reach.maximumXinteger: The largest sample along x drops may reach.maximumZinteger: The largest sample along z drops may reach.dropsinteger: How many drops of rain to send.
Erode(Libraries.Game.Graphics.Worlds.TerrainData data, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ, integer drops)
This action wears a rectangle of ground as weather would: rain erosion with a number of drops, then a few passes of slope settling to soften what the rain left too steep. Ground only moves within the rectangle and one sample around it.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain.minimumXinteger: The rectangle's smallest sample along x.minimumZinteger: The rectangle's smallest sample along z.maximumXinteger: The rectangle's largest sample along x.maximumZinteger: The rectangle's largest sample along z.dropsinteger: How many drops of rain to send down the ground.
ErodeDrop(Libraries.Game.Graphics.Worlds.TerrainData data, number startX, number startZ, Libraries.Compute.Random random, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ)
This action sends one drop of rain from a point: it rolls downhill, picking up ground where it can carry more and leaving it where it can carry less, as ErodeWithRain describes, until it leaves the rectangle, reaches ground with no region, runs dry, or has rolled maximumSteps samples. A brush's stroke uses it to rain where the brush is.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain.startXnumber: Where the drop starts along x, in samples.startZnumber: Where the drop starts along z, in samples.randomLibraries.Compute.Random: The random numbers, for directions on flat ground.minimumXinteger: The smallest sample along x the drop may reach.minimumZinteger: The smallest sample along z the drop may reach.maximumXinteger: The largest sample along x the drop may reach.maximumZinteger: The largest sample along z the drop may reach.
ErodeWithRain(Libraries.Game.Graphics.Worlds.TerrainData data, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ, integer drops)
This action sends drops of rain down a rectangle of ground. Each drop starts at a random sample, rolls downhill, picks up ground where it can carry more (it is fast, or the ground falls away steeply), and drops ground where it can carry less (it slows, or the ground rises). When it leaves the rectangle, reaches ground with no region, or runs dry, it drops what it carries. So ground is moved, not added or taken away. Ground picked up, and what a drop leaves when it stops, is shared among the samples near it (spreadRadius), as in Beyer's method, so single samples aren't dug into pits or piled into spikes.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain.minimumXinteger: The rectangle's smallest sample along x.minimumZinteger: The rectangle's smallest sample along z.maximumXinteger: The rectangle's largest sample along x.maximumZinteger: The rectangle's largest sample along z.dropsinteger: How many drops.
ErodeWithRain(Libraries.Game.Graphics.Worlds.TerrainData data, integer startMinimumX, integer startMinimumZ, integer startMaximumX, integer startMaximumZ, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ, integer drops)
This action sends drops of rain as the other ErodeWithRain does, but the drops start in one rectangle and may run on through a larger one.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain.startMinimumXinteger: The smallest sample along x where drops start.startMinimumZinteger: The smallest sample along z where drops start.startMaximumXinteger: The largest sample along x where drops start.startMaximumZinteger: The largest sample along z where drops start.minimumXinteger: The smallest sample along x drops may reach.minimumZinteger: The smallest sample along z drops may reach.maximumXinteger: The largest sample along x drops may reach.maximumZinteger: The largest sample along z drops may reach.dropsinteger: How many drops.
FillAutoPaintGoal(Libraries.Game.Graphics.Worlds.TerrainData data, integer x, integer z, integer layerCount, Libraries.Containers.Number32BitArray goals, integer start)
This action works out the splat weights the painting rules give one sample, as AutoPaint describes, and puts its four numbers (red, green, blue and alpha) into an array, from a place in it. Layers past the terrain's count are folded into the first, as TerrainData's FoldSplatLayers does. A brush's stroke uses these as the goals it paints toward.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain.xinteger: The sample along x.zinteger: The sample along z.layerCountinteger: How many layers the terrain has, from 1 to 4.goalsLibraries.Containers.Number32BitArray: The array the four weights go into.startinteger: Where in the array the first weight goes.
GeneratePreset(Libraries.Game.Graphics.Worlds.TerrainData data, integer preset, integer startX, integer startZ, integer width, integer depth, number height)
This action fills a rectangle of samples with ground from a preset, from 0 up to the given height. Samples with no region are skipped.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain.presetinteger: The preset, such as PRESET_ROLLING_HILLS.startXinteger: The first sample along x.startZinteger: The first sample along z.widthinteger: How many samples along x.depthinteger: How many samples along z.heightnumber: The tallest the ground can be, in world units.
GetHollowDepth(Libraries.Game.Graphics.Worlds.TerrainData data, integer x, integer z)
This action returns how far a sample lies below the average of the ground around it, within hollowRadius samples, or 0 if it isn't lower. Every other sample is read, which gives nearly the same average at a quarter of the cost.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain.xinteger: The sample along x.zinteger: The sample along z.
Returns
number: The depth of the hollow, in world units.
GetPresetCount()
This action returns how many presets there are.
Returns
integer: The number of presets.
GetPresetName(integer preset)
This action returns a preset's name, for menus and speech.
Parameters
presetinteger: The preset, such as PRESET_MOUNTAINS.
Returns
text: The name.
GetSeed()
This action returns the seed.
Returns
number: The seed.
GetStampCount()
This action returns how many stamp shapes there are.
Returns
integer: The number of stamp shapes.
GetStampMode(integer stamp)
This action returns how a stamp shape is usually pressed into the ground: a crater is added, since it has a dip below the ground; the others are raised to, so they keep their own shape over bumpy ground rather than adding the bumps to it, and leave ground that is already higher alone.
Parameters
stampinteger: The shape.
Returns
integer: STAMP_ADD or STAMP_RAISE.
GetStampName(integer stamp)
This action returns a stamp shape's name, for menus and speech.
Parameters
stampinteger: The shape, such as STAMP_CRATER.
Returns
text: The name.
MakeStampShape(integer stamp, integer width)
This action makes a stamp shape: a square of numbers, one row after another, mostly from 0 to 1. A crater's middle goes below 0. The middle of the square is the stamp's center, and its edges are the stamp's radius away.
Parameters
stampinteger: The shape, such as STAMP_MOUNTAIN.widthinteger: How many numbers along each side. Odd numbers put a value exactly at the center.
Returns
Libraries.Containers.Number32BitArray: The shape.
SampleShape(Libraries.Containers.Number32BitArray shape, integer width, number across, number deep)
This action reads a shape between its numbers, blending the four nearest.
Parameters
shapeLibraries.Containers.Number32BitArray: The shape.widthinteger: How many numbers along each side.acrossnumber: Where to read along the shape's rows, from 0 to 1.deepnumber: Where to read down the shape's columns, from 0 to 1.
Returns
number: The blended value.
SetPaintRules(number rockLow, number rockHigh, number sandHeight, number sandFade, number hollowDepth)
This action sets auto paint's rules.
Parameters
rockLownumber: The slope, in degrees, where rock starts.rockHighnumber: The slope where the ground is all rock. It must be more than rockLow.sandHeightnumber: The height below which the ground is all sand.sandFadenumber: How far above sandHeight sand fades out.hollowDepthnumber: How far below the ground around it a hollow must be to be all dirt.
SetSeed(number seed)
This action sets the seed for presets, stamps' detail and rain erosion. The same seed always gives the same result.
Parameters
seednumber: The seed.
Settle(Libraries.Game.Graphics.Worlds.TerrainData data, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ, integer passes, number degrees)
This action lets ground slide off slopes steeper than an angle. Each pass looks at every sample in a rectangle, and if it stands higher above its lowest neighbor than the angle allows, moves half the extra to that neighbor. Ground is moved, not added or taken away.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain.minimumXinteger: The rectangle's smallest sample along x.minimumZinteger: The rectangle's smallest sample along z.maximumXinteger: The rectangle's largest sample along x.maximumZinteger: The rectangle's largest sample along z.passesinteger: How many times to go over the rectangle.degreesnumber: The steepest slope that stays, in degrees.
SettleAfterRain(Libraries.Game.Graphics.Worlds.TerrainData data, integer minimumX, integer minimumZ, integer maximumX, integer maximumZ)
This action lets ground slide off slopes steeper than the generator's settle angle, three passes over a rectangle, as erosion does after its rain. A brush's stroke uses it after raining.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain.minimumXinteger: The rectangle's smallest sample along x.minimumZinteger: The rectangle's smallest sample along z.maximumXinteger: The rectangle's largest sample along x.maximumZinteger: The rectangle's largest sample along z.
Smooth(number value)
This action is a smooth step: 0 at or below 0, 1 at or above 1, and an S-shaped curve between.
Parameters
valuenumber: The value.
Returns
number: The smoothed value, from 0 to 1.
Stamp(Libraries.Game.Graphics.Worlds.TerrainData data, Libraries.Containers.Number32BitArray shape, integer width, number centerX, number centerZ, number radius, number degrees, number height, integer mode)
This action presses a shape into the ground. The shape covers the square around a center, a radius away on each side, turned by an angle. Each sample under it changes by the shape's value there times the height: added (STAMP_ADD), taken away (STAMP_SUBTRACT), or with STAMP_RAISE, the ground is raised to the shape standing on the ground under the center, and left alone where it is already higher. Samples with no region are skipped.
Parameters
dataLibraries.Game.Graphics.Worlds.TerrainData: The terrain.shapeLibraries.Containers.Number32BitArray: The shape, width by width numbers, one row after another.widthinteger: How many numbers along each side of the shape.centerXnumber: The center along x, in world units.centerZnumber: The center along z, in world units.radiusnumber: Half the width of the square the shape covers, in world units.degreesnumber: How far to turn the shape, in degrees.heightnumber: The height of a shape value of 1, in world units.modeinteger: STAMP_ADD, STAMP_SUBTRACT or STAMP_RAISE.
Terrace(number level, integer steps)
This action turns a level from 0 to 1 into flat steps: most of each step is level, and the last fifth rises steeply to the next, like plateaus and cliffs.
Parameters
levelnumber: The level, from 0 to 1.stepsinteger: How many steps from 0 to 1.
Returns
number: The stepped level.