Intro The 3D SDF functions article is pretty popular, so I decided to write a similar one for 2D primitives, since most of the 3D primitives are grown as extrusions or revolutions of these 2D shapes. So getting these right is important. In particular, the functions bellow use the minimum number of square roots and divisions possible, and also produce better/faster results than constructing them from other primitives (when possible). Note that all the primitives listed here come with a link to a realtime online demo in Shadertoy. In fact, this public playlist contains all the Shadertoy examples: https://www.shadertoy.com/playlist/MXdSRf, so don't miss that out. Lastly, as with all in this website, all formulas and code are derived by myself (unless otherwise stated), so there might be errors or better ways to do things. Let me know if you think that's the case. Primitives All primitives are centered at the origin; you will have to transform the point to get arbitrarily rotated, translated and scaled objects (see below). The dot2(v) function returns the dot product of a vector with itself (or the square of its length). Chamfer Box - exact   (https://www.shadertoy.com/view/3fc3zs) float sdChamferBox( in vec2 p, in vec2 b, in float chamfer ) { p = abs(p)-b; p = (p.y>p.x) ? p.yx : p.xy; p.y += chamfer; const float k = 1.0-sqrt(2.0); if( p.y<0.0 && p.y+p.x*k<0.0 ) return p.x; if( p.x<p.y ) return (p.x+p.y)*sqrt(0.5); return length(p); } Oriented Box - exact (https://www.shadertoy.com/view/stcfzn) float sdOrientedBox( in vec2 p, in vec2 a, in vec2 b, float th ) { float l = length(b-a); vec2 d = (b-a)/l; vec2 q = (p-(a+b)*0.5); q = mat2(d.x,-d.y,d.y,d.x)*q; q = abs(q)-vec2(l,th)*0.5; return length(max(q,0.0)) + min(max(q.x,q.y),0.0); } Rhombus - exact (https://www.shadertoy.com/view/XdXcRB) float sdRhombus( in vec2 p, in vec2 b ) { b.y = -b.y; p = abs(p); float h = clamp( (dot(b,p)+b.y*b.y)/dot(b,b), 0.0, 1.0 ); p -= b*vec2(h,h-1.0); return length(p)*sign(p.x); } Isosce...
First seen: 2025-12-28 19:58
Last seen: 2025-12-29 02:59