From 12396a8bb0505a3cddeb7bad8f134e0cc2d3a31b Mon Sep 17 00:00:00 2001 From: tyrolyean Date: Wed, 20 Jul 2022 23:45:51 +0200 Subject: [PATCH] Add more audio shaders Signed-off-by: tyrolyean --- shaders/eclipse.frag | 84 ++++++++++++++++++++++ shaders/fft.frag | 65 +++++++++++++++++ shaders/grid.frag | 158 ++++++++++++++++++++++++++++++++++++++++++ shaders/spectrum.frag | 38 ++++++++++ 4 files changed, 345 insertions(+) create mode 100644 shaders/eclipse.frag create mode 100644 shaders/fft.frag create mode 100644 shaders/grid.frag create mode 100644 shaders/spectrum.frag diff --git a/shaders/eclipse.frag b/shaders/eclipse.frag new file mode 100644 index 0000000..d599602 --- /dev/null +++ b/shaders/eclipse.frag @@ -0,0 +1,84 @@ +// credit: https://www.shadertoy.com/view/4tGXzt + +#define BEATMOVE 1 + +const float FREQ_RANGE = 64.0; +const float PI = 3.1415; +const float RADIUS = 0.6; +const float BRIGHTNESS = 0.2; +const float SPEED = 0.5; + +//convert HSV to RGB +vec3 hsv2rgb(vec3 c){ + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} + +float luma(vec3 color) { + return dot(color, vec3(0.299, 0.587, 0.114)); +} + +float getfrequency(float x) { + return texture(iChannel0, vec2(floor(x * FREQ_RANGE + 1.0) / FREQ_RANGE, 0.25)).x + 0.06; +} + +float getfrequency_smooth(float x) { + float index = floor(x * FREQ_RANGE) / FREQ_RANGE; + float next = floor(x * FREQ_RANGE + 1.0) / FREQ_RANGE; + return mix(getfrequency(index), getfrequency(next), smoothstep(0.0, 1.0, fract(x * FREQ_RANGE))); +} + +float getfrequency_blend(float x) { + return mix(getfrequency(x), getfrequency_smooth(x), 0.5); +} + +vec3 doHalo(vec2 fragment, float radius) { + float dist = length(fragment); + float ring = 1.0 / abs(dist - radius); + + float b = dist < radius ? BRIGHTNESS * 0.3 : BRIGHTNESS; + + vec3 col = vec3(0.0); + + float angle = atan(fragment.x, fragment.y); + col += hsv2rgb( vec3( ( angle + iTime * 0.25 ) / (PI * 2.0), 1.0, 1.0 ) ) * ring * b; + + float frequency = max(getfrequency_blend(abs(angle / PI)) - 0.02, 0.0); + col *= frequency; + + // Black halo + col *= smoothstep(radius * 0.5, radius, dist); + + return col; +} + +vec3 doLine(vec2 fragment, float radius, float x) { + vec3 col = hsv2rgb(vec3(x * 0.23 + iTime * 0.12, 1.0, 1.0)); + + float freq = abs(fragment.x * 0.5); + + col *= (1.0 / abs(fragment.y)) * BRIGHTNESS * getfrequency(freq); + col = col * smoothstep(radius, radius * 1.8, abs(fragment.x)); + + return col; +} + + +void mainImage( out vec4 fragColor, in vec2 fragCoord ) { + vec2 fragPos = fragCoord / iResolution.xy; + fragPos = (fragPos - 0.5) * 2.0; + fragPos.x *= iResolution.x / iResolution.y; + + vec3 color = vec3(0.0134, 0.052, 0.1); + color += doHalo(fragPos, RADIUS); + + float c = cos(iTime * SPEED); + float s = sin(iTime * SPEED); + vec2 rot = mat2(c,s,-s,c) * fragPos; + color += doLine(rot, RADIUS, rot.x); + + color += max(luma(color) - 1.0, 0.0); + + fragColor = vec4(color, 1.0); +} diff --git a/shaders/fft.frag b/shaders/fft.frag new file mode 100644 index 0000000..fd38b9d --- /dev/null +++ b/shaders/fft.frag @@ -0,0 +1,65 @@ +/* + + Linear vs Logarithmic FFT + + some good test songs: + + https://soundcloud.com/kraddy/winning + https://soundcloud.com/grey-houston/soothing-piano-melody + https://soundcloud.com/pointpoint/life-in-gr + +*/ + +//from https://stackoverflow.com/questions/35799286 +float toLog(float value, float min, float max){ + float exp = (value-min) / (max-min); + return min * pow(max/min, exp); +} + +float getLevel(float samplePos){ + // the sound texture is 512x2 + int tx = int(samplePos*512.0); + // first row is frequency data (48Khz/4 in 512 texels, meaning 23 Hz per texel) + return texelFetch( iChannel0, ivec2(tx,0), 0 ).x; +} + + +void mainImage( out vec4 fragColor, in vec2 fragCoord ) +{ + vec2 uv = fragCoord.xy / iResolution.xy; + + float xPos; + float fft; + + if (uv.y > 0.5){ + + //linear sampling + xPos = uv.x; + fft = getLevel(xPos); + + }else{ + + //crop bottom and top of range + uv.x = mix(0.3,0.7, uv.x); + + //logarithmic sampling + xPos = toLog(uv.x, 0.01, 1.0); + + fft = getLevel(xPos); + + //boost contrast + fft = pow(fft,3.0); + + //boost gain + fft *= 1.5; + + //contrast / brightness + float contrast = 1.4; + float brightness = 0.; + fft = (fft - 0.5) * contrast + 0.5 + brightness; + + } + + fragColor = vec4(vec3(fft),1.0); + +} diff --git a/shaders/grid.frag b/shaders/grid.frag new file mode 100644 index 0000000..eb34fe3 --- /dev/null +++ b/shaders/grid.frag @@ -0,0 +1,158 @@ +//https://www.shadertoy.com/view/XlBXRh with mic not soundcloud +#define preset4 + +#ifdef preset1 + #define cells vec2(14.,14.) + #define persp 1.5 + #define height 1. + #define linewidth .5 + #define lineexp 4. + #define brightness .7 +#endif + + +#ifdef preset2 + #define cells vec2(10.,5.) + #define persp 2.5 + #define height 1. + #define linewidth 3. + #define lineexp 6. + #define brightness .4 +#endif + + +#ifdef preset3 + #define OPAQUE_MODE + #define INVERSE + #define cells vec2(16.,16.) + #define persp 1. + #define height 1.5 + #define linewidth .1 + #define lineexp .5 + #define brightness .8 +#endif + +#ifdef preset4 + #define OPAQUE_MODE + #define cells vec2(10.,10.) + #define persp 2. + #define height .75 + #define linewidth .2 + #define lineexp 1. + #define brightness 1.5 +#endif + +#ifdef preset5 + #define INVERSE + #define cells vec2(6.,25.) + #define persp 1. + #define height 2. + #define linewidth .07 + #define lineexp .3 + #define brightness .35 +#endif + + +#ifdef preset6 + #define INVERSE + #define OPAQUE_MODE + #define cells vec2(15.,15.) + #define persp 2.5 + #define height 1. + #define linewidth .05 + #define lineexp .5 + #define brightness 1. +#endif + + + + +#define hcells (cells*.5) + + +vec3 segment(vec2 p, vec3 from, vec3 to, float width, float dist) { +width=1./width; +vec2 seg=from.xy-to.xy; +float halfdist=distance(from.xy,to.xy)*.5; +float ang=atan(seg.y,seg.x); +float sine=sin(ang); +float cose=cos(ang); +p-=from.xy; +p*=mat2(cose,sine,-sine,cose); +float dx=abs(p.x+halfdist)-halfdist; +float dy=abs(p.y); +float h=1.-abs(p.x+halfdist*2.)/halfdist/2.; +float pz=-from.z-(to.z-from.z)*h; +float l=1.-clamp(max(dx,dy)*width/(pz+dist)*dist*dist,0.,.1)/.1; +l=pow(abs(l),lineexp)*(1.-pow(clamp(abs(dist-pz)*.45,0.,1.),.5))*4.; +return normalize(.25+abs(mix(from,to,h)))*l; +} + +mat3 rotmat(vec3 v, float angle) +{ + angle=radians(angle); + float c = cos(angle); + float s = sin(angle); + + return mat3(c + (1.0 - c) * v.x * v.x, (1.0 - c) * v.x * v.y - s * v.z, (1.0 - c) * v.x * v.z + s * v.y, + (1.0 - c) * v.x * v.y + s * v.z, c + (1.0 - c) * v.y * v.y, (1.0 - c) * v.y * v.z - s * v.x, + (1.0 - c) * v.x * v.z - s * v.y, (1.0 - c) * v.y * v.z + s * v.x, c + (1.0 - c) * v.z * v.z + ); +} + +float getz(vec2 xy) { +xy=xy*10.+hcells; +//float pos=length(pow(abs(xy/cells),vec2(3.)))*8.; +float pos=(xy.y*cells.x+xy.x)/(cells.x*cells.y); +float s=texture(iChannel0,vec2(.5+pos*.5,.1)).x; +return .25-pow(s,1.5)*height; +} + +void mainImage( out vec4 fragColor, in vec2 fragCoord ) +{ + vec2 uv = (gl_FragCoord.xy / iResolution.xy-.5)*2.; + uv.y*=iResolution.y/iResolution.x; + mat3 camrot=rotmat(normalize(vec3(0.,0.,1.)),iTime*25.)*rotmat(normalize(vec3(1.,0.*sin(iTime*.5),0.)),60.+30.*sin(iTime*.5)); + float s=.1,maxc=0.; + vec3 p1,p2,p3; + vec3 rotv=vec3(0.,0.,1.); + float h; + vec3 col=vec3(0.); + float dist=1.2+pow(abs(sin(iTime*.3)),5.)*.5; + vec3 c=vec3(0.); + for (float y=0.; yuv.x-linewidth/4. && min(p1.x,p2.x)uv.y-linewidth/4. && min(p1.y,p2.y)uv.x-linewidth/4. && min(p1.x,p3.x)uv.y-linewidth/4. && min(p1.y,p3.y)