SimplifyMesh mesh,keep#[,flags]

Parameters

mesh - mesh to simplify, in place

keep# - share of its triangles to keep, from 0 to 1. 1 leaves the mesh as it is

flags (optional) - add these together
0: nothing special (default)
1: keep open edges where they are, for pieces that must meet other meshes exactly, such as wall sections or tiles
2: let small separate parts disappear

Description

Removes triangles from a mesh while keeping its shape as well as it can.

Use it to make a cheaper model yourself: a low-detail stand-in for AddEntityLOD, a lighter mesh for PhysicsBodyMesh, or a trimmed-down import that came with far more triangles than your game needs. It merges triangles where the shape is flattest first, and keeps hard edges, texture seams and the places where materials meet for as long as it can. Where the shape cannot lose more without breaking, it keeps more triangles than you asked for.

Every vertex that stays is exactly as it was, with its normal, texture coordinates, colour and bone weights, and the surfaces stay in the same order; a surface can end up empty. The vertices are renumbered, so do not hold on to vertex indices across the call.

Gotcha: SimplifyMesh changes the mesh data, so every CopyEntity copy sharing it changes too, as with ScaleMesh. Take a CopyMesh first when you want to keep the original, as the example does. Morphed meshes are refused: use ClearMorphTargets first. For versions that swap in automatically with distance, use AutoEntityLODs, which does all of this for you.

See also: AutoEntityLODs, AddEntityLOD, SmoothMesh, CopyMesh, CountTriangles.

Example

; SimplifyMesh Example
; --------------------

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

camera=CreateCamera()
PositionEntity camera,0,0,-5

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

; The left ball keeps every triangle
full=CreateSphere(32)
PositionEntity full,-1.3,0,0
EntityColor full,90,160,230

; The right ball is a copy, simplified to keep about half its triangles
keep#=0.5
simple=CopyMesh(full)
SimplifyMesh simple,keep
PositionEntity simple,1.3,0,0
EntityColor simple,230,160,90

wire=True

While Not KeyDown(1)

    ; [ / ] keep fewer or more triangles: simplify a fresh copy each time
    old#=keep
    If KeyHit(26) And keep>0.15 Then keep=keep-0.1
    If KeyHit(27) And keep<0.95 Then keep=keep+0.1
    If keep<>old
        FreeEntity simple
        simple=CopyMesh(full)
        SimplifyMesh simple,keep
        PositionEntity simple,1.3,0,0
        EntityColor simple,230,160,90
    EndIf

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

    TurnEntity full,0,0.5,0
    TurnEntity simple,0,0.5,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   [ / ] : keep   Space: wireframe   Esc: exit"
    Text 0,20,"SimplifyMesh copy,"+keep
    Text 0,40,"Left: "+CountTriangles(GetSurface(full,1))+" triangles   Right: "+CountTriangles(GetSurface(simple,1))

    Flip

Wend

End

Index