Make fft work #1

Merged
goliath merged 3 commits from tyrolyean/shadermeh:master into master 2022-11-09 20:48:05 +01:00
4 changed files with 345 additions and 0 deletions
Showing only changes of commit 12396a8bb0 - Show all commits

84
shaders/eclipse.frag Normal file
View file

@ -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);
}

65
shaders/fft.frag Normal file
View file

@ -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);
}

158
shaders/grid.frag Normal file
View file

@ -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.; y<cells.y; y++) {
for (float x=0.; x<cells.x; x++) {
p1=vec3(x-hcells.x,y-hcells.y,0.)*.1; p1.z=getz(p1.xy);
p2=vec3(p1.x+.1,p1.y ,0.); p2.z=getz(p2.xy);
p3=vec3(p1.x ,p1.y+.1,0.); p3.z=getz(p3.xy);
p1*=camrot; p2*=camrot; p3*=camrot;
p1.xy*=persp/max(0.1,p1.z+dist);
p2.xy*=persp/max(0.1,p2.z+dist);
p3.xy*=persp/max(0.1,p3.z+dist);
if (max(p1.x,p2.x)>uv.x-linewidth/4. && min(p1.x,p2.x)<uv.x+linewidth/4. && x<cells.x-1.) {
if (max(p1.y,p2.y)>uv.y-linewidth/4. && min(p1.y,p2.y)<uv.y+linewidth/4.) {
#ifdef OPAQUE_MODE
c=max(c,segment(uv,p1,p2,linewidth,dist)*1.5);
#else
c+=segment(uv,p1,p2,linewidth,dist);
#endif
}
}
if (max(p1.x,p3.x)>uv.x-linewidth/4. && min(p1.x,p3.x)<uv.x+linewidth/4. && y<cells.y-1.) {
if (max(p1.y,p3.y)>uv.y-linewidth/4. && min(p1.y,p3.y)<uv.y+linewidth/4.) {
#ifdef OPAQUE_MODE
c=max(c,segment(uv,p1,p3,linewidth,dist)*1.5);
#else
c+=segment(uv,p1,p3,linewidth,dist);
#endif
}
}
}
}
c*=brightness;
#ifdef INVERSE
fragColor = vec4(1.-c,1.);
#else
fragColor = vec4(c,1.);
#endif
}

38
shaders/spectrum.frag Normal file
View file

@ -0,0 +1,38 @@
/*
2D LED Spectrum - Visualiser
Based on Led Spectrum Analyser by: simesgreen - 27th February, 2013 https://www.shadertoy.com/view/Msl3zr
2D LED Spectrum by: uNiversal - 27th May, 2015
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
*/
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// create pixel coordinates
vec2 uv = fragCoord.xy / iResolution.xy;
// quantize coordinates
const float bands = 30.0;
const float segs = 40.0;
vec2 p;
p.x = floor(uv.x*bands)/bands;
p.y = floor(uv.y*segs)/segs;
// read frequency data from first row of texture
float fft = texture( iChannel0, vec2(p.x,0.0) ).x;
// led color
vec3 color = mix(vec3(0.0, 2.0, 0.0), vec3(2.0, 0.0, 0.0), sqrt(uv.y));
// mask for bar graph
float mask = (p.y < fft) ? 1.0 : 0.1;
// led shape
vec2 d = fract((uv - p) *vec2(bands, segs)) - 0.5;
float led = smoothstep(0.5, 0.35, abs(d.x)) *
smoothstep(0.5, 0.35, abs(d.y));
vec3 ledColor = led*color*mask;
// output final color
fragColor = vec4(ledColor, 1.0);
}