I am trying to implement an emboss filter in SparkAR. It works, but the final output is flipped. The code below is what I modified from https://www.shadertoy.com/view/ld3XW4 so it would work within the software, but idk what I could have done that would possibly result in the flipped final image. I'm not too versed with coding image processing stuff, so any pointers would be massively appreciated.
The language is a shading language from META: SparkSL Help save what I have left of my hair. Thanks so much.
Edit: Oh my god the syntax for the code is so messy. I'm trying to edit it please bear with me if you come across the mess below before I can.
// Source:
// http://coding-experiments.blogspot.com/2010/07/convolution.html
#define EMBOSS_WIDTH 0.0015
#define EMBOSS_HEIGHT 0.0050
// samples a pixel centerd at "uv" and with offset of dx|dy
const vec4 sample_pixel(std:: Texture2d myTex, in vec2 uv, in float dx, in float dy)
{
return myTex.sample( uv + vec2(dx, dy));
}
// convolves a SINGLE channel of input color_matrix
const float convolve(in float[9] kernel, in vec4[9] color_matrix)
{
float res = 0.0;
for (int i=0; i<9; i++)
{
res += kernel[i] * color_matrix[i].a;
}
return clamp(res + 0.5, 0.0 ,1.0);
}
// builds a 3x3 color matrix centerd at "uv"
const void build_color_matrix(std:: Texture2d myTex, in vec2 uv, out vec4[9] color_matrix)
{
float dxtex = EMBOSS_WIDTH;
float dytex = EMBOSS_HEIGHT;
color_matrix[0].rgb = sample_pixel(myTex, uv, -dxtex, -dytex) .rgb;
color_matrix[1].rgb = sample_pixel(myTex, uv, -dxtex, 0.0) .rgb;
color_matrix[2].rgb = sample_pixel(myTex, uv, -dxtex, dytex) .rgb;
color_matrix[3].rgb = sample_pixel(myTex, uv, 0.0, -dytex) .rgb;
color_matrix[4].rgb = sample_pixel(myTex, uv, 0.0, 0.0) .rgb;
color_matrix[5].rgb = sample_pixel(myTex, uv, 0.0, dytex) .rgb;
color_matrix[6].rgb = sample_pixel(myTex, uv, dxtex, -dytex) .rgb;
color_matrix[7].rgb = sample_pixel(myTex, uv, dxtex, 0.0) .rgb;
color_matrix[8].rgb = sample_pixel(myTex, uv, dxtex, dytex) .rgb;
}
// builds a mean color matrix (off of .rgb of input).
// NOTE: stores the output in alpha channel
void build_mean_matrix(inout vec4[9] color_matrix)
{
for (int i=0; i<9; i++)
{
color_matrix[i].a = (color_matrix[i].r + color_matrix[i].g + color_matrix[i].b) / 3.;
}
}
vec4 mainImage( in std:: Texture2d myTex, out vec4 )
{
/*2. 0. 0.
0. -1 0.
0. 0. -1 */
float kerEmboss[9];
kerEmboss[0] = 2.;
kerEmboss[1] = 0.0;
kerEmboss[2] = 0.0;
kerEmboss[3] = 0.0;
kerEmboss[4] = -1.;
kerEmboss[5] = 0.0;
kerEmboss[6] = 0.0;
kerEmboss[7] = 0.0;
kerEmboss[8] = -1.;
vec4 pixel_matrix[9];
vec2 iResolution = std::getRenderTargetSize();
vec4 fragCoord = std:: getFragmentCoord();
vec2 uv = fragCoord.xy / iResolution.xy;
build_color_matrix(myTex, uv, pixel_matrix);
build_mean_matrix(pixel_matrix);
float convolved = convolve(kerEmboss, pixel_matrix);
vec4 fragColor = vec4(vec3(convolved), 1.0);
return fragColor;
}
The convolution "shader" is supposed to produce an embossed image, but flips it (or rotates) vertically. I tried negating as many numbers as I could find, but nada.