Affecting only the shader parameter color in shader

Godot Version

4.2

Question

I’m using a shader to make a “glowing” effect (and other things too) in a piece when I hovered the piece with the mouse, so it makes the outline of that piece to fade in and out based on the color selected for this outline.

Before, I was using a const in the shader, but now I’m using a uniform, so I can change this color at any moment easily. The problem is, since I was using a const before (and it was blue), I just set it to affect the COLOR.b parameter. Now that I’m using a uniform and then I can choose any color, how can I make it to affect only that color?

shader_type canvas_item;

// Original colors
uniform vec4 outline_color:source_color;
uniform vec4 base_color:source_color;
uniform vec4 shadow_color:source_color;
uniform vec4 light_color:source_color;

// New colors
uniform vec4 piece_outline:source_color;
uniform vec4 piece_base:source_color;
uniform vec4 piece_shadow:source_color;
uniform vec4 piece_light:source_color;

uniform vec4 hovered_outline:source_color; // This was a const before with value vec4(0.0, 0.0, 1.0, 1.0)
uniform bool is_covering = false;
uniform bool is_hovered = false;
const float transparency = 0.15;

void fragment() {
	vec4 current_pixel = texture(TEXTURE, UV);
	
	// Check outline color
	if (current_pixel == outline_color) {
		COLOR = piece_outline;
		if (is_hovered == true) {
			COLOR = hovered_outline;
			COLOR.b = hovered_outline.b * (clamp(cos(TIME * 4.0), 0.1, 2.0));
		} else COLOR = outline_color;
		if (is_covering == true) {
			COLOR.a = transparency;
			}
	}
	// Check base color
	else if (current_pixel == base_color) {
		COLOR = piece_base;
		if (is_covering == true) {
			COLOR.a = transparency;
			}
		
	}

	// Check shadow color
	else if (current_pixel == shadow_color) {
		COLOR = piece_shadow;
		if (is_covering == true) {
			COLOR.a = transparency;
			}
	}

	// Check light color
	else if (current_pixel == light_color) {
		COLOR = piece_light;
		if (is_covering == true) {
			COLOR.a = transparency;
			}
		
	}
	
	else COLOR = current_pixel;
}

Does this work?

		if (is_hovered == true) {
			COLOR = hovered_outline;
			COLOR. *=  (clamp(cos(TIME * 4.0), 0.1, 2.0));
		}

It didn’t :confused: I guess I need to know how to calculate the correct variations of that color, from darkest to brightest.

I think I solved it!

You need to change this part here:

to this:

float modified_outline = (hovered_outline.r * hovered_outline.g * hovered_outline.b) * 0.1;
COLOR.rgb = hovered_outline.rgb * (clamp(cos(TIME * 4.0), modified_outline, 2.0));
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.