3
$\begingroup$

I'm trying to understand all primitives In this article.After hard work I just learn Sphere and Cube , I wrote description here.

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

Tours 2D

Torus - signed - exact

Tour 3D

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

Length

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


Cone 2D

Cone - signed - exact

Cone 3D

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.

$\endgroup$
  • $\begingroup$ What about them do you have trouble understanding? It's hard to explain without knowing what the specific problem is. $\endgroup$ – Dan Hulme Apr 10 '18 at 7:54
  • $\begingroup$ @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 $\endgroup$ – Seyed Morteza Kamali Apr 10 '18 at 7:55
  • $\begingroup$ @DanHulme I edit my question and add some detail that what is my problem I hope you help me to understanding this shapes. $\endgroup$ – Seyed Morteza Kamali Apr 10 '18 at 8:58
  • $\begingroup$ Please do not edit your question after an answer has been posted. If you have follow up questions, post them separately. $\endgroup$ – Federico Apr 11 '18 at 8:59
  • $\begingroup$ @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. $\endgroup$ – Seyed Morteza Kamali Apr 11 '18 at 13:01
5
$\begingroup$

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

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.