BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

What goes into a terrain

A terrain is a heightmap for the physics and a compiled render mesh for the renderer, cut into square patches. Each patch has its own textures: a colour map, detail maps that mix tiling detail textures, a low-detail map for distance and a baked light map. Roads are separate meshes laid on top.

BF22142Heroes

The files#

LayerFiles (level server.zip / client.zip)Read by
heightsHeightdata.con (the cluster: a primary map and eight secondary maps around it), HeightmapPrimary.raw (16-bit), HeightmapSecondary_*.raw (8-bit), HeightmapPrimary.mat (material ids)bf2-terrain heightdata.rs, heightmap.rs
render meshterraindata.raw (version 26): patch size, patches per side, per patch its vertices and layer index lists, the surrounding far patches, the cull treebf2-terrain terraindata.rs
colourColormaps/tx<xx>x<zz>.dds, one per patchDDS through bf2-texture
detailDetailmaps/tx<xx>x<zz>_<i>.dds (blend weights per layer) mixing the detail textures terraindata.raw names (common/terrain/textures/detail/...)the same
farLowDetailmaps/tx<xx>x<zz>.dds, lowdetailtexture.ddsthe same
lightLightmaps/tx<xx>x<zz>.dds, baked by the 2005 editorthe same
roadsCompiledRoads.con (object.geometry.loadMesh), Roads/*_compiled.meshbf2-mesh road.rs RoadMesh
vegetationUndergrowth.*, Overgrowth/* with their atlases (.tai)bf2-terrain undergrowth.rs, overgrowth.rs, tai.rs

The patch suffix is "%02ix%02i" of the patch's grid position shifted to start at 0 (RendDX9.dll!0x1018a790, terrain_con.rs patch_suffix); detail maps add _<layer>. The byte layouts, the sampling rules and the retail numbers are in notes/formats/terrain.md; what an editor has to write back byte for byte is the level editor plan.

Village, layer by layer#

Heroes village decoded by bf2-terrain: the heights sampled through HeightmapCluster::getHeight with the 2 x 2 patch grid of terraindata.raw and the nine compiled roads, then each per-patch texture set stitched into one map (colour, the detail weights of layer 1, the low-detail weights, two light map channels). North is up.
Heroes village decoded by bf2-terrain: the heights sampled through HeightmapCluster::getHeight with the 2 x 2 patch grid of terraindata.raw and the nine compiled roads, then each per-patch texture set stitched into one map (colour, the detail weights of layer 1, the low-detail weights, two light map channels). North is up.

Village is small: a 257 x 257 primary heightmap with 2 m cells covers 512 m, cut into 2 x 2 patches of 128 cells; the eight secondary maps around it (4 m cells, 8-bit) form the land beyond the playable square. Each patch has seven layer blocks of index lists in terraindata.raw (the detail layers plus the extra slot); the detail weight maps exist for the layers a patch uses (village: 1 and 2). The heights the compiled mesh stores follow from the heightmap cluster byte for byte (compile.rs, exact on village, BF2 Strike_at_Karkand and 2142 Camp_Gibraltar).

How the renderer uses them (stages 1, 2 and 4 of the render pipeline): terrain pass 1 fills the depth buffer and writes a screen-sized light buffer from the light map (lightmap.b * GICOLOR and lightmap.g) and the sun shadow; pass 2 draws the colour map, the detail textures mixed by the detail maps near the camera and the low-detail map far away, reading the light buffer instead of lighting again (Terrain::drawHi, RendDX9.dll!0x100e0fa0; bf2-render terrain/mod.rs). Roads are drawn after the terrain colour, depth-biased onto it (roads.rs). The physics never sees the textures: collisions and the server use the heightmap and its material map alone.

Per game#

BF22142Heroes 1.126
primary heightmap513 x 513 or 1025 x 1025, 16-bitthe samethe same format; village 257 x 257
patches4 or 8 per side, patch size 128the samevillage 2 per side
terraindata.rawversion 26, flag 1 (all 30 levels parse)the same readerthe same; consumed exactly by terraindata::parse
detail mapstexture category 6the same files, category 8 (RendDX9.dll@2142!0x1014b6a0)the same names
level extrasgroundhemi.dds, environment mapsthe sameadds the cartoon light-warp ramps lightWarpBundle.dds / lightWarpSkinned.dds per level; variant levels (village_snow) ship their own terrain textures and mount the base level's archives (archives)

Size limits#

How big a terrain can get before cells go missing, read out of the terrain loader and the culler (H2938). The world is W = (size - 1) x pws.x metres wide, size being the primary heightmap's side and pws the primary world scale (heightmap.setScale of the primary; the 2005 editor copies it into terrain.primaryWorldScale, EditorRendDX9.dll, 0x102d4e20). It is cut into P = (size - 1) / patchSize patches per side (the same function: it asserts a square map at least one patch wide and divides).

The cull tree in terraindata.raw is a complete quadtree over the world. Node i's children are 4i+1..4i+4 (Terrain::loadCullTree, RendDX9.dll!0x100e46c0). The editor splits it down to 32 m nodes, marks the 64 m nodes as the cells that draw, and marks the patchSize x pws.x nodes as patch roots (EditorRendDX9.dll, 0x1023a890, with the culler's 32 / 64 from EditorRendDX9.dll, 0x1026739d; the game's own builder for old files, RendDX9.dll!0x100e4d60 with 0x100e3430(terrain, 32, 64) from Terrain::load 0x100de6d0, does the same). bf2_terrain::limits::cull_tree builds that tree, and it equals all 76 retail trees of BF2, 2142 and Heroes 1.126 node for node (boxes, flags, patch bytes: crates/bf2-terrain/tests/retail_limits.rs). Four limits follow:

LimitWhereWhat breaks
a cell names its patch with one byte, 0xff = noneTerrainCullNode::load RendDX9.dll!0x100e36c0; Heroes RendDX9.dll@bfh126!0x101ba0b4only patches 0..254 draw: P x P <= 255, so P <= 8. At P = 16 patch 255 (the +x / +z corner, tx15x15) never draws; at P = 32, 769 of 1024 patches do not
one shared LOD index table of 85 entries; entries 0..20 have 4 children and the stitching ranges of levels 1 and 2, entries 21..84 have neitherTerrain::loadIndexData RendDX9.dll!0x100db020; a node below its patch root takes its parent entry's child (0x100e46c0)a cell d = log2(patchSize x pws.x / 64) levels under its patch root is whole for d <= 2. At d = 3 (patches of 512 m, e.g. 1025 at scale 4) every cell next to a coarser LOD draws nothing: holes on the rings where the LOD changes. At d >= 4 (1024 m) cells have no entry and the loader reads the 32 m nodes' entries through a null one
patch roots and cells found by an exact float compare of the node width0x100e4d60, EditorRendDX9.dll, 0x1023a890P a power of two, W and patchSize x pws.x 64 m times a power of two, patchSize x pws.x >= 64; otherwise no node matches and nothing draws (a 769 map: 6 patches per side)
patch size"m_iPatchSize <= 255", EditorRendDX9.dll, 0x102dd760; vertex x / z are bytes in terraindata.rawpatchSize <= 255

Together: 64 m <= patchSize x pws.x <= 256 m and P <= 8, so the widest terrain that draws every cell is 8 x 256 = 2048 m (BF2's 1025 at scale 2). The surrounding ring also wants whole scales: it is placed with round(pws.x) and round(sws.x) (SurroundingPatch::setup, RendDX9.dll!0x1018eb90). The height bit resolution (heightmap.setBitResolution) only sets the height step and plays no part.

HeightmappatchscaleWPDraws
2571282512 m2whole (Heroes village)
51312821024 m4whole (Strike at Karkand)
102512811024 m8whole (Heroes Lunar)
102512822048 m8whole (most BF2 / 2142 maps)
5136442048 m8whole (coarser heights)
102512844096 m8holes at every LOD seam (index entries 21..84)
204912824096 m16one patch missing (patch 255, the +x / +z corner)
409712814096 m32769 patches missing

For a wanted width the editor's Terrain panel ("Size limits and surrounding terrain", editor_terrain_limits, bf2_terrain::limits::suggest) lists the layouts of that width, those that draw whole first: patch size 128, then the finer heights. Past 2048 m no layout draws whole. The least bad is 2049 at scale 2, which loses only the corner patch (keep it outside the combat area). The other option is staying at 2048 m and letting the 8 surrounding tiles carry the land beyond. Our client follows the same reader rules (bf2-render terrain/lod.rs Culler::new, test large_terrains_lose_the_cells_the_retail_reader_loses), so it drops the same cells.

Sun shadows on the ground (H2959)#

A dynamic object's shadow on the terrain is drawn by the terrain itself. Each caster gets a shadow record, and Terrain::drawHi (RendDX9.dll!0x100e0fa0) walks the culled cells patch by patch. It offers each patch to every record, and QuadPatch::addShadowReceiverQuads (0x1018ade0) writes the cells under the record's rectangle into one dynamic index buffer. The rectangle is the caster's light-space box with its far distance times ((0.643 + sun.y) + 1) x 4 (0x100e1088). The buffer holds 0xc000 (49,152) indices per frame (Terrain +0x458, Terrain::Terrain 0x100df7c0). A level-0 receiver quad is one heightmap cell of 6 indices, so a frame holds 8,192 cells: 8,192 x scale² m² of receiver rectangles. That is 32,768 m² at scale 2, 8,192 m² at scale 1 and 131,072 m² at scale 4. Once count + 12 passes the buffer, drawHi stops offering patches; the patches later in the culled cells' order (the cull tree's -x / -z quadrant first) get no ground shadow that frame. The casters themselves keep their self-shadowing.

The terrain's size does not enter. Only the casters inside shadowMapDepthDistance count (LightManager +0x214, 40 m from the camera; the sun pass's caster query and the frustum it fits, 0x10164fb0 / 0x101628a0), and the scale sets their cost. Measured in our client, which ports these routines (crates/bf2-editor/tests/large_shadows_bf2.rs, BF2 Kubra_Dam, 2048 m at scale 2, dynamic shadows on): one M1A2 (4 shadow records) takes 4,194 indices both at the terrain's centre and at its far quarter (768, 768). A crowd of 16 tanks around the camera takes 45,690, and 24 fill the buffer. With a lone tank on the patch east of x = 768 and 21 tanks on the patch west of it, the west patch is offered first, the buffer is full before the east patch, and the lone tank's ground shadow is gone (about 4,000 shadowed pixels around it before, under 20 after):

Kubra_Dam at the far quarter, dynamic shadows on. Left: a lone M1A2 east of the patch boundary at x = 768, its ground shadow drawn (4,194 of 49,152 receiver indices). Right: 21 more tanks on the patch west of it fill the buffer (88 records) before the east patch is offered, and the lone tank's ground shadow is gone.
Kubra_Dam at the far quarter, dynamic shadows on. Left: a lone M1A2 east of the patch boundary at x = 768, its ground shadow drawn (4,194 of 49,152 receiver indices). Right: 21 more tanks on the patch west of it fill the buffer (88 records) before the east patch is offered, and the lone tank's ground shadow is gone.

So about 12 tank-sized casters near the camera fill the buffer at scale 2, about 3 at scale 1 (Heroes Lunar and ruin are scale 1), and about 47 at scale 4. The Terrain panel prints this budget for the open level. Other things that are the same on every map size: dynamic shadows reach only those 40 m; the surrounding tiles have no shadow pass (Terrain::drawSurrounding 0x100e15e0 draws only Shared_SurroundingTerrain); and the baked light maps keep the texel size the level was baked with (BF2 512 px and Heroes village 1024 px per 256 m patch). Retail's receiver quads snap to 2 m steps (2 << level in 0x1018ade0) and divide by the whole scale. At scale 4 the level-0 quads stop up to one cell short of a rectangle's far edge, and the level-1 quads start on odd vertices. Scale 4 with 128-sample patches already breaks the cells (see above).

Surrounding terrain#

The land beyond the primary map has two parts. The physics side is the height cluster's secondary maps (Heightdata.con); the drawn side is the far tiles compiled into terraindata.raw. The engine draws at most 8 far tiles: 4 corners (even index) and 4 edges (odd index), each one primary width wide. The loader stores tile index in an 8-slot table with no range check (Terrain::loadSurroundingPatches, RendDX9.dll!0x100d9200; Heroes RendDX9.dll@bfh126!0x10144b6e). The draw walks the 4 corner slots, then the 4 edge slots (Terrain::drawSurrounding, RendDX9.dll!0x100e15e0; Heroes RendDX9.dll@bfh126!0x1014afb5). The setup knows positions 0..7 only and logs "unknown surrounding terrain patch type" for any other (RendDX9.dll!0x1018eb90). The 2005 editor's create-all / remove-all loop 0..7 (EditorRendDX9.dll, 0x102e0b10 / 0x102e0b50). A tile's grid has n = (int)((size - 1) x pws.x / sws.x) steps, finest next to the primary map (Terrain::createSurroundingGrids, RendDX9.dll!0x100df110). The height cluster can be bigger than 3 x 3 (setClusterSize allocates size x size cells, bf2_w32ded.exe!0x00696910), but cells past the inner ring have collision only and no far tile.

Every console command the original parses for it. The Terrain.con ones run only in the 2005 editor's branch; the game reads their values from terraindata.raw:

CommandArgsFileOriginalbf2-rs
terrain.secondaryWorldScaleVec3Terrain.conany value, stored as is (EditorRendDX9.dll, 0x101e36c0 -> 0x1037ad10); the editor's create copies the secondary map's heightmap.setScale; the game rounds x to place the ringread from terraindata.raw; grids and placement as retail (bf2-terrain surrounding.rs, bf2-render terrain/surround.rs); shown
terrain.lowDetailmapSizeintTerrain.constored as is (EditorRendDX9.dll, 0x101e3ce0)read, written back; shown
terrain.lowDetailmapBaseNamestringTerrain.confar tile i loads <base>_s<i>.dds (RendDX9.dll!0x1018f190)the same names (terrain_con.rs surrounding_texture); shown
terrain.farSideTilingVec2Terrain.conany value (EditorRendDX9.dll, 0x101e0c40; game setter RendDX9.dll!0x100e3000)drawn as retail; edited in the editor, saved in terraindata.raw
terrain.farTopTilingHifloatTerrain.conany value (0x101e0f60; RendDX9.dll!0x100e2f00)drawn as retail; edited, saved
terrain.farTopTilingLowfloatTerrain.conany value (0x101e1270; RendDX9.dll!0x100e2ee0); before version 23 the Hi valuedrawn on low terrain quality; edited, saved
terrain.FarTopTilingfloatTerrain.conregistered (EditorRendDX9.dll, 0x101e1359); executor not tracednot used
terrain.farYOffsetfloatTerrain.conany value (0x101e1830; RendDX9.dll!0x100e2f20)drawn as retail; edited, saved
terrain.surroundingcolormapSettingsstringeditorcolour-map generation settings for the far tiles (EditorRendDX9.dll, 0x101e8d80); not in terraindata.rawnot used (far colour maps re-lit from the saved sky, H2784)
terrain.surroundinglowDetailmapSettingsstringeditorthe same for the low-detail maps (0x101e9110)not used
terrain.createSurroundingPatchint 0..7editorcreates tile i in the 8-slot table (EditorRendDX9.dll, 0x102e0a20; its range check reads i < 0 && i > 7 and lets every value through)tiles come from terraindata.raw; the editor lists which of the 8 exist
terrain.removeSurroundingPatchint 0..7editorremoves tile i (0x102e0aa0, the same check)not used
terrain.createSurroundingAllPatchseditortiles 0..7 (0x102e0b10)not used
terrain.removeSurroundingAllPatchseditortiles 0..7 (0x102e0b50)not used
terrain.surroundingTerrainEngineModebooleditorswitches the 8 tiles between the game's and the editor's patch (0x102e0b90)not used
heightmapcluster.setClusterSizeintHeightdata.coneven sizes + 1; size x size cells (bf2_w32ded.exe!0x00696910); 3 in every retail levelthe same cluster (bf2-terrain heightdata.rs); shown
heightmapcluster.setHeightmapSizeintHeightdata.conone cell's world width (0x006983a0)the same
heightmapcluster.addHeightmapclass, x, zHeightdata.conthe tile location: cell (x, z) with no bounds check, so x and z must lie in -size/2..size/2; offset round(x * width), round(z * width) (0x00698120)the same; the sculpt crosses the edge (H2401)
heightmap.setSizew, hHeightdata.conthe secondary map's samples (0x006a83e0)the same; shown
heightmap.setScaleVec3Heightdata.conmetres per sample, height step (0x006a92f0)the same; shown
heightmap.setBitResolutionintHeightdata.con8 or 16 bits (0x006a8470); 8 in every retail secondarythe same; shown
heightmap.loadHeightDatafileHeightdata.conw x h x bits / 8 bytes (0x006ab4a0)the same; written back after a sculpt
heightmapcluster.importSurroundingRawHeightMapfileeditorthe 2005 editor's import of one raw over the secondary mapsnot used (per-tile import, H2505)

The editor's Terrain panel shows the tiles present, the scales, the grid steps, the secondary maps and this table (editor_far_terrain). It edits the far tiling and the far y offset, one undo step each, and Save writes them into terraindata.raw (editor_far_terrain_set; crates/bf2-editor/tests/far_terrain_bfheroes.rs: heat's edit redraws, saves, and undoes to the shipped bytes). The secondary world scale, the low-detail map size and the tile set are shown but not edited: changing them rebuilds the tiles' grids and textures, which the 2005 editor's compiler did and which is not in our data.

How it is tested#

  • Rebuilt byte for byte (crates/bf2-terrain/tests): the compiled terrain of one level of each game is compiled again from its heightmaps and must equal the shipped file in every byte of every patch vertex (retail_compile.rs); recompiling an unedited patch gives the shipped file back (retail_recompile.rs); every patch's layer blocks are rebuilt from the rules on this page (retail_layers.rs).
  • Every shipped level is read and consumed exactly (retail_levels.rs), and every level's cull tree equals the one the port builds, node for node (retail_limits.rs).
  • The editor's side: size limits and surrounding terrain on two Heroes levels, heightmap export and import, shadows on a 2048 m terrain.

All of these need an install and are skipped without one. Five of the page's 52 addresses stand in a test's text; the formats are proven by the files, not by the functions' addresses.

Where it is read and drawn#

Stepbf2-rs
heightmap cluster, samplingbf2-terrain heightdata.rs apply, heightmap.rs HeightmapCluster::height
compiled terrainbf2-terrain terraindata.rs parse / write, compile.rs
texture namesbf2-terrain terrain_con.rs patch_suffix, patch_texture, detail_texture
roadsbf2-mesh road.rs RoadMesh::parse
drawbf2-render terrain/mod.rs, roads.rs, undergrowth.rs, overgrowth.rs
the picturecrates/bf2-terrain/examples/terrain_layers.rs, crates/bf2-mesh/examples/mesh_json.rs (roads)

The whole level folder is on levels; the textures' formats on textures. How the engine uses these layers (the height at a point, rays, materials, growth): terrain.

Source: docs/dev/assets/terrain.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.