Gaussian Splatting From Zero · Episode 03

Anisotropy: the Covariance Matrix

Anisotropy: the Covariance Matrix is Episode 03 of Gaussian Splatting From Zero, a MasterAllArts free interactive lesson for artists learning creative 3D on the web.

Part of the MasterAllArts Creative 3D Roadmap .

Episode 03 · Gaussian Splatting From Zero

A round blob can't draw a flat wall. To reconstruct a real surface, a splat has to be able to stretch and tilt — to lie flat against the thing it's representing, like a sticker. This episode gives every splat a 3-axis scale and a rotation, turning it from a sphere into an ellipsoid, and does the one piece of real math in the whole series: projecting that 3D ellipsoid onto your screen as a 2D ellipse. Drag the flatness slider in the demo and watch a fuzzy ball of blobs collapse into a crisp, solid planet. That collapse is 3D Gaussian Splatting.

0 fps
drag to orbit · scroll to zoom
EWA splatting · 45k ellipsoids
Each splat is a flat disc lying tangent to the surface. The blobs become a solid planet.

From a sphere to an ellipsoid: scale + rotation

An isotropic Gaussian is described by one number: its radius. An anisotropic one needs more — three axis lengths (how far it stretches along its own X, Y, Z) and an orientation (which way those axes point in the world). That's exactly the two missing rows from Episode 01's table:

  • scale — a vec3, the half-length of each of the three axes. A flat disc is something like (0.05, 0.05, 0.003): wide in two directions, paper-thin in the third.
  • rotation — a vec4 quaternion that tilts those axes so the thin direction points along the surface normal.

Mathematicians bundle "an ellipsoid's shape and orientation" into a single 3×3 matrix called the covariance matrix, written Σ (capital sigma). The recipe is simple — build a rotation matrix R from the quaternion, a diagonal scale matrix S, and combine:

Σ = R · S · Sᵀ · Rᵀ = M · Mᵀ where M = R · S

You never have to love this formula, just trust two things about it: it produces a symmetric 3×3 matrix that encodes a tilted ellipsoid, and reading it back out (which way is the long axis? how long?) is an eigenvalue problem. We'll do exactly one eigen-decomposition later, on a tiny 2×2 matrix, and the GPU won't even notice.

The hard part: projecting Σ to the screen (EWA)

Here's the crux of splatting, and the reason it took until 2023 to go real-time. A 3D Gaussian, seen through a perspective camera, projects to a 2D Gaussian on your screen — approximately. The approximation (linearizing the perspective divide at the splat's center) is the EWA splatting method from a 2001 paper, and it's what every real-time splat renderer uses today. Three steps:

  1. Rotate Σ into view space. Multiply by the camera's rotation W: Σ_view = W · Σ · Wᵀ.
  2. Apply the projection's Jacobian J. Perspective isn't linear, but near a single point it's well-approximated by its derivative — a 2×3 matrix J built from the focal length and the splat's view-space position. This squashes the 3D view-space ellipsoid into a 2D screen ellipse: Σ_2D = J · Σ_view · Jᵀ.
  3. Read off the ellipse. Eigen-decompose the 2×2 Σ_2D to get two perpendicular axes and their lengths — that's the ellipse to draw.

Folded together, T = J · W and Σ_2D = T · Σ · Tᵀ. Here's the whole vertex shader's core — read it slowly, it's the densest code in the season:

// build the 3D covariance from this splat's scale + rotation
mat3 R     = quatToMat(iQuat);
mat3 S     = mat3(s.x,0.,0.,  0.,s.y,0.,  0.,0.,s.z);
mat3 M     = R * S;
mat3 Sigma = M * transpose(M);          // Σ = M Mᵀ

// center in view space
vec4 mv = modelViewMatrix * vec4(iCenter, 1.0);

// Jacobian of the perspective projection at this point
float zinv = 1.0 / mv.z, zinv2 = zinv*zinv;
mat3 J = mat3(
  uFocal.x*zinv, 0.0,           0.0,
  0.0,           uFocal.y*zinv, 0.0,
  -uFocal.x*mv.x*zinv2, -uFocal.y*mv.y*zinv2, 0.0);

mat3 W   = mat3(modelViewMatrix);       // world → view rotation
mat3 T   = J * W;
mat3 cov = T * Sigma * transpose(T);     // 2D covariance lives in cov[0..1][0..1]

// 2×2 screen covariance, with a ½-pixel low-pass so tiny splats don't vanish
float a = cov[0][0] + 0.3, b = cov[0][1], c = cov[1][1] + 0.3;

The + 0.3 on the diagonal is the famous "dilation" or low-pass filter: it guarantees every splat is at least a fraction of a pixel wide, so sub-pixel splats flicker into a stable shimmer instead of disappearing. Leave it out and distant splats sparkle like broken glass.

Eigen-decomposing a 2×2 (the one bit of linear algebra)

We have a 2×2 symmetric matrix [[a, b], [b, c]] and we want its two axes. For 2×2 there's a closed form — no iteration, no library:

// eigenvalues = the two squared axis lengths (in pixels²)
float halfTr = 0.5 * (a + c);
float det    = a*c - b*b;
float d      = sqrt(max(halfTr*halfTr - det, 0.0));
float l1 = halfTr + d;          // major
float l2 = halfTr - d;          // minor

