ChromaticAberration Shader (no texscreen function)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By blank

Hey guys, just a quick question, as I really don’t understand shaders, and last time I used them was in 2.1, I was wondering what is the alternative for the no more existent function texscreen() which was used to read screen with Vec2 parameter.

I have a chromatic aberration shader hacked together from multiple sources from a few years prior that I’m hopelessly trying to fix because of this, anyone able to help?

shader_type canvas_item;

uniform float r_offset_x = -0.2;
uniform float r_offset_y = 0;
uniform float g_offset_x = 0;
uniform float g_offset_y = 0;
uniform float b_offset_x = 0.2;
uniform float b_offset_y = 0;

void fragment() {
 vec2 PixelSize = TEXTURE_PIXEL_SIZE;
 vec3 r_val;
 vec3 g_val;
 vec3 b_val;
 r_val = texscreen(SCREEN_UV + vec2(r_offset_x, r_offset_y) * PixelSize); 
 g_val = texscreen(SCREEN_UV + vec2(g_offset_x, g_offset_y) * PixelSize); 
 b_val = texscreen(SCREEN_UV + vec2(b_offset_x, b_offset_y) * PixelSize); 

 vec3 Chromatic_Aberration = vec3(r_val.r, g_val.g, b_val.b);

 COLOR.rgb = Chromatic_Aberration;
}
:bust_in_silhouette: Reply From: Zylann

If you are writing a screen-reading shader, you may replace TEXTURE_PIXEL_SIZE by SCREEN_PIXEL_SIZE.
Also, replace texscreen by texture(SCREEN_TEXTURE, the_uv);

First of all thank you very much, as this worked for the chromatic aberration, now I tried adding the pixelization effect from the official demos like this:

shader_type canvas_item;

uniform float size_x=0.005;
uniform float size_y=0.005;

uniform float r_offset_x = -0.2;
uniform float r_offset_y = 0;
uniform float g_offset_x = 0;
uniform float g_offset_y = 0;
uniform float b_offset_x = 0.2;
uniform float b_offset_y = 0;

void fragment() {
vec2 uv = SCREEN_UV;
uv-=mod(uv,vec2(size_x,size_y));

vec2 PixelSize = TEXTURE_PIXEL_SIZE;
vec4 r_val = texture(SCREEN_TEXTURE, uv + vec2(r_offset_x, r_offset_y) * PixelSize);
vec4 g_val = texture(SCREEN_TEXTURE, uv + vec2(g_offset_x, g_offset_y) * PixelSize);
vec4 b_val = texture(SCREEN_TEXTURE, uv + vec2(b_offset_x, b_offset_y) * PixelSize);

vec3 Chromatic_Aberration = vec3(r_val.r, g_val.g, b_val.b);

COLOR.rgb = Chromatic_Aberration;
//COLOR.rgb= textureLod(SCREEN_TEXTURE,uv,0.0).rgb;
}

But for some reason I get left with a weird screen tearing, as shown in picture attached.

enter image description here

Both of these shaders work great now separately but combining them creates this kinda mess :/. Can’t get my head around this stuff somehow.

blank | 2018-02-28 14:41

Scratch that, fixed it by using textureLod() function instead.

blank | 2018-02-28 15:06