DEV Community

Rodion Kazennov
Rodion Kazennov

Posted on AI-assisted

I orbited my game's camera and 1,366 grass pixels popped every step; measuring from the pivot made it 0

When the camera in my Godot 4 game circled the rover, about 1,366 pixels of grass changed brightness from one step to the next. Nothing moved. Nothing was added. The pixels were plants switching to a cheaper or richer version of themselves. After I changed what "distance" means in the level-of-detail code, the same orbit gave 0 popping pixels in six of six positions. The first version of that fix showed no change at all, and the reason was a bug in my measurement, not in the fix.

Below: how I counted popping pixels without eyeballing a video, the shader change (a few lines), the second source of popping that the shader change did not touch, and one question I still cannot answer.

The game is a Godot 4 project where a small rover explores a hex-tile planet covered in meadow: millions of blades, tufts and flowers drawn as instanced meshes. Numbers are from my own test runs on one Windows PC.

What was wrong

Level of detail (LOD) means a far plant is drawn simpler than a near one. My meadow has several levels per kind of plant, for example "carpet" blades at 0-70 m, 70-180 m and 180-330 m, tufts at 0-85 m and beyond, flowers at 0-130 m and beyond. Each plant picks its level from its distance to the camera's eye.

That is the standard setup, and it is fine while the camera moves forward. It is not fine when the camera turns around the player. The game's view circles a point above the rover. A plant 100 m from the rover can be 84 m from the eye at one end of the turn and 166 m at the other. On the way it crosses every level boundary between those two numbers. The plant itself never moved. So the grass shimmered on every orbit, and the shimmer was worst on the flowers, which are small bright dots.

I could see it. I could not say how much, or whether a fix helped. "Looks calmer" is not a measurement.

Counting pops

The test renders each step of an orbit twice with time stopped:

  • frame A: plants have the levels the previous camera position gave them;
  • frame B: plants have the levels the current position gives them.

Same camera, same lighting, same pixels, so any pixel that differs by more than 8 out of 255 in luma is a pop. To say which level change caused it, I render a third and fourth copy where every plant is painted flat with a colour for its level, plus a black one, and read each popping pixel's level before and after from those.

# tools/orbit_pops.py (trimmed)
pop = np.abs(luma(A) - luma(B)) > 8        # pixels that changed between steps
la  = levels(paint_A, paint_black)          # level index per pixel, -1 = no plant
lb  = levels(paint_B, paint_black)
for a, b in zip(la[pop], lb[pop]):
    kinds[f"{NAMES[a]} -> {NAMES[b]}"] += 1
Enter fullscreen mode Exit fullscreen mode

On six positions of the orbit around the 120 m view, with levels chosen by the eye's distance:

Position Popping pixels Plant pixels Share
0 2,385 457,502 0.52 %
1 913 608,702 0.15 %
2 1,200 611,786 0.20 %
3 1,537 500,263 0.31 %
4 1,303 475,910 0.27 %
5 856 425,950 0.20 %
Total 8,194 1,366 a step

Under half a percent of the plant pixels, but they are the bright ones, and the eye is built to notice a dot that blinks.

One detail I did not expect: of the 8,194 pops, 3,271 went from a level to the same level. The largest single kind was "flowers 0-130 to flowers 0-130" with 2,264. My reading, which I did not verify: those are plants in the middle of a cross-fade, where the level is the same but the fade amount changed with distance. The fix below removed them too, so the cause is the distance, whichever way it acts.

The fix: measure from the pivot

The orbit keeps one thing constant: the distance between the eye and the point it turns around. So I stopped using the eye's distance to the plant and used this instead:

// meadow_plant.gdshader (trimmed)
global uniform vec4 lod_focus;   // xyz: the point the camera turns round, w: eye-to-point distance

