summaryrefslogtreecommitdiffstats
path: root/iq_sdf.cginc
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2024-04-22 16:02:38 -0700
committeryum <yum.food.vr@gmail.com>2024-04-22 16:02:38 -0700
commitdcb15c0d7425f2b3ede070cd97ac8f1bd8386df1 (patch)
treee17e5b620e0784226b6d24acce43c8bbcee22f1f /iq_sdf.cginc
Initial commit
Code bomb. Put tooner in version control.
Diffstat (limited to 'iq_sdf.cginc')
-rw-r--r--iq_sdf.cginc102
1 files changed, 102 insertions, 0 deletions
diff --git a/iq_sdf.cginc b/iq_sdf.cginc
new file mode 100644
index 0000000..472256c
--- /dev/null
+++ b/iq_sdf.cginc
@@ -0,0 +1,102 @@
+#include "pema99.cginc"
+
+// The MIT License
+// Copyright © 2019-2021 Inigo Quilez
+// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+float distance_from_octahedron(in float3 p)
+{
+ float s = 1.0;
+ float3 pp = abs(p);
+ float m = pp.x+pp.y+pp.z-s;
+ float3 q;
+ if( 3.0*pp.x < m ) q = pp.xyz;
+ else if( 3.0*pp.y < m ) q = pp.yzx;
+ else if( 3.0*pp.z < m ) q = pp.zxy;
+ else return m*0.57735027;
+
+ float k = clamp(0.5*(q.z-q.y+s),0.0,s);
+ return length(float3(q.x,q.y-s+k,q.z-k));
+}
+
+float distance_from_sphere(float3 p, float r)
+{
+ return length(p) - r;
+}
+
+float distance_from_cut_sphere( in float3 p, in float r, in float h )
+{
+ float w = sqrt(r*r-h*h); // constant for a given shape
+
+ float2 q = float2( length(p.xz), p.y );
+
+ float s = max( (h-r)*q.x*q.x+w*w*(h+r-2.0*q.y), h*q.x-w*q.y );
+
+ return (s<0.0) ? length(q)-r :
+ (q.x<w) ? h - q.y :
+ length(q-float2(w,h));
+}
+
+float distance_from_cut_hollow_sphere( float3 p, float r, float h, float t )
+{
+ float2 q = float2( length(p.xz), p.y );
+
+ float w = sqrt(r*r-h*h);
+
+ return ((h*q.x<w*q.y) ? length(q-float2(w,h)) :
+ abs(length(q)-r) ) - t;
+}
+
+float distance_from_box(float3 p, float3 b)
+{
+ float3 q = abs(p) - b;
+ return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0);
+}
+
+float distance_from_box_frame(float3 p, float3 b, float e)
+{
+ p = abs(p)-b;
+ float3 q = abs(p+e)-e;
+
+ return min(min(
+ length(max(float3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0),
+ length(max(float3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)),
+ length(max(float3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0));
+}
+
+float distance_from_pyramid(float3 p, float h, bool invert)
+{
+ float m2 = h*h + 0.25;
+
+ // symmetry
+ p.xz = abs(p.xz); // do p=abs(p) instead for double pyramid
+ p.xz = (p.z>p.x) ? p.zx : p.xz;
+ p.xz -= 0.5;
+
+ // project into face plane (2D)
+ float3 q = float3( p.z, h*p.y-0.5*p.x, h*p.x+0.5*p.y);
+
+ float s = max(-q.x,0.0);
+ float t = clamp( (q.y-0.5*q.x)/(m2+0.25), 0.0, 1.0 );
+
+ float a = m2*(q.x+s)*(q.x+s) + q.y*q.y;
+ float b = m2*(q.x+0.5*t)*(q.x+0.5*t) + (q.y-m2*t)*(q.y-m2*t);
+
+ float d2 = max(-q.y,q.x*m2+q.y*0.5) < 0.0 ? 0.0 : min(a,b);
+
+ // recover 3D and scale, and add sign
+ return sqrt( (d2+q.z*q.z)/m2 ) * sign(max(q.z,-p.y));;
+}
+
+float distance_from_plane(float3 p, float3 n, float h)
+{
+ // n must be normalized
+ return dot(p,n) + h;
+}
+
+float3 op_rep(in float3 p, in float3 c)
+{
+ return glsl_mod(p+0.5*c,c)-0.5*c;
+}
+
+// End licensed section