0 votes

Hey,
I've written the following shader code, which is working just fine.
Only issue is that it's used for a mobile game and slows down fps on older devices. I'm now trying to optimize the code, but have no idea what to do. Can someone help me or give me some hints?

The code is fading an object in on the x axis. I use clip height to determine the coordinate where to fade out the object. fade offset determines, how roughly the object is faded.

Would be appreciated!

shader_type spatial;
render_mode blend_mix,depth_draw_alpha_prepass,cull_disabled,diffuse_burley,specular_schlick_ggx;
uniform vec4 albedo : hint_color = vec4(1,1,1,1);
uniform vec4 statusAlbedo : hint_color = vec4(1,1,1,1);
uniform vec4 unfocusAlbedo : hint_color = vec4(1,1,1,1);
uniform sampler2D texture_albedo : hint_albedo;
uniform float specular;
uniform float metallic;
uniform float roughness : hint_range(0,1);
uniform float point_size : hint_range(0,128);
uniform sampler2D texture_metallic : hint_white;
uniform vec4 metallic_texture_channel;
uniform sampler2D texture_roughness : hint_white;
uniform vec4 roughness_texture_channel;
uniform vec3 uv1_scale;
uniform vec3 uv1_offset;
uniform vec3 uv2_scale;
uniform vec3 uv2_offset;

uniform float clip_height = 0;
uniform float fade_offset = 0.1;
uniform int axis = 1;

uniform float alphaFrom;
uniform float alphaTo;

void vertex() {
    UV=UV*uv1_scale.xy+uv1_offset.xy;
}

void fragment() {
    vec2 base_uv = UV;
    vec4 albedo_tex = texture(texture_albedo,base_uv);
    ALBEDO = albedo.rgb * statusAlbedo.rgb * unfocusAlbedo.rgb * albedo_tex.rgb;
    float metallic_tex = dot(texture(texture_metallic,base_uv),metallic_texture_channel);
    METALLIC = metallic_tex * metallic;
    float roughness_tex = dot(texture(texture_roughness,base_uv),roughness_texture_channel);
    ROUGHNESS = roughness_tex * roughness;
    SPECULAR = specular;

    vec4 world_vertex = CAMERA_MATRIX * vec4(VERTEX, 1.0);


    float pre_alpha =  albedo.a * albedo_tex.a;


    ALPHA = smoothstep(alphaFrom, alphaTo, (world_vertex.x - clip_height)/fade_offset) * pre_alpha;
}
asked Apr 24 in Engine by TobiLa (160 points)

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.