float dl = distance(CAMERA_POSITION_WORLD, o);          // before
if (lod_focus.w > 0.0) {
    vec3 fo = o - lod_focus.xyz;
    dl = sqrt(dot(fo, fo) + lod_focus.w * lod_focus.w);  // after
}
Enter fullscreen mode Exit fullscreen mode

The plant's distance is now the hypotenuse of two legs: plant to pivot, and pivot to eye. When the camera orbits, both legs stay the same, so dl stays the same, so no plant changes level. When the camera does not orbit (a cutscene camera with w = 0), the code falls back to the eye's distance as before.

The game's camera writes the uniform once a frame:

lod_focus_v = Vector4(look.x, look.y, look.z, orbit_cam.global_position.distance_to(look))
RenderingServer.global_shader_parameter_set("lod_focus", lod_focus_v)
Enter fullscreen mode Exit fullscreen mode

The shader gets one extra square root per plant, which I did not measure separately.

The fix that measured nothing

I ran the test with the new shader. The numbers were identical to the old ones, to the pixel: 8,169 popping pixels with the eye's distance, 8,169 with the focus.

I did not believe the fix did nothing; the arithmetic is simple. The test script read the focus back from the engine, and the engine returned an empty vector for it, so the "by the focus" run silently fell back to the eye. The comparison was the old code against the old code. After the test took the focus from the game's camera object instead, the same run gave 1 popping pixel in 8,141.

That is the part I would tell my past self: when a fix changes nothing, check that the test can see the change before you doubt the fix. Identical numbers to the last pixel were the clue. Real noise does not repeat that precisely.

The second source: whole pieces

One pixel was not zero, and the shader is not the only thing that decides what is drawn. The meadow is cut into pieces (22,233 of them), and the engine hides a whole piece when its middle is farther from the eye than the piece's visibility range. Those ranges also use the eye. In the same orbit, 2 to 8 pieces switched on or off per step, with up to 1,136 plants inside them that the shader had just decided to draw.

The engine does not let me give a piece a different measure of distance, so I took the ranges over:

  • at load, each piece's visibility range is saved in metadata and set to 0 in the engine;
  • each frame a script tests the pieces by the same formula as the shader, with the pivot and the eye distance;
  • the pass over 22,233 pieces cost 1.4 to 1.6 ms on my Mac, so a quarter of the pieces are tested per frame, and only once the pivot or the distance has moved by 25 cm.
static func shows(mid: Vector3, r: Vector2, m: Vector4) -> bool:
    var d := sqrt(mid.distance_squared_to(Vector3(m.x, m.y, m.z)) + m.w * m.w)
    return d >= r.x - SLACK and d <= r.y + SLACK   # SLACK 4 m hides the quarter-frame lag
Enter fullscreen mode Exit fullscreen mode

A unit test on 684 pieces laid out as the game lays them, with the eye turned around the pivot at 64 m: by the engine's ranges, 223 pieces changed state and 114 were hidden while plants in them were in range. By the pivot, none.

Final orbit, six positions out of six: 0 popping pixels, and 0 pieces switching.

What I take from it

  • LOD by the eye's distance assumes the eye only approaches or leaves. A camera that orbits a point breaks that assumption, and the symptom is shimmer you cannot catch in a screenshot.
  • Render the same step twice with only the thing under test changed, and diff. It gave me a number I could chase down to zero.
  • If a fix shows the same number to the last digit, suspect the harness.
  • Every system that decides visibility needs the same measure. The shader and the engine's piece ranges disagreed, and that disagreement was the last pixel and the last 114 pieces.

The part I still do not know

My measure only works for a camera that circles a known point. The game also has a free camera that moves and turns at the same time. For that camera I fall back to the eye, so the grass pops again whenever it turns.

I could use the point the camera looks at on the ground, but that point jumps when the view crosses a ridge or a tree. I could smooth it over a second, but then the plants near the pivot lag behind the camera. Has anyone found a distance for LOD that is stable under a camera that both orbits and travels? Or do you accept some pops and hide them with a longer cross-fade?

Top comments (0)