Skip to content

Instantly share code, notes, and snippets.

@mrange
Last active October 25, 2026 17:33
Show Gist options
  • Select an option

  • Save mrange/2d6a251f40f3a9bfbbc797f796ae08bb to your computer and use it in GitHub Desktop.

Select an option

Save mrange/2d6a251f40f3a9bfbbc797f796ae08bb to your computer and use it in GitHub Desktop.
Vertex Shader Example

LINES Multipass Audio Reactive shader

These shaders are designed to be used with kodelife.

Pass 0,2,3,4 are normal full fragment shader setups (ie orthographic projection) Pass 1 uses Perspective projection.

Passes file are prefixed with it's position in the pipeline, 0 coming before 1 and so on. They are suffixed with either _vs or _fs indicating if it's a vertex shader or fragement shader.

Passes might take other passes output as input.

Pass 0: smoothspectrum

Smooths the raw kodelife spectrum to be more useful for use in the main effect

Pass 1: lines

The main effect, generates vertices that are intended to be drawn using LINES mode and 20480 instances. POINTS mode also looks good.

Projection should be Perspective with camera:0,0,12, center: 0,2,0, up: 1,4,0, fov: 60, near: 1, far: 1000.

Pass 2: blur x direction

Takes lines output as input and applies gaussian blur in x direction

Pass 3: blur y direction

Takes blur_x output as input and applies gaussian blur in y direction, also uses itself blur_y as input to apply a blur fade effect.

Pass 4: post process

Takes lines and blur_y output as inputs and combines them to produce the final image.

