This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

Shader on Sprite3D doesn't work on all images

LouisSimon22LouisSimon22 Posts: 6Member

Hello everyone,

I was playing around with shaders on Sprite3D.
Here's my simple shader (it hides pixels whose X is greater than progress):

shader_type spatial;

uniform sampler2D texture_albedo : hint_albedo;
uniform float progress: hint_range(0, 1);

void fragment() {
    vec4 albedo_tex = texture(texture_albedo, UV);
    ALBEDO = albedo_tex.rgb;
    ALPHA = ceil(progress - UV.x);
}

I apply it on the sprite3D through an override shader material.

Is the problem with the image or with the shader?
Demo

Here's the original image
Here's the image

Best Answer

  • TwistedTwiglegTwistedTwigleg Posts: 2,561
    Accepted Answer

    I think the issue is the shader is not taking the image's alpha into account, which is why the image is all black.

    I made a few adjustments, and while I have not tested it, I believe the shader should now take the alpha of the image into account, while also working with the progress range.

    shader_type spatial;
    uniform sampler2D texture_albedo : hint_albedo;
    uniform float progress: hint_range(0, 1);
    void fragment()
    {
        vec4 albedo_tex = texture(texture_albedo, UV);
        ALBEDO = albedo_tex.rgb;
        ALPHA = albedo_tex.a;
    
        // May need to be flipped. I do not remember if UVs start at 1 or 0...
        if (UV.x > progress)
        {
            ALPHA = 0;
        }
    }
    

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 2,561Admin
    Accepted Answer

    I think the issue is the shader is not taking the image's alpha into account, which is why the image is all black.

    I made a few adjustments, and while I have not tested it, I believe the shader should now take the alpha of the image into account, while also working with the progress range.

    shader_type spatial;
    uniform sampler2D texture_albedo : hint_albedo;
    uniform float progress: hint_range(0, 1);
    void fragment()
    {
        vec4 albedo_tex = texture(texture_albedo, UV);
        ALBEDO = albedo_tex.rgb;
        ALPHA = albedo_tex.a;
    
        // May need to be flipped. I do not remember if UVs start at 1 or 0...
        if (UV.x > progress)
        {
            ALPHA = 0;
        }
    }
    
  • LouisSimon22LouisSimon22 Posts: 6Member

    Thanks again TwistedTwigleg.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.