WorldTerrain

Package: Libraries.Game.Graphics.Worlds · Inherits from: Libraries.Language.Object

A WorldTerrain is ground for a large world. It holds a TerrainData, which has all of the heights, split into regions. Build makes one TerrainRegionModel for each region, which draws that region and gives it a collision shape. The models are not added to the game automatically, so a game can add them however it likes, for example to a particular layer. After Build, add each one with GetRegionModelCount and GetRegionModel. Collision works in one of three ways, as in Terrain3D: 1. COLLISION_FULL, the default here: every region model has a collision shape for its whole region. 2. COLLISION_DYNAMIC, Terrain3D's default: the region models only draw, and a TerrainCollision keeps small collision pieces near a target, such as the player. Call Update every frame with the target's position. 3. COLLISION_DISABLED: no collision at all. Far regions can be drawn with less detail, by setting a far radius before StartStreaming. Regions past the draw radius and within the far radius are drawn by a TerrainRegionFarModel, which uses only every few samples, so the world has a horizon at little cost. For a world larger than should be in memory at once, call StartStreaming instead of Build. Then only regions near the target are loaded, from a folder the world was saved in, and only those within the draw radius are drawn. Update loads and unloads regions, and adds and removes their models from the layer, a little each frame. A region's model is kept when it is taken out, and used again when the region comes back unchanged. This class plays the part of the Terrain3D node in Terrain3D, which holds a Terrain3DData and builds the drawing and collision for it.

Example Code

use Libraries.Game.Game
use Libraries.Game.Graphics.Worlds.WorldTerrain
use Libraries.Game.Graphics.Worlds.TerrainData
use Libraries.Compute.Noise.PerlinNoise

class Main is Game
WorldTerrain terrain

action Main
    StartGame()
end

action CreateGame
    EnablePhysics3D(true)
    PerlinNoise noise
    noise:SetFrequency(0.02)
    TerrainData data = terrain:GetData()
    data:SetRegionSize(64)
    data:GenerateHeights(noise, -128, -128, 256, 256, 10, 0)

    terrain:SetTexture("Textures/grass.png")
    terrain:Build()
    integer i = 0
    repeat while i < terrain:GetRegionModelCount()
        Add(terrain:GetRegionModel(i))
        i = i + 1
    end
end
end

Variables

  • integer COLLISION_FULL
  • integer COLLISION_DISABLED: The ways the terrain can be collided with. See the class description.
  • integer COLLISION_DYNAMIC

Actions

AreLayersReady()

This action returns true once every ground texture has its place in the renderer, so the splat shader can draw them. Until then, splatted ground is drawn a plain green.

Returns

boolean: True if the ground textures are ready.

Build()

This action makes a model for every region in the data, with a mesh, and with a collision shape in COLLISION_FULL mode. Then it clears the data's edited area. Any models from an earlier build are replaced, so remove them from the game first. Their meshes are freed, so remove them from the game first.

DisposeModels()

This action frees every region model the terrain made, near and far, such as when the terrain is no longer used. Their meshes are freed, so remove them from the game first. A model kept in more than one list is only freed once, since freeing a model again does nothing.

GetCollision()

This action returns the TerrainCollision that keeps collision near a target in COLLISION_DYNAMIC mode. Its shape size and radius can be changed here.

Returns

Libraries.Game.Graphics.Worlds.TerrainCollision: The terrain's dynamic collision.

GetCollisionMode()

This action returns how the terrain can be collided with.

Returns

integer: COLLISION_DISABLED, COLLISION_DYNAMIC or COLLISION_FULL.

GetData()

This action returns the terrain's data, which holds all of its heights. Change the heights here, then call Build.

Returns

Libraries.Game.Graphics.Worlds.TerrainData: The terrain's data.

GetDrawRadius()

This action returns how near, in world units, a region must be to the target to be drawn while streaming.

Returns

number: The draw radius.

GetFarModelCount()

This action returns how many far models are in the layer.

Returns

integer: The number of far models being drawn.

GetFarRadius()

This action returns how far, in world units, regions are drawn with less detail while streaming.

Returns

number: The far radius, or 0 for none.

GetFarStep()

This action returns how many samples apart the vertices of far models are.

Returns

integer: The number of samples between vertices.

GetFarTint()

This action returns the color the far models' texture is multiplied by.

Returns

Libraries.Game.Graphics.Color: The color, or undefined for none.

GetFriction()

This action returns how much the ground grips what rests on it.

Returns

number: The friction.

GetHeightBlendSoftness()

This action returns how softly texture set layers blend where they meet.

Returns

number: The softness, from 0.01 to 1.

GetInPlaceBuildCount()

This action returns how many region and far model builds so far updated an existing mesh in place, using no new GPU memory.

Returns

integer: The number of builds that updated a mesh in place.

GetLayerMaterial(integer layer)

This action returns a ground layer's texture set.

Parameters

  • layer integer: The layer: 0 for red, 1 green, 2 blue, 3 alpha.

Returns

Libraries.Game.Graphics.Models.Data.Material: The Material, or undefined if the layer uses a plain texture.

GetLayerTable()

This action returns the 4 by 1 texture that tells the splat shader each texture set layer's texture places and tile size.

Returns

Libraries.Game.Graphics.Texture: The layer table, or undefined if no layer has a Material or it hasn't been made.

GetName()

This action returns the name given to the region models.

Returns

text: The name.

GetNewMeshBuildCount()

This action returns how many region and far model builds so far loaded a new mesh. Each one uses GPU memory that can't yet be freed, so while editing this should stay the same.

Returns

integer: The number of builds that loaded a new mesh.

GetRegionModel(integer index)

This action returns one of the region models the last Build made.

Parameters

  • index integer: Which model, from 0 to one less than GetRegionModelCount.

Returns

Libraries.Game.Graphics.Worlds.TerrainRegionModel: The region model.

GetRegionModelCount()

This action returns how many region models the last Build made.

Returns

integer: The number of region models.

GetSkirtDepth()

This action returns how deep, in world units, the skirts of far models hang down.

Returns

number: The skirt depth.

GetSplatPixel(integer sampleX, integer sampleZ)

This action returns the packed splat weights the splat texture was last made from at a sample, for checking that painting reaches the texture.

Parameters

  • sampleX integer: The sample along x.
  • sampleZ integer: The sample along z.

Returns

integer: The packed weights, or -1 if the sample is outside the splat texture.

GetSplatProgram()

This action returns the shader program the terrain's models are drawn with while splatting.

Returns

Libraries.Game.Graphics.Shaders.Programs.ShaderProgram: The splat program, or undefined if none was set.

GetSplatTexture()

This action returns the terrain's splat texture, which holds the splat weights of the whole terrain.

Returns

Libraries.Game.Graphics.Texture: The splat texture, or undefined if splatting is off or it hasn't been made.

GetSplatTextureCount()

This action returns how many splat textures have been made, including the first. Each one uses another of the renderer's texture places. A new one is only made when the splat area changes size; other changes update the existing one (see GetSplatUpdateCount).

Returns

integer: The number of splat textures made.

GetSplatUpdateCount()

This action returns how many times the splat texture has been loaded again at the same size, such as after painting. Each of these keeps the texture's place in the renderer.

Returns

integer: The number of updates.

GetStreamer()

This action returns the TerrainStreamer that loads and unloads regions while streaming.

Returns

Libraries.Game.Graphics.Worlds.TerrainStreamer: The streamer.

GetTexture()

This action returns the texture laid over the ground.

Returns

Libraries.Game.Graphics.Texture: The texture, or undefined if there is none.

GetTextureRepeat()

This action returns how far apart, in world units, the texture repeats.

Returns

number: The distance between repeats.

GetTextureSplat()

This action returns the ground textures used for texture splatting.

Returns

Libraries.Game.Graphics.Worlds.TextureSplat: The TextureSplat, or undefined if splatting is off.

HasLayerMaterials()

This action returns true if any ground layer has a texture set.

Returns

boolean: True if a layer has a Material.

IsDrawable(integer locationX, integer locationZ)

This action returns true if a loaded region can be drawn: each of its eight neighbors is either loaded or doesn't exist. A region's mesh reaches into its neighbors along x and z, and its normals read one sample into every neighbor, so they must be there first.

Parameters

  • locationX integer: The region location along x.
  • locationZ integer: The region location along z.

Returns

boolean: True if the region can be drawn.

IsRegionDrawnInFull(integer locationX, integer locationZ)

This action returns true if a region is drawn in full detail: always when not streaming, and while streaming, when its full model is in the layer, within the draw radius. A region drawn by a far model, with fewer heights, returns false.

Parameters

  • locationX integer: The region location along x.
  • locationZ integer: The region location along z.

Returns

boolean: True if the region is drawn in full detail.

Example

use Libraries.Game.Graphics.Worlds.WorldTerrain

WorldTerrain terrain
boolean full = terrain:IsRegionDrawnInFull(0, 0)

IsStreaming()

This action returns true if the terrain is streaming its regions.

Returns

boolean: True after StartStreaming.

Rebuild()

This action brings the region models up to date after heights have changed. Only the models touched by the data's edited area are rebuilt, and then the edited area is cleared. The models stay in the game, so nothing needs to be added or removed. Regions added to or removed from the data since the last Build are not handled here. Call Build again for those. Each rebuilt model gets a new mesh, and the renderer cannot yet free the old one, so its memory stays in use.

Returns

integer: How many region models and splat textures were rebuilt.

RefreshLayers()

This action makes the layer table again the next time the terrain updates, such as after a texture set layer's tile size changed. The texture sets aren't loaded again.

ReleaseTexturePlaces()

This action gives back the terrain's block of texture places, such as when the terrain is no longer used. The models must not be drawn afterward.

SetBuildsPerUpdate(integer count)

This action sets how many region models may be built or rebuilt in one update while streaming. The default is 1.

Parameters

  • count integer: The number of models. It must be at least 1.

SetCollisionMode(integer mode)

This action sets how the terrain can be collided with: COLLISION_DISABLED, COLLISION_DYNAMIC or COLLISION_FULL. It takes effect the next time Build is called. The default is COLLISION_FULL.

Parameters

  • mode integer: The collision mode.

SetDrawRadius(number radius)

This action sets how near, in world units, a region must be to the target to be drawn while streaming. Set it before StartStreaming. The default is 100.

Parameters

  • radius number: The draw radius. It must be more than 0.

SetFarRadius(number radius)

This action sets how far, in world units, regions are drawn with less detail while streaming. Regions past the draw radius and within this radius are drawn by far models. Set it before StartStreaming. The default is 0, which draws nothing past the draw radius.

Parameters

  • radius number: The far radius, or 0 for none.

SetFarStep(integer step)

This action sets how many samples apart the vertices of far models are. It must divide the region size evenly. The default is 4. Set it before StartStreaming.

Parameters

  • step integer: The number of samples between vertices.

SetFarTint(Libraries.Game.Graphics.Color tint)

This action sets a color the far models' texture is multiplied by, so it is easy to see which regions are drawn with less detail. Far models already built are changed too. Pass undefined for no tint, which is the default.

Parameters

SetFriction(number friction)

This action sets how much the ground grips what rests on it. It takes effect the next time Build is called. The default is 1.

Parameters

  • friction number: The friction.

SetHeightBlendSoftness(number softness)

This action sets how softly texture set layers blend where they meet. Near 0, the higher layer at each point wins outright, for crisp stones in grass. Near 1, the layers fade into each other, much like plain weight blending. It takes effect when the layers become ready, so set it before Build or StartStreaming.

Parameters

  • softness number: From 0.01 to 1. The default is 0.2.

SetInPlaceUpdates(boolean inPlace)

This action sets whether rebuilt region models update their meshes in place, instead of making new ones. It is on by default, and affects models made after it is set. Turning it off is only useful for comparing with the old behavior.

Parameters

  • inPlace boolean: True to update meshes in place when possible.

SetLayerMaterial(integer layer, Libraries.Game.Graphics.Models.Data.Material material)

This action draws one ground layer with a texture set: a Material made with SetTextureSet, such as an AmbientCG set. The layer then has the set's color, bumps, roughness and ambient occlusion, and blends with the other texture set layers by height, so stones show through grass at their edges rather than fading into it. Layers without a Material use the plain textures from SetTextureSplat. The set is loaded and packed right away, which takes a moment for each set. Call this before Build or StartStreaming. The terrain links the set's textures into its own block of texture places, so the splat shader finds them however many textures were loaded before. It needs the Vulkan renderer and a splat program (SetSplatProgram). The Material's texture scale (SetTextureScale) is how many world units one tile covers, from 0.25 to 63.75.

