Blitz3D+ Command Reference

TerrainNormalMap terrain,texture[,uv_scale#][,strength#]

Parameters

terrain - terrain entity created with CreateTerrain or LoadTerrain

texture - tangent-space normal map tiled over the terrain; 0 turns normal mapping off

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

strength (optional) - how strongly the bumps show, 0 for flat; 1 (default)

Description

Adds a shared tiled detail normal map to the terrain surface.

A height field is a smooth sheet, and up close it looks like one. A detail normal map is what makes it read as ground: grain in the soil, pebbles in the gravel, ripples in the sand. It is tiled over the whole terrain and applied after the four colour layers have been blended, so one map lifts every layer at once.

It is deliberately one shared map rather than one per layer. Four colour layers plus a splat map already fill most of the renderer's texture stages, and four extra normal maps would not fit; in practice a single fine-grained detail normal does nearly all of the visible work anyway, because at the distance where you can see bumps you are usually standing on one material.

uv_scale tiles it independently of the colour layers and is normally a much higher repeat, since it stands for surface grain rather than for regions of the map. strength scales the effect: 0 flattens it back to the plain surface normal, 1 is the map as authored, and higher values exaggerate it. Fading strength down with distance-of-day or weather is an easy way to make wet ground look flatter than dry.

Gotcha: uv_scale must be positive and strength must not be negative - both raise a runtime error otherwise. Passing 0 for texture turns normal mapping off but keeps the scale and strength you passed in, so you can toggle it back on later with the same look. The map is lighting detail only: it does not change the collision surface, picking, or the height TerrainY reports.

See also: TerrainLayer, TerrainSplatMap, TerrainLayerColor, ClearTerrainMaterial, BrushNormalMap.

Example

; TerrainNormalMap 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

; Low ambient light so the bump shading stands out
AmbientLight 50,60,70

; 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

; Plain grass texture on layer 0
grass=CreateTexture(8,8,1+8)
SetBuffer TextureBuffer(grass) : ClsColor 75,135,60 : Cls : SetBuffer BackBuffer()
TerrainLayer terrain,0,grass,0.2

; A rippled tangent-space normal map written pixel by pixel
bumps=CreateTexture(32,32,1)
SetBuffer TextureBuffer(bumps)
LockBuffer TextureBuffer(bumps)
For y=0 To 31
    For x=0 To 31
        nx=128+Sin(x*45)*90
        ny=128+Sin(y*45)*90
        WritePixelFast x,y,$FF000000 Or (nx Shl 16) Or (ny Shl 8) Or 255,TextureBuffer(bumps)
    Next
Next
UnlockBuffer TextureBuffer(bumps)
SetBuffer BackBuffer()

strength#=1
enabled=1
sun_angle#=-30

While Not KeyDown(1)

    ; Press [ / ] to change strength; Space toggles the map on and off
    If KeyDown(26) And strength>0 Then strength=strength-0.02
    If KeyDown(27) And strength<3 Then strength=strength+0.02
    If KeyHit(57) Then enabled=1-enabled

    If enabled
        TerrainNormalMap terrain,bumps,0.1,strength
    Else
        ; A zero texture disables normal mapping but keeps the settings
        TerrainNormalMap terrain,0,0.1,strength
    EndIf

    ; The moving sun makes the bumps easy to see
    sun_angle=sun_angle+0.3
    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   [ / ] : strength   Space: on/off   Esc: exit"
    If enabled
        Text 0,20,"TerrainNormalMap terrain,bumps,0.1,"+strength
    Else
        Text 0,20,"TerrainNormalMap terrain,0,0.1,"+strength+" (disabled)"
    EndIf

    Flip

Wend

End

Index