Hi there! I’m working on an implementation of the well-known reaction-diffusion model with p5 shaders. However, I’m experiencing issues because the model seems to work, but after a few iterations, it gets stuck and stops progressing. I don’t think it’s a performance issue since I’ve implemented it without shaders, and it doesn’t get stuck in the same way. Does anyone have any ideas on why this might be happening? I don’t think it’s a matter of miscalculating something. I suspect it might be related to the shaders, but honestly, I have no clue. Any insights would be greatly appreciated!
--------------------------------------------------- sketch.js ---------------------------------------------------------------------
let golShader;
let prevFrame;
function preload() {
golShader = loadShader('gol.vert', 'gol.frag');
}
function setup() {
createCanvas(400, 400, WEBGL);
pixelDensity(1);
noSmooth();
background(255,0,0);
stroke(0,0,255)
fill(0,0,255)
rect(0,0,50,50);
shader(golShader);
golShader.setUniform("normalRes", [1.0 / width, 1.0 / height]);
}
function draw() {
golShader.setUniform('tex', get());
rect(-width / 2, -height / 2, width, height);
}
------------------------------------------------------------gol.vert-------------------------------------------------------------------
attribute vec3 aPosition;
attribute vec2 aTexCoord;
varying vec2 vTexCoord;
void main() {
// copy the texcoords
vTexCoord = aTexCoord;
vec4 positionVec4 = vec4(aPosition, 1.0);
positionVec4.xy = positionVec4.xy * 2.0 - 1.0;
gl_Position = positionVec4;
}
---------------------------------------------------------gol.frag---------------------------------------------------------
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 vTexCoord;
uniform sampler2D tex;
uniform vec2 normalRes;
float laPlacian(vec2 uv, float m) {
float laplacian = 0.0;
float mult ;
for (float i = -1.0; i < 2.0; i++) {
for (float j = -1.0; j < 2.0; j++) {
float x = uv.x + i * normalRes.x;
float y = uv.y + j * normalRes.y;
if (i == 0.0 && j == 0.0) {
mult = -1.0;
} else if (i == j || i == -j) {
mult = 0.05;
} else {
mult = 0.2;
}
if (m == 0.0) {
laplacian += texture2D(tex, vec2(x, y)).r * mult;
} else {
laplacian += texture2D(tex, vec2(x, y)).b * mult;
}
}
}
return laplacian;
}
void main() {
vec2 uv = vTexCoord;
uv.y = 1.0 - uv.y;
float Da = 1.0;
float Db = 0.5;
float f = 0.055;
float k = 0.062;
vec4 col = texture2D(tex, uv);
float a = col.r;
float b = col.b;
float new_a = a + Da * laPlacian(uv, 0.0) - a * b * b + f * (1.0 - a);
float new_b = b + Db * laPlacian(uv, 1.0) + a * b * b - (k + f) * b;
gl_FragColor = vec4(new_a, 0.0, new_b, 1.0);
}