TerrainRegionModel

Package: Libraries.Game.Graphics.Worlds · Inherits from: Libraries.Game.Graphics.Worlds.TerrainPatch, Libraries.Game.Graphics.Model, Libraries.Game.Graphics.Worlds.Terrain, Libraries.Interface.Item3D, Libraries.Interface.Item, Libraries.Language.Object

A TerrainRegionModel draws one TerrainRegion. A WorldTerrain makes one for each region, so a large terrain is drawn as many smaller pieces. Each piece can be culled on its own, and a change to the ground only needs the pieces it touched rebuilt. It is a TerrainPatch, so it can also be what the physics engine collides with. When a WorldTerrain uses full collision, each region model has a collision shape for its whole region. When it uses dynamic collision, the region models only draw, and a TerrainCollision keeps small collision pieces near the player instead. The mesh covers its region's samples and reaches one sample into the next region along x and along z, where there is one, so neighboring pieces share their edges and there are no gaps between them. Normals are worked out from the TerrainData as well, across region edges, so the lighting matches where two pieces meet. In Terrain3D, drawing is done by Terrain3DMesher and collision by Terrain3DCollision.

Example Code

use Libraries.Game.Game
use Libraries.Game.Graphics.Worlds.TerrainData
use Libraries.Game.Graphics.Worlds.TerrainRegionModel

class Main is Game
action Main
    StartGame()
end

action CreateGame
    TerrainData data
    data:AddRegion(0, 0)
    TerrainRegionModel model
    model:Build(data, data:GetRegion(0, 0), undefined, 4, false)
    model:PlaceInWorld()
    Add(model)
end
end

Actions

Build(Libraries.Game.Graphics.Worlds.TerrainData data, Libraries.Game.Graphics.Worlds.TerrainRegion region, Libraries.Game.Graphics.Texture texture, number textureRepeat, boolean withCollision)

This action builds the model's mesh, and its collision shape if asked, for one region. Call PlaceInWorld afterward, once physics has been set up, to place and scale the model to match the TerrainData's vertex spacing. Building again updates the mesh in place when the region's grid size and splat setting are unchanged. Otherwise it makes a new mesh, and the renderer cannot yet free the old one, so its memory stays in use.

Parameters

GetTerrainRegion()

This action returns the region this model draws.

Returns

Libraries.Game.Graphics.Worlds.TerrainRegion: The region, or undefined if the model has not been built.

HasCollisionShape()

This action returns true if the model has a collision shape for its whole region.

Returns

boolean: True if the model can be collided with.

NeedsRebuild()

This action returns true if the model must be rebuilt before it is shown again.

Returns

boolean: True if the model is out of date.

Rebuild(Libraries.Game.Graphics.Texture texture, number textureRepeat)

This action builds the model again from the current heights, after they have changed, and moves it to its new height. It keeps its physics settings, and keeps or leaves out its collision shape as it was built. It must have been built with Build first. A rebuild updates the mesh in place when the region's grid size and splat setting are unchanged, which is the usual case for edits. Otherwise it makes a new mesh, and the old one is freed.

Parameters

  • texture Libraries.Game.Graphics.Texture: The texture laid over the ground, or undefined for none.
  • textureRepeat number: How far apart, in world units, the texture repeats.

SetInPlaceUpdates(boolean inPlace)

This action sets whether rebuilding may update the existing mesh in place, rather than making a new one. It is on by default.

Parameters

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

SetNeedsRebuild(boolean rebuild)

This action marks whether the ground this model draws has changed while it was out of the game, so it must be rebuilt before it is shown again.

Parameters

  • rebuild boolean: True if the model must be rebuilt.

SetSplatArea(integer startX, integer startZ, integer width, integer depth)

This action sets which samples the terrain's splat texture covers, so the model's texture coordinates point at the right pixels. It takes effect the next time the model is built.

Parameters

  • startX integer: The first sample along x the splat texture covers.
  • startZ integer: The first sample along z the splat texture covers.
  • width integer: How many samples the splat texture covers along x.
  • depth integer: How many samples the splat texture covers along z.

SetTerrainRegion(Libraries.Game.Graphics.Worlds.TerrainRegion region)

This action sets the region this model draws, without rebuilding it. When a streamed region is loaded again, it is a new TerrainRegion at the same location, and the model built for it before can be reused.

Parameters

SetUsesSplat(boolean splat)

This action sets whether the model is drawn with texture splatting. It takes effect the next time the model is built.

Parameters

  • splat boolean: True to draw with texture splatting.

UsesSplat()

This action returns true if the model is drawn with texture splatting.

Returns

boolean: True if the model uses texture splatting.

WasBuiltInPlace()

This action returns true if the last build updated the existing mesh in place, or false if it loaded a new mesh.

Returns

boolean: True if the last build was an update in place.

Inherited Actions

110 actions from Libraries.Game.Graphics.Model
14 actions from Libraries.Game.Graphics.Worlds.Terrain
13 actions from Libraries.Game.Graphics.Worlds.TerrainPatch
62 actions from Libraries.Interface.Item
211 actions from Libraries.Interface.Item3D
3 actions from Libraries.Language.Object