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 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 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 console log d pawnx x 1 y 2 z 0 Combined SDF function combinedSDF x y z 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 1 renderSDF
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 console log d pawnx x 1 y 2 z 0 Combined SDF function combinedSDF x y z 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 1 renderSDF