Skip to content

Instantly share code, notes, and snippets.

@lunasorcery
Created October 3, 2018 21:53
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/b2f315e2e0ae582127a6d04843835d5e to your computer and use it in GitHub Desktop.
Save lunasorcery/b2f315e2e0ae582127a6d04843835d5e to your computer and use it in GitHub Desktop.
#version 410 core
uniform float fGlobalTime; // in seconds
uniform vec2 v2Resolution; // viewport resolution (in pixels)
layout(location = 0) out vec4 out_color; // out_color must be written in order to see anything
#define iTime fGlobalTime
const float FONT_SCALE = 2;
const float POINT_SIZE = 1.5;
const float LINE_SIZE = 1.;
const float FOV = 2.;
const int NUM_POINTS = 128;
const float SPEED = 1;
float noise(float a)
{
return fract(sin(a)*43758.5453123);
}
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
);
}
void spin(inout vec3 p)
{
p.yz = rotate(p.yz, sin(iTime*.01)*.1+.05);
p.xz = rotate(p.xz, iTime * .1);
}
mat4 projMtx()
{
float aspect = v2Resolution.x / v2Resolution.y;
float nearz = 0.1;
float farz = 100;
return mat4(
1/(aspect*tan(FOV/2)), 0,0,0,
0,-1/tan(FOV/2),0,0,
0,0,(-nearz-farz)/(nearz-farz),(2*farz*nearz)/(nearz-farz),
0,0,1,0
);
}
vec3 cameraPos()
{
vec3 pos = vec3(0,0,-2.);
spin(pos);
return pos;
}
vec3 rayDir()
{
vec2 uv = (gl_FragCoord.xy/v2Resolution.xy)-.5;
uv.x *= v2Resolution.x / v2Resolution.y;
uv *= 2;
vec3 dir = normalize(vec3(
uv,(4.8/tan(FOV/2)) // where's the 4.8 from?!??!?!?!?!
));
spin(dir);
return dir;
}
mat4 cameraMtx()
{
vec3 pos = cameraPos();
vec3 dir = vec3(0,0,1);
spin(dir);
vec3 up = vec3(0,1,0);
vec3 right = cross(dir, up);
up = cross(right, dir);
mat4 orientation = mat4(
right.x, up.x, dir.x, 0,
right.y, up.y, dir.y, 0,
right.z, up.z, dir.z, 0,
0,0,0,1
);
mat4 translation = mat4(
1,0,0,0,
0,1,0,0,
0,0,1,0,
-pos.x, -pos.y, -pos.z, 1
);
return orientation * translation;
}
vec2 project(vec3 p3, mat4 mtx)
{
vec4 p = mtx * vec4(p3,1);
p /= p.w;
return (p.xy*.5+.5)*v2Resolution.xy;
}
void drawLine2D(inout vec4 fragColor, vec2 a, vec2 b, vec4 color)
{
a-=.5;
b-=.5;
vec2 midpoint = (a+b)*.5;
float radius = distance(a,b)*.5;
vec2 normal = normalize((b-a).yx*vec2(-1,1));
float d = dot(normal, gl_FragCoord.xy-midpoint);
fragColor += (clamp(LINE_SIZE-abs(d),0,1) * smoothstep(radius+.5,radius-.5,distance(gl_FragCoord.xy,midpoint))) * color;
}
void drawLine3D(inout vec4 fragColor, vec3 a, vec3 b, vec4 color, mat4 mtx)
{
vec2 pa = project(a, mtx);
vec2 pb = project(b, mtx);
drawLine2D(fragColor, pa, pb, color);
}
void drawPoint2D(inout vec4 fragColor, vec2 a, vec4 color)
{
a-=.5;
float d = distance(a,gl_FragCoord.xy);
fragColor += smoothstep(POINT_SIZE,POINT_SIZE-1,d) * color;
}
void drawPoint3D(inout vec4 fragColor, vec3 a, vec4 color, mat4 mtx)
{
drawPoint2D(fragColor,project(a,mtx),color);
}
void drawCrosshair3D(inout vec4 fragColor, vec3 a, vec4 color, mat4 mtx)
{
vec2 o = vec2(.007, 0);
drawLine3D(fragColor, a+o.xyy, a-o.xyy, color, mtx);
drawLine3D(fragColor, a+o.yxy, a-o.yxy, color, mtx);
drawLine3D(fragColor, a+o.yyx, a-o.yyx, color, mtx);
}
void drawDotGrid3D(inout vec4 fragColor, float y, vec4 color)
{
vec3 camPos = cameraPos();
vec3 rayDir = rayDir();
float distGrid = (camPos.y - y) / -rayDir.y;
vec3 hitGrid = abs(camPos + rayDir * distGrid);
if (hitGrid.x <= 1. && hitGrid.z <= 1.)
{
vec2 gridUv = fract(hitGrid.xz * 16.);
float dotSize = distGrid*.07; //length(dFdy(hitGrid.xz * 16.)) * 2.;
fragColor += smoothstep(dotSize,0.,length(gridUv-.5)) * color;
}
}
void drawBarcodeGrid3D(inout vec4 fragColor, float y)
{
vec3 camPos = cameraPos();
vec3 rayDir = rayDir();
float distGrid = (camPos.y - y) / -rayDir.y;
vec3 hitGrid = camPos + rayDir * distGrid;
vec2 absHitGrid = abs(hitGrid.xz);
if (absHitGrid.x <= 1. && absHitGrid.y <= 1.)
{
vec2 gridUv = hitGrid.xz*.5+.5;
float speed = .3;
gridUv.x += iTime*speed;
float mask = 1.-step(1./32,mod(gridUv.x,4.));
float barcode = step(.5, fract(iTime*2.+noise(floor(gridUv.y * 256.)+.1*floor(gridUv.x*64.))));
fragColor += mask * barcode * .5;
}
}
vec3 starPosition(int i)
{
float f = float(i);
vec3 p = vec3(
noise(f),
noise(f+.1),
noise(f+.2)
);
p=p*2-1;
p/=sqrt(3);
p*=pow(length(p), 2.3);
return clamp(p*1.2,-1,1);
}
void textWriter(inout vec4 fragColor, ivec2 p, int digit)
{
p--;
int[10]font=int[10](
0x75557,
0x23227,
0x74717,
0x74747,
0x55744,
0x71747,
0x71757,
0x74444,
0x75757,
0x75747
);
if (p.x<0||p.y<0||p.x>=3||p.y>=5) return;
fragColor += float((font[digit]>>(p.x+p.y*4))&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);
out_color = vec4(0);
mat4 mtx = projMtx() * cameraMtx();
float time = iTime * SPEED;
int currentPointIdx = int(floor(time)) % NUM_POINTS;
int nextPointIdx = (currentPointIdx+1) % NUM_POINTS;
float timeBetweenPoints = fract(time);
vec3 currentPoint = starPosition(currentPointIdx);
vec3 nextPoint = starPosition(nextPointIdx);
vec3 lerpPoint = mix(currentPoint, nextPoint, timeBetweenPoints < .5 ? 0 : timeBetweenPoints*2-1);
for(int i=0;i<NUM_POINTS;++i)
{
drawCrosshair3D(out_color, starPosition(i), i <= currentPointIdx ? vec4(1,0,0,1) : vec4(1), mtx);
}
drawLine3D(out_color, vec3(lerpPoint.x,lerpPoint.y,-1), vec3(lerpPoint.x,lerpPoint.y,1), vec4(1), mtx);
drawLine3D(out_color, vec3(-1,lerpPoint.y,lerpPoint.z), vec3(1,lerpPoint.y,lerpPoint.z), vec4(1), mtx);
drawLine3D(out_color, vec3(-1,lerpPoint.y,-1), vec3(1,lerpPoint.y,-1), vec4(1), mtx);
drawLine3D(out_color, vec3(1,lerpPoint.y,-1), vec3(1,lerpPoint.y,1), vec4(1), mtx);
drawLine3D(out_color, vec3(1,lerpPoint.y,1), vec3(-1,lerpPoint.y,1), vec4(1), mtx);
drawLine3D(out_color, vec3(-1,lerpPoint.y,1), vec3(-1,lerpPoint.y,-1), vec4(1), mtx);
if (timeBetweenPoints < .5)
{
drawLine3D(out_color, lerpPoint, vec3(lerpPoint.x, 0, lerpPoint.z), vec4(1), mtx);
vec2 screenPoint = project(lerpPoint, mtx);
for(int x=0;x<7;++x)
textWriter(out_color, ivec2((gl_FragCoord.xy-4-screenPoint)/FONT_SCALE-vec2(x*4,0)), int(noise(x+7*currentPointIdx)*10));
}
float gridHeight = (1 - fract(time / 8) * 2);
drawDotGrid3D(out_color, gridHeight, vec4(1.-abs(gridHeight)) * .5);
if (gridHeight < 0)
drawDotGrid3D(out_color, 0, vec4(1.+gridHeight) * .5);
drawBarcodeGrid3D(out_color, lerpPoint.y);
out_color = clamp(out_color, 0, 1);
out_color = pow(out_color, vec4(1.3,1.15,1,1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment