Skip to content

Instantly share code, notes, and snippets.

@shinmai
Forked from caiwan/distantfunctions.inc.glsl
Created November 13, 2017 21:22
Show Gist options
  • Save shinmai/62c4c47e180c45e16adf4b1f76688146 to your computer and use it in GitHub Desktop.
Save shinmai/62c4c47e180c45e16adf4b1f76688146 to your computer and use it in GitHub Desktop.
Raymarch stuff
//http://iquilezles.org/www/articles/distfunctions/distfunctions.htm
// -- Distant functions
float sdPlane( vec3 p ) {return p.y+0.2;}
float sdSphere( vec3 p, float s ) {return length(p)-s;}
float sdCylinder( vec3 p, vec3 c ){ return length(p.xz-c.xy)-c.z; }
float udRoundBox( vec3 p, vec3 b, float r ){return length(max(abs(p)-b,0.0))-r;}
float udRoundBoxInf( vec3 p, vec2 b, float r ){return length(max(vec3(abs(p.xz)-b,0.0),0.0))-r;}
float maxcomp( vec3 p ) {return max(p.x,max(p.y,p.z));}
float length2( vec2 p ) {return sqrt( p.x*p.x + p.y*p.y );}
float length6( vec2 p ) {p = p*p*p; p = p*p; return pow( p.x + p.y, 1.0/6.0 );}
float length8( vec2 p ) {p = p*p; p = p*p; p = p*p; return pow( p.x + p.y, 1.0/8.0 );}
float helix (vec2 p ) {float a=atan(p.y,p.x)*0.1, b=mod(p.z,0.6283)-0.314159; a=abs(a-b); if(a>0.314159)a=0.6283-a; return length(vec2(length(p.xy)-1.0,a))-0.15;}
vec2 sdSegment (vec3 p, in vec3 a, in vec3 b ) {vec3 pa = p - a; vec3 ba = b - a; float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); return vec2( length( pa - ba*h ), h );}
float sdHexPrism( vec3 p, vec2 h ) {vec3 q = abs(p); return max(q.z-h.y,max((q.x*0.866025+q.y*0.5),q.y)-h.x);}
float sdCross (vec3 p ) { return min(maxcomp(abs(p.xy)),min(maxcomp(abs(p.yz)),maxcomp(abs(p.zx))))-1.0;}
float sdCapsule( vec3 p, vec3 a, vec3 b, float r ){ vec3 pa = p - a, ba = b - a; float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); return length( pa - ba*h ) - r;}
float sdCappedCylinder( vec3 p, vec2 h ) { vec2 d = abs(vec2(length(p.xz),p.y)) - h; return min(max(d.x,d.y),0.0) + length(max(d,0.0)); }
// c must be normalized
float sdCone( vec3 p, vec2 c ){return dot(c,vec2(length(p.xy),p.z));}
float sdCappedCone( in vec3 p, in vec3 c ){vec2 q = vec2( length(p.xz), p.y ), v = vec2( c.z*c.y/c.x, -c.z ), w = v - q, vv = vec2( dot(v,v), v.x*v.x ), qv = vec2( dot(v,w), v.x*w.x ), d = max(qv,0.0)*qv/vv; return sqrt( dot(w,w) - max(d.x,d.y) )* sign(max(q.y*v.x-q.x*v.y,w.y));}
/**
* Prototype of the map
* @param pos ray position
* @param color hit record
*/
float map(in vec3 pos, out vec3 color);
/**
* Finds the intersection point
@param ro ray origin
@param rd ray direction
@param color hit record (diffuse color)
*/
float intersect(in vec3 ro, in vec3 rd, out vec3 color)
{
float h = MIN_PRECISSION*2.0;
float t = 0.0;
color = vec3(0.);
for( int i=0; i<MAX_ITERATION; i++ )
{
if( abs(h)<MIN_PRECISSION||t>MAX_DEPTH ) break;
t += h;
h = map( ro+rd*t, color );
}
return t;
}
vec4 map( in vec3 p )
{
float d = sdBox(p,vec3(1.0));
vec4 res = vec4( d, 1.0, 0.0, 0.0 );
float ani = smoothstep( -0.2, 0.2, -cos(0.5*iGlobalTime) );
float off = 1.5*sin( 0.01*iGlobalTime );
float s = 1.0;
for( int m=0; m<4; m++ )
{
p = mix( p, ma*(p+off), ani );
vec3 a = mod( p*s, 2.0 )-1.0;
s *= 3.0;
vec3 r = abs(1.0 - 3.0*abs(a));
float da = max(r.x,r.y);
float db = max(r.y,r.z);
float dc = max(r.z,r.x);
float c = (min(da,min(db,dc))-1.0)/s;
if( c>d )
{
d = c;
res = vec4( d, min(res.y,0.2*da*db*dc), (1.0+float(m))/4.0, 0.0 );
}
}
return res;
}
// --- operations
float opS (float d1, float d2 ) { return max(-d2,d1); }
vec2 opS (vec2 d1, vec2 d2) {return (d1.x>-d2.x) ? d1 : d2;}
float opU (float d1, float d2 ) {return min(d1, d2); }
vec2 opU (vec2 d1, vec2 d2) {return (d1.x<d2.x) ? d1 : d2;}
vec3 opRep (vec3 p, vec3 c ) { return mod(p,c)-0.5*c; }
//vec3 opTwist( vec3 p ){float c = cos(10.0*p.y+10.0), s = sin(10.0*p.y+10.0); mat2 m = mat2(c,-s,s,c); return vec3(m*p.xz,p.y);}
//vec3 opTwist( vec3 p, float tc, float ts ){float c = cos(tc*p.y+ts), s = sin(tc*p.y+ts);mat2 m = mat2(c,-s,s,c); return vec3(m*p.xz,p.y);}
//vec3 opTwist2( vec3 p, float tc, float ts, float cc){float c = cos(cc*tc*p.y+ts), s=sin(cc*tc*p.y+ts); mat2 m = mat2(c,-s,s,c); return vec3(m*p.xz,p.y);}
//vec3 opTwist3( vec3 p, float tc, float ts, float cc){float c = cos(sin(tc*p.y)*cc*tc*p.y+ts), s = sin(sin(tc*p.y)*cc*tc*p.y+ts); mat2 m = mat2(c,-s,s,c); return vec3(m*p.xz,p.y);}
vec2 sincos( float x ){return vec2(sin(x), cos(x));}
// exponential smooth min (k = 32);
float smin_exp( float a, float b, float k ) {return -log( exp( -k*a ) + exp( -k*b ); )/k;}
// polynomial smooth min (k = 0.1);
float smin_poly( float a, float b, float k ) {float h = clamp( 0.5+0.5*(b-a)/k, 0.0, 1.0 ); return mix( b, a, h ) - k*h*(1.0-h);}
// power smooth min (k = 8);
float smin_pow( float a, float b, float k ) { a = pow( a, k ); b = pow( b, k ); return pow( (a*b)/(a+b), 1.0/k ); }
// --- transformations
vec2 angularRepeat(const float a, in vec2 v) { float an = atan(v.y,v.x); float len = length(v); an = mod(an+a*.5,a)-a*.5; return vec2(cos(an),sin(an))*len;}
vec3 rotate3D(float phi, vec3 v, vec3 p){ vec2 sc = sincos(phi); return mat3( v.x*v.x*(1.-sc.y)+sc.y, v.y*v.x*(1.-sc.y)+v.z*sc.x, v.z*v.x*(1.-sc.y)-v.y*sc.x, v.x*v.y*(1.-sc.y)-v.z*sc.x, v.y*v.y*(1.-sc.y)+sc.y, v.z*v.y*(1.-sc.y)+v.x*sc.x, v.x*v.z*(1.-sc.y)+v.y*sc.x, v.y*v.z*(1.-sc.y)-v.x*sc.x, v.z*v.z*(1.-sc.y)+sc.y) * p;}
#define MAX_DEPTH 60.
#define MAX_ITERATION 64
#define MIN_PRECISSION 0.0001
/**
* Prototype of the map
* @param pos ray position
* @param color hit record
*/
float map(in vec3 pos, out vec3 color);
/**
*/
vec3 calcNormal( in vec3 pos )
{
vec3 c;
vec3 eps = vec3( 0.001, 0.0, 0.0 );
vec3 nor = vec3(
map(pos+eps.xyy, c) - map(pos-eps.xyy, c),
map(pos+eps.yxy, c) - map(pos-eps.yxy, c),
map(pos+eps.yyx, c) - map(pos-eps.yyx, c));
return normalize(nor);
}
/**
*/
float shadow( in vec3 ro, in vec3 rd, float mint, float maxt )
{
vec3 c;
for( float t=mint; t < maxt; )
{
float h = map(ro + rd*t, c);
if( h<0.001 )
return 0.0;
t += h;
}
return 1.0;
}
/**
*/
float softshadow( in vec3 ro, in vec3 rd, in float mint, in float maxt, in float k )
{
float res = 1.0;
float dt = 0.02;
float t = mint;
vec3 c;
for( int i=0; i<20; i++ )
{
if( t<maxt )
{
float h = map( ro + rd*t, c );
res = min( res, k*h/t );
t += 0.02;
}
}
return clamp( res, 0.0, 1.0 );
}
/**
*/
float calcAO( in vec3 pos, in vec3 nor )
{
float totao = 0.0;
float sca = 1.0;
vec3 c;
for( int aoi=0; aoi<5; aoi++ )
{
float hr = 0.01 + 0.05*float(aoi);
vec3 aopos = nor * hr + pos;
float dd = map(aopos, c);
totao += -(dd-hr)*sca;
sca *= 0.75;
}
return clamp( 1.0 - 4.0*totao, 0.0, 1.0 );
}
/**--------------------------------------------------
Camera
*/
vec2 pos = -1.0+2.0*(fragCoord.xy/iResolution.xy);
pos.x *= iResolution.x / iResolution.y;
// camera
vec3 eye = vec3( 0.0, 0.5 , 1.5); // szemkoordinata (ray orign)
vec3 center = vec3( 0.0, 0.0, 0.0 ); // referenciapont
// camera tx
vec3 cw = normalize(center-eye); // szem -> ref.pont = nezet irany
vec3 cp = vec3( 0.0, 1.0, 0.0 ); // "felfele" vektor
vec3 cu = normalize( cross(cw,cp) ); // nezet irany X felfele mutato vektor = up iranyvektor
vec3 cv = normalize( cross(cu,cw) ); // up X nezet irany
vec3 rayDir = normalize( pos.x*cu + pos.y*cv + 2.5*cw ); // sugar irany (ray dir.)
/**--------------------------------------------------
Frensel with chomatic dispersion
*/
// Parameter for RGB
vec3 eta = vec3(.005, .017, .012);
// ---
float rc = fb + fs * pow(1.+dot(vpos, nor),fp);
vec3 vl = reflect(vpos, nor);
mat3 vr = mat3(refract(vpos, nor, eta.r), refract(vpos, nor, eta.g),refract(vpos, nor, eta.b));
vec3 cl = textureCube(iChannel0, vl).rgb;
vec3 cr = vec3(textureCube(iChannel0, vr[0]).r,textureCube(iChannel0, vr[1]).g,textureCube(iChannel0, vr[2]).b);
vec3 cf = mix(cr, cl, clamp(rc,0.,1.));
//--
// panoramic projection by aiming rays using angles
vec3 ray=normalize(camS*sin(uv.x*3.5) + camF*cos(uv.x*3.5) + camU*uv.y*3.14);
// --
// fisheye
#ifndef HALSZEM_KI
float a = .5+3.*(1.-dot(cw, rayDir));
vec3 rayDir_fisheye = normalize( a*pos.x*cu + a*pos.y*cv + 2.5*cw );// sugar irany (ray dir.)
rayDir = rayDir_fisheye;
#endif /*HALSZEM_KI*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment