// @name Soft Club Orbital Chrome Knot
// @group Knot Geometry
// @param uScale "Major Radius" 0.86 0.35 1.35 0.01
// @param uDetail "Knot Lobes" 3.0 2.0 8.0 1.0
// @param uThickness "Ribbon Thickness" 0.19 0.05 0.42 0.01
// @param uRoundness "Secondary Strand" 0.72 0.0 1.5 0.01
// @param uWarp "Knot Lift" 0.31 0.0 0.7 0.01
// @param uTwist "Radial Twist" 0.22 -1.0 1.0 0.01
// @param uPulse "Inflation Pulse" 0.06 0.0 0.4 0.01
// @param uPulseSpeed "Pulse Speed" 0.58 0.0 3.0 0.01
// @param uOffsetX "Screen X Position" 0.0 -1.25 1.25 0.01
// @param uOffsetY "Screen Y Position" 0.0 -0.75 0.75 0.01
// @group Motion
// @param uTilt "Tilt" -0.34 -3.14 3.14 0.01
// @param uTurn "Turn" 0.25 -3.14 3.14 0.01
// @param uSpin "Spin Speed" 0.16 -1.5 1.5 0.01
// @group Chrome Plastic
// @param uHue "Plastic Hue" 0.94 0.0 1.0 0.005
// @param uSaturation "Plastic Saturation" 0.32 0.0 1.0 0.01
// @param uMetalness "Metalness" 0.92 0.0 1.0 0.01
// @param uRoughness "Roughness" 0.08 0.01 0.8 0.01
// @param uReflectivity "Reflection" 1.35 0.0 2.5 0.01
// @param uClearcoat "Clearcoat" 1.18 0.0 2.5 0.01
// @param uIridescence "Iridescence" 0.38 0.0 2.0 0.01
// @param uTransmission "Transmission" 0.05 0.0 1.5 0.01
// @param uIOR "Index of Refraction" 1.48 1.01 2.2 0.01
// @param uGlow "Inner Glow" 0.16 0.0 2.0 0.01
// @param uGlowHue "Glow Hue" 0.88 0.0 1.0 0.005
// @group Environment Map
// @param uEnvHue "Environment Hue" 0.60 0.0 1.0 0.005
// @param uEnvSat "Environment Saturation" 0.55 0.0 1.0 0.01
// @param uEnvPower "Environment Brightness" 0.86 0.0 2.5 0.01
// @param uEnvRotation "Environment Rotation" -0.35 -3.14 3.14 0.01
// @param uEnvSpin "Environment Spin" 0.025 -0.5 0.5 0.005
// @param uCardPower "Softbox Power" 2.1 0.0 5.0 0.05
// @param uNeonHue "Neon Hue" 0.92 0.0 1.0 0.005
// @param uNeonPower "Neon Power" 0.85 0.0 4.0 0.01
// @group Camera & Finish
// @param uCameraDistance "Camera Distance" 3.65 2.2 6.5 0.05
// @param uCameraHeight "Camera Height" 0.20 -1.5 2.0 0.01
// @param uCameraOrbit "Camera Orbit" 0.0 -3.14 3.14 0.01
// @param uOrbitSpeed "Camera Orbit Speed" 0.025 -0.5 0.5 0.005
// @param uFocalLength "Focal Length" 1.62 1.0 3.0 0.01
// @param uRaySteps "Raymarch Steps" 82.0 40.0 104.0 1.0
// @param uExposure "Exposure" 1.20 0.2 3.0 0.01
// @param uVignette "Vignette" 0.34 0.0 1.0 0.01
// @group Audio
// @param uAudioReact "Audio Reactivity" 0.65 0.0 2.0 0.01
// @param uBassPulse "Bass Inflation" 0.38 0.0 1.5 0.01
// @param uMidSpin "Mid Spin" 0.32 0.0 1.5 0.01
// @param uTrebleFlash "Treble Flash" 0.48 0.0 2.0 0.01

