Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use backbuffer to render reaction-diffusion not working, any problem with my code? #59

Open
zemora opened this issue Nov 15, 2019 · 0 comments

Comments

@zemora
Copy link

zemora commented Nov 15, 2019

Hi this is my code for animating the reaction-diffusion simulation, I want to use mouse click to add species B to the screen, I run it in glslEditor, it reports no error but only shows a black background.

Hope you help me check what's wrong with my code

#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform sampler2D u_buffer0;

const vec4 params = vec4(0.16, 0.08, 0.06, 0.062);
const vec4 color1 = vec4(0.0, 0.0, 0.0, 0.3137);
const vec4 color2 = vec4(1.0, 0.1843, 0.5333, 0.3765);
const vec4 color3 = vec4(0.8549, 1.0, 0.5333, 0.3882);
const vec4 color4 = vec4(0.3765, 1.0, 0.4784, 0.3922);
const vec4 color5 = vec4(1.0);

void main()
{
    vec2 uv_texcoord = gl_FragCoord.xy / u_resolution.xy;

#if defined( BUFFER0 )

    if(u_mouse.x < -5.0)
    {
        gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
        return;
    }

    vec2 pixelSize = 1.0 / u_resolution.xy;
    vec2 cen = texture2D(u_buffer0, uv_texcoord).xy;
    vec2 rig = texture2D(u_buffer0, uv_texcoord + vec2( pixelSize.x,          0.0)).xy;
    vec2 top = texture2D(u_buffer0, uv_texcoord + vec2(         0.0,  pixelSize.y)).xy;
    vec2 lef = texture2D(u_buffer0, uv_texcoord + vec2(-pixelSize.x,          0.0)).xy;
    vec2 bot = texture2D(u_buffer0, uv_texcoord + vec2(         0.0, -pixelSize.y)).xy;
    
    float Du   = params.x;
    float Dv   = params.y;
    float feed = params.z;
    float kill = params.w;
    
    vec2 lapl = rig + top + lef + bot - 4.0 * cen;
    float du = Du * lapl.x - cen.x * cen.y * cen.y + feed * (1.0 - cen.x);
    float dv = Dv * lapl.y + cen.x * cen.y * cen.y - (feed + kill) * cen.y;
    vec2 newValue = cen + 0.6 * vec2(du, dv);
    
    if(u_mouse.x > 0.0)
    {
        vec2 diff = (gl_FragCoord.xy - u_mouse.xy);
        float dist = dot(diff, diff);
        if(dist < 1.0)
            newValue.y = 0.9;
    }
    
    gl_FragCoord = vec4(newValue, 0.0, 1.0);
    
#else 
    
    float value = texture2D(u_buffer0, uv_texcoord).y;
    float a;
    vec3 col;
    
    if (value <= color1.w)
    {
        col = color1.xyz;
    }
    
    else if (value > color1.w && value <= color2.w)
    {
        a = (value - color1.w) / (color2.w - color1.w);
        col = mix(color1.xyz, color2.xyz, a);
    }
    else if (value > color2.w && value <= color3.w)
    {
        a = (value - color2.w) / (color3.w - color2.w);
        col = mix(color2.xyz, color3.xyz, a);
    }
    else if (value > color3.w && value <= color4.w)
    {
        a = (value - color3.w) / (color4.w - color3.w);
        col = mix(color3.xyz, color4.xyz, a);
    }
    else if (value > color4.w && value <= color5.w)
    {
        a = (value - color4.w) / (color5.w - color4.w);
        col = mix(color4.xyz, color5.xyz, a);
    }
    
    gl_FragColor = vec4(col.xyz, 1.0);

#endif
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant