SimplifyEntityLOD ( entity,level,keep# )

Parameters

entity - mesh entity, or a parent of mesh entities, that gets the level

level - which level of detail to make, from 1 (the first simpler one) to one more than the mesh has; at most 8

keep# - the share of the mesh's triangles the level keeps, more than 0 and less than 1: 0.5 keeps about half

Description

Makes one level of detail of a mesh, keeping the share of its triangles you choose.

AutoEntityLODs decides how simple each version is. SimplifyEntityLOD lets you choose: level 1 at half the triangles, level 2 at a fifth, level 3 at a twentieth, or whatever suits your model. Call it once for each level, in order; a level that already exists is made again. It returns how many meshes got the level, counting the entity and its child meshes, so a model loaded with LoadAnimMesh gets it throughout. A part too simple to lose any more triangles is left with the levels it has.

Each level takes over where its loss of detail would look smaller than a pixel, as automatic versions do. SetEntityLODDistance moves the switch to the distance you want. The simplifier only chooses which vertices to keep, and keeps those exactly as they were, with their texture coordinates, colours, bone weights and morph deltas, so the level has the mesh's surfaces and materials, is lightmapped with it, and moves with its skeleton.

The levels belong to the mesh data, so CopyEntity copies share them. Making a level costs about 3 ms per 1,000 triangles, so it is meant for loading screens and tools: SaveMesh keeps the levels in the model's file, and loading that file gives them back ready to draw, with nothing to make while the game runs.

Gotcha: automatic versions from AutoEntityLODs become levels you keep, so you can replace one of them. A mesh with hand-made levels from AddEntityLOD needs ClearEntityLODs first. Changing the mesh afterwards (ScaleMesh, VertexCoords and so on) leaves the levels showing an old shape, so it draws at full detail until you make them again.

See also: SetEntityLODMesh, SetEntityLODDistance, AutoEntityLODs, EntityLODTriangles, SimplifyMesh, SaveMesh.

Example

; SimplifyEntityLOD Example
; -------------------------

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

camera=CreateCamera()
PositionEntity camera,0,2,-6

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

; A detailed boulder: 6,240 triangles
rock=CreateSphere(40)
ScaleMesh rock,1.2,0.8,1
EntityColor rock,150,140,120

; Three simpler versions, each keeping a share of the triangles
keep#=0.5
SimplifyEntityLOD rock,1,keep
SimplifyEntityLOD rock,2,0.15
SimplifyEntityLOD rock,3,0.04

; Copies share the versions; a row of them runs into the distance
Dim rocks(9)
For n=0 To 9
    rocks(n)=CopyEntity(rock)
    PositionEntity rocks(n),(n Mod 2)*4-2,0,4+n*6
Next
HideEntity rock

wire=False

While Not KeyDown(1)

    ; [ / ] make level 1 keep fewer or more triangles
    change#=0
    If KeyHit(26) And keep>0.1 Then change=-0.1
    If KeyHit(27) And keep<0.85 Then change=0.1
    If change<>0
        keep=keep+change
        SimplifyEntityLOD rock,1,keep
        For n=0 To 9
            FreeEntity rocks(n)
            rocks(n)=CopyEntity(rock)
            ShowEntity rocks(n)
            PositionEntity rocks(n),(n Mod 2)*4-2,0,4+n*6
        Next
    EndIf

    ; Space shows the triangles
    If KeyHit(57) Then wire=Not wire
    WireFrame wire

    ; 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

    For n=0 To 9
        TurnEntity rocks(n),0,0.2,0
    Next

    RenderWorld

    Text 0,0,"Arrow keys: move camera   [ / ] : level 1's share   Space: wireframe   Esc: exit"
    Text 0,20,"SimplifyEntityLOD rock,1,"+keep+"   level 1 has "+EntityLODTriangles(rocks(0),1)+" of "+EntityLODTriangles(rocks(0),0)+" triangles"
    Text 0,40,"Triangles drawn: "+TrisRendered()

    Flip

Wend

End

Index