0 votes

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!

Godot version 3.5
in Engine by (83 points)

1 Answer

+1 vote
Best answer

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); 
by (1,109 points)
selected by

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?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.