Blitz3D+ Command Reference

TerrainDetail terrain,detail_level[,morph]

Parameters

terrain - terrain handle

detail_level - triangle budget for the terrain; 2000 (default)

morph (optional) - True to enable vertex morphing, False to disable it; False (default)

Description

Sets how many triangles a terrain is allowed to spend on itself.

A terrain redraws itself at a different resolution every frame, fine near the camera and coarse in the distance. detail_level is the budget for that: the terrain keeps subdividing the parts that need it most until it runs out of triangles. It is the single dial that trades landscape quality against frame rate, and it is the first thing to turn down if a scene is running slowly.

A new terrain starts at 2000 with morphing off. Around 2000 to 4000 looks fine for a game where you are on the ground; 8000 or more is worth it for a flight view where you can see a long way. Because the budget is spent where the camera is looking, a bigger terrain does not automatically need a bigger number - a 512 terrain at 4000 triangles simply shows detail over a smaller area at a time.

The morph parameter is the one people forget, and you almost always want it True. Without it, the terrain swaps between resolutions in single steps and you see vertices jump, especially on ridge lines ahead of you. With morphing on, vertices slide between the two levels instead, and the change becomes hard to spot. It costs a little but it is nearly always the right trade.

There is no point pushing detail_level past the terrain's full resolution, which is grid_size * grid_size * 2 triangles - once every square is drawn at full size, extra budget does nothing. At the other end the terrain always draws at least a couple of triangles, so a tiny or negative number gives you a nearly flat sheet rather than nothing at all.

You can call this every frame if you want to adapt to the frame rate, and it is cheap to do so while the number stays the same. Changing the number does reallocate the terrain's working mesh, so avoid nudging it by one every frame; step it in chunks, or only when the frame time has drifted. Note that morphing is applied even when detail_level has not changed, so TerrainDetail terrain,detail,True is a valid way to switch morphing on by itself.

Read TrisRendered after RenderWorld to see what the whole scene actually cost, which is the honest way to check whether a change to the budget helped.

See also: CreateTerrain, LoadTerrain, TerrainShading, TrisRendered, TerrainLODRanges.

Example

; TerrainDetail Example
; ---------------------

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

camera=CreateCamera()
CameraRange camera,1,500

light=CreateLight()
RotateEntity light,45,45,0

; Load a 256x256 height map as a terrain
terrain=LoadTerrain("media/Height_map.bmp")

; Stretch it into a landscape: 2 units per grid square, 60 units tall
ScaleEntity terrain,2,60,2

; Texture the landscape
grass_tex=LoadTexture("media/MossyGround.BMP")
EntityTexture terrain,grass_tex

; Start in the middle of the terrain
PositionEntity camera,256,30,256

detail=4000
morph=True

While Not KeyDown(1)

    ; Change the triangle budget with [ and ], toggle morphing with Space
    If KeyDown(26) And detail>200 Then detail=detail-25
    If KeyDown(27) And detail<10000 Then detail=detail+25
    If KeyHit(57) Then morph=1-morph

    ; TerrainDetail sets how many triangles the LOD terrain may use;
    ; vertex morphing smooths the 'pop-in' as detail shifts around
    TerrainDetail terrain,detail,morph

    ; Drift forward on its own; arrow keys steer and speed up
    MoveEntity camera,0,0,0.2
    If KeyDown(200) Then MoveEntity camera,0,0,1
    If KeyDown(208) Then MoveEntity camera,0,0,-1
    If KeyDown(203) Then TurnEntity camera,0,1,0
    If KeyDown(205) Then TurnEntity camera,0,-1,0

    ; Glide at a fixed height above the ground
    x#=EntityX(camera)
    z#=EntityZ(camera)
    y#=TerrainY(terrain,x,EntityY(camera),z)+8
    PositionEntity camera,x,y,z

    RenderWorld

    Text 0,0,"[ / ]: change detail   Space: toggle morphing   Arrows: fly   Esc: exit"
    If morph Then Text 0,20,"TerrainDetail terrain,"+detail+",True" Else Text 0,20,"TerrainDetail terrain,"+detail+",False"
    Text 0,40,"Rendered triangles: "+TrisRendered()

    Flip

Wend

End

Index