0 votes

Background:
I'm working on a flag shader that makes sprites look like there waving like flag. I had to change my code when working on the shader since it was taking the SCREEN_TEXTURE not TEXTURE.
It worked, but now when a shader is applied to a sprite, it seems jagged, like there's no anti-aliasing.

Question:
Is there a way to apply some sort of anti-aliasing to a shader?

Here's an example:
https://imgur.com/a/iwiaTgg

Here's the code for the curious:

shader_type canvas_item;

//uniform vec3 shadow_color = texture(TEXTURE, UV + vec2(0.5,0.5)).rgb;// 
vec3(0.0,0.5,0.5);
uniform int OCTAVES = 1;
uniform float SPD_OF_FOG = 0.1;

float rand(vec2 coord) {
return fract(sin(dot(coord,vec2(12.9898, 78.233)))* 43758.5453123);
}

float noise(vec2 coord) {
vec2 i = floor(coord);
vec2 f = fract(coord);

float a = rand(i);
float b = rand(i + vec2(1.0, 0.0));
float c = rand(i + vec2(0.0, 1.0));
float d = rand(i + vec2(1.0, 1.0));

vec2 cubic = f * f * (3.0 - 2.0 * f);

return mix(a, b, cubic.x) + (c - a) * cubic.y * (1.0 - cubic.x) + (d - b) * cubic.x * cubic.y;
}


float fbm(vec2 coord) {
float value = 0.0; // color changer
float scale = 0.01;
//
for(int i = 0; i < OCTAVES; i++) 
{
    value += noise(coord) * scale;
    scale *= 0.0;
    coord *= 2.0;
}
return value;
}

void fragment() {

// Wavy Shader
vec2 sprite_scale = vec2(textureSize(TEXTURE, 0));

float jagged_scale = 40.0;
vec2 noisecoord1 = UV * sprite_scale / jagged_scale;
vec2 noisecoord2 = UV * sprite_scale / jagged_scale + 4.0;

vec2 motion1 = vec2(TIME * 1.3, TIME * -0.4); // vec2(TIME * 0.3, TIME * -0.4)
vec2 motion2 = vec2(TIME * 0.1, TIME * 1.5); // vec2(TIME * 0.1, TIME * 0.5);

vec2 distort1 = vec2(noise(noisecoord1 + motion1), noise(noisecoord2 + motion1)) - vec2(0.5);
vec2 distort2 = vec2(noise(noisecoord1 + motion2), noise(noisecoord2 + motion2)) - vec2(0.5);

float powerOfDistort = 15.0; //Higher = Less Distort 
vec2 distort_sum = (distort1 + distort2) / powerOfDistort * UV.y * UV.y; // Wavy at the bottom more
vec4 color = textureLod(TEXTURE, UV + distort_sum, 0.0);


float flag_sat = 1.0;
color.rgb = mix(vec3(0.5), color.rgb, flag_sat);

float near_top = (UV.y + distort_sum.y) / (0.2 / (sprite_scale.y / 30.0));
near_top = clamp(near_top, 0.0, 1.0);
near_top = 1.0 - near_top;

color = mix(color, vec4(1.0), near_top);

float edge_lower = 0.0;
float edge_upper = edge_lower + 0.1;

if (near_top > edge_lower) {
    color.a = 0.0;
    if (near_top < edge_upper) {
        color.a = 0.0;//(edge_upper - near_top) / (edge_upper - edge_lower);
        }
}

COLOR = color;
}
in Engine by (27 points)
edited by

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.