const len2 a return Math sqrt a x 2 a y 2 const len3 a return Math sqrt a x 2 a y 2 a z 2 const dot2 a b return a x b x a y b y const dot3 a b return a x b x a y b y a z b z const sub2 a b return x a x b x y a y b y const sub3 a b return x a x b x y a y b y z a z b z const scale2 a b return x a x b y a y b const sign a return a 0 0 1 0 1 0 const vec2 a b return x a y b const vec3 a b c return x a y b z c const vec2a aa return x aa x y aa y const abs2 a return x Math abs a x y Math abs a y const abs3 a return x Math abs a x y Math abs a y z Math abs a z const max2 a b return x Math max a x b x y Math max a y b y const max3 a b return x Math max a x b x y Math max a y b y z Math max a z b z const min2 a b return x Math min a x b x y Math min a y b y const mat2 a b c d return m0 a m1 b m2 c m3 d const mat2t m v return vec2 m m0 v x m m2 v y m m1 v x m m3 v y function sphere2 p r return len2 p r function ellipse2 p r let k0 len2 x p x r x y p y r y let k1 len2 x p x r x r x y p y r y r y return k0 k0 1 0 k1 function mix x y a return 1 a x y a function clamp x minVal maxVal return Math min Math max x minVal maxVal function blend d1 d2 k let h clamp 0 5 0 5 d2 d1 k 0 0 1 0 return mix d2 d1 h k h 1 0 h function pawnx p var p2 vec2 len2 vec2 p x p z p y var dt sphere2 sub2 vec2 0 1 p2 1 0 var dn ellipse2 sub2 vec2 0 0 15 p2 vec2 1 0 0 3 var dw0 ellipse2 sub2 vec2 0 0 p2 vec2 0 5 0 8 var dw1 ellipse2 sub2 vec2 0 2 3 p2 vec2 0 9 0 3 var dw2 ellipse2 sub2 vec2 0 2 1 p2 vec2 1 4 0 3 var db0 ellipse2 sub2 vec2 0 2 3 p2 vec2 1 2 0 6 var db1 ellipse2 sub2 vec2 0 3 3 p2 vec2 2 0 0 6 var db2 ellipse2 sub2 vec2 0 3 8 p2 vec2 2 1 0 5 var r blend dt dn 0 3 r Math min r blend dw0 dw1 3 0 r Math min r dw2 r Math min r blend blend db0 db1 1 2 db2 0 3 return r function box2 p r var d sub2 abs2 p r return Math min Math max d x d y 0 0 len2 max2 d vec2 0 0 0 0 function box3 p r var d sub3 abs3 p r return Math min Math max d x Math max d y d z 0 0 len3 max3 d vec3 0 0 0 function base p rad let p2 vec2 len2 vec2 p x p z p y let dn ellipse2 sub2 vec2 0 1 0 p2 vec2 1 3 rad 1 0 let db0 ellipse2 sub2 vec2 0 2 3 p2 vec2 1 6 rad 0 6 let db1 ellipse2 sub2 vec2 0 3 3 p2 vec2 2 5 rad 0 6 let db2 ellipse2 sub2 vec2 0 3 8 p2 vec2 2 6 rad 0 5 let dw ellipse2 sub2 vec2 0 2 1 p2 vec2 1 8 rad 0 3 let r blend blend db0 db1 1 0 db2 0 3 r Math min r dw return r function base1 p return base p 1 0 function rook p let base base1 p let p2 vec2 len2 vec2 p x p z p y let dn ellipse2 sub2 vec2 0 1 0 p2 vec2 1 2 1 3 let dc ellipse2 sub2 vec2 0 0 5 p2 vec2 1 7 0 2 let r blend base dn 1 0 r blend r dc 1 4 const xx box2 sub2 vec2 1 4 1 1 p2 vec2 0 2 0 6 r Math min r xx let b3 p const ang 3 141593 2 0 3 0 const rot mat2 Math cos ang Math sin ang Math sin ang Math cos ang for let i 0 i 3 i r Math max r box3 sub3 vec3 0 1 4 0 b3 vec3 2 0 0 6 0 2 let b3xz mat2t rot vec2 b3 x b3 z b3 vec3 b3xz x b3 y b3xz y return r console log d rook x 1 y 2 z 0 Combined SDF function combinedSDF x y z let point x x y y z z let bb rook point return bb let pawn2 pawnx x x y y z z return pawn2 Test the combined SDF function function renderSDF let canvas document createElement canvas canvas width 600 canvas height 600 document body appendChild canvas let ctx canvas getContext 2d let z 0 Fixed z value for 2D cross section for let x 0 x canvas width x for let y 0 y canvas height y let sx x canvas width 8 4 let sy y canvas height 8 4 let d combinedSDF sx sy z let color Math max Math min 1 d 1 0 255 ctx fillStyle rgb color color color ctx fillRect x y 1 1 if d aa d aa 0 01 ctx fillStyle red ctx fillRect x y 1 1 var aa 0 2 renderSDF
r return Math min Math max d x d y 0 0 len2 max2 d vec2 0 0 0 0 function box3 p r var d sub3 abs3 p r return Math min Math max d x Math max d y d z 0 0 len3 max3 d vec3 0 0 0 function base p rad let p2 vec2 len2 vec2 p x p z p y let dn ellipse2 sub2 vec2 0 1 0 p2 vec2 1 3 rad 1 0 let db0 ellipse2 sub2 vec2 0 2 3 p2 vec2 1 6 rad 0 6 let db1 ellipse2 sub2 vec2 0 3 3 p2 vec2 2 5 rad 0 6 let db2 ellipse2 sub2 vec2 0 3 8 p2 vec2 2 6 rad 0 5 let dw ellipse2 sub2 vec2 0 2 1 p2 vec2 1 8 rad 0 3 let r blend blend db0 db1 1 0 db2 0 3 r Math min r dw return r function base1 p return base p 1 0 function rook p let base base1 p let p2 vec2 len2 vec2 p x p z p y let dn ellipse2 sub2 vec2 0 1 0 p2 vec2 1 2 1 3 let dc ellipse2 sub2 vec2 0 0 5 p2 vec2 1 7 0 2 let r blend base dn 1 0 r blend r dc 1 4 const xx box2 sub2 vec2 1 4 1 1 p2 vec2 0 2 0 6 r Math min r xx let b3 p const ang 3 141593 2 0 3 0 const rot mat2 Math cos ang Math sin ang Math sin ang Math cos ang for let i 0 i 3 i r Math max r box3 sub3 vec3 0 1 4 0 b3 vec3 2 0 0 6 0 2 let b3xz mat2t rot vec2 b3 x b3 z b3 vec3 b3xz x b3 y b3xz y return r console log d rook x 1 y 2 z 0 Combined SDF function combinedSDF x y z let point x x y y z z let bb rook point return bb let pawn2 pawnx x x y y z z return pawn2 Test the combined SDF function function renderSDF let canvas document createElement canvas canvas width 600 canvas height 600 document body appendChild canvas let ctx canvas getContext 2d let z 0 Fixed z value for 2D cross section for let x 0 x canvas width x for let y 0 y canvas height y let sx x canvas width 8 4 let sy y canvas height 8 4 let d combinedSDF sx sy z let color Math max Math min 1 d 1 0 255 ctx fillStyle rgb color color color ctx fillRect x y 1 1 if d aa d aa 0 01 ctx fillStyle red ctx fillRect x y 1 1 var aa 0 2 renderSDF