Back to shaders

Shader test bench

Displace.fs

vidvox-isf-files utility glsl runnable fragment MIT
Source
runnable fragment

Complete GLSL fragment shader. Stronghold runs it directly when the browser can compile it.

Code

uniform sampler2D inputImage;
uniform sampler2D displaceImage;
uniform float uDisplaceAmt;
uniform int xComponent;
uniform int yComponent;
uniform int relativeShift;
precision mediump float;
/*
{
  "CATEGORIES" : [
    "Distortion Effect"
  ],
  "DESCRIPTION" : "Simple Displace",
  "ISFVSN" : "2",
  "INPUTS" : [
    {
      "NAME" : "inputImage",
      "TYPE" : "image"
    },
    {
      "NAME" : "displaceImage",
      "TYPE" : "image",
      "LABEL" : "displace image"
    },
    {
      "NAME" : "uDisplaceAmt",
      "TYPE" : "float",
      "MAX" : 2,
      "DEFAULT" : 0,
      "MIN" : -2
    },
    {
      "LABELS" : [
        "Luma",
        "R",
        "G",
        "B",
        "A"
      ],
      "NAME" : "xComponent",
      "TYPE" : "long",
      "DEFAULT" : 1,
      "VALUES" : [
        0,
        1,
        2,
        3,
        4
      ]
    },
    {
      "LABELS" : [
        "Luma",
        "R",
        "G",
        "B",
        "A"
      ],
      "NAME" : "yComponent",
      "TYPE" : "long",
      "DEFAULT" : 2,
      "VALUES" : [
        0,
        1,
        2,
        3,
        4
      ]
    },
    {
      "NAME" : "relativeShift",
      "TYPE" : "bool",
      "DEFAULT" : 0
    }
  ],
  "CREDIT" : "by @colin_movecraft"
}
*/



void main(){
	vec2	p = (gl_FragCoord.xy / u_resolution.xy).xy;
	vec4	displacePixel = texture2D(displaceImage, p);
    
    float	r = (displacePixel.r);
    float	g = (displacePixel.g);
    float	b = (displacePixel.b);
    float	a = (displacePixel.a);
    float	avg = (r+g+b)/3.0;
    
    vec2	displace = vec2(avg,avg);
    if (xComponent==1)
    	displace.x = r;
	else if (xComponent==2)
    	displace.x = g;
    else if (xComponent==3)
    	displace.x = b;
	else if (xComponent==4)
    	displace.x = a;

    if (yComponent==1)
    	displace.y = r;
	else if (yComponent==2)
    	displace.y = g;
    else if (yComponent==3)
    	displace.y = b;
	else if (yComponent==4)
    	displace.y = a;
    
    displace = (relativeShift) ? 2.0 * (displace - vec2(0.5)) : displace;
    displace *= uDisplaceAmt;
    
	gl_FragColor = texture2D(inputImage, p+displace);
}