// CC0: Audio reactive vertices
// An example of using KodeLife's audio spectrum with vertex and fragment shaders
// This file is released under CC0 1.0 Universal (Public Domain Dedication).
// To the extent possible under law, mrange has waived all copyright
// and related or neighboring rights to this work.
// See <https://creativecommons.org/publicdomain/zero/1.0/> for details.
#version 300 es
precision highp float;
uniform sampler2D input_spectrum;
uniform sampler2D history;
out vec4 FC;
void main() {
ivec2
xy=ivec2(gl_FragCoord.xy)
;
float
fft=sqrt(texelFetch(input_spectrum,xy,0)).x
;
vec4
h=texelFetch(history,xy,0)
;
FC = vec4(
fft
, max(.93*h.y,fft)
, mix(h.z,fft,.3)
, max(mix(fft,h.w,.93),fft)
);
}
// CC0: Audio reactive vertices
// An example of using KodeLife's audio spectrum with vertex and fragment shaders
// This file is released under CC0 1.0 Universal (Public Domain Dedication).
// To the extent possible under law, mrange has waived all copyright
// and related or neighboring rights to this work.
// See <https://creativecommons.org/publicdomain/zero/1.0/> for details.
#version 300 es
// Minimal vertex shader; transforms vertices using the current (orthographic) projection
uniform mat4 mvp;
in vec4 a_position;
void main() {
gl_Position = mvp * a_position;
}
#version 300 es
precision highp float;
uniform float time;
out vec4 FC;
in float v_spiral_length;
void main() {
vec3
col=vec3(0)
;
vec4
l=1.1+sin(vec4(6,1,8,0)+v_spiral_length*sqrt(.5)-time)
;
col+=l.w*l.xyz*exp(-.02*v_spiral_length);
FC=vec4(col,1);
}
#version 300 es
precision highp float;
uniform float time;
uniform mat4 mvp;
uniform sampler2D smooth_spectrum;
in vec4 a_position;
out float v_spiral_length;
const float
B=.4
, PI=acos(-1.)
, TAU=2.*PI
;
float spiralLength(float b, float a) {
// https://en.wikipedia.org/wiki/Archimedean_spiral
return 0.5*b*(a*sqrt(1.0+a*a)+log(a+sqrt(1.0+a*a)));
}
// License: Unknown, author: Unknown, found: don't remember
float hash(float co) {
return fract(sin(co*12.9898) * 13758.5453);
}
vec4 lintex(float x) {
float
i=clamp(x*255.,0.,254.)
, f=fract(i)
;
int
n0=int(i)
, n1=n0+1
;
ivec2
itx0=ivec2(n0&0xF,n0>>4)
, itx1=ivec2(n1&0xF,n1>>4)
;
vec4
c0 = texelFetch(smooth_spectrum, itx0, 0)
, c1 = texelFetch(smooth_spectrum, itx1, 0)
;
return mix(c0,c1,f);
}
float spectrum(float x) {
x*=.125;
float
n=floor(x)
, c=fract(x)
, s=mod(n,2.)
;
return lintex(.01+.85*mix(c,1.-c,s)).w;
}
void main() {
int
iid=gl_InstanceID
, vid=gl_VertexID
, nid=iid>>9
, cid=iid&0x1FF
, i=(cid*6+vid+1)>>1
, bin=i
;
float
id=float(i)
, NID=float(nid)
, a=sqrt(id)
, h0=hash(NID+.123)
, h1=hash(NID+.245)
, o=TAU*(h0*h0*h0*h0)-.3*time
, r=B*a
, A=spiralLength(B,a)
, s=spectrum(A+h1)
, y=(3.+2.*r)*s
, R=sqrt(y)*2.
, x=(r+R)*sin(a+o)
, z=(r+R)*cos(a+o)
;
vec3
p=vec3(x,y,z)
;
gl_Position = mvp*vec4(p,1);
v_spiral_length=A;
}
#version 300 es
precision highp float;
uniform sampler2D lines;
out vec4 FC;
vec3 gb(sampler2D pp, ivec2 dir, ivec2 xy) {
const float
blurriness =128.
;
ivec2
off
;
vec3
col=texelFetch(pp,xy,0).xyz
;
float
w
, ws=1.
, I
;
for(int i=1;i<19;++i) {
I=float(i);
w=exp(-(I*I)/blurriness);
off=dir*i;
col+=w*(texelFetch(pp,xy-off,0).xyz+texelFetch(pp,xy+off,0).xyz);
ws+=2.*w;
}
col/=ws;
return col;
}
void main(void) {
vec3
p=gb(lines, ivec2(1,0), ivec2(gl_FragCoord.xy))
;
FC=vec4(p,1);
}
// CC0: Audio reactive vertices
// An example of using KodeLife's audio spectrum with vertex and fragment shaders
// This file is released under CC0 1.0 Universal (Public Domain Dedication).
// To the extent possible under law, mrange has waived all copyright
// and related or neighboring rights to this work.
// See <https://creativecommons.org/publicdomain/zero/1.0/> for details.
#version 300 es
// Minimal vertex shader; transforms vertices using the current (orthographic) projection
uniform mat4 mvp;
in vec4 a_position;
void main() {
gl_Position = mvp * a_position;
}
#version 300 es
precision highp float;
uniform float time;
uniform sampler2D blur_x;
uniform sampler2D blur_y;
out vec4 FC;
vec3 gb(sampler2D pp, ivec2 dir, ivec2 xy) {
const float
blurriness =128.
;
ivec2
off
;
vec3
col=texelFetch(pp,xy,0).xyz
;
float
w
, ws=1.
, I
;
for(int i=1;i<19;++i) {
I=float(i);
w=exp(-(I*I)/blurriness);
off=dir*i;
col+=w*(texelFetch(pp,xy-off,0).xyz+texelFetch(pp,xy+off,0).xyz);
ws+=2.*w;
}
col/=ws;
return col;
}
void main(void) {
vec3
p=gb(blur_x, ivec2(0,1), ivec2(gl_FragCoord.xy))
, P=texelFetch(blur_y,ivec2(gl_FragCoord.xy+20.*normalize(sin(.3*time*vec2(1, sqrt(.5))))),0).xyz
;
FC=vec4(mix(p,P,.5),1);
}
// CC0: Audio reactive vertices
// An example of using KodeLife's audio spectrum with vertex and fragment shaders
// This file is released under CC0 1.0 Universal (Public Domain Dedication).
// To the extent possible under law, mrange has waived all copyright
// and related or neighboring rights to this work.
// See <https://creativecommons.org/publicdomain/zero/1.0/> for details.
#version 300 es
// Minimal vertex shader; transforms vertices using the current (orthographic) projection
uniform mat4 mvp;
in vec4 a_position;
void main() {
gl_Position = mvp * a_position;
}
#version 300 es
precision highp float;
uniform vec2 resolution;
uniform sampler2D lines;
uniform sampler2D blur_y;
out vec4 FC;
// License: Unknown, author: Claude Brezinski, found: https://mathr.co.uk/blog/2017-09-06_approximating_hyperbolic_tangent.html
vec3 tanh_approx(vec3 x) {
// Found this somewhere on the interwebs
// return tanh(x);
vec3 x2 = x*x;
return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0);
}
void main() {
vec4
p=texelFetch(lines,ivec2(gl_FragCoord.xy),0)
, P=texelFetch(blur_y,ivec2(gl_FragCoord.xy),0)
;
vec3
col=p.xyz+P.xyz
;
col*=2.;
col=tanh_approx(col);
col=max(col,0.);
col=sqrt(col)-.01*vec3(2,3,1);
col=max(col,0.);
FC=vec4(col,1.);
}
// CC0: Audio reactive vertices
// An example of using KodeLife's audio spectrum with vertex and fragment shaders
// This file is released under CC0 1.0 Universal (Public Domain Dedication).
// To the extent possible under law, mrange has waived all copyright
// and related or neighboring rights to this work.
// See <https://creativecommons.org/publicdomain/zero/1.0/> for details.
#version 300 es
// Minimal vertex shader; transforms vertices using the current (orthographic) projection
uniform mat4 mvp;
in vec4 a_position;
void main() {
gl_Position = mvp * a_position;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment