Masking color with a packed image

Godot Version

4.6.1

Question

I’m trying to write a shader that uses a packed image to mask colors. I have a packed image and I want to use each channel of the image to create a mask that I can set to a specific color.

Something like “color_1” would correspond to everything masked in the red channel, “color_2” for the blue, and “color_3” for the green. I’m used to working in Unreal, where I could use mask and lerp nodes to control this, but I haven’t figured out how to do this in Godot. I tried recreating that logic with:

vec3 color = inverse_lerp(color_1, (inverse_lerp(color_2, color_3, texture(mask_image, UV).b), texture(mask_image,UV).r);

But that throws an error that the “inverse_lerp” function doesn’t exist. I found it in the documentation here but I’m definitely using it wrong. I’m super new to Godot so any advice or information would be incredibly helpful!

That documentation is extremtly old (Godot 3.0), here is the correct documentation:

I was able to figure it out! for anyone looking for a quick way to do this, here is my code:

shader_type spatial;

uniform sampler2D mask;

void fragment() {
	vec3 color_1 = vec3(1,0,1);
	vec3 color_2 = vec3(0,1,0);
	vec3 color_3 = vec3(0,0,1);
	vec3 color = texture(mask,UV).rgb;
	
	ALBEDO = mix(mix(color_1, color_2, color.r), color_3, color.g);
}