<p>This turned into a rabbit hole for me hopefully it helps answer your question, I recommend trying out the various functions on the <a href="https://www.shadertoy.com/view/3ltSW2" rel="nofollow noreferrer">ShaderToy</a> website. Just drop them in and change the call to &quot;sdCircle&quot;. I used the values R=0.75 and r=0.35 to test them out.</p>
<p><strong>Part 1: Defining what an SDF means</strong></p>
<p>Signed Distance Field or SDF, in this context, is a function that returns the signed distance from a point to a primitive. The returned value is always a float that can take on 3 context specific meanings.</p>
<ul>
<li>zero: This is on the surface, while it is rare to hit exactly on the surface of the primitive we can approximate a hit with a simple inequality like <code>abs(d)&lt;sigma</code> where d is the signed distance and sigma is some small value like 0.001.</li>
<li>negative: In this context negative values represent hits inside the primitive but choosing the meaning of a negative return value is context specific. Smaller (more negative) values represent values that are deeper in the interior of the primitive.</li>
<li>positive: Again this is context specific, but in this case this is a hit outside the primitive and larger values mean it is farther away from the primitive.</li>
</ul>
<p>Stop for a second and take another look at the first case, &quot;zero&quot;. This may seem unimportant at first glance but it turns out that by defining a generic base primitive and then changing the value of sigma we can easily compute a primitive of any size. In the case of a torus we could have a very thin torus or one that is so big it envelopes itself. Then by adding one more variable representing the large Radius of the torus, the torus can now be changed to fit our needs as desired just by controlling two variables.</p>
<p><strong>Part 2: Defining the primitive</strong></p>
<p>Take a look at IQ's 2D function for a circle.</p>
<pre><code>float sdCircle( vec2 p, float r ){ return length(p) - r; }
</code></pre>
<p>All this function is doing is computing the &quot;distance&quot; using the length function of our point &quot;p&quot; from our circle (which is sitting at the origin) then subtracting our radius &quot;r&quot;. And easy as that any point greater then radius is positive, any point less then radius is negative.</p>
<p>This is almost a torus already, we are just missing the signed distance for the inside diameter of the 2D torus. But we already know that is really just some sigma value from our original circle, so all we have to do is add a second radius and compute the sigma.
So:</p>
<pre><code>float sdTorus2D( vec2 p, float R, float r )
{
  float q = length(p)-R;
  return abs(q)-r;
}
</code></pre>
<p>No need to recalculate the distance, so just figure out sigma to our second circle with <code>abs(q)-r</code>.</p>
<p><strong>Part 3: Going 3D</strong></p>
<p>If you stop and think about it for a second (ok...maybe more then a second) then it is easy to realize that our 2D torus is just a slice of a 3D torus. When you think about it like that then we can take another slice of our torus in 2D on its edge just by rotating our torus 90 degrees. This should result in two SDF circles side by side.</p>
<p>But turning the torus on its side is really just a matter of computing our signed distances in the opposite order...lets try:</p>
<pre><code>float sdTorus2DSide( vec2 p, float R, float r )
{
  vec2 q = vec2(abs(p.x)-R,p.y);
  return length(q)-r;
}
</code></pre>
<p>The two different views essentially compute the distance in two different ways, so by combining them we should be able to view our torus in 3D from any angle just by adding an extra component &quot;z&quot; so that both computations can be independent of each other. Then combine our two radii into a vec2 and we end up with a function that looks just like IQ's.</p>
<pre><code>float sdTorus( vec3 p, vec2 t )
{
  vec2 q = vec2(length(p.xz)-t.x,p.y);
  return length(q)-t.y;
}
</code></pre>
<p>This would still look two dimensional in the &quot;circle&quot; shadertoy, but now we can vary z to look at the torus slices. Or rotate our view point and see it emerge from the 2D world.  Neat stuff!</p>
<p>BTW you can go through this same process with many primitives and get many different shapes as IQ has demonstrated on his website.</p>