#define PI 3.14159265359
mat2 scRot(float a){float s=sin(a),c=cos(a);return mat2(c,-s,s,c);} vec3 scHsv(vec3 c){vec3 p=abs(fract(c.xxx+vec3(0,.666667,.333333))*6.-3.);return c.z*mix(vec3(1),clamp(p-1.,0.,1.),c.y);}
float scShape(vec3 p){float a=atan(p.z,p.x),r=length(p.xz),lob=max(2.,floor(uDetail+.5));float pulse=1.+sin(iTime*uPulseSpeed)*uPulse+iBass*uAudioReact*uBassPulse*.12;float rr=uScale+cos(a*lob+iTime*.12)*uWarp+sin(a*lob*2.)*uTwist*.08;float yy=sin(a*lob+iTime*.12)*uWarp;float d=length(vec2(r-rr,p.y-yy))-uThickness*pulse;float rr2=uScale+cos(a*lob+PI)*uWarp;float yy2=sin(a*lob+PI)*uWarp;float d2=length(vec2(r-rr2,p.y-yy2))-uThickness*.72;return mix(d,min(d,d2),uRoundness);}
vec2 scMap(vec3 p){p.xz=scRot(uTurn+iTime*(uSpin+iMid*uAudioReact*uMidSpin*.08))*p.xz;p.yz=scRot(uTilt)*p.yz;return vec2(scShape(p)*.72,1);}
vec3 scNormal(vec3 p){vec2 e=vec2(.0018,0);return normalize(vec3(scMap(p+e.xyy).x-scMap(p-e.xyy).x,scMap(p+e.yxy).x-scMap(p-e.yxy).x,scMap(p+e.yyx).x-scMap(p-e.yyx).x));}
vec2 scMarch(vec3 ro,vec3 rd){float d=0.,id=-1.,steps=max(40.,floor(uRaySteps+.5));for(int i=0;i<104;i++){if(float(i)>=steps)break;vec2 h=scMap(ro+rd*d);if(abs(h.x)<.0014){id=h.y;break;}d+=max(h.x*.72,.001);if(d>9.)break;}return vec2(d,id);}
vec3 scEnv(vec3 d){d.xz=scRot(uEnvRotation+iTime*uEnvSpin)*d.xz;vec3 e=mix(scHsv(vec3(uEnvHue,uEnvSat,.13*uEnvPower)),scHsv(vec3(fract(uEnvHue+.1),uEnvSat*.7,.78*uEnvPower)),pow(1.-abs(d.y),2.));float a=pow(max(dot(d,normalize(vec3(.62,.42,.66))),0.),28.),b=pow(max(dot(d,normalize(vec3(-.72,.18,.67))),0.),18.);float ring=exp(-pow((abs(d.y)-.32)*18.,2.));return e+(vec3(1,.88,.95)*a+vec3(.55,.76,1)*b)*uCardPower+scHsv(vec3(uNeonHue,.8,1))*ring*uNeonPower;}
void mainImage(out vec4 fragColor,in vec2 fragCoord){vec2 uv=(fragCoord-.5*iResolution.xy)/iResolution.y;float ca=uCameraOrbit+iTime*uOrbitSpeed;vec3 ro=vec3(sin(ca)*uCameraDistance,uCameraHeight,cos(ca)*uCameraDistance),ta=vec3(0),fw=normalize(ta-ro),rt=normalize(cross(fw,vec3(0,1,0))),up=cross(rt,fw),rd=normalize(fw*uFocalLength+rt*(uv.x-uOffsetX)+up*(uv.y-uOffsetY));vec3 col=scEnv(rd)*.13;vec2 hit=scMarch(ro,rd);if(hit.y>0.){vec3 p=ro+rd*hit.x,n=scNormal(p),v=-rd,l=normalize(vec3(-.6,.75,.5)),h=normalize(l+v);float ndv=max(dot(n,v),0.),fres=pow(1.-ndv,4.),ndl=max(dot(n,l),0.),spec=pow(max(dot(n,h),0.),mix(18.,280.,1.-uRoughness));vec3 base=scHsv(vec3(uHue,uSaturation,.62)),env=scEnv(normalize(mix(reflect(-v,n),n,uRoughness*uRoughness*.4))),refr=scEnv(refract(-v,n,1./uIOR));vec3 dielectric=base*(.12+.65*ndl)+env*uReflectivity*(.12+.88*fres),metal=env*mix(vec3(.8),base*1.8,.35)*uReflectivity;col=mix(dielectric,metal,uMetalness);col=mix(col,refr,clamp(uTransmission*(1.-fres)*.55,0.,1.));col+=vec3(1,.93,.98)*spec*(1.+uClearcoat)+(.52+.48*cos((1.-ndv)*13.+vec3(0,2.1,4.2)))*uIridescence*fres;col+=scHsv(vec3(uGlowHue,.8,1))*uGlow*(.08+.2*(1.-ndv));col+=vec3(1)*iTreble*uAudioReact*uTrebleFlash*spec*.5;}col*=uExposure*(1.-uVignette*smoothstep(.2,1.05,length(uv)));col=col/(1.+col*.58);fragColor=vec4(pow(max(col,0.),vec3(.8,.82,.86)),1);}
