Blitz3D+ Command Reference

TerrainLayer terrain,layer,texture[,uv_scale#]

Parameters

terrain - terrain entity created with CreateTerrain or LoadTerrain

layer - which of the four ground layers to set:
   0: base layer, shown wherever the splat weights are zero
   1: second layer
   2: third layer
   3: fourth layer

texture - texture tiled over the terrain for this layer; 0 clears the layer

uv_scale (optional) - texture repeats per terrain grid unit; 1 (default)

Description

Sets one of the terrain's four tiled ground textures.

This is the way in to the built-in layered terrain material - the modern way to paint a landscape. The first time you call it (or any of its companions) the terrain stops using the single EntityTexture route and starts using the terrain shader, which tiles up to four ground textures and blends between them.

The usual set is grass on layer 0, dirt on 1, rock on 2 and snow on 3, but the layers have no built-in meaning - they are just four textures. Where each one shows is decided by TerrainSplatMap. Layer 0 is special only in that it is the fallback: anywhere the splat weights add up to nothing, layer 0 is what you see, so put your most common ground there and paint the rest on top.

uv_scale is repeats per terrain grid unit, so on a 128 grid a scale of 0.2 tiles the texture about 25 times across the whole landscape. Small numbers give big tiles, large numbers give fine ones. Each layer keeps its own scale, which lets coarse rock faces and fine gravel tile at rates that suit them. The colour layers are treated as sRGB images and converted to linear light by the normal texture pipeline, so an ordinary photo-style ground texture looks right without any preparation.

Passing 0 for texture clears just that layer and leaves the other three, their scales and tints alone - handy for a summer/winter swap where only the top layer changes.

Gotcha: layer must be 0 to 3 or you get "Terrain layer must be from 0 to 3", and uv_scale must be greater than zero. Once the built-in material is active, EntityTexture no longer paints the terrain - it writes the legacy brush and logs a warning telling you to use TerrainLayer or ClearTerrainMaterial instead.

See also: TerrainSplatMap, TerrainLayerColor, TerrainNormalMap, ClearTerrainMaterial, CreateTerrain.

Example

; TerrainLayer Example
; --------------------

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

camera=CreateCamera()
PositionEntity camera,0,6,-40
CameraClsColor camera,110,150,200

light=CreateLight(1)
RotateEntity light,50,-30,0

; Rolling hills built from a sine-wave height field
terrain=CreateTerrain(128)
For z=0 To 127
    For x=0 To 127
        ModifyTerrain terrain,x,z,0.2+Sin(x*10)*Cos(z*10)*0.12
    Next
Next
ScaleEntity terrain,0.5,20,0.5
PositionEntity terrain,-32,-5,-32
TerrainShading terrain,True
TerrainDetail terrain,8000,True

; A grass/stone checker texture so the layer's uv scale is easy to see
checker=CreateTexture(64,64,1+8)
SetBuffer TextureBuffer(checker)
Color 90,150,70
Rect 0,0,64,64,True
Color 210,205,190
Rect 0,0,32,32,True
Rect 32,32,32,32,True
SetBuffer BackBuffer()

uv#=0.25
sun_angle#=-30

While Not KeyDown(1)

    ; Press [ / ] to change how often the layer texture repeats
    If KeyDown(26) And uv>0.05 Then uv=uv-0.005
    If KeyDown(27) And uv<1 Then uv=uv+0.005

    ; Set diffuse layer 0 of the built-in terrain material
    TerrainLayer terrain,0,checker,uv

    ; The sun slowly circles so the hills' shading shifts
    sun_angle=sun_angle+0.2
    RotateEntity light,50,sun_angle,0

    ; Arrow keys move the camera
    If KeyDown(200) Then MoveEntity camera,0,0,0.1
    If KeyDown(208) Then MoveEntity camera,0,0,-0.1
    If KeyDown(203) Then TurnEntity camera,0,1,0
    If KeyDown(205) Then TurnEntity camera,0,-1,0

    RenderWorld

    Text 0,0,"Arrow keys: move camera   [ / ] : change uv scale   Esc: exit"
    Text 0,20,"TerrainLayer terrain,0,checker,"+uv

    Flip

Wend

End

Index