MToonShader with dither fade turns black instead of transparent (works only if initial uniform is 0)

Godot Version

Godot 4.5.1

Question

I’m modifying the MToonShader to add dither fade effect to it. The shader works perfectly if I set the opacity uniform to 0 in the editor before running the game. I can change the value in gdscript without any issues. However if I start the game with opacity to 1, when I decrease the opacity using gdscript, it turns black instead of transparent.

I’ve tried added depth_draw_always and depth_prepass_alpha but no luck.

Here’s the video showing the issue: Video Unavailable

This is shader snippet inside fragment()

render_mode skip_vertex_transform;
uniform float ditherOpacity = 0;
const float bayer4x4[16] = float[](
	0.0 / 16.0,  8.0 / 16.0,  2.0 / 16.0, 10.0 / 16.0,
	12.0 / 16.0, 4.0 / 16.0, 14.0 / 16.0, 6.0 / 16.0,
	3.0 / 16.0, 11.0 / 16.0, 1.0 / 16.0,  9.0 / 16.0,
	15.0 / 16.0, 7.0 / 16.0, 13.0 / 16.0, 5.0 / 16.0
);

float fade_opacity = ditherOpacity;
if (fade_opacity <= 0.001) {
	discard;
}
if (fade_opacity < 1.0) {
	int x = int(mod(FRAGCOORD.x, 4.0));
	int y = int(mod(FRAGCOORD.y, 4.0));
	int index = y * 4 + x;
	float threshold = bayer4x4[index];

	if (fade_opacity <= threshold) {
		discard;
	}
}

Figured out by myself now while it took 2 days to get my post approved, I can’t believe it took this long.

I need to set the shader parameter of next_pass of the material as well, so I assumed they are outline material

for mat in materials:
	mat.set_shader_parameter("opacity", opacity)
	#Add these two lines and it works fine
	if mat.next_pass:
		mat.next_pass.set_shader_parameter("opacity", opacity)

I really hope that this reply doesn’t need approval.

1 Like