I'm looking into drawing a box using Inigo Quilez's code:

float sdBox( in vec2 p, in vec2 b )
    vec2 d = abs(p)-b;
    return length(max(d,vec2(0))) + min(max(d.x,d.y),0.0);

Do I understand correctly that for p is a point anywhere in 2d space?

If so, what is b?

I tried to draw a 2x2 rectangle at origin (assuming b is then vec2(2,2)) and do those calculations but nothing good came out. It should calculate the distance between the point and nearest edge, right?

  • \$\begingroup\$ "nothing good came out" - what output did you expect, and what output did you get instead? \$\endgroup\$ – DMGregory Dec 20 '19 at 17:50
  • \$\begingroup\$ Well, it calculated 0, when the distance as drawn on paper was 2. I think I dont understand whats b is clearly. \$\endgroup\$ – Janis Taranda Dec 20 '19 at 17:52
  • \$\begingroup\$ What arguments did you provide to get this outcome? (If you explained this in your question, I wouldn't have to ask follow-up questions) \$\endgroup\$ – DMGregory Dec 20 '19 at 17:53
  • \$\begingroup\$ b = vec2(2,2), p = 1,2 \$\endgroup\$ – Janis Taranda Dec 20 '19 at 17:56
  • \$\begingroup\$ The point 1, 2 is exactly on the edge of a box centered at the origin with half-extents (2, 2), so 0 is exactly the number we'd hope to get, is it not? \$\endgroup\$ – DMGregory Dec 20 '19 at 17:58

b is the size of the box, measured from the center to one corner - what we often call the "half-extents" of the shape.

So a box 10 units wide and 2 units tall would use b = vec2(5, 1)

To draw a 2x2 square, you'd want b = vec2(1, 1)


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.