/*
* 4D SHAPES
*//**
* \brief Signed Distance Function for a sphere
*
* \param p center point of object
* \param r radius of the sphere
*/floatsdSphere(float4p,floatr){floatd=length(p)-r;returnd;}/**
* \brief Signed Distance Function for a box
*
* \param p center point of object
* \param s float 3 of box shape
* x, y, z -> width, height, depth
*/floatsdBox(float4p,float4s){floatd=length(max(abs(p)-s,0));returnd;}/**
* \brief Signed Distance Function for a torus
*
* \param p center point of object
* \param r1 major radius - torus ring
* \param r2 minor radius - torus thickness
* \param r3 minor radius in the 4th dimension
*/floatsdTorus(float4p,floatr1,floatr2,floatr3){floatd=length(float2(length(float2(length(p.zx)-r1,p.y))-r2,p.w))-r3;returnd;}/**
* \brief Signed Distance Function for a cone with
* the tip extending into the y axis
*
* \param r radius at base of cone
* \param h height of cone
*/floatsdConeW(float4p,floatr,floath){float2q=float2(length(p.xyz),p.w);float2tip=q-float2(0,h);float2mantleDir=normalize(float2(h,r));floatmantle=dot(tip,mantleDir);floatd=max(mantle,-q.y);floatprojected=dot(tip,float2(mantleDir.y,-mantleDir.x));// distance to tipif((q.y>h)&&(projected<0)){d=max(d,length(tip));}// distance to base ringif((q.x>r)&&(projected>length(float2(h,r)))){d=max(d,length(q-float2(r,0)));}returnd;}/**
* \brief Signed Distance Function for a cone with
* the tip extending into the w axis
*
* \param r radius at base of cone
* \param h height of cone
*/floatsdConeY(float4p,floatr,floath){float2q=float2(length(p.xzw),p.y);float2tip=q-float2(0,h);float2mantleDir=normalize(float2(h,r));floatmantle=dot(tip,mantleDir);floatd=max(mantle,-q.y);floatprojected=dot(tip,float2(mantleDir.y,-mantleDir.x));// distance to tipif((q.y>h)&&(projected<0)){d=max(d,length(tip));}// distance to base ringif((q.x>r)&&(projected>length(float2(h,r)))){d=max(d,length(q-float2(r,0)));}returnd;}/**
* \brief Signed Distance Function for a capsule
*
* \param p center point of object
* \param a origin of first sphere cap
* \param b origin of second sphere cap
* \param r radius of capsule
*/floatsdCapsule(float4p,float4a,float4b,floatr){float4pa=p-a,ba=b-a;floath=clamp(dot(pa,ba)/dot(ba,ba),0.0,1.0);returnlength(pa-ba*h)-r;}/**
* \brief Signed Distance Function for a capsule
* elongated along the x axis
*
* \param p center point of object
* \param length length of the capsule
* \param r radius of capsule
*/floatsdCapsuleX(float4p,floatlength,floatr){floatl=length/2;returnsdCapsule(p,float4(-l,0,0,0),float4(l,0,0,0),r);}/**
* \brief Signed Distance Function for a capsule
* elongated along the w axis
*
* \param p center point of object
* \param length length of the capsule
* \param r radius of capsule
*/floatsdCapsuleW(float4p,floatlength,floatr){floatl=length/2;returnsdCapsule(p,float4(0,0,0,-l),float4(0,0,0,l),r);}/**
* \brief Signed Distance Function for a pentachoron
* Also known as a 5-cell or hyper tetrahedron
*
* \param p center point of object
* \param s scale of object (radius)
*/floatsdPentachoron(float4p,floats){floata=+p.x+p.y-p.z-p.w;floatb=-p.x-p.y-p.z-p.w;floatc=+p.x-p.y+p.z-p.w;floatd=-p.x+p.y+p.z-p.w;floate=p.w;return(max(max(max(a,b),max(c,d)),e)-s)/sqrt(5.);}/*
* 3D SHAPES
*//**
* \brief Signed Distance Function for a sphere
*
* \param p center point of object
* \param r radius of sphere
*/floatsdSphere(float3p,floatr){floatd=length(p)-r;returnd;}/**
* \brief Signed Distance Function for a box
*
* \param p center point of object
* \param s float 3 of box shape
* x, y, z -> width, height, depth
*/floatsdBox(float3p,float3s){floatd=length(max(abs(p)-s,0));returnd;}/**
* \brief Signed Distance Function for a torus
*
* \param p center point of object
* \param r1 major radius - torus ring
* \param r2 minor radius - torus thickness
*/floatsdTorus(float3p,floatr1,floatr2){floatd=length(float2(length(p.zx)-r1,p.y))-r2;returnd;}/**
* \brief Signed Distance Function for a cone extending along Z
*
* \param p center point of object
* \param radius radius of the base of the cone
* \param height height of the cone
*/floatsdConeZ(float3p,floatradius,floatheight){float2q=float2(length(p.xy),p.z);float2tip=q-float2(0,height);float2mantleDir=normalize(float2(height,radius));floatmantle=dot(tip,mantleDir);floatd=max(mantle,-q.y);floatprojected=dot(tip,float2(mantleDir.y,-mantleDir.x));// distance to tipif((q.y>height)&&(projected<0)){d=max(d,length(tip));}// distance to base ringif((q.x>radius)&&(projected>length(float2(height,radius)))){d=max(d,length(q-float2(radius,0)));}returnd;}/**
* \brief Signed Distance Function for a cone extending along Y
*
* \param p center point of object
* \param radius radius of the base of the cone
* \param height height of the cone
*/floatsdConeY(float3p,floatradius,floatheight){float2q=float2(length(p.xz),p.y);float2tip=q-float2(0,height);float2mantleDir=normalize(float2(height,radius));floatmantle=dot(tip,mantleDir);floatd=max(mantle,-q.y);floatprojected=dot(tip,float2(mantleDir.y,-mantleDir.x));// distance to tipif((q.y>height)&&(projected<0)){d=max(d,length(tip));}// distance to base ringif((q.x>radius)&&(projected>length(float2(height,radius)))){d=max(d,length(q-float2(radius,0)));}returnd;}/**
* \brief Signed Distance Function for a capsule
*
* \param p center point of object
* \param a origin of first sphere cap
* \param b origin of second sphere cap
* \param r radius of capsule
*/floatsdCapsule(float3p,float3a,float3b,floatr){float3ab=b-a;float3ap=p-a;floatt=dot(ab,ap)/dot(ab,ab);t=clamp(t,0.,1.);float3c=a+t*ab;floatd=length(p-c)-r;returnd;}/**
* \brief Signed Distance Function for a capsule
* elongated along the x axis
*
* \param p center point of object
* \param length length of the capsule
* \param r radius of capsule
*/floatsdCapsuleX(float3p,floatlength,floatr){floatl=length/2;returnsdCapsule(p,float3(-l,0,0),float3(l,0,0),r);}/**
* \brief Signed Distance Function for a capsule
* elongated along the y axis
*
* \param p center point of object
* \param length length of the capsule
* \param r radius of capsule
*/floatsdCapsuleZ(float3p,floatlength,floatr){floatl=length/2;returnsdCapsule(p,float3(0,0,-l),float3(0,0,l),r);}/**
* \brief Signed distance function for an octohedron
*
* \param p center point of object
* \param s size of the object
*/floatsdOctahedron(float3p,floats){floatc=sqrt(3);return((dot(abs(p),float3(1,1,1))-s)/c);}/**
* \brief Signed Distance Function for a Tetrahedron
*
* \param p center point of object
* \param s scale of object (radius)
*/floatsdTetrahedron(float3p,floats){floata=+p.x+p.y-p.z;floatb=-p.x-p.y-p.z;floatc=+p.x-p.y+p.z;floatd=-p.x+p.y+p.z;return(max(max(a,b),max(c,d))-s)/sqrt(5.);}