Is it possible to make a shader with an optional texture
Asked Answered
F

4

0

In this shader there are 2 images that will always exist: base_texture and frame_texture. But sometimes an object that uses this shader can also have an optional texture: overlay_texture. How can I check if this texture was sent or if it is <empty>?

I found a tip to check the alpha of the optional texture and only use it if it is greater than 0.0, but when I do this it only leaves a white image and not the combination that should exist.

shader_type spatial;

uniform sampler2D base_texture : source_color;
uniform sampler2D frame_texture : source_color;
uniform sampler2D overlay_texture : source_color;

void fragment() {

    vec4 base_color = texture(base_texture, UV);
    vec4 frame_color = texture(frame_texture, UV);
    vec4 overlay_color = texture(overlay_texture, UV);
    vec4 combined_color = mix(base_color, frame_color, frame_color.a);
    vec4 final_color;

    if (overlay_color.a > 0.0) {
        final_color = mix(combined_color, overlay_color, overlay_color.a);
    } else {
        final_color = combined_color;
    }

    ALBEDO = final_color.rgb;
    ALPHA = final_color.a;

}

Is there any correct way to check if the texture is set or <empty>?

Fulviah answered 26/9, 2023 at 0:56 Comment(0)
B
0

Fulviah A shader has no way of knowing if texture is assigned to a sampler or not. The default sampled value when there is no texture may vary between gpu drivers so you can't rely on it. Either make two versions of the shader (with and without overlay) or assign a dummy (1x1 pixel) transparent texture in case there is no overlay. That way the shader can always mix, without needing to check.

Boykins answered 26/9, 2023 at 1:25 Comment(0)
F
0

Boykins I got it, thanks.

One more question:
Could I then create two different material shaders, one to combine 2 textures and another to combine 3 textures and change the material shader of a MeshInstance3D node via gdscript?

Thank you for your help

Fulviah answered 26/9, 2023 at 12:11 Comment(0)
B
1

Fulviah Sure, that's the idea. You can assign to MeshInstance3D::material_override

Boykins answered 26/9, 2023 at 13:12 Comment(0)
F
0

Boykins thanks! This was very useful \o/

Fulviah answered 26/9, 2023 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.