Skip to content

Instantly share code, notes, and snippets.

@brainrake
Created February 12, 2020 01:11
Show Gist options
  • Save brainrake/5e18b2b424636da186ecd04b8a3f55dc to your computer and use it in GitHub Desktop.
Save brainrake/5e18b2b424636da186ecd04b8a3f55dc to your computer and use it in GitHub Desktop.
grey-scott reaction-diffusion http://www.karlsims.com/rd.html
#version 150
uniform float time;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 spectrum;
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D texture3;
uniform sampler2D prevFrame;
uniform sampler2D prevPass;
uniform float d_a;
uniform float d_b;
uniform float dt;
uniform float feed;
uniform float kill;
in VertexData
{
vec4 v_position;
vec3 v_normal;
vec2 v_texcoord;
} inData;
out vec4 fragColor;
void main(void)
{
vec2 uv = vec2(inData.v_texcoord.x, inData.v_texcoord.y);
vec4 old = texture2D(prevFrame, uv);
float old_a = old.r;
float old_b = old.b;
vec4 left = texture2D(prevFrame, uv - vec2(1/resolution.x, 0));
vec4 right = texture2D(prevFrame, uv + vec2(1/resolution.x, 0));
vec4 up = texture2D(prevFrame, uv - vec2(0, 1/resolution.y));
vec4 down = texture2D(prevFrame, uv + vec2(0, 1/resolution.y));
vec4 left_up = texture2D(prevFrame, uv + vec2(-1/resolution.x, -1/resolution.y));
vec4 right_up = texture2D(prevFrame, uv + vec2(1/resolution.x, -1/resolution.y));
vec4 right_down = texture2D(prevFrame, uv + vec2(1/resolution.x, 1/resolution.y));
vec4 left_down = texture2D(prevFrame, uv + vec2(-1/resolution.x, 1/resolution.y));
vec4 laplace1 = left/4.0 + right/4.0 + up/4.0 + down/4.0;
vec4 laplace = left/5.0 + right/5.0 + up/5.0 + down/5.0 + left_up/20.0 + right_up/20.0 + right_down/20.0 + left_down/20.0 ;
float laplace_a = laplace.r - old_a;
float laplace_b = laplace.b - old_b;
float dt_ = dt * 1;
float a = old_a + (d_a * laplace_a - old_a * old_b * old_b + feed * (1 - old_a)) * dt_;
float b = old_b + (d_b * laplace_b + old_a * old_b * old_b - ((kill + feed) * old_b)) * dt_;
if (abs(uv.x - mouse.x) < 0.005 && abs(1 - uv.y - mouse.y) < 0.01) {
fragColor = vec4(1,0,1,0);
} else {
fragColor = vec4(a, 0, b, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment