Skip to content

Instantly share code, notes, and snippets.

@Pikaurd
Last active January 24, 2018 08:14
Show Gist options
  • Save Pikaurd/5e498a0613bf6db7c608f40b6bae050a to your computer and use it in GitHub Desktop.
Save Pikaurd/5e498a0613bf6db7c608f40b6bae050a to your computer and use it in GitHub Desktop.
#ifdef GL_ES
precision mediump float;
#endif
#define PI 3.1415927
#define PI2 2.0 * PI
#define FX_MIN_DURATION 0.1
#define FX_GAP 4.0
varying vec2 textureCoordinate;
uniform sampler2D inputImageTexture;
uniform float u_time;
vec2 frameTranslate(vec2 st, float offsetThreshold);
vec2 distortion(vec2 st, float cycle, float timeScale, float waveScale, float time);
vec2 scaleFromCenter2D(vec2, vec2);
float vhs(vec2 pos, float col, float t);
vec4 vhsVec4(vec4, vec2, float);
void main (void) {
float cycle = 4.0;
vec2 st = textureCoordinate;
vec4 img = texture2D(inputImageTexture, st);
// ---------------------------------------------------------------------
vec4 result = img;
float modularByTime = mod(u_time, FX_GAP);
float stepTime = FX_MIN_DURATION;
if (modularByTime <= stepTime * 2.0) { // step 1
st = frameTranslate(st, 0.66);
st = distortion(st, 4.0, FX_MIN_DURATION, 0.1, u_time);
result = vhsVec4(img, st, u_time);
}
else if (modularByTime <= stepTime * 3.0) { // step 2
st = scaleFromCenter2D(st, vec2(2.0));
st = distortion(st, 4.0, FX_MIN_DURATION, 0.1, u_time);
result = texture2D(inputImageTexture, st);
}
else if (modularByTime <= stepTime * 4.0) { // step 3
st = scaleFromCenter2D(st, vec2(4.0));
st = distortion(st, 4.0, FX_MIN_DURATION, 0.1, u_time);
vec4 img = texture2D(inputImageTexture, st);
float avgColor = (img.x + img.y + img.z) / 3.0; // monochrome
result = vec4(avgColor * 0.9, avgColor, avgColor * 0.9, 1.0);
}
else if (modularByTime <= stepTime * 5.0) { // step 4
st = distortion(st, 4.0, FX_MIN_DURATION, 0.1, u_time);
result = texture2D(inputImageTexture, st);
}
else if (modularByTime <= stepTime * 8.0) { // step 5
result = vhsVec4(img, st, u_time);
}
else if (modularByTime <= stepTime * 10.0) { // step 6
result = img;
}
else if (modularByTime <= stepTime * 12.0) { // step 7
st = distortion(st, 4.0, FX_MIN_DURATION, 0.05, u_time);
result = texture2D(inputImageTexture, st);
}
// ---------------------------------------------------------------------
gl_FragColor = result;
}
vec2 frameTranslate(vec2 st, float offsetThreshold) {
float x = st.x + offsetThreshold;
if (x > 1.0) {
st.x = x - 1.0;
}
else {
st.x = x;
}
return st;
}
vec2 distortion(vec2 st, float cycle, float timeScale, float waveScale, float time) {
float scale = (sin( st.y * PI * 2.0 * cycle ) + sin( st.y * PI * 2.0 * 1.0 )) * 0.1;
float timeCycle = abs(sin(time * timeScale));
st.x = st.x - scale * timeCycle;
return st;
}
vec2 scaleFromCenter2D(vec2 coord, vec2 scale) {
vec2 scaleCenter = vec2(0.5);
vec2 st = coord - scaleCenter;
st = st * scale + scaleCenter;
if (st.x < 0.0) {
st.x = mod(st.x, 1.0);
}
else if (st.x > 1.0) {
st.x = fract(st.x);
}
if (st.y < 0.0) {
st.y = mod(st.y, 1.0);
}
else if (st.y > 1.0) {
st.y = fract(st.y);
}
return st;
}
// vhs from https://gist.github.com/antoineMoPa/4312982a08679e36f893402b592fb9de
float vhs(vec2 pos, float col, float t) {
float noise = 0.0;
if(sin(690.0 * pos.y) < 0.1){
if(sin(35.0 * pos.y + PI2 * u_time) < 0.2){
if(sin(50.0 * pos.y) < 0.2){
float n = 0.4 * sin(pos.x * 30.0 + PI2 * u_time + pos.y * 30.0);
n *= 1.0 + 0.1 * cos(pos.x * 4035.0 + PI2 * u_time);
noise += n;
}
} else {
noise = 0.4 * sin(pos.x * pos.y);
}
}
noise *= cos(4.0 * pos.x + PI2 * u_time);
col += noise;
col *= 1.0 + 0.1 * (sin(PI2 * u_time) + 0.1 * sin(4.0 * PI2 * u_time));
float d = pow(length(pos - vec2(0.5 + 0.1 * cos(20.0 * pos.x + 23.0 * pos.y),0.5)),2.0);
col *= 1.0 - 2.0 * d;
return col;
}
vec4 vhsVec4(vec4 color, vec2 st, float time) {
vec4 result = color;
result.r = vhs(st, color.r, time);
result.g = vhs(st, color.g, time + 0.2);
result.b = vhs(st + vec2(0.01,0.003), color.g, time + 0.2);
result.a = 1.0;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment