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 const mat3 a b c d e f g h i return m0 a m1 b m2 c m3 d m4 e m5 f m6 g m7 h m8 i const mat3t m v return vec3 m m0 v x m m3 v y m m6 v z m m1 v x m m4 v y m m7 v z m m2 v x m m5 v y m m8 v z 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 sphere p r return len3 p r function ellipse p r let k0 len3 x p x r x y p y r y z p z r z let k1 len3 x p x r x r x y p y r y r y z p z r z r z 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 roundCone p r1 r2 h let q vec2 len2 vec2 p x p z p y let b r1 r2 h let a Math sqrt 1 0 b b let k dot2 q vec2 b a if k 0 0 return len2 q r1 if k a h return len2 sub2 q vec2 0 0 h r2 return dot2 q vec2 a b r1 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 base2 p let r base p 1 2 let p2 vec2 len2 vec2 p x p z p y let dn ellipse2 sub2 vec2 0 1 4 p2 vec2 1 15 2 7 let dc ellipse2 sub2 vec2 0 2 0 p2 vec2 1 6 0 3 let dc1 ellipse2 sub2 vec2 0 2 2 p2 vec2 1 5 0 2 let dc2 ellipse2 sub2 vec2 0 2 8 p2 vec2 1 2 0 2 let ds ellipse2 sub2 vec2 0 5 9 p2 vec2 1 9 2 8 let dcut box2 sub2 vec2 0 7 2 p2 vec2 3 0 2 5 r blend r dn 1 8 r blend r dc 1 8 r Math min r dc1 r blend r dc2 0 55 r blend r ds 1 1 return Math max r dcut 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 function knight p let base base1 p p x Math abs p x let ds1 sphere sub3 vec3 0 0 2 0 0 0 p 4 0 let ds2 ellipse sub3 vec3 0 0 2 0 0 0 p vec3 2 0 5 0 1 8 let dn roundCone sub3 vec3 0 3 1 0 0 5 p 0 8 2 2 2 2 let dncut ellipse sub3 vec3 2 2 0 0 0 0 p vec3 1 5 2 5 5 0 const a 1 3 const rot mat3 1 0 0 0 Math cos a Math sin a 0 Math sin a Math cos a const gg sub3 vec3 0 0 2 5 0 5 p let dh roundCone mat3t rot gg 1 2 0 6 1 9 let de ellipse sub3 vec3 0 5 3 5 0 5 p vec3 0 4 0 5 0 35 let dhcut1 0 5 p x let dhcut2 sphere sub3 vec3 2 1 2 8 1 9 p 2 0 let dhs ellipse sub3 vec3 0 0 2 2 0 0 p vec3 2 0 1 3 2 3 let r2 Math max dn dncut let h dh h Math max h dhcut1 h Math max h dhcut2 h Math max h dhs h Math min h Math max de dhcut1 r2 blend r2 h 0 7 return Math min base Math max Math max r2 ds1 ds2 function bishop p let base base1 p let p2 vec2 len2 vec2 p x p z p y let dn ellipse2 sub2 vec2 0 1 4 p2 vec2 1 0 1 6 let dc ellipse2 sub2 vec2 0 0 7 p2 vec2 1 6 0 3 let dc1 ellipse2 sub2 vec2 0 0 9 p2 vec2 1 5 0 2 let dc2 ellipse2 sub2 vec2 0 1 5 p2 vec2 1 2 0 2 let dh ellipse2 sub2 vec2 0 2 6 p2 vec2 1 3 1 5 let dt ellipse2 sub2 vec2 0 4 2 p2 vec2 0 4 0 4 const ang 0 4 const rot mat2 Math cos ang Math sin ang Math sin ang Math cos ang let c3 sub3 vec3 0 8 3 7 0 0 p let gg mat2t rot vec2 c3 x c3 y c3 vec3 gg x gg y c3 z let cut box3 c3 vec3 0 2 1 0 2 0 let r blend base dn 0 9 r blend r dc 1 5 r Math min r dc1 r blend r dc2 0 55 r Math min r dh r Math min r dt return Math max r cut function king p let base base2 p let p2 vec2 len2 vec2 p x p z p y let dh ellipse2 sub2 vec2 0 4 6 p2 vec2 1 8 0 4 let dt1 box3 sub3 vec3 0 5 2 0 p vec3 0 3 1 5 0 25 let dt2 box3 sub3 vec3 0 5 8 0 p vec3 1 0 0 3 0 25 let r Math min base dh r Math min r dt1 return Math min r dt2 function queen p let base base2 p let p2 vec2 len2 vec2 p x p z p y let dh ellipse2 sub2 vec2 0 4 0 p2 vec2 1 3 1 5 let dhcut box2 sub2 vec2 0 2 0 p2 vec2 3 0 2 0 let dt ellipse2 sub2 vec2 0 5 6 p2 vec2 0 5 0 5 let pc vec3 Math abs p x p y Math abs p z if pc x pc z pc vec3 pc z pc y pc x let dccut sphere sub3 vec3 1 0 4 7 2 2 pc 1 1 let r Math min base Math max dh dhcut return Math max Math min r dt dccut console log d queen x 1 y 2 z 0 Combined SDF function combinedSDF x y z let point x x y y z z let bb queen point return bb 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 20 10 let sy y canvas height 20 10 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 04 ctx fillStyle red ctx fillRect x y 1 1 var aa 0 2 renderSDF
2 5 r blend r dn 1 8 r blend r dc 1 8 r Math min r dc1 r blend r dc2 0 55 r blend r ds 1 1 return Math max r dcut 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 function knight p let base base1 p p x Math abs p x let ds1 sphere sub3 vec3 0 0 2 0 0 0 p 4 0 let ds2 ellipse sub3 vec3 0 0 2 0 0 0 p vec3 2 0 5 0 1 8 let dn roundCone sub3 vec3 0 3 1 0 0 5 p 0 8 2 2 2 2 let dncut ellipse sub3 vec3 2 2 0 0 0 0 p vec3 1 5 2 5 5 0 const a 1 3 const rot mat3 1 0 0 0 Math cos a Math sin a 0 Math sin a Math cos a const gg sub3 vec3 0 0 2 5 0 5 p let dh roundCone mat3t rot gg 1 2 0 6 1 9 let de ellipse sub3 vec3 0 5 3 5 0 5 p vec3 0 4 0 5 0 35 let dhcut1 0 5 p x let dhcut2 sphere sub3 vec3 2 1 2 8 1 9 p 2 0 let dhs ellipse sub3 vec3 0 0 2 2 0 0 p vec3 2 0 1 3 2 3 let r2 Math max dn dncut let h dh h Math max h dhcut1 h Math max h dhcut2 h Math max h dhs h Math min h Math max de dhcut1 r2 blend r2 h 0 7 return Math min base Math max Math max r2 ds1 ds2 function bishop p let base base1 p let p2 vec2 len2 vec2 p x p z p y let dn ellipse2 sub2 vec2 0 1 4 p2 vec2 1 0 1 6 let dc ellipse2 sub2 vec2 0 0 7 p2 vec2 1 6 0 3 let dc1 ellipse2 sub2 vec2 0 0 9 p2 vec2 1 5 0 2 let dc2 ellipse2 sub2 vec2 0 1 5 p2 vec2 1 2 0 2 let dh ellipse2 sub2 vec2 0 2 6 p2 vec2 1 3 1 5 let dt ellipse2 sub2 vec2 0 4 2 p2 vec2 0 4 0 4 const ang 0 4 const rot mat2 Math cos ang Math sin ang Math sin ang Math cos ang let c3 sub3 vec3 0 8 3 7 0 0 p let gg mat2t rot vec2 c3 x c3 y c3 vec3 gg x gg y c3 z let cut box3 c3 vec3 0 2 1 0 2 0 let r blend base dn 0 9 r blend r dc 1 5 r Math min r dc1 r blend r dc2 0 55 r Math min r dh r Math min r dt return Math max r cut function king p let base base2 p let p2 vec2 len2 vec2 p x p z p y let dh ellipse2 sub2 vec2 0 4 6 p2 vec2 1 8 0 4 let dt1 box3 sub3 vec3 0 5 2 0 p vec3 0 3 1 5 0 25 let dt2 box3 sub3 vec3 0 5 8 0 p vec3 1 0 0 3 0 25 let r Math min base dh r Math min r dt1 return Math min r dt2 function queen p let base base2 p let p2 vec2 len2 vec2 p x p z p y let dh ellipse2 sub2 vec2 0 4 0 p2 vec2 1 3 1 5 let dhcut box2 sub2 vec2 0 2 0 p2 vec2 3 0 2 0 let dt ellipse2 sub2 vec2 0 5 6 p2 vec2 0 5 0 5 let pc vec3 Math abs p x p y Math abs p z if pc x pc z pc vec3 pc z pc y pc x let dccut sphere sub3 vec3 1 0 4 7 2 2 pc 1 1 let r Math min base Math max dh dhcut return Math max Math min r dt dccut console log d queen x 1 y 2 z 0 Combined SDF function combinedSDF x y z let point x x y y z z let bb queen point return bb 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 20 10 let sy y canvas height 20 10 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 04 ctx fillStyle red ctx fillRect x y 1 1 var aa 0 2 renderSDF