Skip to main content

Command Palette

Search for a command to run...

Signed Distance Functions (SDFs) and why they are cool

Published
6 min read
Signed Distance Functions (SDFs) and why they are cool
T

Just a guy who loves to write code and watch anime.

First, what problem are we even solving?

Before we talk about SDFs, let's talk about how 3D graphics normally work.

Take a round ball in any video game. Looks smooth, right? It's not. If you zoom in close enough, you'll find out that ball is actually hundreds of flat triangles arranged in a ball shape. That's how almost every 3D object in games works. Everything is triangles.

Why triangles? Because graphics cards (GPUs) are really, really fast at drawing triangles. They can draw billions per second. So game engines built everything around triangles.

But triangles have some big problems:

  1. Up close, you see the edges. A "smooth" ball is never actually smooth. It's always made of flat faces.

  2. Combining shapes is a nightmare. Want to merge two spheres into one blob? You'd have to manually delete and rebuild triangles. Engines have whole complicated systems just for this.

  3. Each shape uses memory. Every triangle stores coordinates. Detailed models can be millions of triangles.

  4. You can't do infinity. Want a cube repeated endlessly in every direction? You'd need endless memory. Not possible.

SDFs solve every one of these problems. But to understand how, we have to think about what a shape IS in a completely different way.

The big idea: a shape is a question

Forget triangles for a minute. I want you to think of a shape as something that answers a question.

Imagine there's a ball floating in space. You stand somewhere in the room and you ask the ball: "how far am I from your surface?"

The ball gives you a number:

  • If you're standing outside the ball, you get a positive number (like +5)

  • If you're standing inside the ball, you get a negative number (like -2)

  • If you're touching the ball's surface exactly, you get zero

That's it. That's what an SDF is. Just that. A function that takes a point and gives you a distance.

Let me break down the name:

  • Signed means the number can have a plus or minus sign. That's how it tells you "inside" vs "outside".

  • Distance just means how far.

  • Function is the math word for "a rule that takes inputs and gives outputs."

So a Signed Distance Function is just: a rule. Point goes in, distance comes out.

Wait, so where is the shape stored?

This is the part that takes a minute to click. It's also the coolest part.

The shape isn't stored anywhere. There are no triangles. There's no data saying "here are the points of the ball." There's only the rule.

The shape exists as "all the points in space where the rule gives back zero." That's the definition.

If this feels weird, that's normal. You're used to thinking of shapes as solid things you can touch or see. SDFs treat shapes as pure instructions. You never actually "build" the ball. You just write a rule that happens to describe a ball.

A real example so it feels concrete

The simplest SDF is a sphere sitting at the center of space. Here's the rule:

distance = length(point) - radius

Let me explain every word.

  • point is wherever you're asking about. It can be any spot in 3D space.

  • length(point) means "how far is this point from the center of space (the origin)?" It's just the straight line distance. Pythagoras stuff. If the point is 5 steps from the center, length is 5.

  • radius is how big the sphere is. If the sphere has radius 3, it reaches 3 units in every direction from the center.

  • distance is what the rule gives back.

Let's walk through three specific cases to make this click:

Case 1: The point is 5 units away from the center of space. The sphere's radius is 3.

length(point) = 5
distance = 5 - 3 = +2

Answer: you're 2 units OUTSIDE the sphere.

Case 2: The point is 1 unit away from the center. Same sphere.

length(point) = 1
distance = 1 - 3 = -2

Answer: you're 2 units INSIDE the sphere.

Case 3: The point is exactly 3 units from the center.

distance = 3 - 3 = 0

Answer: you're right ON the surface.

That's a full sphere. One line of math. No triangles. No stored data. Just a rule that can answer any question about that sphere.

Why nothing is stored, and how pixels get their color

OK so remember: the sphere is just a rule. distance = length(point) - radius. A formula. Three words of math.

The computer stores the formula. It does NOT store the sphere.

The sphere gets "built" fresh every single frame, one pixel at a time.

Here's how it actually works. Your screen has pixels. Let's say 1920 across, 1080 down. That's about 2 million pixels. For each frame of the game, every single pixel needs a color.

The computer goes pixel by pixel. For each pixel, it does this:

  1. Imagine a straight line (a "ray") going from your eye, through the pixel on the screen, out into the 3D world behind it.

  2. Walk along that ray, one step at a time, asking the rule: "how far to the nearest surface from here?"

  3. The rule answers with a distance. Say it says "5 units." That means the ray can safely jump 5 units forward without hitting anything. So we jump 5 units.

  4. Ask again. "2 units." Jump 2.

  5. Ask again. "0.3 units." Jump 0.3.

  6. Ask again. "Basically zero." BOOM. The ray has hit a surface. This pixel is touching the sphere.

  7. Paint this pixel the sphere's color.

Then the computer moves to the NEXT pixel and does the whole process again. And again. 2 million times. Every frame.

That's the whole game. A flipbook of "ask the rule, walk, ask again, walk, hit something, color a pixel."

Why is nothing stored? Because it doesn't need to be. The formula can answer any question about the sphere on demand. Storing the sphere somewhere would just be a copy of what the rule already tells us. Wasted memory.

It's like if I asked you "what's 7 times 8?" You don't need to have the answer written down somewhere. You know the rule for multiplication. You can compute the answer whenever I ask. The answer exists in the moment you need it, then it's gone.

The sphere is 7 times 8. It's not a stored number. It's a computation that happens when asked. The result appears on your screen, and the moment the frame is done, the sphere is gone until next frame, when we rebuild it from scratch, one pixel at a time, at 60 times per second.

Signed Distance Functions (SDFs) and why they are cool