Shader not working at runtime.
Asked Answered
B

3

0

This shader i wrote is suppose to take the background sample color provided by user, and make it transparent.
It seems to work while in the editor but when i run it, the background color is not transparent.

Shader is applied to an animated sprite.

shader_type canvas_item;
uniform vec4 bg_color : hint_color;

void fragment(){
	vec4 col = texture(TEXTURE,UV).rgba;
	if (col == bg_color){
		col.a = 0.0;
	}
	COLOR = col;
}
Beatrizbeattie answered 13/9, 2022 at 22:12 Comment(0)
B
0

Looks like i found someone with a similar issue, and changed it to this. Works now.

shader_type canvas_item;
uniform vec4 bg_color : hint_color;

void fragment(){
	vec4 col = texture(TEXTURE,UV).rgba;
	vec4 d4 = abs(col - bg_color);
	float d = max(max(d4.r,d4.g),d4.b);
	if (d < 0.01){
		col.a = 0.0;
	}
	COLOR = col;
}
Beatrizbeattie answered 14/9, 2022 at 0:9 Comment(0)
L
0

Beatrizbeattie Works now.

You might want to set your follow up post as the answer then. 🙂

Lechery answered 14/9, 2022 at 1:33 Comment(0)
S
0

The reason it likely didn't work was because of floating point inaccuracy. For example, gray can be 0.5 (on all 3 channels). But in the computer, it might be stored as 0.50000001 or 0.500004 or something not quite exactly 0.5. So you can't compare floating point numbers that way (or composite objects, that are made up of floats/doubles like vector or color). However, it should not have worked in the editor either, the code was just incorrect. So I'm not sure how it ever would work.

Slander answered 14/9, 2022 at 2:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.