Parameters

SetName(text name)

This action sets the name given to the region models. Each model is named this, followed by its region's location.

Parameters

  • name text: The name.

SetSkirtDepth(number depth)

This action sets how far, in world units, the skirts of far models hang down. Deeper skirts hide larger cracks between far models and their neighbors. The default is 1. Set it before StartStreaming.

Parameters

  • depth number: The skirt depth. It must be more than 0.

SetSplatProgram(Libraries.Game.Graphics.Shaders.Programs.ShaderProgram program)

This action sets the shader program the terrain's region and far models are drawn with while splatting, such as a TerrainSplatShaderProgram compiled with InstallAsProgram. Set it before Build or StartStreaming.

Parameters

SetTexture(text path)

This action loads a texture from a file and lays it over the ground. It takes effect the next time Build is called. The texture gets mipmaps, so the ground doesn't shimmer in the distance.

Parameters

  • path text: The texture file's path.

SetTexture(Libraries.Game.Graphics.Texture texture)

This action lays a texture over the ground. It takes effect the next time Build is called.

Parameters

SetTextureRepeat(number distance)

This action sets how far apart, in world units, the texture repeats. A larger value stretches the texture over more ground. The default is 4.

Parameters

  • distance number: The distance between repeats. It must be more than 0.

SetTextureSplat(Libraries.Game.Graphics.Worlds.TextureSplat splat)

This action draws the ground with texture splatting: up to four ground textures, from the TextureSplat's red, green, blue and alpha textures, blended by each sample's splat weights in the TerrainData. A missing texture uses the red one. Its splat map is not used, since each region makes its own from the TerrainData. Set it before Build or StartStreaming. The default is undefined, which draws the plain texture set with SetTexture. The models need a shader that does the blending: give them one with SetSplatProgram, such as a TerrainSplatShaderProgram. With splatting, the far tint is not used, since the models' color override carries the ground textures' places.

Parameters

StartStreaming(Libraries.System.File folder, Libraries.Game.Layer3D layer, number targetX, number targetZ)

This action starts streaming the terrain from a folder it was saved in, in place of Build. Any regions already loaded are unloaded first, and changed ones are saved to the folder. Then the folder's terrain.json is read, every saved region near the target is loaded, and those within the draw radius get models in the layer. After this, call Update every frame. Regions are loaded within the draw radius plus one and a half regions, so every drawn region's neighbors are loaded too. A region's mesh reaches into its neighbors, so it is only drawn once they are loaded.

Parameters

  • folder Libraries.System.File: The folder the terrain was saved in, with TerrainData's Save.
  • layer Libraries.Game.Layer3D: The layer to draw the region models in, usually the game's current 3D layer.
  • targetX number: The target's position along x, in world units.
  • targetZ number: The target's position along z, in world units.

Update(Libraries.Game.Layer3D layer, number targetX, number targetZ)

This action keeps the terrain up to date around a target, usually the player or camera. Call it every frame, after Build or StartStreaming. While streaming, it loads and unloads regions, and adds and removes their models from the layer, a little at a time. In COLLISION_DYNAMIC mode, it keeps collision near the target, which only takes work when the target moves into a new square of the collision grid.

Parameters

  • layer Libraries.Game.Layer3D: The layer to put the collision pieces in, usually the game's current 3D layer.
  • targetX number: The target's position along x, in world units.
  • targetZ number: The target's position along z, in world units.

Returns

boolean: True if any regions, region models or collision pieces changed.

UpdateRegionModels(Libraries.Game.Layer3D layer)

This action brings the region models up to date after regions have been added to or removed from the data, such as by a terrain editor's region brush or by undoing one. It is for terrain made with Build; a streaming terrain keeps its models up to date in Update. Models whose region is gone are taken out of the layer. Regions with no model get one, put in the layer. If a new region lies outside the area the splat texture covers, the splat texture is made again to cover it, and every model's texture coordinates are rebuilt to match. Call Rebuild afterward, so the neighbors of added and removed regions, whose meshes reach one sample into them, are rebuilt too.

Parameters

Returns

integer: How many models were added or removed.

UsesInPlaceUpdates()

This action returns whether rebuilt region models update their meshes in place.

Returns

boolean: True if meshes are updated in place when possible.

Inherited Actions

3 actions from Libraries.Language.Object