yum-archive/SoggyShaders
Old Unity shaders.
git clone https://git.yummers.dev/yum-archive/SoggyShaders
8427cb4
master
1#ifndef __IQ_SDF_INC__ 2#define __IQ_SDF_INC__ 3 4#include "pema99.cginc" 5 6// The MIT License 7// Copyright © 2020 Inigo Quilez 8// 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. 9 10float distance_from_octahedron(in float3 p) 11{ 12 float s = 1.0; 13 float3 pp = abs(p); 14 float m = pp.x+pp.y+pp.z-s; 15 float3 q; 16 if( 3.0*pp.x < m ) q = pp.xyz; 17 else if( 3.0*pp.y < m ) q = pp.yzx; 18 else if( 3.0*pp.z < m ) q = pp.zxy; 19 else return m*0.57735027; 20 21 float k = clamp(0.5*(q.z-q.y+s),0.0,s); 22 return length(float3(q.x,q.y-s+k,q.z-k)); 23} 24 25float distance_from_sphere(float3 p) 26{ 27 return length(p); 28} 29 30float distance_from_sphere(float3 p, float3 c, float r) 31{ 32 return length(p - c) - r; 33} 34 35float distance_from_cut_sphere( in float3 p, in float r, in float h ) 36{ 37 float w = sqrt(r*r-h*h); // constant for a given shape 38 39 float2 q = float2( length(p.xz), p.y ); 40 41 float s = max( (h-r)*q.x*q.x+w*w*(h+r-2.0*q.y), h*q.x-w*q.y ); 42 43 return (s<0.0) ? length(q)-r : 44 (q.x<w) ? h - q.y : 45 length(q-float2(w,h)); 46} 47 48float distance_from_cut_hollow_sphere( float3 p, float r, float h, float t ) 49{ 50 float2 q = float2( length(p.xz), p.y ); 51 52 float w = sqrt(r*r-h*h); 53 54 return ((h*q.x<w*q.y) ? length(q-float2(w,h)) : 55 abs(length(q)-r) ) - t; 56} 57 58float distance_from_box(float3 p, float3 b) 59{ 60 float3 q = abs(p) - b; 61 return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0); 62} 63 64float distance_from_box_frame(float3 p, float3 b, float e) 65{ 66 p = abs(p)-b; 67 float3 q = abs(p+e)-e; 68 69 return min(min( 70 length(max(float3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0), 71 length(max(float3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)), 72 length(max(float3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0)); 73} 74 75float distance_from_pyramid(float3 p, float h, bool invert) 76{ 77 float m2 = h*h + 0.25; 78 79 // symmetry 80 p.xz = abs(p.xz); // do p=abs(p) instead for double pyramid 81 p.xz = (p.z>p.x) ? p.zx : p.xz; 82 p.xz -= 0.5; 83 84 // project into face plane (2D) 85 float3 q = float3( p.z, h*p.y-0.5*p.x, h*p.x+0.5*p.y); 86 87 float s = max(-q.x,0.0); 88 float t = clamp( (q.y-0.5*q.x)/(m2+0.25), 0.0, 1.0 ); 89 90 float a = m2*(q.x+s)*(q.x+s) + q.y*q.y; 91 float b = m2*(q.x+0.5*t)*(q.x+0.5*t) + (q.y-m2*t)*(q.y-m2*t); 92 93 float d2 = max(-q.y,q.x*m2+q.y*0.5) < 0.0 ? 0.0 : min(a,b); 94 95 // recover 3D and scale, and add sign 96 return sqrt( (d2+q.z*q.z)/m2 ) * sign(max(q.z,-p.y));; 97} 98 99float distance_from_plane(float3 p, float3 n, float h) 100{ 101 // n must be normalized 102 return dot(p,n) + h; 103} 104 105float distance_from_torus( float3 p, float2 t ) 106{ 107 float2 q = float2(length(p.xz)-t.x,p.y); 108 return length(q)-t.y; 109} 110 111float distance_from_capped_torus( float3 p, float2 sc, float ra, float rb) 112{ 113 p.x = abs(p.x); 114 float k = (sc.y*p.x>sc.x*p.y) ? dot(p.xy,sc) : length(p.xy); 115 return sqrt( dot(p,p) + ra*ra - 2.0*ra*k ) - rb; 116} 117 118float3 op_rep(in float3 p, in float3 c) 119{ 120 return glsl_mod(p+0.5*c,c)-0.5*c; 121} 122 123float smoothstep_cubic(float x) 124{ 125 return x * x * (3.0 - 2.0 * x); 126} 127 128float smoothstep_quintic(float x) 129{ 130 return x*x*x*(x*(x*6.0-15.0)+10.0); 131} 132 133float distance_from_line_segment( float3 p, float3 a, float3 b, float r ) 134{ 135 float3 pa = p - a, ba = b - a; 136 float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); 137 return length( pa - ba*h ) - r; 138} 139 140// End licensed section 141 142#endif // __IQ_SDF_INC__ 143