Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 7.2 — Near, Far and the Depth Buffer
3D Web · Example 1 of 2

The smallest gap a depth buffer can tell apart

Two panels cross at 10 m. The log shows idealised 24-bit numbers; a real GPU may keep more bits, so the picture can look better than the table says. The last part of each row comes from rounding two depths to 24 bits.

The GPU decides what is in front with a depth buffer: one number per pixel. Those numbers are bunched up near the camera, so a tiny near and a huge far make surfaces flicker (z-fighting), and a wrong near or far makes objects vanish.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 3 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write smallestGap(near, far, d) for a 24-bit buffer and print it in millimetres for near 0.5 and far 200, at 5 m and 50 m.

02

A scene shows a bike 2 m away with near 3. Say what the screen shows and give the smallest change that fixes it.

Explain it without notes

01

Why does lowering near from 0.1 to 0.01 hurt depth precision, while raising far from 100 to 1000 hardly does?

02

A decal (a logo) lies flat on the bike's frame and flickers when the camera moves. List three ways to fix it.

The smallest gap a depth buffer can tell apart
Press Run to see the page.
idle

Console

Nothing logged yet.

Expected console output

logarithmic depth buffer: false
near 0.1, far 100: smallest gap at 10 m = 0.0595 mm; surfaces 2 mm apart are told apart
near 1, far 100: smallest gap at 10 m = 0.00590 mm; surfaces 2 mm apart are told apart
near 0.01, far 1000: smallest gap at 10 m = 0.596 mm; surfaces 2 mm apart are told apart
near 0.0001, far 1000000000: smallest gap at 10 m = 59.6 mm; surfaces 2 mm apart FIGHT (same stored depth)