TerrainFoliage
Package: Libraries.Game.Graphics.Worlds · Inherits from: Libraries.Language.Object
TerrainFoliage holds the foliage of one terrain: where each foliage asset grows, and the copies drawn from that. Where an asset grows is its coverage: for each region, a FoliageCoverage grid with a value from 0 to 1 at each spot. The Foliage brush paints it (PaintCoverage). An asset can instead grow wherever one of the terrain's material layers is painted. The copies are worked out from the coverage and the asset's settings (Generate, GenerateArea), the same way every time, so a change to an asset's density, size or slopes changes the whole painted area, and the same scene always has the same foliage. The copies are kept in FoliageCells: each region of the terrain is split into square cells about 32 units wide, and each cell holds one asset's copies, so a cell can be drawn and culled as one piece. A program can also place copies itself (AddCopy, AddCopyOnGround); those stay when the worked-out copies are replaced, but they aren't saved. How copies are worked out: each cell has an endless list of candidate spots, made by a random number generator seeded by the cell and the asset, and each candidate has its own steady random rank, turn, size and shade. A cell uses as many candidates as its asset's density asks for over the cell's area, and a candidate grows a copy where its rank is below the coverage there and the slope suits the asset. Raising the density adds candidates at the end of the list, so the copies already there stay; lowering it takes the last ones away. The coverage is saved beside the terrain's height images, one file per region, such as "region_-1_2_coverage.json". Saving never deletes a file: a region whose coverage was all removed gets a file with none. The assets themselves (which model, how dense) are in a FoliageList; cells and coverage name their asset by its number in that list.
Example Code
use Libraries.Game.Graphics.Worlds.TerrainItem
use Libraries.Game.Graphics.Worlds.TerrainFoliage
TerrainItem ground
ground:Load("Scenes/Terrains/Terrain1/terrain.json")
TerrainFoliage foliage = ground:GetTerrainFoliage()
// Asset 0 grows fully in a circle 10 units wide.
boolean changed = foliage:PaintCoverage(0, 10, 20, 5, 1, false)
foliage:GenerateArea(ground:GetFoliageList(), 10, 20, 5)
Actions
AddCopy(integer asset, number x, number y, number z, number turn, number scale, number shade)
This action adds a copy of an asset.
Parameters
assetinteger: The asset's number in the foliage list.xnumber: Where it stands along x, in world units.ynumber: Its height, in world units.znumber: Where it stands along z, in world units.turnnumber: Its turn about the up axis, in degrees.scalenumber: Its scale.shadenumber: Its shade, 1 for the asset's own color.
Returns
Libraries.Game.Graphics.Worlds.FoliageCell: The cell it was added to.
AddCopyOnGround(integer asset, Libraries.Game.Graphics.Worlds.FoliageAsset settings, number x, number z, Libraries.Compute.Random random)
This action adds a copy of an asset standing on the ground at a place, with the asset's height offset and its random scale, turn and shade. It adds nothing where there is no ground, or where the slope is outside the asset's range.
Parameters
assetinteger: The asset's number in the foliage list.settingsLibraries.Game.Graphics.Worlds.FoliageAsset: The asset, for its offset, variation and slopes.xnumber: Where it stands along x, in world units.znumber: Where it stands along z, in world units.randomLibraries.Compute.Random: Where the random variation comes from.
Returns
boolean: True if a copy was added.
Empty()
This action removes every copy and all coverage. Files already saved stay as they are until the next save.
FollowGround(Libraries.Game.Graphics.Worlds.FoliageList list, number x, number z, number radius)
This action keeps the foliage on the ground after the ground was sculpted. In an area, such as under the brush during a stroke, every copy moves up or down onto the ground, keeping its asset's height offset, which is quick. For the whole terrain, such as when a stroke ends or after an undo, placed copies move onto the ground and the rest are worked out again from their coverage, since the slopes changed too. Copies with no ground under them, such as on a removed region, stay where they are.
Parameters
listLibraries.Game.Graphics.Worlds.FoliageList: The foliage list, for each asset's settings, or undefined for none.xnumber: The area's middle along x, in world units.znumber: The area's middle along z, in world units.radiusnumber: How far the area reaches from its middle; a negative radius is the whole terrain.
Returns
Libraries.Containers.Array<Libraries.Game.Graphics.Worlds.FoliageCell>: The cells whose copies moved, or every cell for the whole terrain.
Generate(Libraries.Game.Graphics.Worlds.FoliageList list)
This action works out every cell's copies again, from the coverage and the assets' settings, such as after an asset's settings changed or the foliage was loaded. Copies a program placed stay.
Parameters
listLibraries.Game.Graphics.Worlds.FoliageList: The foliage list.
GenerateArea(Libraries.Game.Graphics.Worlds.FoliageList list, number x, number z, number radius)
This action works out the copies of every asset again in the cells a circle reaches, such as after the brush painted coverage there.
Parameters
listLibraries.Game.Graphics.Worlds.FoliageList: The foliage list.xnumber: The circle's middle along x, in world units.znumber: The circle's middle along z, in world units.radiusnumber: The circle's radius, in world units.
GenerateRegion(Libraries.Game.Graphics.Worlds.FoliageList list, integer locationX, integer locationZ)
This action works out the copies of every asset in one region's cells, such as when a streaming terrain loads the region. Its coverage should be loaded first (LoadRegion).
Parameters
listLibraries.Game.Graphics.Worlds.FoliageList: The foliage list.locationXinteger: The region's location along x.locationZinteger: The region's location along z.
Returns
Libraries.Containers.Array<Libraries.Game.Graphics.Worlds.FoliageCell>: The region's cells that have copies now.
GetCellAtIndex(integer index)
This action returns one of the cells.
Parameters
indexinteger: The cell, from 0.
Returns
Libraries.Game.Graphics.Worlds.FoliageCell: The cell.
GetCellCount()
This action returns how many cells there are, including empty ones.
Returns
integer: The count.
GetCellSize()
This action returns how wide a cell is, in world units.
Returns
number: The width.
GetCellsPerRegion()
This action returns how many cells each region is split into along x and along z.
Returns
integer: The count, at least 1.
GetCopyCount(integer asset)
This action returns how many copies there are of one asset.
Parameters
assetinteger: The asset's number, or -1 for every asset.
Returns
integer: The count.
GetCopyCount()
This action returns how many copies there are of every asset.
Returns
integer: The count.
GetCoverage(integer asset, number x, number z)
This action returns an asset's coverage at a place, blended from the spots around it: 0 where it doesn't grow, up to 1 where it grows at its full density.
Parameters
assetinteger: The asset's number in the foliage list.xnumber: The place along x, in world units.znumber: The place along z, in world units.
Returns
number: The coverage, from 0 to 1.
GetCoveragesNear(integer asset, number x, number z, number radius, boolean make)
This action returns the coverages a circle reaches: of one asset, made where they aren't there yet if make is true (only over regions that have ground), or of every asset, with an asset of -1, only those there.
Parameters
assetinteger: The asset's number, or -1 for every asset.xnumber: The circle's middle along x, in world units.znumber: The circle's middle along z, in world units.radiusnumber: The circle's radius, in world units.makeboolean: True to make an asset's coverage where it isn't there yet.
Returns
Libraries.Containers.Array<Libraries.Game.Graphics.Worlds.FoliageCoverage>: The coverages.
GetRegionFileName(integer locationX, integer locationZ)
This action returns the name of a region's coverage file, such as "region_-1_2_coverage.json".
Parameters
locationXinteger: The region's location along x.locationZinteger: The region's location along z.
Returns
text: The file's name.
GetSpotSpacing()
This action returns how far apart coverage spots are, in world units.
Returns
number: The spacing.
GetSpotsPerRegion()
This action returns how many coverage spots each region has along each side.
Returns
integer: The count, at least 1.
GetTerrainData()
This action returns the terrain data the foliage stands on.
Returns
Libraries.Game.Graphics.Worlds.TerrainData: The data, or undefined before it is set.
Load(Libraries.System.File folder)
This action reads the coverage of every region of the terrain from a folder, replacing what is held. Regions with no coverage file have none. Call Generate with the foliage list afterward to work out the copies; TerrainItem does this when it draws the foliage.
Parameters
folderLibraries.System.File: The terrain's folder, holding its terrain.json.
LoadRegion(Libraries.System.File folder, integer locationX, integer locationZ)
This action reads one region's coverage file, if it has one. Coverage made with another grid size is skipped.
Parameters
folderLibraries.System.File: The terrain's folder.locationXinteger: The region's location along x.locationZinteger: The region's location along z.
PaintCoverage(integer asset, number x, number z, number radius, number target, boolean lower)
This action paints coverage in a circle: of one asset, moving it toward a target, or lowering one asset's or every asset's toward none. The effect is full near the middle and fades to nothing at the edge.
Parameters
assetinteger: The asset's number, or -1 to lower every asset's.xnumber: The circle's middle along x, in world units.znumber: The circle's middle along z, in world units.radiusnumber: The circle's radius, in world units.targetnumber: The coverage to move toward, from 0 to 1, when not lowering.lowerboolean: True to lower coverage toward none.
Returns
boolean: True if any coverage changed.
RemoveAsset(integer asset)
This action removes every copy of an asset, and moves the numbers of the assets after it down one, for when the foliage list loses that asset.
Parameters
assetinteger: The asset's number.
RemoveCopies(integer asset, number x, number z, number radius)
This action removes the copies whose middles are within a distance of a point, of one asset or of all of them.
Parameters
assetinteger: The asset's number, or -1 for every asset.xnumber: The point along x, in world units.znumber: The point along z, in world units.radiusnumber: The distance, in world units.
Returns
integer: How many copies were removed.
Save(Libraries.System.File folder)
This action writes the coverage of every region that has any, or had a coverage file when loaded, to a folder. Regions without coverage that never had a file get none. Nothing is deleted. Copies a program placed aren't saved.
Parameters
folderLibraries.System.File: The terrain's folder, holding its terrain.json.
Save(Libraries.System.File folder, Libraries.Game.Graphics.Worlds.FoliageList list)
This action is Save, also writing each asset's name beside its coverage, from the foliage list, so a file can be checked by hand.
Parameters
folderLibraries.System.File: The terrain's folder, holding its terrain.json.listLibraries.Game.Graphics.Worlds.FoliageList: The foliage list, or undefined to write no names.
SetTerrainData(Libraries.Game.Graphics.Worlds.TerrainData terrainData)
This action sets the terrain data the foliage stands on. TerrainItem calls it when it loads or makes a terrain.
Parameters
terrainDataLibraries.Game.Graphics.Worlds.TerrainData: The terrain's data.
SwapAssets(integer first, integer second)
This action swaps two assets' numbers on every copy, for when the foliage list's assets are swapped, so each copy keeps its asset.
Parameters
firstinteger: One asset's number.secondinteger: The other's.
UnloadRegion(integer locationX, integer locationZ)
This action lets go of one region's coverage and cells, such as when a streaming terrain unloads the region. Its coverage file is untouched; LoadRegion reads it again when the region comes back.
Parameters
locationXinteger: The region's location along x.locationZinteger: The region's location along z.
Returns
Libraries.Containers.Array<text>: The keys ("asset/cellX/cellZ") of the cells let go, so their drawing can go too.