(SOLVED)glitch shader, it compiles but doesn't work!!

edited November 2017 in GLSL / Shaders

hi guys...i'm working on this glitch shader i've found on this website: https://www.shadertoy.com/view/ls3Xzf

now i am editing part of the code to make it compile by processing.

It actually compile but it doesn't work

processing code:

PShader glitch;
PImage img, glitchImg;

void setup() {
  size(540, 540, P3D);
  img=loadImage("pietroGogh.jpg");
  glitchImg=loadImage("glitch.jpg");
  glitch = loadShader("glitch2.glsl");
  stroke(255);
  background(0);
  glitch.set("iResolution", new PVector(800., 600., 0.0) );
}

void draw() {
  strokeWeight(1);
  //glitch.set("iGlobalTime", random(0, 60.0));
  glitch.set("iTime", millis());
  if (random(0.0, 1.0) < 0.4) {
    shader(glitch);
  }
  image(img, 0, 0);
}

glsl code:

uniform sampler2D texture;
uniform vec2 iResolution;
uniform float iTime;
//varying vec4 vertTexCoord;


float rand () {
    return fract(sin(iTime)*1e4);
}

void main()
{
    vec2 uv = gl_FragCoord.xy / iResolution.xy;

    vec2 uvR = uv;
    vec2 uvB = uv;

    uvR.x = uv.x * 1.0 - rand() * 0.02 * 0.8;
    uvB.y = uv.y * 1.0 + rand() * 0.02 * 0.8;

    // 
    if(uv.y < rand() && uv.y > rand() -0.1 && sin(iTime) < 0.0)
    {
        uv.x = (uv + 0.02 * rand()).x;
    }

    vec4 c;
    c.r = texture(texture, uvR).r;
    c.g = texture(texture, uv).g;
    c.b = texture(texture, uvB).b;


    float scanline = sin( uv.y * 800.0 * rand())/30.0; 
    c *= 1.0 - scanline; 

    //vignette
    float vegDist = length(( 0.5 , 0.5 ) - uv);
    c *= 1.0 - vegDist * 0.6;

    gl_FragColor = c;
}

does anyone have any idea why this happens? sorry if there are trivial mistakes, but i'm quite new to shader language thank you all!!

Tagged:

Answers

  • If you are working with a ShaderToy demo, then the PixelFlow library has a wrapper for them so that (I believe, untested) you don't even need to adapt them:

    https://forum.processing.org/two/discussion/comment/106411/#Comment_106411

  • really interesting...just update the library. but for me would be usefull to learn something about shader...not just import it!! thank you for the reply :)

  • It also might be worth looking t the source code of the library to see how it works. And / or talking to the author....

  • why not? i'm doing it just now! thanks you for the reply!! :)

  • if someone is interested i've debugged the code...this is a working version:

    glsl code:

    #ifdef GL_ES
    precision mediump float;
    #endif
    
    uniform sampler2D texture;
    uniform vec2  iResolution;          
    uniform float iTime;      
    
    float rand () {
        return fract(sin(iTime)*1e4);
    }
    
    void main() {
        vec2 uv = gl_FragCoord.xy/iResolution;  
        uv.y=1.-uv.y;
        vec2 uvR = uv;
        vec2 uvB = uv;
    
        uvR.x = uv.x * 1.0 - rand() * 0.02 * 0.8;
        uvB.y = uv.y * 1.0 + rand() * 0.02 * 0.8;
    
        if(uv.y < rand() && uv.y > rand() -0.1 && sin(iTime) < 0.0)
        {
            uv.x = (uv + 0.02 * rand()).x;
        }
    
        vec4 c;
        c.r = texture(texture, uvR).r;
        c.g = texture(texture, uv).g;
        c.b = texture(texture, uvB).b;
        c.a=1.;
    
        float scanline = sin( uv.y * abs(sin(iTime*0.005))*800.0 * rand())/30.0; 
        c *= 1.0 - scanline; 
    
        float vegDist = length(( 0.5 , 0.5 ) - uv);
        c *= 1.0 - vegDist * 0.6;
    
        gl_FragColor =c;
    }
    

    processing code:

    PShader myShader;
    PImage img;
    
    void settings() {
      img=loadImage("img.jpg");
      size(img.width, img.height, P3D);
    }
    
    void setup() {
      myShader = loadShader("glitch.glsl");
      myShader.set("iResolution", float(width), float(height));
    }
    
    void draw() {
      myShader.set("iTime", millis()/1.);
      shader(myShader);
      image(img, 0, 0, width, height);
    }
    
  • @pietroLama Really cool. Thxs for sharing.

    Kf

  • @kfrajer thank you so much :)

Sign In or Register to comment.