且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Libgdx,Alpha文本使用Shader无法正常工作

更新时间:2023-01-26 12:56:47

我不是着色器专家但我已经成功地做到了这一点,我认为这是一个简单的方法。 (我可能会非常困惑)。

I am not a shader expert but I have done this successfully in what I think is an easy way. (I may be very confused).

在我的java代码中,我的Main函数中有一个变量,它包含alpha值(我只使用一个用于gamestate过渡):

In my java code I have a variable in my Main function that holds alpha values (I only use one for gamestate transitions):

public static float newAlph = 1;

在我的* .vert文件中,我声明了一个统一变量,将alpha作为输入,然后设置它是变量变量的值,所以.frag文件得到它。

In my *.vert file I declared a uniform variable to take the alpha as an input and then set it's value to a varying variable so the .frag file gets it.

...

uniform float u_newAlpha;
varying float v_newAlpha;

...

void main() {
    ...
    v_newAlpha = u_newAlpha;
}

然后在.frag文件中声明相同的变量变量并简单地乘以alpha。

Then in the .frag file i declare the same varying variable and simply multiply the alpha.

...
varying float v_newAlpha;
...

void main() {
    ...
    gl_FragColor = vec4(v_color.rgb, alpha*v_newAlpha);
}

最后在调用着色器时我设置了alpha值(这样你就可以了你在draw方法中的标签类)

Finally when calling the shader i set the alpha value(this you would do in your label class in the draw method)

sb.setShader(fontShader);
fontShader.setUniformf("u_newAlpha", Main.newAlph);

所以即使渲染的其余部分的alpha值正在改变,这个着色器也会渲染你的任何东西正在使用它拥有的alpha进行渲染。

So even if the alpha of the rest of everything that is rendering is changing this shader will render whatever you are rendering with the alpha it has.

在我的使用中,我使用了主类中的公共变量,但是你的draw方法将alpha作为输入,因此将其设置为制服.vert文件中的变量应该可以工作。

In mine I use a public variable from the main class but your draw method takes the alpha as an input so setting that to the uniform variable inside the .vert file should work.

因此你会得到:

public void draw(Batch batch, float parentAlpha) {
        if(shaderActive) {
            batch.setShader(Assets.assetFont.fontShader);
        }
        Assets.assetFont.fontShader.setUniformf("u_newAlpha", parentAlpha);
        super.draw(batch, parentAlpha);
        batch.setShader(null);          
    }

如果有意义,请告诉我!

Let me know if that makes sense!