The GeoMipTerrain generates terrain geometry from a heightfield image, but it does more than any bruteforce terrain generator: the GeoMipTerrain divides the terrain into multiple chunks, where each of them can have a different level of detail. The advantage of this approach is that, when the focal point (the place where the terrain has the highest quality, probably the camera) moves, not the entire terrain has to be regenerated to match the correct detail level, like the HeightfieldTesselator, but only the chunks that have a different LOD can be regenerated. Also, it improves culling and collision detection.
Using the GeoMipTerrain is quite easy, it does not require to write many complicated calculations:
terrain = GeoMipTerrain("mySimpleTerrain") terrain.setHeightfield("yourHeightField.png") #terrain.setBruteforce(True) terrain.getRoot().reparentTo(render) terrain.generate()
First, the code creates a GeoMipTerrain instance. The
set_heightfield() call loads in a heightfield image.
Preferably this is a size of a power of two plus one (like 129, 257, 513, 1025,
etc.), but if it is not, the GeoMipTerrain will automatically scale it up to the
nearest correct size (which is quite slow).
set_heightfield() can take a
PNMImage, Texture or a path
according to the Panda Filename Syntax.
set_bruteforce(True) call sets the
terrain to bruteforce rendering – this means that the terrain is created at the
highest quality (the lowest detail level), and LOD is not applied.
In the next section we will explain how to set a LOD level and a Focal Point.
get_root() call returns the NodePath of the terrain.
It is then reparented to
render to be a part of the scene graph. You can
apply Common State Changes to this NodePath.
generate() call generates the terrain
geometry. Note that if the terrain is still quite flat, you will have to scale
the terrain NodePath in the Z direction, because by default, the Z positions are
between 0 and 1. To fix this, scale the terrain up in the Z direction (before
generating it, otherwise it might require you to regenerate it):
This code shows a dynamically updated terrain with LOD:
# Set up the GeoMipTerrain terrain = GeoMipTerrain("myDynamicTerrain") terrain.setHeightfield("yourHeightField.png") # Set terrain properties terrain.setBlockSize(32) terrain.setNear(40) terrain.setFar(100) terrain.setFocalPoint(base.camera) # Store the root NodePath for convenience root = terrain.getRoot() root.reparentTo(render) root.setSz(100) # Generate it. terrain.generate() # Add a task to keep updating the terrain def updateTask(task): terrain.update() return task.cont taskMgr.add(updateTask, "update")
This code shows a dynamically updated terrain, which is updated every frame with the camera as focal point. You see that a few functions are called: The blocksize is set to 32. This means that GeoMipTerrain has to divide the terrain in chunks of 32x32 quads. Then, the near and far distances are set. The Near distance is the distance from the focal point to where the terrain starts to decrease in quality. The far clip is the distance where the terrain is lowest quality. Also, the focal point is set to the Camera’s NodePath; you can specify any NodePath you want, but also a Point2 or Point3. If you specify the latter, please note that only the X and Y positions are used to calculate the distance; the Z position is disregarded. Note that you need to experiment with those values to get a good quality terrain while still maintaining a good performance.
Next, for convenience, the terrain root is stored in a separate variable, which
is scaled and placed in the scene graph. The terrain is then initially
generated, and a task is created which calls
terrain.update() every frame. This function
calculates the new LOD levels based on the movement of the focal point and
updates the chunks which have got a new LOD level.
The GeoMipTerrain provides some advanced features over the terrain:
You can specify a minimum LOD level to GeoMipTerrain. You can do this if you find the terrain a bit too high quality near the focal point, and this could waste your performance. If you set a minimum LOD level, you can prevent this and force the chunks to have a minimum level of detail:
If you make the value higher, it will decrease the quality level near the focal point.
Since flattening the terrain root might interfere with the updating system, GeoMipTerrain provides an auto-flattening function, which can be really useful if you have too many meshes in your scene. This function calls one of NodePath’s flattening functions every time the terrain is regenerated, and each time before the chunks are modified the terrain is restored from a backup node:
There are multiple options: AFM_strong for
flatten_medium(), AFM_light for
flatten_light(), and AFM_off for no flattening at all.
After setting the AutoFlattenMode, GeoMipTerrain will automatically take care of
it at the next
For a full function overview, see the
GeoMipTerrainpage in the API Reference.
The GeoMipTerrain generates texture coordinates between 0 and 1, making the texture stretched over the entire terrain. If you are using a shader, please do not directly base the coordinates on the
vtx_position, because since the terrain can have multiple chunks the vertex position is relative to the chunk. Instead, base your shader calculations on the
vtx_texcoord0generated by the GeoMipTerrain.
The GeoMipTerrain class implements part of the GeoMipMapping algorithm, described in this paper by Willem H. de Boer.