TerrainRegion
Package: Libraries.Game.Graphics.Worlds · Inherits from: Libraries.Language.Object
A TerrainRegion holds the heights for one square piece of a terrain. A terrain is made of many regions laid side by side, like tiles on a floor, and TerrainData keeps track of them. Splitting the ground this way means a large world only stores the pieces that are actually used, and an edit only has to rebuild the piece it touched. Each region holds a square of height samples, region size by region size. A sample is one point on the ground, and its height is in world units, so a height of 2.5 is 2.5 units above zero. Samples are found by their place in the region, from 0 to one less than the region size, across (x) and deep (z). Each region also knows its location: which tile it is in the grid of regions. The region at location (0, 0) starts at the world's origin, the one at (1, 0) is next to it along x, and the one at (-1, 0) is on the other side. This class is modeled on Terrain3DRegion from Terrain3D, which also stores its heights one region at a time.
Example Code
use Libraries.Game.Graphics.Worlds.TerrainRegion
TerrainRegion region
region:SetRegionSize(64)
region:SetHeight(10, 20, 3.5)
output region:GetHeight(10, 20)
Actions
CalculateHeightRange()
This action looks at every sample to find the exact lowest and highest heights in the region.
Contains(integer x, integer z)
This action returns true if a sample position is inside the region.
Parameters
xinteger: The sample's place across the region.zinteger: The sample's place deep into the region.
Returns
boolean: True if both are from 0 to one less than the region size.
Copy()
This action makes a separate copy of the region: its size, location, heights, splat weights, height range, and modified and painted flags. Changing one afterward doesn't change the other. Undo uses copies to put a region back the way it was.
Returns
Libraries.Game.Graphics.Worlds.TerrainRegion: The copy.
CopyFrom(Libraries.Game.Graphics.Worlds.TerrainRegion other)
This action makes this region the same as another one: its size, location, heights, splat weights, height range, and modified and painted flags. The other region is not changed.
Parameters
otherLibraries.Game.Graphics.Worlds.TerrainRegion: The region to copy from.
Fill(number height)
This action sets every sample to the same height, and marks the region as modified.
Parameters
heightnumber: The height, in world units.
FillSplat(integer code)
This action sets every sample's splat weights to the same value, and marks the region as modified and painted.
Parameters
codeinteger: The packed splat weights.
GetHeight(integer x, integer z)
This action returns the height of one sample.
Parameters
xinteger: The sample's place across the region.zinteger: The sample's place deep into the region.
Returns
number: The height, in world units.
GetHeightAt(integer index)
This action returns a height by its place in the region's list of samples, z times the region size plus x. It is quicker than GetHeight for going through every sample.
Parameters
indexinteger: The sample's place in the list.
Returns
number: The height, in world units.
GetHeightFromPixel(integer code, number lowest, number highest)
This action reads a height back from a pixel made by GetPixelFromHeight.
Parameters
codeinteger: The pixel, as read by PixelMap's GetPixel.lowestnumber: The height that level 0 stands for.highestnumber: The height that the top level stands for.
Returns
number: The height.
GetHeightImage()
This action makes an image of the region's heights, for saving with PixelMap's Save. Each pixel holds one sample's height as a level from 0 to 16777215 between the region's lowest and highest heights: red holds the coarsest part, then green, then blue, and alpha is always full. The exact height range is worked out first, and must be saved alongside the image to read it back. Red and green are the coarse and fine parts in the same way Terrain reads a height map, and the image is laid out the same way: the first number given to PixelMap's SetPixel is the sample's z. So a saved region can also be loaded as an ordinary 16-bit height map. Like PixelMap itself, this needs a running game.
Returns
Libraries.Game.Graphics.PixelMap: A new image, region size by region size.
GetHighestHeight()
This action returns the highest height in the region. After heights have been lowered, the true highest may be lower than this until CalculateHeightRange is called.
Returns
number: The highest height, in world units.
GetLocationX()
This action returns the region's place along the x axis in the grid of regions.
Returns
integer: The region's x location.
GetLocationZ()
This action returns the region's place along the z axis in the grid of regions.
Returns
integer: The region's z location.
GetLowestHeight()
This action returns the lowest height in the region. After heights have been lowered, the true lowest may be higher than this until CalculateHeightRange is called.
Returns
number: The lowest height, in world units.
GetMemorySize()
This action returns about how many bytes the region's heights and splat weights take up: 8 for each sample.
Returns
integer: The size in bytes.
GetPixelFromHeight(number height, number lowest, number highest)
This action packs a height into a pixel, as a level from 0 to 16777215 between a lowest and a highest height. Heights outside the range are limited to it. Red holds the coarsest part of the level, then green, then blue, and alpha is always full.
Parameters
heightnumber: The height to pack.lowestnumber: The height that becomes level 0.highestnumber: The height that becomes the top level.
Returns
integer: The pixel, ready for PixelMap's SetPixel.
GetRegionSize()
This action returns how many samples run along each side of the region.
Returns
integer: The number of samples along each side, or 0 if no size has been set.
GetSplat(integer x, integer z)
This action returns the splat weights at one sample: how much of each of up to four ground textures to show there, packed into one number the way a PixelMap pixel is, red in the highest byte, then green, blue and alpha. TerrainData's GetSplatWeight reads one weight from it.
Parameters
xinteger: The sample's place across the region.zinteger: The sample's place deep into the region.
Returns
integer: The packed splat weights.
GetSplatAt(integer index)
This action returns packed splat weights by the sample's place in the region's list of samples, z times the region size plus x.
Parameters
indexinteger: The sample's place in the list.
Returns
integer: The packed splat weights.
GetSplatImage()
This action makes an image of the region's splat weights, for saving with PixelMap's Save. Each pixel is one sample's packed weights, exactly, so nothing is lost. It is laid out like GetHeightImage: the first number given to PixelMap's SetPixel is the sample's z. Like PixelMap itself, this needs a running game.
Returns
Libraries.Game.Graphics.PixelMap: A new image, region size by region size.
IsModified()
This action returns true if the heights have changed since the region was last marked as unmodified, such as after it was saved.
Returns
boolean: True if the region has changed.
IsSplatPainted()
This action returns whether the region's splat weights have been painted, or loaded from a saved splat image, rather than left as the default. A region that isn't painted saves no splat image, since loading gives it the default weights anyway.
Returns
boolean: True if the splat weights are painted.
SetHeight(integer x, integer z, number height)
This action sets the height of one sample, and marks the region as modified. If the height is outside the region's height range, the range grows to include it.
Parameters
xinteger: The sample's place across the region.zinteger: The sample's place deep into the region.heightnumber: The new height, in world units.
SetHeightsFromImage(Libraries.Game.Graphics.PixelMap map, number lowest, number highest)
This action reads the region's heights from an image made by GetHeightImage, given the height range that was saved with it. The region is marked as unmodified, since it now matches what was saved.
Parameters
mapLibraries.Game.Graphics.PixelMap: The image, which must be region size by region size.lowestnumber: The region's lowest height when it was saved.highestnumber: The region's highest height when it was saved.
SetLocation(integer x, integer z)
This action sets which tile this region is in the grid of regions.
Parameters
xinteger: The region's place along the x axis.zinteger: The region's place along the z axis.
SetModified(boolean modified)
This action marks whether the region has changed since it was last saved.
Parameters
modifiedboolean: True if the region has changed.
SetRegionSize(integer size)
This action sets how many samples run along each side of the region, and makes every height 0. Any heights the region held before are lost.
Parameters
sizeinteger: The number of samples along each side. It must be at least 2.
SetSplat(integer x, integer z, integer code)
This action sets the splat weights at one sample, and marks the region as modified.
Parameters
xinteger: The sample's place across the region.zinteger: The sample's place deep into the region.codeinteger: The packed splat weights, as made by TerrainData's PackSplat.
SetSplatFromImage(Libraries.Game.Graphics.PixelMap map)
This action reads the region's splat weights from an image made by GetSplatImage. It does not mark the region as modified, since it is used when loading, but it marks the weights as painted, so they are saved again.
Parameters
mapLibraries.Game.Graphics.PixelMap: The image, which must be region size by region size.
SetSplatPainted(boolean painted)
This action sets whether the region's splat weights count as painted. Setting it to false doesn't change the weights; it only stops the next save writing a splat image for the region.
Parameters
paintedboolean: True if the splat weights are painted.