On this page
Shadows and lighting
Most of the light in a Battlefield level was computed years ago, in the editor, and ships as textures. What the game computes each frame is small and exact: one shadow map from the sun for the things that move, a light factor for each soldier, and a few point lights. This page says which is which, in the order a frame meets them. The frame's passes as a whole are on the render pipeline page.
The frame around it: the render pipeline.
Baked light#
Lightmaps. Every static mesh instance and every terrain patch has a lightmap: the sun, the sky and the shadows of everything static, computed by the editor. The shader multiplies it in. Nothing moves in it: a building's shadow on the ground is paint.
The hemisphere map. groundhemi.dds is one texture a level that gives ambient colour by position: a vehicle under trees is tinted darker and greener than one in the open. Meshes that move sample it at their position.
The simple shadow map. Soldiers and other moving meshes cannot use lightmaps, so a level ships two small maps (SimpleShadowmap.raw and SimpleShadowmapIO.raw, loaded by RendDX9.dll!0x100f4280):
| Map | A texel says | Packed as |
|---|---|---|
| shadow level | how dark the ground is here | 2 bits: floor(v * 0.011764706 + 0.5) |
| the IO map (indoors / outdoors) | one bit a texel that sorts the spot into one of two kinds | 1 bit: v >= 0x80 |
Both cover the playable terrain at half the heightmap's resolution. From them and two rays (is there a roof above; how high above the ground am I) comes a soldier's light factor (RendDX9.dll!0x101a8139):
no roof above, the IO bit clear: 1
no roof above, the IO bit set: lerp(level, 1, clamp((height - 3) / 3))
a roof above, the IO bit clear: level
a roof above, the IO bit set: 1
factor = pow(that, shadowSunScalePower)So a soldier walking into a baked shadow darkens with it, and one who jumps or stands 3 to 6 m above the ground fades back to full light. The notes give the four cases by the bit's value and do not say which value the level makers painted for "indoors"; the piece keeps to the bit. It is cheap, and it is why soldiers never look pasted onto a shaded street.
The sun's shadow map#
The LightManager (RendDX9.dll!0x100f4ed0) draws dynamic shadows. Its pass (0x10167b90) runs right after the frame begins and only when three things hold: there is a sun, the video option for dynamic shadows is on, and shadow updates are enabled.
Setting (shadowManager.*) | Default | Meaning |
|---|---|---|
shadowMapSize | 1024 | texels a side of the sun's map |
shadowMapObjectSize | 256 | texels of a per-object map |
shadowMapDepthDistance | 40 | how far from the camera shadows are drawn, in metres |
shadowTSMFocus | 0.52 | where the trapezoid of the projection puts its detail |
shadowSunScalePower | 1 | the power of the soldier's light factor |
No shipped script changes them. The map itself depends on the graphics card, as the original chose in 2005: a hardware depth texture where the card compares depths itself, otherwise a floating point colour target that the shader compares.
Casters. Objects whose template says castsDynamicShadow and that lie inside the light's frustum are drawn from the sun into the map (0x10164fb0), with the viewport inset by 3 texels.
The projection. A square map over 40 m would waste most texels far from the camera. The original warps the map with a trapezoid so that texels are dense near the viewer (computeSunShadowProjection, 0x101628a0): it takes each caster's bounds in camera space, the camera's frustum cut at the depth distance, the sun's direction and the focus, and builds the two matrices the receivers use (ShadowProjMat, ShadowTrapMat). The port's function answers the same bits as the original on 5,000 cases.
Receivers. Static meshes, bundled meshes (vehicles), skinned meshes (soldiers), the terrain, the undergrowth and near water each have a shader variant that looks its pixel up in the map and compares depths. On the terrain the receiver quads are gathered per caster into one index buffer of fixed size, which puts a ceiling on how much ground can receive shadows in one frame (the terrain page of the assets area has the numbers).
Point lights#
A muzzle flash, an explosion or a lamp is a point light. The light manager keeps the frame's list, filters it per mesh, and the mesh is drawn again for each light that reaches it with additive blending (a "delta pass"). The notes also list a smoothed light scale for bundled and skinned meshes.
Night maps#
The Special Forces night levels light differently: lights accumulate into a buffer before the main pass, static meshes have night values for their materials, and per-light shadow maps exist at the highest shadow setting. The port runs the frame update of these lights, the per-light records and maps and the shadowed terrain loop; the static lights' terrain receivers and the statics' accumulation lighting are listed as open.
What the port draws#
| Part | State in bf2-render |
|---|---|
| lightmaps, the hemisphere map | drawn |
| simple shadow maps and the soldier's light factor | ported; a test loads the maps of all 30 Battlefield 2 levels |
| the sun's projection and the receiver matrices | ported, the same bits as the original in the emulator |
| the sun map on the GPU: static, bundled and skinned casters | drawn |
| receivers: static, bundled, skinned, terrain, undergrowth, water | drawn; the terrain and soldier cases match native Direct3D 9 pictures after three measured rules of the old shader models |
| point lights on bundled, skinned and static meshes | drawn |
| per-object shadow maps, float maps for cards without hardware depth, the occlusion map of the highest quality, parts of the night lighting | not drawn: traced in the notes and listed as open |
The renderer is tested against pictures the original's own shaders make on native Direct3D 9, case by case ("a soldier in shadow", "terrain at night"). A case counts the pixels that differ by more than 2 of 255. The three rules that closed the last shadow cases were not in any document: how an old pixel shader compares a hardware shadow map, how the alpha test rounds, and that the vertex shader's multiply-add is fused on the hardware measured.
How it is tested#
- Every level's simple shadow maps are read (
crates/bf2-render/tests/retail_shadows.rs; skipped without an install). - The editor's bakes: baked light maps written in the game's own layout and read back from an exported package, and shipped light maps rewritten through the port's writer within a stated error (
crates/bf2-editor/tests/game_export_lightmaps_bf2.rs,parity_terrain_lightmap.rs). - Shadows on a large terrain: a 2048 m level receives the same shadows at its far quarter.
None of this page's six addresses stands in a test's text, and the parts the table above lists as not drawn have no test at all. Nothing here compares a frame with the original's.
In the code#
| What | Where |
|---|---|
| simple shadow maps, light factor | crates/bf2-render/src/shadows.rs |
| the sun's projection | crates/bf2-render/src/sunshadow.rs |
| the shadow map pass and receivers | crates/bf2-render/src/shadowmap.rs, bundledmesh.rs |
| the maps' file reader | crates/bf2-terrain/src/bytemap.rs |
Notes: notes/subsystems/renderer.md ("Shadows, lighting" and the Render sections), notes/subsystems/shaders.md.
docs/dev/engine/render-shadows-and-lighting.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.