// major-axis direction (eigenvector), minor is perpendicular
vec2 e1 = (abs(b) > 1e-6) ? normalize(vec2(b, l1 - a))
                          : (a >= c ? vec2(1.,0.) : vec2(0.,1.));
vec2 e2 = vec2(-e1.y, e1.x);

// draw the quad out to 3 sigma along each axis (sqrt of eigenvalue = 1 sigma)
vec2 axis1 = e1 * (3.0 * sqrt(l1));
vec2 axis2 = e2 * (3.0 * sqrt(l2));
vec2 offsetPx = position.x * axis1 + position.y * axis2;  // position.xy ∈ [-1,1]

Those two axes, in pixels, are the ellipse. We push the quad's corner out along them, convert pixels back into clip space, and we're done — the billboard is no longer a square, it's a correctly oriented, correctly sized ellipse that tracks the camera through every angle:

vec4 clip = projectionMatrix * mv;
clip.xy += (offsetPx / uViewport) * 2.0 * clip.w;   // pixels → clip space
gl_Position = clip;

The fragment shader is unchanged from Episode 02 — a Gaussian falloff — except now vQuad carries the corner in sigma units, so the same exp(−½·r²) automatically produces the right elliptical gradient.

Stage 1 — Isotropic: a fuzzy ball

Set "flatness" to 1.0 (or pick Stage 1). Every splat is a tiny sphere, so it projects to a circle no matter how you look at it. The planet's surface is a vague, foamy shell — you can sort of tell there's a ball there, but it has no crispness, no sense of a real surface. This is the ceiling of what round blobs can do, and it's why Episode 02's nebula was the right showcase for them: clouds are vague.

Stage 2 — Anisotropic: a solid planet

Now drop the flatness toward 0.08 (Stage 2). Each splat flattens into a thin disc, and because we oriented every disc tangent to the sphere, they lie flush against the surface like tiles or scales. Overnight the foam becomes a solid, readable planet with crisp continents. Nothing changed about where the splats are — only their shape. That's the entire magic trick of splatting: a surface isn't modeled, it's tiled by thousands of little oriented ellipses, each one flat against it.

Orbit to the planet's edge and look at the silhouette. The discs there are seen nearly edge-on, so the projection correctly squashes them into thin slivers — that's the EWA Jacobian doing its job in real time. If your projection math is wrong, this is where it screams: the limb will bulge, sparkle, or fly apart.

Stage 3 — Morph it yourself

Stage 3 just unlocks the flatness slider across its full range so you can feel the transition. Somewhere around 0.3 the ball "sets" like jelly into a surface. Real captures live near the flat end for walls and floors, and near the round end for foliage, smoke, and fuzz — the optimizer picks per-splat, which is why a trained scene has splats of wildly different shapes all mixed together.

Why a quaternion and not Euler angles?

The rotation is stored as a 4-component quaternion because that's what the training optimizer wants: quaternions have no gimbal lock and differentiate cleanly, so gradient descent can nudge a splat's orientation smoothly. You'll almost never read or write them by hand — Three.js's Quaternion.setFromUnitVectors() built ours (align the disc's thin axis to the surface normal), and a real .ply file hands you four floats per splat that you drop straight into the iQuat attribute.

Common pitfalls

  • Ellipsoids point the wrong way / discs face the camera instead of the surface. Your quaternion→matrix function is transposed (it's building Rᵀ instead of R). GLSL's mat3(...) constructor is column-major — fill it by columns, not by rows. This is the single most common covariance bug.
  • The whole planet bulges or sparkles at the silhouette. A sign error in the Jacobian, or you forgot that view-space z is negative. Because cov = T·Σ·Tᵀ is a quadratic form, the overall sign of T cancels — but the relative signs inside J don't. Match the layout above exactly.
  • Distant splats flicker like static. You dropped the + 0.3 diagonal dilation. Sub-pixel ellipses need that low-pass floor.
  • Everything is a circle no matter the flatness. Your iScale or iQuat attribute isn't reaching the shader, so every splat falls back to a default sphere. Check the attribute is an InstancedBufferAttribute with the right itemSize (3 and 4).
  • NaNs / black holes punched in the surface. A splat sitting exactly on the camera plane makes 1.0/mv.z explode. Real renderers near-clip these; for now keep the camera out of the cloud.
  • It's an ellipse but the gradient looks square. You passed position.xy to the fragment instead of the sigma-scaled vQuad. The falloff must be measured in the ellipse's own units.

Exercises

  1. In the generator, give the in-plane axes different lengths (e.g. x = 1.4·r, y = 0.7·r) and a random twist around the normal. The planet grows a brushed, furry texture — you're seeing true 2D anisotropy, not just flatness.
  2. Color each splat by its minor eigenvalue (pass l2 to the fragment). Splats seen edge-on light up — a live visualization of the projection.
  3. Replace the sphere with a torus in the generator (you'll need the torus's surface normal for the quaternion). Watch the discs wrap the tube.
  4. Turn the + 0.3 dilation up to + 2.0. The planet goes soft and waxy as every splat is forced to a minimum size. This is the knob between "crisp and sparkly" and "smooth and blurry."

What's next

Episode 04 — Color, Opacity & Alpha Blending. We made the planet solid by cheating: opaque splats and a depth buffer. Real splats are transparent, and blending thousands of transparent ellipses correctly is its own deep problem — premultiplied alpha, the "over" operator, and the first sighting of the dreaded popcorn artifact. After that, Episode 05 fixes popcorn for good with depth sorting.