Skip to content

Instantly share code, notes, and snippets.

@lunasorcery
Created September 12, 2018 21:23
Show Gist options
  • Save lunasorcery/8f020ed8403817eae07014c14f7bfb61 to your computer and use it in GitHub Desktop.
Save lunasorcery/8f020ed8403817eae07014c14f7bfb61 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
#define ORTHO 1
#define SPIN 0
#define iTime (fGlobalTime * 6.0)
#define iLoopedTime (mod(iTime, NUM * 2.))
#define iLoopedTime2 (mod(iTime / (NUM * 2.), 3.))
#define NUM 3
#define pi (acos(-1.))
#define tau (pi * 2.)
#define halfPi (pi * .5)
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 min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
}
float sdBox2( vec2 p, vec2 b )
{
vec2 d = abs(p) - b;
return min(max(d.x,d.y),0.0) + length(max(d,0.0));
}
float sdSpinningBox(vec3 p)
{
if (iLoopedTime < NUM)
{
p.x -= NUM;
p.z -= floor(iLoopedTime);
p.zy = rotate(p.zy, fract(iTime)*halfPi);
}
else
{
p.x += floor(iLoopedTime-NUM-NUM+1);
p.z -= NUM;
p.xy = rotate(p.xy, (1.-fract(iTime))*halfPi);
}
return sdBox(p+vec3(.5,-.5,.5), vec3(.4)) - .1;
}
float sdFloor(vec3 p)
{
return sdBox(p+vec3(-NUM,NUM,-NUM)*.5, vec3(NUM * .5));
}
float scene(vec3 p)
{
return min(
sdFloor(p),
sdSpinningBox(p)
);
}
float cellMask(vec2 p)
{
return smoothstep(.0, -.1, sdBox2(p-.5,vec2(.37)) - .1 );
}
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,-5);
vec3 dir = normalize(vec3(uv,1));
#if ORTHO
cam = vec3(uv*7.,-5);
dir = vec3(0,0,1);
#endif
dir.xy = rotate(dir.xy, -floor(iLoopedTime2) * tau / 3);
cam.xy = rotate(cam.xy, -floor(iLoopedTime2) * tau / 3);
#if SPIN
dir.xy = rotate(dir.xy, (iLoopedTime2 - .5) * tau / 3);
cam.xy = rotate(cam.xy, (iLoopedTime2 - .5) * tau / 3);
#endif
cam.yz = rotate(cam.yz, atan(1,sqrt(2)));
dir.yz = rotate(dir.yz, atan(1,sqrt(2)));
cam.xz = rotate(cam.xz, -pi/4);
dir.xz = rotate(dir.xz, -pi/4);
float t = 0;
for(int i=0;i<100;++i)
{
t += scene(cam+dir*t);
}
vec3 h = cam+dir*t;
vec2 o = vec2(.01,0);
vec3 n = 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)
));
bool isCube = h.y > .001;
if (isCube)
{
n.xz = -n.xz;
if (iLoopedTime2 >= 2.)
{
n = n.zxy;
}
else if (iLoopedTime2 >= 1.)
{
n = n.yzx;
}
n.xz = -n.xz;
out_color = vec4(n*.5+.5, 1);
vec3 light = normalize(vec3(-4,5,-2));
out_color = vec4(pow(dot(n,light), 4.)) + vec4(.15,.1,.4,1);
}
else
{
float side = 0;
if (n.x < -.5)
{
h = h.zxy;
h.z = -h.z;
side = 1;
}
if (n.z < -.5)
{
h = h.yzx;
h.x = -h.x;
side = 2;
}
ivec2 cell = ivec2(floor(h.xz));
if (max(cell.x,cell.y)==NUM-1 && cell.x >= 0 && cell.y >= 0)
{
float mask = cellMask(fract(h.xz));
float intensity = NUM + cell.y - cell.x;
intensity /= (NUM*2);
intensity = (side+intensity) / 3;
intensity = fract(intensity - fract(iLoopedTime2) / 3);
intensity = intensity * intensity * 3.5;
out_color = vec4(fract(h.xz), 0, 1);
out_color = vec4(mask * intensity) * vec4(.15,.1,.4,1);
}
else
{
out_color = vec4(0);
}
//out_color = vec4(fract(h.xz), 0, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment