Skip to content

Instantly share code, notes, and snippets.

@gam0022
Last active May 8, 2022 14:11
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 gam0022/23fc2128753495f88b6824e1dd134168 to your computer and use it in GitHub Desktop.
Save gam0022/23fc2128753495f88b6824e1dd134168 to your computer and use it in GitHub Desktop.
#version 150
uniform float time;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 spectrum;
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D texture3;
uniform sampler2D prevFrame;
uniform sampler2D prevPass;
uniform sampler2D midi1;
in VertexData
{
vec4 v_position;
vec3 v_normal;
vec2 v_texcoord;
} inData;
out vec4 fragColor;
/*
void main(void)
{
vec2 uv = -1. + 2. * inData.v_texcoord;
fragColor = vec4(
abs(sin(cos(time+3.*uv.y)*2.*uv.x+time)),
abs(cos(sin(time+2.*uv.x)*3.*uv.y+time)),
spectrum.x * 100.,
1.0);
}
*/
// Original Bonzomatic Shader
// https://gist.github.com/gam0022/6332d497d886bdf9d3dbd714b88d852c
#define fragCoord (inData.v_texcoord * resolution.xy)
#define iResolution resolution
#define PI acos(-1.)
#define TAU (2. * PI)
#define saturate(x) clamp(x, 0., 1.)
float sdBox(vec3 p, vec3 b) {
vec3 q = abs(p) - b;
return length(max(q, 0.)) + min(0., max(q.x, max(q.y, q.z)));
}
void U(inout vec4 m, float d, float a, float b, float c) {
if (d < m.x) m = vec4(d, a, b, c);
}
ivec2 midiCoord(int offset)
{
int x = offset % 32;
int y = offset / 32;
return ivec2(x,y);
}
float midi(int ccNumber) {
return texture(midi1, vec2((1./32.) * midiCoord(3 * 127 + ccNumber))).r;
}
void rot(inout vec2 p, float a) { p *= mat2(cos(a), sin(a), -sin(a), cos(a)); }
vec4 map(vec3 p) {
vec3 pos = p;
p = mod(p, 1.) - 0.5;
vec4 m = vec4(1, 1, 1, 1);
float s = 1.;
for (int i = 0; i < 10 * midi(0); i++) {
p = abs(p) - 0.5;
rot(p.xy, -0.5);
p = abs(p) - 0.4;
rot(p.yz, -0.1);
float a = 1.0 + midi(1);
p *= a;
s *= a;
}
U(m, sdBox(p, vec3(0.5, 0.05, 0.05)) / s, 1., 1., 0.);
U(m, sdBox(p, vec3(0.5 + 0.5 * (cos(TAU * time * 0 / 4.)), 0.06, 0.05)) / s, 0., 0.1, 0.5);
U(m, sdBox(p, vec3(0.2, 0.6, 0.1)) / s, 0., saturate(cos(TAU * (time + pos.z / 8.))), -0.5);
return m;
}
vec3 fbm(vec3 p) { return sin(p) + sin(p * 2.) / 2. + sin(p * 4.) / 4.; }
void main(void) {
vec2 uv = vec2(fragCoord.x / iResolution.x, fragCoord.y / iResolution.y);
uv -= 0.5;
uv /= vec2(iResolution.y / iResolution.x, 1);
vec2 m;
m.x = atan(uv.x / uv.y) / 3.14;
m.y = 1. / length(uv) * .2;
float d = m.y;
vec3 col = vec3(0);
vec3 ro = vec3(0, 0, time);
vec3 ray = vec3(uv, 1.1 + cos(TAU * time / 8.));
ray += 0.1 * fbm(vec3(1, 2, 3) + TAU * time / 4.);
// rot(ray.xy, time);
// rot(ray.yz, time);
ray = normalize(ray);
float t = 0.;
for (int i = 0; i < 100; i++) {
vec3 p = ro + ray * t;
vec4 m = map(p);
float d = m.x;
if (m.y == 1.) {
t += d;
if (d < 0.001) {
col += 0.005 * float(i);
break;
}
} else {
t += abs(d) * 0.5 + 0.01;
col += saturate(0.001 * vec3(1. + m.w, 1, 1. - m.w) * m.z / abs(d));
}
}
col = mix(vec3(0), col, exp(-0.7 * t));
// col = texture(midi1, inData.v_texcoord).rgb;
fragColor = vec4(col, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment