Changing between alpha channel using if statement

Godot Version

Godot_v4.6.1-stable

Question

I have encounter a very strange problem, I was building a cel shader with varies function and everything works exactly that it should but, when i tried to switch the alpha

eg. making it use alpha or alpha scissor

i have encounter a very strange issue.

The very strange thing happens is if i only put the following code in one file:

//alpha 
vec4 albedo_tex = texture(albedo_texture, base_uv);
ALPHA *= albedo_tex.a;
ALPHA_SCISSOR_THRESHOLD = alpha_scissor_threshold;

or only this:

//alpha 
vec4 albedo_tex = texture(albedo_texture, base_uv);
//ALPHA *= albedo_tex.a;
ALPHA = alpha;

it works according what i have planned.

However, the issue arises when i combined it using an if else statement and a Boolean or anything that can makes a switch between 2 style, only alpha scissor works and the alpha makes the mesh straight up disapear if the value is less than 1.0

May I what is the best course of action how to tackle this problem or how do I solve it? Preferably, i would like to have a single file instead of 2 shader file.

Can you post the code that doesn’t work and say exactly what you expect and what happens instead?

the part that doesn’t works is the one that i posted earlier, it was strange enough when by itself, the code works, however when I combine it into a if else statement with a boolean check, when it switched to the

//alpha 
vec4 albedo_tex = texture(albedo_texture, base_uv);
//ALPHA *= albedo_tex.a;
ALPHA = alpha;

this part,

strangely it just straight up disappear when the alpha value is less than 1.0.

As i mention before, if i replace this whole part with this with this

//alpha scissor
vec4 albedo_tex = texture(albedo_texture, base_uv);
ALPHA *= albedo_tex.a;
ALPHA_SCISSOR_THRESHOLD = alpha_scissor_threshold;

it can show what it supposed to do,

eg:

the one with alpha scissor can make the transparent part of the image transparent while the one with just alpha, it just make the whole mesh being transparent.

however when combine them both using an if else statement, from my understanding i believe only one of them will work at a time, the one with just alpha, just straight up disappear when the value is less than 1.0

for more info,

shader_type spatial;
render_mode depth_draw_opaque, specular_schlick_ggx;

here’s the render mode.

thanks for helping

Ok, can you please post the entirety of the code, the one with the if statement.. details matter :slight_smile:

Shader’s transparency mode is decided prior to compilation. You cant branch it. If you write to ALPHA anywhere in the shader, the mode will be alpha. Ditto for ALPHA_SCISSOR_THRESHOLD. This is determined pre-compilation so it doesn’t matter if your code branches in a way that never reaches those assignments.

So it’ll have to be either two shaders or compiler preprocessor switching using #define and #ifdef.

1 Like

can you please explain how can I use #define and #ifdefine ?

I am not very sure how does that works, also which one do you this is more cheap in terms of processing? i heard to have the game optimize, having the less material and the less shadercode file is better isn’t it?

the one i have posted is the one i tried with if statement, sadly i have deleted the one i have experimented it :C

It works prior to compilation, same as C preprocessor, look that up. The thing is, you can’t switch at runtime with this. If you need runtime switching, use two shaders.

1 Like

I see, that clears thing up, thank you for helping :D!

1 Like