A photo keeps the direction to each point and loses the distance. Getting 3D back starts with a choice: how to store a shape. On this ring at 64 cells across, a voxel grid stores 4,096 cells and answers “inside?” in one lookup. The 252 surface points take 504 numbers and cannot answer at all.
Pick a way to store the ring, set the detail and move the probe. Read what each one stores and what it can answer. Then we build each one, from a single pixel to a radiance field you train from photos.
You need what a neural network layer is and basic vectors. We build the rest from zero.
Follows the 3D Vision lecture of Stanford CS 231n. It leads to NeRF and 3D Gaussian Splatting and Multi-View Geometry.
Illustrative 2-D slice: a ring of centre radius 0.75 and half-thickness 0.22 in a box 2.4 wide. Detail sets the cell size (2.4 divided by the count). Points and mesh vertices sit one cell apart; Gaussians sit two cells apart and store 6 numbers each. You can also drag the probe on the picture. In 3-D the grid grows as N³ and the surface as N², so the gap widens.
Chapter 0
Slide a point along its ray and watch its pixel refuse to move
A robot arm must grasp a mug. Its camera returns a grid of colours, and the handle sits at pixel column 100. The arm needs the distance to the handle in centimetres. The photo does not hold it.
A camera keeps the direction to each point and loses the distance. Every point on one line from the lens lands on the same pixel. That line is a ray. A small mug up close and a big mug far away make the same picture.
So inverse graphics, getting the 3-D scene back from images, is hard. Rendering goes forward: scene in, one image out. Going backward has many answers. We need more: a second view, a depth sensor, or a learned sense of which shapes are likely.
Top view. Camera A (yellow) sees the point at one pixel. Drag the depth. Then switch on camera B, half a metre to the right, and drag again.
Illustrative pinhole, exact geometry: focal length 500 px, no lens distortion. The point stays on camera A's ray through pixel 100, so its offset is always 0.2 times its depth. Camera B sits 0.5 m to the right of A, facing the same way.
Call the lens's focal length f, in pixels. Call the point's sideways offset from the lens axis X and its depth along the axis Z, both in metres. The pixel column where it lands is u.
u equals f times X over Z.
Divide the offset X by the depth Z, then multiply by f. The result is the pixel u. Only the ratio of X to Z survives, so scaling both by the same factor leaves u where it was.
A point 0.4 m right and 2 m deep: u = 500 × 0.4 / 2 = 100 px. A point 0.8 m right and 4 m deep: u = 500 × 0.8 / 4 = 100 px again. Two different points, one pixel.
A second camera breaks the tie. Camera B sits 0.5 m to the right, so it sees the offset X minus 0.5. At 2 m deep it reads −25 px; at 4 m it reads +37.5 px. Now each depth has its own pixel pair. Chapter 9 uses this with many views to train a whole scene.
Chapter 1
Ask a point cloud and a mesh whether a probe is inside, and count the work
The most direct way to store a shape is to store its surface. That is an explicit representation: you keep points that lie on the surface. Asking for a surface point is free, because you already have a list of them.
A point cloud is an unordered set of 3-D points, each an (x, y, z), sometimes with a colour or a surface direction. A LiDAR scanner and a depth camera produce exactly this. It is simple and compact, but it has nothing between its points: no surface, no inside, no outside.
A triangle mesh adds the missing glue. It keeps a list of vertices, the 3-D points, and a list of faces, each a triple of vertex numbers. The triangles tile a surface. Games, films and CAD tools all draw meshes.
Drag the probe on the picture. With the mesh, a ray runs to the right and every edge it crosses lights up. Then break one edge and try again.
Illustrative 2-D shape: an outer loop and a hole, each a polygon. In 2-D a mesh face is an edge; in 3-D it is a triangle and the same crossing count works with a ray against triangles.
The mesh answers “inside?” by casting a ray. Count the edges the ray crosses: an odd count means inside, even means outside. The answer is right, but it costs a test against every edge. The point cloud cannot answer at all.
Memory is easy to count. A closed triangle mesh with no holes through it has about twice as many faces as vertices; Euler's rule gives faces = 2 × vertices − 4. One million vertices at 3 floats of 4 bytes is 12 MB. The 1,999,996 faces at 3 integers of 4 bytes are 24 MB. That is about 36 MB for a detailed scan.
A third explicit form is the parametric surface: a formula that turns two numbers into a 3-D point. A sphere of radius R is (R sin φ cos θ, R sin φ sin θ, R cos φ). Sampling means evaluating the formula. Splines glue many such patches together.
Chapter 2
Store a function of space, read its sign, and pull the surface out with a grid
Flip the idea. Do not store the surface. Store a function you can ask at any point in space. Its sign says inside or outside, and the surface is wherever it equals zero. That is an implicit representation.
The most useful one is the signed distance function (SDF). At each point it gives the distance to the nearest surface point, negative inside and positive outside. Take a sphere. Call its centre c, its radius r and a query point x. The SDF is d.
d of x equals the length of x minus c, minus r.
Measure how far x is from the centre c. Subtract the radius r. The result d tells you how far x is from the skin and, by its sign, which side it is on.
Centre at the origin, r = 1. At x = (2, 1, 2) the length is 3, so d = 3 − 1 = 2: outside, 2 from the skin. At x = (0.3, 0.4, 0) the length is 0.5, so d = −0.5: inside, half a unit deep.
Shapes combine with one operation each. The union of two SDFs is their minimum; the intersection is their maximum; A minus B is the maximum of A and minus B. This is constructive solid geometry (CSG), the way CAD tools cut a pipe from two cylinders. A smooth minimum blends shapes like two drops merging.
Teal is inside, blue is outside, brighter near the skin. The warm line is the surface pulled from the grid; the dashed line is the exact one. Pick an operation, drag the probe, and coarsen the grid.
Illustrative 2-D field: a circle of radius 0.5 and a square box of half-width 0.38. Marching squares pulls out the surface, with straight-line interpolation along each cell edge. The smooth blend uses a polynomial smooth minimum of width 0.25.
Testing a point is now one evaluation. Getting the surface back is the hard part. Marching cubes samples the function on a grid. Each cube has 8 corners, each inside or outside: 28 = 256 patterns, which symmetry reduces to 15 cases. Each case has a fixed set of triangles, placed by interpolating where the sign flips. The device runs the 2-D version, marching squares, with 16 patterns.
Chapter 3
Count the cells a voxel grid wastes, and refine only where the surface is
Images are grids of pixels, and convolutional networks love grids. So the first idea for learning 3-D was a grid of voxels, volume pixels, each marked full or empty. Slide a 3 × 3 × 3 filter through it and you have a 3-D convolutional network.
The price is cubic. A grid with N cells per side has N³ cells: 32,768 at 32, 2,097,152 at 128, and 16,777,216 at 256. That last one is 67 MB as 4-byte floats, for one shape. Most of those cells are empty air or solid inside. The shape's detail lives in a thin shell at the surface, and the shell grows only as N².
The same circle, stored as a uniform grid or as a quadtree. Raise the depth and compare the cell counts.
Illustrative 2-D quadtree on a circle of radius 0.45 in a unit box: a cell splits into 4 while it touches the outline. The 3-D numbers in the read-out count a sphere of radius 0.45 the same way. There, an octree stores 8 children for every cell that touches the surface.
An octree fixes the waste. Start with one cube. Split a cube into 8 only if it touches the surface, and repeat. Empty and solid regions stay as big blocks. For a sphere at 256 per side, the octree holds about 773,000 nodes against 16.8 million uniform cells, 22 times fewer. Double N and the octree grows 4 times, the grid 8 times.
A second escape skips 3-D grids. A multi-view CNN renders the shape from 12 directions and runs an ordinary image network on each picture. Then it takes the element-wise maximum of the 12 feature vectors. On the 40-class ModelNet40 benchmark, Su et al. (2015) reached 89.9% accuracy, against 77.3% for the voxel network 3D ShapeNets. The image network had first learned from millions of photos.
Chapter 4
Run one small network on every point, then keep the largest value of each feature
A LiDAR scan arrives as a list of points. The list has an order, but the order means nothing: shuffle it and the shape is the same. A plain network that reads the points as one long vector gives a different answer for every shuffle. N points have N! orders, so you cannot teach it all of them.
We need a function whose answer does not change when you reorder the inputs. That is permutation invariance. Sum, mean and maximum all have it. PointNet (Qi et al., 2017) builds a network around the maximum.
Call the points x1 to xN. Call the small network run on each point f; it is the same network, with shared weights, for every point. Call the network that reads the pooled vector g. The output is h.
h of x-one to x-N equals g of the max over i of f of x-i.
Run f on each point x-i by itself. For every feature, keep the largest value over all points. Feed that one vector to g. Reorder the points and every maximum stays the same, so h stays the same.
With made-up numbers: points (1, 0), (0, 2) and (−1, 1), and a toy f(p) = (px + py, py − px). The features are (1, −1), (2, 2) and (0, 2). The max is (2, 2) in any order. With g = the sum, h = 4.
Each row is one point's feature vector from the shared network f. The bottom row is the pooled vector. Shuffle the rows, then switch the pooling to an order-dependent reader and shuffle again.
Illustrative: 8 points in 2-D, a fixed random f with 6 ReLU features, and no training. “Ordered” weights row i by a different number for each position, the way a plain network would read a list.
The real PointNet maps each point through shared layers of width 64, 64, 64, 128 and 1024, max-pools to one 1024-long vector, and classifies it. It reached 89.2% on ModelNet40. The points that win at least one maximum are its critical points: they alone decide the answer, so a sparse skeleton is enough.
For labelling each point, PointNet glues the pooled vector back onto each point's own features and runs a per-point classifier. Its weakness is that points meet only at the final max. PointNet++ applies PointNet inside small neighbourhoods first, then repeats on the results, like a convolution.
Chapter 5
Feed a point and a shape code to one network, and fill in the half you never saw
Voxels cost N³. Point clouds have no surface. Meshes are hard for a network to output, because it must choose both the points and how they connect. Chapter 2 offers a way out: a function of space. Let a network be that function.
DeepSDF (Park et al., 2019) trains one network for a whole family of shapes, such as chairs. Its input is a query point and a short vector, the latent code, that says which chair. Its output is the signed distance. The network has eight fully connected layers of 512 units, and every shape shares its weights.
Call the network fθ with weights θ. Call the code of shape i zi, a query point x and its true distance s. A clamp limits both distances to plus or minus δ.
L equals the sum over shapes i and samples x and s, of the absolute value of clamp of f-theta of x and z-i, minus clamp of s.
For each sample, ask the network for the distance at x, given the shape's code z-i. Clamp the prediction and the truth s to delta. Add up the gaps. Training lowers L by changing both the weights theta and every code z-i.
With made-up numbers and delta 0.1: predicted 0.35, true 0.50. Both clamp to 0.1, so the gap is 0: far from the skin, close enough. Predicted 0.03, true 0.05: gap 0.02. Predicted −0.02, true 0.04: gap 0.06, the wrong side of the skin.
The clamp spends the network's effort near the surface, where shape detail lives. There is no encoder. Each training shape's code is a free variable, found by gradient descent with the weights. This is an auto-decoder. For a new scan, freeze θ and search only for the code that best fits the observed points.
Yellow dots are the scan: points seen on the target's surface. Fit the code and watch the purple shape grow. Narrow the scan, or pick a target the family never contained.
Illustrative. The “trained decoder” is a fixed hand-made function in place of the network. Code z1 blends a circle into a rounded square; code z2 sets the size. The fit runs 80 steps of gradient descent on the clamped loss with delta 0.1.
Occupancy networks (Mescheder et al., 2019) predict the chance that a point is inside, trained with cross-entropy. An encoder maps an image or a scan to the code in one pass. Either way, a mesh comes out by evaluating the network on a grid and running marching cubes.
Chapter 6
March along one ray and add each sample's colour, weighted by the light that reaches it
DeepSDF learns from 3-D meshes, which are expensive to collect. Photos are cheap. Learning from photos needs a renderer that lets gradients flow from each pixel back to the model: differentiable rendering.
NeRF (Mildenhall et al., 2020) stores a scene as a network. In: a 3-D position and a viewing direction. Out: a colour and a density σ, how strongly that point blocks light per unit length. Colour also depends on direction, so a highlight can move as you walk past.
To draw one pixel, take samples along its ray. Sample i stands for a stretch of length δi with density σi. The share of light it stops is its opacity αi.
Alpha-i equals one minus e to the minus sigma-i delta-i.
Multiply the density sigma-i by the stretch length delta-i. Take e to the minus that: the share of light that passes. One minus it is alpha-i, the share stopped.
With made-up numbers and delta 0.5: density 2 gives alpha = 1 − e−1 = 0.632. Density 4 gives 1 − e−2 = 0.865. Density 0, empty air, gives alpha 0.
Add the samples front to back. The transmittance Ti is the light still left at sample i. Sample i adds its colour ci, weighted by Ti times αi, to the pixel colour C.
C equals the sum over i of T-i alpha-i c-i, where T-i is the product over j before i of one minus alpha-j.
Walk the samples in order. Each one takes the share alpha-i of the light T-i that is still left and paints it with its colour c-i. Then T shrinks by the factor one minus alpha-i. Add the paint. That is C.
The three samples above, front to back: air, clay at density 2, sea at density 4. Weights: 0, then 1 × 0.632 = 0.632, then 0.368 × 0.865 = 0.318. The 0.050 left goes to the background. The pixel is 63% clay, 32% sea and 5% background.
Pink bars are each sample's weight, T times alpha; the dashed line is T. Raise the clay blob's density, then cut the samples to 4.
Illustrative 1-D scene, 4 units long: clay from 1.0 to 1.5, sea of density 4 from 2.5 to 3.0, grey background. Each sample reads the density at its midpoint. At 8 samples the stretch is 0.5, as in the worked example.
Every step is a multiply, an add or an exponential, so gradients flow back to every density and colour. NeRF trains on posed photos with the squared pixel error. Occlusion comes for free: once T falls near zero, everything behind gets no weight.
Chapter 7
Rewrite each coordinate as sines and cosines, then spend samples where the surface is
A network fed raw (x, y, z) draws blurry scenes. Trained by gradient descent, networks learn smooth, slowly changing functions first. This tendency is called spectral bias. Bricks, text and leaves change fast across space.
NeRF's fix is positional encoding. Before the network sees a coordinate p, replace it with sines and cosines at doubling frequencies. Call the number of frequencies L and the frequency index k. The encoding is γ(p).
Gamma of p equals the sine and cosine of two to the k, times pi, times p, for k from zero to L minus one.
For each k from 0 to L minus 1, multiply p by pi and by 2 to the k. Take the sine and the cosine. List all 2L numbers. Do it for x, y and z separately and feed the list to the network.
p = 0.3 and L = 3. For k = 0: sin(0.3π) = 0.809, cos = 0.588. For k = 1: 0.951 and −0.309. For k = 2: −0.588 and −0.809. NeRF's position encoding has 3 × 2 × 10 = 60 numbers.
The dashed line is the signal: square steps with a fast ripple. The purple line is a small network learning it, live. Set L to 0 (raw input) and train, then try 4.
Illustrative and seeded; it runs in your browser. The network has 2 hidden layers of 32 ReLU units, trained by Adam at learning rate 0.01 on 128 points, full batch. L = 0 feeds the raw coordinate; L above 0 feeds the 2L encoded numbers. The signal’s variance is 0.27.
The second trick saves work. Most of a ray is empty air. NeRF first takes Nc = 64 even samples with a coarse network. It reads their weights Tiαi as a map of where the surface is. It then draws Nf = 128 more samples there and runs all 192 through a fine network.
The cost is still large. Each ray needs 64 + 192 = 256 network queries. An 800 × 800 image has 640,000 rays, so about 164 million queries per frame. The NeRF paper reports about 30 seconds per frame on one V100 GPU, and 1 to 2 days of training per scene.
Chapter 8
Project fuzzy ellipses onto the image, sort them by depth, and composite front to back
NeRF asks a network hundreds of times per pixel. 3D Gaussian Splatting (Kerbl et al., 2023) stores the scene as a few million explicit, fuzzy blobs and draws them onto the screen. Each 3-D Gaussian has a centre, an opacity and a colour that can change with the view direction. A 3 × 3 covariance sets its size and tilt.
To render, project each Gaussian onto the image. A 3-D Gaussian lands as a 2-D Gaussian with covariance J W Σ WT JT. Here W turns world axes into camera axes, and J is the local stretch of the perspective. Then sort the splats by depth and composite them with Chapter 6’s rule.
Only the opacity changes. Call splat i’s opacity oi, its projected centre μ′i, its projected covariance Σ′i, and the pixel p. Its opacity at that pixel is αi(p).
Alpha-i of p equals o-i times e to the minus one half, p minus mu-prime-i transpose, Sigma-prime-i inverse, p minus mu-prime-i.
Measure the gap from pixel p to the centre mu-prime-i, in units of the splat’s width given by Sigma-prime-i. Square it, halve it, and take e to the minus that. Multiply by the opacity o-i.
With made-up numbers: a round splat 2 px wide (covariance 4 px²), a pixel 2 px from its centre, opacity 0.8. The exponent is −½ × 4 / 4 = −0.5, and e−0.5 = 0.607. So alpha = 0.8 × 0.607 = 0.485.
Three splats: clay in front, then sea, then a grey one at the back. Drag the probe pixel. Stretch and turn the front splat, then switch off the depth sort.
Illustrative 2-D splats with opacities 0.8, 0.9 and 0.9. With the sort off, the splats composite in storage order, back one first.
Training starts from the sparse 3-D points that camera calibration (structure from motion) already produced: one Gaussian per point. The loss is 0.8 times the L1 pixel error plus 0.2 times a structural error term (D-SSIM). Every 100 iterations it densifies. It clones small Gaussians where detail is missing and splits large ones into two, with their scale divided by 1.6. It removes nearly transparent ones.
After 30,000 iterations the paper’s scenes hold 1 to 5 million Gaussians and render at 30 frames per second or more at 1080p. The price is memory: millions of blobs in place of one small network.
Chapter 9
Fit a grid of density and colour to flat photos, and watch one view fool you
Here is the whole method in a flat world. Two coloured disks sit in a square. Cameras around them each take a photo: a row of 32 pixels. The model is a 17 × 17 grid of numbers holding density and colour, with no network, like the Plenoxels method. Chapter 6’s rule renders the grid into photos, and the optimiser Adam nudges every number to shrink the squared pixel error.
Train with one camera first. The training photo matches almost perfectly. Then look at the held-out camera, one the model never trained on. Density has smeared along the training rays, like the point that slid along its ray in Chapter 0. Switch to 3 cameras, then 12, and train again.
Left: the true scene and the training cameras (yellow). Middle: the learned grid, drawn with its own density and colour. Bottom: the held-out camera’s photo, true above and rendered below. Pick the views, then train.
Illustrative, deterministic, and it runs in your browser. Disks of radius 0.30 and 0.22 with density 12 sit in a 2 by 2 square. Cameras sit on a circle of radius 1.7 with a 71° field of view and take 48 samples per ray. The grid stores a density value and three colour values at each of its 289 corners, read by bilinear interpolation; Adam at learning rate 0.08. Shape overlap is the overlap of learned density above 3 with the true disks, on a 64 × 64 check grid.
Read the scores as PSNR, peak signal-to-noise ratio: 10 times the base-10 log of 1 over the mean squared pixel error. Higher is better; 20 dB means a typical error of a tenth of full brightness. With 1 view the training photo scores 57.2 dB, yet the held-out views score 10.9 dB and the shape overlap is 23%. With 3 views: 13.7 dB and 39%. With 12 views the held-out views reach 27.2 dB and the overlap 90%.
This is Chapter 0’s problem, solved the only honest way: more rays through the same point. Real NeRF and splatting systems need dozens to hundreds of photos for the same reason.
Chapter 10
Keep the equations, the trade-offs, the numbers and the code in one place
A photo gives rays. Explicit forms store the surface and make sampling cheap; implicit forms store a function and make “inside?” cheap. Grids cost N³, so octrees, point networks and networks-as-functions avoid them. Volume rendering makes a scene trainable from photos, and splatting makes it fast to draw.
| Quantity | Formula | Numbers from this page |
|---|---|---|
| Projection | u = f X / Z | 0.4 m at 2 m and 0.8 m at 4 m: both 100 px |
| Sphere SDF | d(x) = ‖x − c‖ − r | (2, 1, 2): d = 2; (0.3, 0.4, 0): d = −0.5 |
| Voxels | N³ cells | 256: 16.8 million; an octree about 0.77 million |
| PointNet | h = g(maxi f(xi)) | features (1, −1), (2, 2), (0, 2): max (2, 2) |
| DeepSDF loss | Σ |clamp(fθ(x, z)) − clamp(s)| | delta 0.1: 0.35 vs 0.50 costs 0 |
| Opacity | α = 1 − e−σδ | σ 2, δ 0.5: 0.632 |
| Pixel | C = Σ Tiαici | 63% clay, 32% sea, 5% background |
| Encoding | sin, cos(2kπp) | L = 10: 60 numbers per position |
| Splat | α(p) = o · exp(−½ dTΣ′−1d) | 2 px off a 2 px splat, o 0.8: 0.485 |
| Representation | Cheap | Costly | Learned by |
|---|---|---|---|
| Point cloud | storing, sampling | no surface, no inside | PointNet, PointNet++ |
| Mesh | drawing, editing | inside test; outputting connectivity | deforming a template mesh |
| Voxels | inside test, 3-D convolution | N³ memory, blocky edges | 3-D CNNs, octree networks |
| SDF or occupancy net | inside test, any resolution | a grid pass to get a mesh | DeepSDF, occupancy networks |
| Radiance field | learning from photos | hundreds of queries per pixel | NeRF, Instant-NGP |
| Gaussians | real-time drawing, editing | millions of blobs in memory | 3D Gaussian Splatting |
pythonimport numpy as np def sdf_sphere(x, c, r): return np.linalg.norm(x - c, axis=-1) - r # negative inside def pointnet(points, f, g): feats = np.stack([f(p) for p in points]) # (N, K), same f for every point return g(feats.max(axis=0)) # order cannot matter def render_ray(sigma, color, delta, background): alpha = 1.0 - np.exp(-sigma * delta) # (S,) T = np.cumprod(np.concatenate([[1.0], 1.0 - alpha])) # (S+1,), T[0] = 1 w = T[:-1] * alpha # weight of each sample return (w[:, None] * color).sum(0) + T[-1] * background def encode(p, L): k = 2.0 ** np.arange(L) * np.pi # 1, 2, 4, ... times pi return np.concatenate([np.sin(k * p), np.cos(k * p)]) # the page's numbers: air, clay at sigma 2, sea at sigma 4, delta 0.5 print(render_ray(np.array([0., 2., 4.]), np.eye(3), 0.5, np.zeros(3))) # [0. 0.632 0.318]
Now press Present or Teach and rebuild it from memory. Cover the ray, the SDF sign, the cubic wall and the max pool. Then cover the clamped loss, the pixel sum, the encoding and the splat. Then go back to the ring, put the probe in the hole, and say which representations get it right and what each one paid.
Follows the 3D Vision lecture of Stanford CS 231n, rebuilt on Engineermaxxing. Published numbers name their paper; every toy number on this page carries the word illustrative.