TerrainStroke

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

A TerrainStroke is one use of a TerrainBrush on a TerrainData: everything from pressing the mouse button (or a key) to letting it go. Begin starts it, Apply is called as the brush moves, usually once a frame, and End finishes it. Apply is given how many seconds have passed since the last call, so a stroke does the same thing at any frame rate. If the brush moved a long way since the last call, the stroke applies it at points along the way, a quarter of the brush's size apart, so fast strokes leave no gaps. The first time a stroke touches a region, it keeps a copy of that region as it was. End makes a TerrainStrokeEdit from those copies, which can undo and redo the whole stroke. Cancel puts everything back right away instead. Changed heights go into the TerrainData's edited area, and changed splat weights into its splat edited area, so a WorldTerrain's Rebuild redraws only what the stroke touched. This follows Terrain3D's editor, whose strokes also start, operate as the mouse moves, and stop, keeping a copy of each region they touch for undo.

Example Code

use Libraries.Game.Graphics.Worlds.TerrainData
use Libraries.Game.Graphics.Worlds.TerrainBrush
use Libraries.Game.Graphics.Worlds.TerrainStroke
use Libraries.Game.Graphics.Worlds.TerrainStrokeEdit
use Libraries.Game.Graphics.Worlds.TerrainTool

TerrainData data
data:AddRegion(0, 0)
TerrainTool tools
TerrainBrush brush
brush:SetTool(tools:SCULPT, tools:ADD)
brush:SetSize(10)
brush:SetStrength(4)

TerrainStroke stroke
stroke:Begin(data, brush)
stroke:Apply(50, 50, 0.5)
stroke:Apply(60, 50, 0.5)
TerrainStrokeEdit edit = stroke:End()
edit:Undo()

Actions

Apply(number worldX, number worldZ, number seconds)

This action applies the brush at a world position. If the brush moved more than its spacing since the last call, it is applied at even steps along the way, sharing out the time between them.

Parameters

  • worldX number: The brush's center along x, in world units.
  • worldZ number: The brush's center along z, in world units.
  • seconds number: How long the brush has been held here, usually the time since the last call.

Begin(Libraries.Game.Graphics.Worlds.TerrainData data, Libraries.Game.Graphics.Worlds.TerrainBrush brush)

This action starts a stroke. The brush's settings are read as the stroke goes, so changing the brush during a stroke changes the rest of it.

Parameters

BeginOperation(Libraries.Game.Graphics.Worlds.TerrainData data, text name, boolean changesShape, boolean changesSplat)

This action starts an operation that isn't made with a brush, such as generating hills, placing a stamp or eroding an area, so it can be undone like a stroke. Call CopyArea for the area the operation will change, then change the TerrainData directly, then call End for the edit.

Parameters

  • data Libraries.Game.Graphics.Worlds.TerrainData: The terrain the operation will change.
  • name text: What the operation does, such as "stamp a mountain", for the edit's undo name.
  • changesShape boolean: True if the operation changes heights.
  • changesSplat boolean: True if the operation changes splat weights.

Cancel()

This action stops the stroke and puts every region it touched back the way it was, as if it never happened.

CopyArea(integer minimumX, integer minimumZ, integer maximumX, integer maximumZ)

This action keeps a copy of every region covering a rectangle of samples, for undoing an operation started with BeginOperation. Call it before changing anything in the rectangle. It can be called more than once; a region is only copied the first time.

Parameters

  • minimumX integer: The rectangle's smallest sample along x.
  • minimumZ integer: The rectangle's smallest sample along z.
  • maximumX integer: The rectangle's largest sample along x.
  • maximumZ integer: The rectangle's largest sample along z.

CopyAreaIncludingEmpty(integer minimumX, integer minimumZ, integer maximumX, integer maximumZ)

This action is CopyArea, but it also notes the locations in the rectangle that have no region, so undo removes regions the operation adds there, such as growing a terrain.

Parameters

  • minimumX integer: The rectangle's smallest sample along x.
  • minimumZ integer: The rectangle's smallest sample along z.
  • maximumX integer: The rectangle's largest sample along x.
  • maximumZ integer: The rectangle's largest sample along z.

End()

This action finishes the stroke and returns an edit that can undo and redo it. If the stroke changed nothing, it returns undefined.

Returns

Libraries.Game.Graphics.Worlds.TerrainStrokeEdit: The edit, or undefined if nothing changed.

GetTouchedRegionCount()

This action returns how many regions the stroke has touched so far.

Returns

integer: The number of regions copied for undo.

HasChanged()

This action returns true if the stroke has changed the terrain so far.

Returns

boolean: True if anything has changed.

IsActive()

This action returns true if the stroke has begun and not yet ended.

Returns

boolean: True while the stroke is going.

SetGenerator(Libraries.Game.Graphics.Worlds.TerrainGenerator terrainGenerator, integer layerCount)

This action gives the stroke a TerrainGenerator, whose rain an ERODE brush uses and whose painting rules an AUTO brush uses, and how many layers the terrain has, so auto paint uses only those. Without one, those brushes change nothing. Other brushes don't use it.

Parameters

Inherited Actions

3 actions from Libraries.Language.Object