How to draw transparent pixels to texture?

ondesicondesic Posts: 3Member

Could someone help me? In other engines I have been able to take a texture or image and draw transparency to it causing a hole in the image. Does anyone know how to do this in godot?

Answers

  • MegalomaniakMegalomaniak Posts: 1,087Admin

    shaders? renderTargets(viewports)... What are you actually trying to achieve?

  • ondesicondesic Posts: 3Member

    Thank you for responding! There are a few things I want to do.
    One of them is I want to have an image of a landscape. Then I want to destroy parts of it like a Worms or Scorched earth game.
    Another thing I am trying to do is create a "fog of war" layer that has a black image that I can draw a transparent circle to. Because it is permanently drawn, I can keep revealing the map as the player walks.

    I haven't learned about shaders yet. I have always done this by drawing transparent pixels to a large black image

  • TwistedTwiglegTwistedTwigleg Posts: 1,049Admin

    I made a texture baker (for baking PBR textures in Godot), but I've yet to find a way to export//get transparent textures from Viewports. If there is any way to get transparent textures from Viewports, I'd also be interested to know :smile:

    I'm not sure on the terrain stuff, but I'd look at the Fog of War example from the Godot 2 demos. It may help give you an idea on where to start.

    I think you could try a black and white bit mask for the terrain, and then use a shader to hide/discard the terrain texture at the correct spots. I think I have a shader from Godot 2 that hides/discards based on a black and white image... Let me know if you want to see it and I'll see if I can find it.

  • ondesicondesic Posts: 3Member

    Yes, id like to see it. Thank you. It sounds like I need to learn shaders.

  • TwistedTwiglegTwistedTwigleg Posts: 1,049Admin

    Ok, here's two different shaders I made. One that clips based on the background, and another that clips based on the objects color. Both of these shaders were written for Godot 2.1, and could probably use some fixes/performance boosts, since I made them back when I was experimenting with shaders.

    Both of the shaders are fragment shaders.


    Clips based on background

    uniform color background=color(0.392157, 0.392157, 0.392157, 1);
    uniform float range;
    if (texscreen(SCREEN_UV).r >= background.r - range && texscreen(SCREEN_UV).r <= background.r + range)
    {
        if (texscreen(SCREEN_UV).g >= background.g - range && texscreen(SCREEN_UV).g <= background.g + range)
        {
            if (texscreen(SCREEN_UV).b >= background.b - range && texscreen(SCREEN_UV).b <= background.b + range)
            {
                COLOR.w *= 0;
            }
            else
            {
                COLOR.a = tex(TEXTURE, UV).a;
            }
        }
        else
        {
            COLOR.a = tex(TEXTURE, UV).a;
        }
    }
    else
    {
        COLOR.a = tex(TEXTURE, UV).a;
    }

    Clips based on object

    uniform color Object_color;
    uniform float Range;
    if (texscreen(SCREEN_UV).r >= Object_color.r - Range && texscreen(SCREEN_UV).r <= Object_color.r + Range)
    {
        if (texscreen(SCREEN_UV).g >= Object_color.g - Range && texscreen(SCREEN_UV).g <= Object_color.g + Range)
        {
            if (texscreen(SCREEN_UV).b >= Object_color.b - Range && texscreen(SCREEN_UV).b <= Object_color.b + Range)
            {
                COLOR.a = tex(TEXTURE, UV).a;
            }
            else
            {
                COLOR.w *= 0;
            }
        }
        else
        {
            COLOR.w *= 0;
        }
    }
    else
    {
        COLOR.w *= 0;
    }
  • JacorJacor Posts: 1Member

    Have you gotten anywhere with these shaders for adding transparency to the background?

Sign In or Register to comment.