Blitz3D+ Command Reference

TerrainVisibleChunks ( terrain )

Parameters

terrain - terrain entity to query

Description

Returns how many of a terrain's chunks survived frustum culling in its last render pass.

The terrain is divided into square chunks of up to 32 by 32 cells each, so a 128 grid becomes a 4 by 4 arrangement of 16 chunks and a 1024 grid becomes 32 by 32. Before anything is drawn, each chunk's world bounding box is tested against the camera frustum; this is the count that passed and went on to be drawn.

Read it after RenderWorld. All five terrain counters are cleared at the start of every render pass and refilled as that pass runs, so they always describe the pass that just finished. If two cameras both see the terrain, the second pass overwrites the first one's numbers, so query them next to the render you actually care about.

Paired with TerrainCulledChunks it tells you what fraction of the landscape the camera is paying for - the two always add up to the terrain's total chunk count. Watching the visible number while you turn is the quickest way to understand what your field of view and camera height are costing you: a high camera looking down at a wide view can easily have every chunk visible, at which point fog and a shorter camera range buy back more than any amount of LOD tuning.

Gotcha: this counts chunks, not draw calls and not triangles. A visible chunk drawn at the coarsest detail costs a fraction of one drawn at full detail, so pair it with TerrainTriangles before concluding anything about cost. It reads 0 before the terrain has been rendered at all.

See also: TerrainCulledChunks, TerrainTriangles, TerrainRebuiltChunks, TerrainLODRanges, RenderDrawCalls.

Example

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

; 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

sun_angle#=-30

While Not KeyDown(1)

    ; 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 - turn away to watch the count fall
    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

    ; Chunk statistics come from the render pass that just finished
    visible=TerrainVisibleChunks(terrain)

    Text 0,0,"Arrow keys: fly (turn to change the view)   Esc: exit"
    Text 0,20,"TerrainVisibleChunks(terrain) = "+visible+" of 16 chunks"
    Text 0,40,"Culled: "+TerrainCulledChunks(terrain)+"   Triangles: "+TerrainTriangles(terrain)

    Flip

Wend

End

Index