+1 vote

Hello I tried to do simple dissolve effect in new visual shaded editor
And was not able to define how to create fringe effect. I done it with "IF" node in Unreal. Hope similar node comes in Godot 3.1.

How do I get new fringe alpha mask with alpha between X and Y from alpha mask texture in visual shader? ( I have tried subtracting but with no success)

in Engine by (13 points)
edited by

1 Answer

0 votes

This is a sharp Alpha cut off shader:

shader_type canvas_item;

uniform sampler2D Mask : hint_white; 
uniform float Clip : hint_range(0.0, 8.0, 0.005) = 1.0;

void fragment(){
    float AlphaCut = 1.0;

    if (texture(Mask,UV).r < Clip) {
        AlphaCut = 0.0;
    }
    COLOR = vec4(texture(TEXTURE,UV).rgb, AlphaCut);
}

Unfortunately "fringe effect" is a name used for so many effects I don't really know what you want.
If you shared more details I could make you a example.

by (1,443 points)

Thank you for answer.
I was talking about simple dissolve shader, something like this
dissolve shaded youtube
But the main question was can we do sick thing in visual shaded, because it has previews it is more convenient to wark in busal editor,
At least for me).

I was talking about simple dissolve shader, something like this

What you have to realize is that the shader uses a trick. First it colors the object then it "cuts" a piece of it. Like this:
enter image description here
What that means is you need to do the edge calculation twice, once for color then again for alpha.
This is the code version:

shader_type canvas_item;

uniform sampler2D Mask : hint_white; 
uniform float Clip : hint_range(0.0, 1.0, 0.005) = 1.0;
uniform float EdgeThickness = 0.05;
uniform vec4 EdgeColor : hint_color;

void fragment(){
    float AlphaCut = 1.0;
    vec3 color = texture(TEXTURE,UV).rgb;

    //First we color the material
    if (texture(Mask,UV).r  < Clip) {
        color = EdgeColor.rgb
    }
    //This is the transparent cut, it has to be a little above the color
    //to get the edge
    if (texture(Mask,UV).r +EdgeThickness < Clip) {
        AlphaCut = 0.0;
    }
    COLOR = vec4(color, AlphaCut);
}

Very simple, However in a visual shader we have no control flow options like IF AND or LOOP. This is intentional as you need some kind of indication of how flow works, and visual shaders just don't have that.

So we go old school, use math to solve the problem.
Once such formula is: The floor of Atan2, clamped to 1, then inverted with 1-X.
We do it twice to get the effect:
enter image description here
So much easier than just a few lines of code. ;) Here is the shader:
https://drive.google.com/open?id=14yjOdJlZ_bGsAK7Zggraaph7y3Gk20H_

Here we can see the code and visual shader side by side:
enter image description here

As you can see both works. The visual shader replaces the complexity of code, with the complexity of math.
Godot's developers will need to add flow options like Unreal's blueprints.
It is actually possible to make a IF function using math, that is indeed how it works, so if you know Boolean math you could make it yourself.

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 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 webmaster@godotengine.org with your username.