Help modifying a shader workflow

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RuKeN

The awesome team of GDQuest have an awesome project with multiples shaders that can be found here:

On of their awesome shaders is named “SphereMask” a shader that is able to make a 3D object invisible and when a sphere collider enters in contact with that object reveals it:

The thing is that in my project, the object I want to hide have a texture with details and this shader is limited to display the object using a color and not it’s original texture

Can anyone help me to find a way to modify this shader to make it display the object in a normal way instead of show the object with a color? Shaders code is extremely different of what GDScript is

Thanks in advance!

:bust_in_silhouette: Reply From: Ninfur

The ALBEDO variable in the fragment shader is the output color per pixel.

Right now it’s set to a mix of object_color and mask_color, which both seems to be fixed colors.

If the shader is running in a secondary pass, after the textured material, I believe you can do something like this to keep the existing color:

ALBEDO = mix(ALBEDO, mask_color.rgb, mask_value); 

If you need to include a texture in the shader, add ‘uniform sampler2D texture;’ somewhere among the other uniform’s. This will add a texture shader parameter.

To read the texture, add ‘vec4 texture_color = texture(texture, UV);’ somwhere in the fragment shader, and finally change the ALBEDO to mix with the texture color.

uniform sampler2D texture;

void fragment() {
    // ...
    vec4 texture_color = texture(texture, UV);
    ALBEDO = mix(texture_color.rgb, mask_color.rgb, mask_value); 
}

Wow thanks for bring a solution and explaining it!

Just thinking in performance, I suppose that fragment() is similar to the process() function right? Is there a ready() function to get the texture just one time instead of in each loop?

RuKeN | 2022-08-30 12:43