CC BY-SA 4.0
2021-03-06T04:16:47.377Z
I'm a bit confused about your sqTorus2D function. Let's say the R=5 & r=3 [Torus is at origin] and let's say there is a point (6,0,0) then the function returns abs(6-5)-3 => 1-3 => -2 but this does not make sense. The point is clearly outside the torus so it should return a positive value[+1 I think]? Have I misunderstood something?
Sync it
CC BY-SA 4.0
2021-03-06T11:22:45.227Z
It computes the length of the input vector first. That gives us the distance to R, that value is a float not a vector. It then computes the inner radius in terms of the outer radius. Plug it into shaderToy and try it. (I gave a link to a super simple shaderToy at the top)
pmw1234
CC BY-SA 4.0
2021-03-05T15:06:07.707Z
2021-03-05T15:06:07.707Z
pmw1234
<p>The following video discusses the SDFs of various primitives (linked to torus section):</p>
<p><a href="https://youtu.be/Ff0jJyyiVyw?t=491" rel="nofollow noreferrer">https://youtu.be/Ff0jJyyiVyw?t=491</a></p>
<p>It discusses a torus but not the capped variant. The rest of the channel has useful content as well.</p>
<p>Additionally, iq has videos on detailed derivations of some primitives and techniques, sometimes hidden in the long livestreams, which are well worth a watch if you want to learn more about graphics methods and demoscene tricks:</p>
<p><a href="https://www.youtube.com/c/InigoQuilez/videos" rel="nofollow noreferrer">https://www.youtube.com/c/InigoQuilez/videos</a></p>

CC BY-SA 4.0
2021-03-06T15:53:32.597Z
Please do not post answers that depend on links cause they might expire in the future. Summarize the key statements from the video so that your answer can be understood without any further information from other sites.
wychmaster
CC BY-SA 4.0
2021-03-05T09:49:04.787Z
2021-03-05T10:22:18.027Z
2021-03-05T10:22:18.027Z
dubious
<p>This topic would contain a lot of sub questions because I have one for each shape but for today I just want to understand the function for a torus and a capped torus</p>
<p><a href="https://iquilezles.org/www/articles/distfunctions/distfunctions.htm" rel="nofollow noreferrer">This</a> is the article for all shapes and I am trying to understand the code for torus</p>
<pre><code>float sdTorus( vec3 p, vec2 t )
{
  vec2 q = vec2(length(p.xz)- 
  t.x,p.y);
  return length(q)-t.y;
 }
</code></pre>
<p>and capped torus</p>
<pre><code> float sdCappedTorus(in vec3 p, 
  in vec2 sc, in float ra, in 
  float rb)
{
  p.x = abs(p.x);

  float k = (sc.y*p.x&gt;sc.x*p.y) ? 
  dot(p.xy,sc) : length(p.xy);

   return sqrt( dot(p,p) + ra*ra 
 - 2.0*ra*k ) - rb;
}
</code></pre>
<p>I have so many questions. I know parameter P is the point for testing but what is t? what is ra and rb? what calculations are they doing what are the mathematics?</p>
<p>Just a theoretical explanation or some images explaining the general formula would be greatly appreciated.</p>

<mathematics><raymarching>
understanding SDF[Signed Distance Functions] for a torus
Sync it
# understanding SDF[Signed Distance Functions] for a torus

(Asked by: username_0 on 2021-03-05)

This topic would contain a lot of sub questions because I have one for each shape but for today I just want to understand the function for a torus and a capped torus

[This](https://iquilezles.org/www/articles/distfunctions/distfunctions.htm) is the article for all shapes and I am trying to understand the code for torus

```
float sdTorus( vec3 p, vec2 t )
{
  vec2 q = vec2(length(p.xz)- 
  t.x,p.y);
  return length(q)-t.y;
 }

```

and capped torus

```
 float sdCappedTorus(in vec3 p, 
  in vec2 sc, in float ra, in 
  float rb)
{
  p.x = abs(p.x);

  float k = (sc.y*p.x>sc.x*p.y) ? 
  dot(p.xy,sc) : length(p.xy);

   return sqrt( dot(p,p) + ra*ra 
 - 2.0*ra*k ) - rb;
}

```

I have so many questions. I know parameter P is the point for testing but what is t? what is ra and rb? what calculations are they doing what are the mathematics?

Just a theoretical explanation or some images explaining the general formula would be greatly appreciated.

**Tags:** mathematics, raymarching


---

## 2 answers

### Answer by username_1 on 2021-03-05. Score: 1 | Accepted answer

This turned into a rabbit hole for me hopefully it helps answer your question, I recommend trying out the various functions on the [ShaderToy](https://www.shadertoy.com/view/3ltSW2) website. Just drop them in and change the call to "sdCircle". I used the values R=0.75 and r=0.35 to test them out.

**Part 1: Defining what an SDF means**

Signed Distance Field or SDF, in this context, is a function that returns the signed distance from a point to a primitive. The returned value is always a float that can take on 3 context specific meanings.

* zero: This is on the surface, while it is rare to hit exactly on the surface of the primitive we can approximate a hit with a simple inequality like `abs(d)<sigma` where d is the signed distance and sigma is some small value like 0.001.
* negative: In this context negative values represent hits inside the primitive but choosing the meaning of a negative return value is context specific. Smaller (more negative) values represent values that are deeper in the interior of the primitive.
* positive: Again this is context specific, but in this case this is a hit outside the primitive and larger values mean it is farther away from the primitive.

Stop for a second and take another look at the first case, "zero". This may seem unimportant at first glance but it turns out that by defining a generic base primitive and then changing the value of sigma we can easily compute a primitive of any size. In the case of a torus we could have a very thin torus or one that is so big it envelopes itself. Then by adding one more variable representing the large Radius of the torus, the torus can now be changed to fit our needs as desired just by controlling two variables.

**Part 2: Defining the primitive**

Take a look at IQ's 2D function for a circle.

```
float sdCircle( vec2 p, float r ){ return length(p) - r; }

```

All this function is doing is computing the "distance" using the length function of our point "p" from our circle (which is sitting at the origin) then subtracting our radius "r". And easy as that any point greater then radius is positive, any point less then radius is negative.

This is almost a torus already, we are just missing the signed distance for the inside diameter of the 2D torus. But we already know that is really just some sigma value from our original circle, so all we have to do is add a second radius and compute the sigma.
So:

```
float sdTorus2D( vec2 p, float R, float r )
{
  float q = length(p)-R;
  return abs(q)-r;
}

```

No need to recalculate the distance, so just figure out sigma to our second circle with `abs(q)-r`.

**Part 3: Going 3D**

If you stop and think about it for a second (ok...maybe more then a second) then it is easy to realize that our 2D torus is just a slice of a 3D torus. When you think about it like that then we can take another slice of our torus in 2D on its edge just by rotating our torus 90 degrees. This should result in two SDF circles side by side.

But turning the torus on its side is really just a matter of computing our signed distances in the opposite order...lets try:

```
float sdTorus2DSide( vec2 p, float R, float r )
{
  vec2 q = vec2(abs(p.x)-R,p.y);
  return length(q)-r;
}

```

The two different views essentially compute the distance in two different ways, so by combining them we should be able to view our torus in 3D from any angle just by adding an extra component "z" so that both computations can be independent of each other. Then combine our two radii into a vec2 and we end up with a function that looks just like IQ's.

```
float sdTorus( vec3 p, vec2 t )
{
  vec2 q = vec2(length(p.xz)-t.x,p.y);
  return length(q)-t.y;
}

```

This would still look two dimensional in the "circle" shadertoy, but now we can vary z to look at the torus slices. Or rotate our view point and see it emerge from the 2D world. Neat stuff!

BTW you can go through this same process with many primitives and get many different shapes as IQ has demonstrated on his website.

> **Comments:**
> **username_0**: I'm a bit confused about your sqTorus2D function. Let's say the R=5 & r=3 [Torus is at origin] and let's say there is a point (6,0,0) then the function returns abs(6-5)-3 => 1-3 => -2 but this does not make sense. The point is clearly outside the torus so it should return a positive value[+1 I think]? Have I misunderstood something?  
> **username_1**: It computes the length of the input vector first. That gives us the distance to R, that value is a float not a vector. It then computes the inner radius in terms of the outer radius. Plug it into shaderToy and try it. (I gave a link to a super simple shaderToy at the top)  

---

### Answer by username_2 on 2021-03-05. Score: 1

The following video discusses the SDFs of various primitives (linked to torus section):

<https://youtu.be/Ff0jJyyiVyw?t=491>

It discusses a torus but not the capped variant. The rest of the channel has useful content as well.

Additionally, iq has videos on detailed derivations of some primitives and techniques, sometimes hidden in the long livestreams, which are well worth a watch if you want to learn more about graphics methods and demoscene tricks:

<https://www.youtube.com/c/InigoQuilez/videos>

> **Comments:**
> **username_3**: Please do not post answers that depend on links cause they might expire in the future. Summarize the key statements from the video so that your answer can be understood without any further information from other sites.  

---

