Skip to content

Instantly share code, notes, and snippets.

@danamuise
Created December 28, 2018 19:33
Show Gist options
  • Save danamuise/77da63512f61eea5a0fe4c32860b02a6 to your computer and use it in GitHub Desktop.
Save danamuise/77da63512f61eea5a0fe4c32860b02a6 to your computer and use it in GitHub Desktop.
This is 3-pass GLSL preprocess fragment shader used in AR Spark (Facebook messenger AR effects)
varying vec2 hdConformedUV;
varying vec2 uv;
uniform sampler2D inputImage;
uniform int passIndex;
uniform vec2 uRenderSize;
uniform float uTime;
uniform float uPixelRes; // For UI parameters
uniform sampler2D pixelate; //pass 0
uniform sampler2D colorize; //pass 1
uniform sampler2D grid; //pass2
uniform float uTileResolution;
// check floor for right luminosity, maybe exp the luma)
vec4 toYCbCr(vec4 rgb)
{
rgb = rgb * 255.;
float r = rgb.r;
float g = rgb.g;
float b = rgb.b;
return vec4(0.299*r+0.587*g+0.114*b,
128.-0.168736*r-0.331264*g+0.5*b,
128.+0.5*r-0.418688*g-0.081312*b,
1.);
}
vec4 toRGB(vec4 ybr)
{
float y = ybr.x;
float b = ybr.y;
float r = ybr.z;
return vec4(y+1.402*(r-128.),
y-0.344136*(b-128.)-0.714136*(r-128.),
y+1.772*(b-128.),
1.)
/255.;
}
float desaturate(vec4 tex, vec2 uv){
vec3 col = texture2D(inputImage, uv).xyz;
return dot(col, vec3(0.3,0.6,0.1));
}
void main()
{ if(passIndex == 0) //pixilate//
{
float dx = 15.0 * (1.0 / uPixelRes);
float dy = 10.0 * (1.0 / uPixelRes);
vec2 uv_ = uv;
uv_ = vec2(dx * floor(uv.x / dx), dy * floor(uv_.y / dy));
gl_FragColor = texture2D(inputImage, uv_);
} else if (passIndex == 1) //colorize//
{
vec3 palette[4];
float palgray[4];
vec3 lum = vec3(0.2126,0.7152,0.0722);
palette[0] = vec3(15,56,15)/256.0;
palette[1] = vec3(48,98,48)/256.0;
palette[2] = vec3(139,171,127)/256.0;
palette[3] = vec3(175,199,81)/256.0;
for (int i = 0; i < 4; ++i)
palgray[i] = dot(palette[i], lum);
for (int i = 0; i < 4; ++i)
palgray[i] /= palgray[3];
float gray = dot(texture2D(pixelate, uv.xy).rgb, lum);
vec3 color;
for (int i = 0; i < 4; ++i) {
if (gray <= palgray[i]) {
color = palette[i];
break;
}
}
gl_FragColor = vec4(color, 1.);
} else if (passIndex == 2) //grid//
{
vec2 uvsample = floor(uv*uTileResolution)/uTileResolution;
float ll = toRGB(toYCbCr(texture2D(inputImage, uvsample))).x;
vec2 uvA = fract(uv*uTileResolution);
vec4 justcolor = toRGB(vec4(toYCbCr(vec4(texture2D(colorize, uvsample).xyz,1.)).xyz,1.));
float a;
vec2 uv2 = uvA * 2. - 1.;
a = uv2.x * uv2.x + uv2.y * uv2.y;
float final_ = distance(0.5, uvA.x)+distance(0.5, uvA.y);
final_ = 1.-length(0.5 - uvA);
final_ = floor(final_+ll);
gl_FragColor = vec4(final_)*justcolor;
} else {
gl_FragColor = texture2D(grid, uv);
}
}
@pjkarlik
Copy link

pjkarlik commented Nov 8, 2019

Are you able to use this with Spark-AR Studio? or is this a compiled thing that requires x-code?

@Naveen701372
Copy link

@pjkarlik

Are you able to use this with Spark-AR Studio? or is this a compiled thing that requires x-code?

Did you get a way to code open GLSL shaders in spark AR ?

@danamuise
Copy link
Author

in the old Spark (pre version 66) they allowed pre & post processsors to have GLSL shaders. (no longer)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment