Shuffle Grid Image


#1

Hi, another ShaderToy effect for you guys…

// iChannel0: Source, filter=linear, wrap=clamp
// BBox: iChannel0

uniform float x = 0.; // X (X position in pixels)
uniform float y = 0.; // Y (Y position in pixels)
uniform float width = 100.; // Width (Width in pixels)
uniform float height = 100.; // Height (Height in pixels)
uniform int wdiv = 4; // Width Division (How many division in width), min=1, max=20
uniform int hdiv = 4; // Height Division (How many division in height), min=1, max=20
uniform float seed = 0; // Seed (Same seed gives the same shuffle)

float random(vec2 co){
  return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
  vec2 coord = fragCoord.xy / iRenderScale;
  vec2 res = iResolution.xy / iRenderScale;
  vec2 uv = coord / res;

  float ww = width / wdiv;
  float hh = height / hdiv;

  float i = floor((coord.x - x) / ww);
  float j = floor((coord.y - y) / hh);

  if  (0 <= i && i < wdiv && 0 <= j && j < hdiv) {
    float ni = mod(i + floor(random(vec2(j, seed))*wdiv), wdiv);
    float nj = mod(j + floor(random(vec2(ni, 2. * seed))*hdiv), hdiv);
    ni = mod(ni + int(random(vec2(nj, 3. * seed))*wdiv), wdiv);
    nj = mod(nj + int(random(vec2(ni, 4. * seed))*hdiv), hdiv);
    float ox = coord.x - i * ww;
    float oy = coord.y - j * hh;
    float nx = ni * ww + ox;
    float ny = nj * hh + oy;
    vec2 nuv = vec2(nx, ny) / res;
    fragColor = texture2D(iChannel0, nuv);
  } else {
    fragColor = texture2D(iChannel0, uv);
  }
}

#2

doesn’t work for me :confused: