Skip to content

Instantly share code, notes, and snippets.

@solsarratea
Created August 31, 2020 14:37
Show Gist options
  • Save solsarratea/becdf619773ceb8f62999ac704a2ad71 to your computer and use it in GitHub Desktop.
Save solsarratea/becdf619773ceb8f62999ac704a2ad71 to your computer and use it in GitHub Desktop.
reaction diffusion
// REACCCION DIFUSION
// adaptacion a The Force
// shader creado por Keijiro
// https://github.com/keijiro/ShaderSketches/blob/master/Fragment/ReactDiffuse.glsl
float rand2(vec2 p)
{
return fract(sin(dot(p, vec2(12.9898, 78.233))) * 43758.5453);
}
vec2 ReactDiffuse(vec2 p)
{
vec4 duv = vec4(1, 1, -1, 0) / resolution.xyxy;
vec2 s = texture2D(backbuffer, p).xy;
vec2 lpc =
texture2D(backbuffer, p - duv.xy).xy * 0.05 +
texture2D(backbuffer, p - duv.wy).xy * 0.20 +
texture2D(backbuffer, p - duv.zy).xy * 0.05 +
texture2D(backbuffer, p - duv.xw).xy * 0.2 +
s * -1. +
texture2D(backbuffer, p + duv.xw).xy * 0.20 +
texture2D(backbuffer, p + duv.zy).xy * 0.05 +
texture2D(backbuffer, p + duv.wy).xy * 0.20 +
texture2D(backbuffer, p + duv.xy).xy * 0.05;
const float f = 0.0545;
const float k = 0.0650;
s += vec2(
lpc.x * 1.00 - s.x * s.y * s.y + f * (1. - s.x),
lpc.y * 0.3 + s.x * s.y * s.y - (k + f) * s.y
);
return s;
}
void main(void)
{
bool initiate = false;
// *** TO INITIATE THE STATE, ENABLE THE LINE BELOW. ***
//initiate = true;
vec2 uv = gl_FragCoord.xy / resolution;
if (initiate)
{
gl_FragColor = vec4(1, step(0.9, rand(uv + time * 0.01)), 0, 0);
}
else if (uv.x < 0.5)
{
gl_FragColor = vec4(ReactDiffuse(uv), 0, 0);
}
else
{
vec2 s = texture2D(backbuffer, uv - vec2(0.5, 0)).xy;
float c = smoothstep(-0.2, 0.2, s.y - s.x);
gl_FragColor = vec4(c, c, c, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment