<h1>Torus</h1>

<p>A torus is defined by two parameters: the major radius, and the minor radius. The major radius (<code>t.x</code>) is the radius of the big ring (in red in the diagram), and the minor radius (<code>t.y</code>) is the radius of the circular cross-section. The <code>x</code> and <code>y</code> here are just indices into the vector: they're unrelated to the x and y axes.</p>

<p>The left side of the diagram shows the xz plane, and the right side shows a cross-section with y going horizontally.</p>

<p><a href="https://i.sstatic.net/pvGwn.png" rel="noreferrer"><img src="https://i.sstatic.net/pvGwn.png" alt="enter image description here"></a></p>

<p><code>p.xz</code> is the projection of the point onto the plane of the torus, so <code>length(p.xz)-t.x</code> gives the distance from that projected point to the inside of the torus. (I've labelled it $r$ on the diagram.) <code>p.y</code> is the distance from the point to the plane of the torus, so the length of the vector ($r$, <code>p.y</code>) is the distance from the point to the inside of the torus. (Computing the length of a two-element vector is a handy way to use Pythagoras' theorem to find the hypotenuse of the triangle.)</p>

<h1>Cone</h1>

<p>Like the torus, the cone code uses the fact that the shape is symmetric about an axis: the z axis in this case. The cone is defined by a unit vector <code>c</code> that's normal to the surface of the cone, and the apex of the cone is at the origin. Similar to the above, <code>length(p.xy)</code> gets the distance from the point to the z axis, so that we can work in the plane where the cone is a triangle. <code>vec2(q,p.z)</code> is the projection of <code>p</code> into this plane. The dot product projects this vector onto the direction <code>c</code>: how far the point is along this direction tells us how far it is from the surface of the triangle.</p>

<p><a href="https://i.sstatic.net/eDKgu.png" rel="noreferrer"><img src="https://i.sstatic.net/eDKgu.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2018-04-10T16:46:34.303Z
2018-04-10T16:46:34.303Z
Dan Hulme
<p>I'm trying to understand all primitives In <a href="http://iquilezles.org/www/articles/distfunctions/distfunctions.htm" rel="nofollow noreferrer">this article</a>.After hard work I just learn Sphere and Cube , I wrote description <a href="https://stackoverflow.com/questions/35326366/glsl-cube-signed-distance-field-implementation-explanation/49726668#49726668">here</a>.</p>

<p>I could draw 2D shapes but I have trouble understanding 3D below codes:</p>

<p><a href="https://i.sstatic.net/YjRDv.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/YjRDv.png" alt="Tours 2D"></a></p>

<p>Torus - signed - exact</p>

<p><a href="https://i.sstatic.net/o8H2x.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/o8H2x.png" alt="Tour 3D"></a></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><a href="https://i.sstatic.net/7ehcE.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/7ehcE.png" alt="Length"></a></p>

<p>I can't imagine how this shape drew and I don't know why two length used here.</p>

<hr>

<p><a href="https://i.sstatic.net/JDBUy.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/JDBUy.png" alt="Cone 2D"></a></p>

<p>Cone - signed - exact</p>

<p><a href="https://i.sstatic.net/ZiPLs.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ZiPLs.png" alt="Cone 3D"></a></p>

<pre><code>float sdCone( vec3 p, vec2 c )
{
    // c must be normalized
    float q = length(p.xy);
    return dot(c,vec2(q,p.z));
}
</code></pre>

<p>I don't know why dot product used here?</p>

<p>I'm confused please help me to understand this shapes.I hope someone explain each shape.</p>

CC BY-SA 3.0
2018-04-10T07:54:45.897Z
What about them do you have trouble understanding? It's hard to explain without knowing what the specific problem is.
Dan Hulme
CC BY-SA 3.0
2018-04-10T07:55:58.683Z
@DanHulme my specific problem Is that I don't know how functions used to draw 3d shapes like dot product in cone shape and like using two length in tours shape
Seyed Morteza Kamali
CC BY-SA 3.0
2018-04-10T08:58:00.390Z
@DanHulme I edit my question and add some detail that what is my problem I hope you help me to understanding this shapes.
Seyed Morteza Kamali
CC BY-SA 3.0
2018-04-11T08:59:51.210Z
Please do not edit your question after an answer has been posted. If you have follow up questions, post them separately.
Federico
CC BY-SA 3.0
2018-04-11T13:01:24.953Z
@Federico I'm sorry I removed new part , I wanted ask as a new question but I thought Instead of asking several questions It's better to ask one question.
Seyed Morteza Kamali
CC BY-SA 3.0
2018-04-11T13:27:54.353Z
No, the opposite is quite true instead, SE sites prefer several well defined questions to one overly broad question
Federico
CC BY-SA 3.0
2018-04-11T13:34:00.067Z
I think your edit made it clear that you have a wider problem understanding linear algebra or signed distance fields, that explaining a bunch of shapes to you one at a time won't fix. Perhaps if you could isolate the common factor that's hard to understand about all the shapes, that would make a better question.
Dan Hulme
CC BY-SA 3.0
2018-04-11T13:42:15.833Z
@DanHulme that's right I should learn more algebra and ask my specific problems thanks.
Seyed Morteza Kamali
CC BY-SA 4.0
2019-06-07T08:47:12.230Z
I'm a bit late tot he game but I had a similar issue, I have no idea how to simplify the Cone but I rewrote the Torus function in a way that seemed more natural to me below.

float SdTorus(float3 position, float innerRadius, float outerRadius)
{
    float thickness = .5 * (outerRadius - innerRadius);
    float2 q = float2(length(position.xz) - outerRadius + thickness, position.y);
    return length(q) - thickness;
}
Benjamin Danger Johnson
<glsl><mathematics><signed-distance-field>
GLSL shapes signed distance field implementation explanation?
Seyed Morteza Kamali
# GLSL shapes signed distance field implementation explanation?

(Asked by: username_0 on 2018-04-10)

I'm trying to understand all primitives In [this article](http://iquilezles.org/www/articles/distfunctions/distfunctions.htm).After hard work I just learn Sphere and Cube , I wrote description [here](https://stackoverflow.com/questions/35326366/glsl-cube-signed-distance-field-implementation-explanation/49726668#49726668).

I could draw 2D shapes but I have trouble understanding 3D below codes:

[![Tours 2D](https://i.sstatic.net/YjRDv.png)](https://i.sstatic.net/YjRDv.png)

Torus - signed - exact

[![Tour 3D](https://i.sstatic.net/o8H2x.png)](https://i.sstatic.net/o8H2x.png)

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

```

[![Length](https://i.sstatic.net/7ehcE.png)](https://i.sstatic.net/7ehcE.png)

I can't imagine how this shape drew and I don't know why two length used here.

---

[![Cone 2D](https://i.sstatic.net/JDBUy.png)](https://i.sstatic.net/JDBUy.png)

Cone - signed - exact

[![Cone 3D](https://i.sstatic.net/ZiPLs.png)](https://i.sstatic.net/ZiPLs.png)

```
float sdCone( vec3 p, vec2 c )
{
    // c must be normalized
    float q = length(p.xy);
    return dot(c,vec2(q,p.z));
}

```

I don't know why dot product used here?

I'm confused please help me to understand this shapes.I hope someone explain each shape.

**Tags:** glsl, mathematics, signed-distance-field

> **Comments:**
> **username_1**: What about them do you have trouble understanding? It's hard to explain without knowing what the specific problem is.  
> **username_0**: @username_1 my specific problem Is that I don't know how functions used to draw 3d shapes like dot product in cone shape and like using two length in tours shape  
> **username_0**: @username_1 I edit my question and add some detail that what is my problem I hope you help me to understanding this shapes.  
> **username_2**: Please do not edit your question after an answer has been posted. If you have follow up questions, post them separately.  
> **username_0**: @username_2 I'm sorry I removed new part , I wanted ask as a new question but I thought Instead of asking several questions It's better to ask one question.  
> **username_2**: No, the opposite is quite true instead, SE sites prefer several well defined questions to one overly broad question  
> **username_1**: I think your edit made it clear that you have a wider problem understanding linear algebra or signed distance fields, that explaining a bunch of shapes to you one at a time won't fix. Perhaps if you could isolate the common factor that's hard to understand about all the shapes, that would make a better question.  
> **username_0**: @username_1 that's right I should learn more algebra and ask my specific problems thanks.  
> **username_3**: I'm a bit late tot he game but I had a similar issue, I have no idea how to simplify the Cone but I rewrote the Torus function in a way that seemed more natural to me below.  float SdTorus(float3 position, float innerRadius, float outerRadius) {     float thickness = .5 * (outerRadius - innerRadius);     float2 q = float2(length(position.xz) - outerRadius + thickness, position.y);     return length(q) - thickness; }  

---

## 1 answers

### Answer by username_1 on 2018-04-10. Score: 6 | Accepted answer

# Torus

A torus is defined by two parameters: the major radius, and the minor radius. The major radius (`t.x`) is the radius of the big ring (in red in the diagram), and the minor radius (`t.y`) is the radius of the circular cross-section. The `x` and `y` here are just indices into the vector: they're unrelated to the x and y axes.

The left side of the diagram shows the xz plane, and the right side shows a cross-section with y going horizontally.

[![enter image description here](https://i.sstatic.net/pvGwn.png)](https://i.sstatic.net/pvGwn.png)

`p.xz` is the projection of the point onto the plane of the torus, so `length(p.xz)-t.x` gives the distance from that projected point to the inside of the torus. (I've labelled it $r$ on the diagram.) `p.y` is the distance from the point to the plane of the torus, so the length of the vector ($r$, `p.y`) is the distance from the point to the inside of the torus. (Computing the length of a two-element vector is a handy way to use Pythagoras' theorem to find the hypotenuse of the triangle.)

# Cone

Like the torus, the cone code uses the fact that the shape is symmetric about an axis: the z axis in this case. The cone is defined by a unit vector `c` that's normal to the surface of the cone, and the apex of the cone is at the origin. Similar to the above, `length(p.xy)` gets the distance from the point to the z axis, so that we can work in the plane where the cone is a triangle. `vec2(q,p.z)` is the projection of `p` into this plane. The dot product projects this vector onto the direction `c`: how far the point is along this direction tells us how far it is from the surface of the triangle.

[![enter image description here](https://i.sstatic.net/eDKgu.png)](https://i.sstatic.net/eDKgu.png)


---

