Skip to content

Instantly share code, notes, and snippets.

@antoinefortin
Created September 8, 2021 05:29
Show Gist options
  • Select an option

  • Save antoinefortin/68483d7217bf84a96bf9ecb6d02eac3a to your computer and use it in GitHub Desktop.

Select an option

Save antoinefortin/68483d7217bf84a96bf9ecb6d02eac3a to your computer and use it in GitHub Desktop.
vec2 rotate(vec2 pos, float angle)
{
float c = cos(angle);
float s = sin(angle);
return mat2(c,s,-s,c) * pos;
}
mat2 Rot(float a) {
float s = sin(a);
float c = cos(a);
return mat2(c, -s, s, c);
}
vec3 R(vec2 uv, vec3 p, vec3 l, float z) {
vec3 f = normalize(l-p),
r = normalize(cross(vec3(0,1,0), f)),
u = cross(f,r),
c = p+f*z,
i = c + uv.x*r + uv.y*u,
d = normalize(i-p);
return d;
}
/* SDF */
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 sdSphere(vec3 pos, float r)
{
return length(pos) - r;
}
float sdPlane(vec3 pos)
{
return pos.y;
}
float sdHexPrism( vec3 p, vec2 h )
{
const vec3 k = vec3(-0.8660254, 0.5, 0.57735);
p = abs(p);
p.xy -= 2.0*min(dot(k.xy, p.xy), 0.0)*k.xy;
vec2 d = vec2(
length(p.xy-vec2(clamp(p.x,-k.z*h.x,k.z*h.x), h.x))*sign(p.y-h.x),
p.z-h.y );
return min(max(d.x,d.y),0.0) + length(max(d,0.0));
}
float sdBox( vec3 p, vec3 b )
{
vec3 q = abs(p) - b;
return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0);
}
vec2 opu(vec2 d1, vec2 d2)
{
return (d1.x < d2.x) ? d1 : d2;
}
float sdCone( in vec3 p, in vec2 c, float h )
{
// c is the sin/cos of the angle, h is height
// Alternatively pass q instead of (c,h),
// which is the point at the base in 2D
vec2 q = h*vec2(c.x/c.y,-1.0);
vec2 w = vec2( length(p.xz), p.y );
vec2 a = w - q*clamp( dot(w,q)/dot(q,q), 0.0, 1.0 );
vec2 b = w - q*vec2( clamp( w.x/q.x, 0.0, 1.0 ), 1.0 );
float k = sign( q.y );
float d = min(dot( a, a ),dot(b, b));
float s = max( k*(w.x*q.y-w.y*q.x),k*(w.y-q.y) );
return sqrt(d)*sign(s);
}
float h(vec2 p)
{
float ba = texture(iChannel1, p * 0.055).x ;
float b = (sin(p.x * 2.)) ;
b*= ba * sin( cos( 2.)) * abs(sin(1.) + 1.25);
b *= b - abs(sin(iTime));
return -b - 1.2;
}
float sdEllipsoid( vec3 p, vec3 r )
{
float k0 = length(p/r);
float k1 = length(p/(r*r));
return k0*(k0-1.0)/k1;
}
/* Scene */
vec2 map(vec3 pos)
{
float box = sdBox(pos, vec3(3., .25, 3.));
float sphere = sdSphere(pos + vec3(0,-1.5, 0.), 1.);
float sphere2 = sdSphere(pos + vec3(3.,-1.5, 0.), 1.);
vec2 res = vec2(box, 1.0 /*Object ID*/);
res = opu(res, vec2(sphere, 1.0 /*Object ID*/));
res = opu(res, vec2(sphere2, 2.0 /*Object ID*/));
// res = opu(res, vec2(arms, 3.));
return res;
}
vec2 castRay(vec3 ro, vec3 rd)
{
float t = 0.0;
float id = -20.;
float farClippingPlane = 120.0;
for(int i = 0; i < 256; i++)
{
vec3 pos = ro + t * rd;
float h = map(pos).x;
id = map(pos).y;
if(h < 0.001)
{
break;
}
t += h;
if(t > farClippingPlane) break;
}
if(t > farClippingPlane) t = -1.0;
return vec2(t, id);
}
vec3 calcNormal(vec3 pos)
{
vec2 e = vec2(0.01, 0.0);
return normalize(vec3(
map(pos+e.xyy).x - map(pos-e.xyy).x,
map(pos+e.yxy).x - map(pos-e.yxy).x,
map(pos+e.yyx).x - map(pos-e.yyx).x
));
}
void resetToZero(inout vec3 r)
{
r = vec3(0.0);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = (fragCoord-.5*iResolution.xy)/iResolution.y;
vec2 m = iMouse.xy/iResolution.xy;
vec3 ro = vec3(0., 3.55 ,-3.5);
//vec3 rd = normalize(vec3(uv.x, uv.y - .2,1.));
ro.yz *= Rot(-m.y+.4);
ro.xz *= Rot(m.x * 12.);
vec3 rd = R(uv, ro, vec3(0,0,0), .7);
// Cube map sampling
vec3 col = texture(iChannel0, rd).rgb;
float hitDistance = castRay(ro, rd).x;
float objectId = castRay(ro, rd).y;
if(hitDistance > 0.)
{
vec3 debugColor = vec3(1.);
vec3 pos = ro + hitDistance * rd;
vec3 normalWS = calcNormal(pos);
/* Global Lighting section */
// Sky
vec3 skyColor = vec3(.4, 0.75, .12);
//col = skyColFactor;
vec3 sundir = normalize(vec3(0.2, 0.4, 0.2));
float sundif = clamp(dot(normalWS, sundir), 0.0, 1.0);
float sun_sha = smoothstep(castRay(pos + normalWS * 0.001, sundir).x,0., 1.);
float sky_dif = clamp(dot(normalWS,vec3(0.0,1.0,0.0)), 0.0, 1.0);;
col = vec3(1.0, 0.7, 0.5) * sundif * sun_sha;
col += vec3(0.0, 0.2, 0.4) * sky_dif;
bool materialSystem = true;
vec3 lig = normalize(vec3(0., 5.7, -2.6));
if(materialSystem)
{
if(objectId == 2.0f)
{
vec3 rRO = ro;
vec3 rRD = rd;
for(int i = 0; i < 3; i++)
{
float t = castRay(rRO, rRD).x;
if(t > 0.)
{
vec3 rcol = vec3(0);
vec3 pos = rRO + rRD * t;
vec3 nor = calcNormal(pos);
vec3 ref = reflect(rRD, nor);
rRO = pos + nor;
vec3 diffuse = vec3(clamp(dot(lig, nor), 0.0, 1.0));
rcol += diffuse;
col = rcol;
}
}
}
}
col = col;
}
// Output to screen
fragColor = vec4(sqrt(col),1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment