Blitz3D+ Command Reference

TerrainLODRanges terrain,near_range#,far_range#

Parameters

terrain - terrain entity to tune

near_range - world distance inside which chunks use full detail; pass 0 with far_range 0 to restore automatic ranges

far_range - world distance beyond which chunks use the coarsest detail; must be greater than near_range, or 0 alongside near_range 0

Description

Sets the world distances over which terrain chunks drop to coarser detail.

The terrain is drawn as a grid of chunks, and each one picks a detail level from how far away it is. Everything inside near_range is drawn at full detail, everything past far_range at the coarsest, and the levels in between are spread evenly across the gap. This command is where you decide how that budget is spent.

Which numbers suit you depends on the game. A strategy camera looking down at a wide landscape wants a short near range and a long far range, so the ground under the cursor is crisp and the distant hills are cheap. A first-person game with the camera at head height wants both pushed out, because the player is looking along the ground and coarse triangles nearby are obvious. Distance is measured from the camera's detail eye to the nearest point of each chunk's world bounding box rather than to the terrain's origin, so a large terrain changes detail across its surface instead of all at once.

Passing 0 for both restores the automatic ranges the engine derives from the terrain's chunk size and world scale. Those are a sensible default and worth returning to whenever hand-tuned numbers stop matching - after a ScaleEntity, for example, since the ranges are in world units and scaling the terrain changes what those units cover.

Level selection uses hysteresis, so a camera sitting right on a boundary does not flip a chunk back and forth and rebuild it every frame. If TerrainDetail's morph argument is on, chunks blend between levels instead of popping.

Gotcha: TerrainDetail is still the overall quality and triangle-budget control - this command moves detail around, it does not decide how much there is. Neither does it cull anything: chunks beyond far_range are still drawn, just coarsely, and frustum culling is a separate thing you can watch with TerrainCulledChunks. Apart from the 0,0 case the values are checked, so near_range must be above 0 and far_range above near_range or you get "Terrain LOD ranges must be positive and far must be greater than near". Chunks that contain holes always draw at full detail whatever you set here.

See also: TerrainDetail, TerrainTriangles, TerrainVisibleChunks, TerrainCulledChunks, CreateTerrain.

Example

; TerrainLODRanges 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 stays the triangle budget; morphing smooths LOD switches
TerrainDetail terrain,8000,True

; Plain grass texture drawn in code
grass=CreateTexture(8,8,1+8)
SetBuffer TextureBuffer(grass) : ClsColor 75,135,60 : Cls : SetBuffer BackBuffer()
EntityTexture terrain,grass

near_range#=12
auto_ranges=0
wire=0
sun_angle#=-30

While Not KeyDown(1)

    ; Press [ / ] to pull the LOD bands in or out; 0 restores automatic
    If KeyDown(26) And near_range>4 Then near_range=near_range-0.2
    If KeyDown(27) And near_range<60 Then near_range=near_range+0.2
    If KeyDown(26) Or KeyDown(27) Then auto_ranges=0
    If KeyHit(11) Then auto_ranges=1

    ; W toggles wireframe so the chunk detail bands are visible
    If KeyHit(17) Then wire=1-wire
    WireFrame wire

    If auto_ranges
        ; Zero for both ranges restores automatic selection
        TerrainLODRanges terrain,0,0
    Else
        TerrainLODRanges terrain,near_range,near_range*4
    EndIf

    ; 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,"Arrows: move   [ / ] : LOD ranges   0: automatic   W: wireframe   Esc: exit"
    If auto_ranges
        Text 0,20,"TerrainLODRanges terrain,0,0 (automatic ranges)"
    Else
        Text 0,20,"TerrainLODRanges terrain,"+near_range+","+(near_range*4)
    EndIf
    Text 0,40,"Triangles this frame: "+TerrainTriangles(terrain)

    Flip

Wend

End

Index