Skip to content

Instantly share code, notes, and snippets.

@lunasorcery
Created November 7, 2018 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lunasorcery/3136d71da8a9315748cf2e8359b7b722 to your computer and use it in GitHub Desktop.
Save lunasorcery/3136d71da8a9315748cf2e8359b7b722 to your computer and use it in GitHub Desktop.
#version 410 core
uniform float fGlobalTime; // in seconds
uniform vec2 v2Resolution; // viewport resolution (in pixels)
uniform sampler1D texFFT; // towards 0.0 is bass / lower freq, towards 1.0 is higher / treble freq
uniform sampler1D texFFTSmoothed; // this one has longer falloff and less harsh transients
uniform sampler1D texFFTIntegrated; // this is continually increasing
uniform sampler2D texChecker;
uniform sampler2D texNoise;
uniform sampler2D texTex1;
uniform sampler2D texTex2;
uniform sampler2D texTex3;
uniform sampler2D texTex4;
layout(location = 0) out vec4 out_color; // out_color must be written in order to see anything
vec2 rotate(vec2 a, float b)
{
float c = cos(b);
float s = sin(b);
return vec2(
a.x * c - a.y * s,
a.x * s + a.y * c
);
}
float sdBox( vec3 p, vec3 b )
{
vec3 d = abs(p) - b;
return length(max(d,0.0)) + min(max(d.x,max(d.y,d.z)),0.0);
}
float sdBeveledBox(vec3 p, vec3 r, float R)
{
float a = sdBox(p,r);
p=abs(p);
float b = (p.x + p.y - r.x - r.y + R) / sqrt(2);
float c = (p.y + p.z - r.y - r.z + R) / sqrt(2);
float d = (p.z + p.x - r.z - r.x + R) / sqrt(2);
float e = (p.x + p.y + p.z - r.x - r.y - r.z + R + R) / sqrt(3);
return max(max(max(a,b),max(c,d)),e);
}
float sdCylinder(vec3 p, float r)
{
return length(p.zy)-r;
}
float sdCappedCylinder(vec3 p, float r, float h)
{
return max(sdCylinder(p,r),abs(p.x)-h);
}
float scene(vec3 p)
{
float box = sdBeveledBox(p, vec3(1.), .1);
p.xz=abs(p.xz);
p.xz=vec2(max(p.x,p.z),min(p.x,p.z));
float a = sdBeveledBox(p-vec3(0,.25,0), vec3(1.1,.5,.2), .1);
float b = sdBeveledBox(p+vec3(0,.6,0), vec3(1.1,.15,.2), .1);
p.y=abs(p.y);
float indent = sdBeveledBox(p-vec3(1.06,.7,.7),vec3(.1),.05);
indent = sdCappedCylinder(p-vec3(1.06,.7,.7),.07,.1);
return max(min(box,max(min(a,b),p.x-1.05)),-indent);
}
float scene2(vec3 p)
{
return -(length(p)-7.);
return -sdBox(p, vec3(7.));
}
float CAMERA_ELEVATION = 0.4;
float SPIN_RATE = .3;
float REFRACTIVITY = sin(fGlobalTime*.5)*.5+.5;
void cameraSpin(inout vec3 p)
{
p.yz = rotate(p.yz, CAMERA_ELEVATION);
p.xz = rotate(p.xz, fGlobalTime*SPIN_RATE);
}
void cameraSpinBackwards(inout vec3 p)
{
p.xz = rotate(p.xz, -fGlobalTime*SPIN_RATE);
p.yz = rotate(p.yz, -CAMERA_ELEVATION);
}
vec3 background(vec2 uv)
{
vec3 cam = vec3(0,0,-5);
vec3 dir = normalize(vec3(uv,1));
float t=0;
float k=0;
for(int i=0;i<100;++i)
{
k = scene2(cam+dir*t);
t+=k;
if (k<.001)
break;
}
vec3 h = cam+dir*t;
float T = length(sin(h*2.))/sqrt(3);
T=fract(T+fGlobalTime*.05);
T=step(.5,T);
vec3 output = vec3(.9)+T*.1-.2;
output -= smoothstep(0,1,length(uv))*.5;
return output;
}
void main(void)
{
vec2 uv = vec2(gl_FragCoord.x / v2Resolution.x, gl_FragCoord.y / v2Resolution.y);
uv -= 0.5;
uv /= vec2(v2Resolution.y / v2Resolution.x, 1);
vec3 cam = vec3(0,0,-10);
vec3 dir = normalize(vec3(uv,2.5));
vec3 fwd = vec3(0,0,1);
cameraSpin(cam);
cameraSpin(dir);
cameraSpin(fwd);
float t=0;
float k=0;
for(int i=0;i<100;++i)
{
k = scene(cam+dir*t);
t+=k;
if (k<.001)
break;
}
vec3 h = cam+dir*t;
vec2 o = vec2(.001, 0);
vec3 worldNormal = normalize(vec3(
scene(h+o.xyy)-scene(h-o.xyy),
scene(h+o.yxy)-scene(h-o.yxy),
scene(h+o.yyx)-scene(h-o.yyx)
));
vec3 eyeNormal = worldNormal;
cameraSpinBackwards(eyeNormal);
vec3 specular = vec3(0);
if (k < .001)
{
uv += reflect(vec3(0,0,1),eyeNormal).xy * REFRACTIVITY * .3;
specular += pow(1.-max(0,dot(-dir,worldNormal)),5.) * REFRACTIVITY * .5;
}
out_color.rgb = background(uv) + specular;
out_color.rgb = pow(out_color.rgb, vec3(1.2,1.1,1.0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment