我在使用舞台操作在文本(使用着色器)中应用 alpha 时遇到一些问题。我正在使用添加窗口演员和窗口添加标签演员的组。我组中的其他演员应用 Actions alpha 工作得很好,但我的标签(使用着色器绘制)不起作用,与其他小部件不同的是我的文本标签正在应用着色器。
字体片段
统一sampler2D u_texture;
varying vec4 v_color;
varying vec2 v_texCoord;
const float smoothing = 0.2;
void main() {
float distance = texture2D(u_texture, v_texCoord).a;
float alpha = smoothstep(0.5 - smoothing, 0.5 + smoothing, distance);
gl_FragColor = vec4(texture2D(u_texture, v_texCoord).rgb, alpha);
}
字体.vert
uniform mat4 u_projTrans;
attribute vec4 a_position;
attribute vec2 a_texCoord0;
attribute vec4 a_color;
varying vec4 v_color;
varying vec2 v_texCoord;
void main() {
gl_Position = u_projTrans * a_position;
v_texCoord = a_texCoord0;
v_color = a_color;
}
我的 Label 将标签从 Libgdx 扩展到应用着色器,
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Batch;
public class Label extends com.badlogic.gdx.scenes.scene2d.ui.Label {
boolean shaderActive = false;
public Label(CharSequence text, LabelStyle style) {
super(text, style);
if(style.font.getScaleX() > 1f)
shaderActive = true;
}
@Override
public void draw(Batch batch, float parentAlpha) {
if(shaderActive) {
batch.setShader(Assets.assetFont.fontShader);
}
super.draw(batch, parentAlpha);
batch.setShader(null);
}
}
工作代码
private void initStage() {
// TODO Stage Here
stage = new Stage();
stage.setViewport(viewport);
Window w2 = new Window("",
new WindowStyle(Assets.assetFont.blackBoldVSmall, Color.BLACK,
new Image(Assets.assetHUD.windowFrame).getDrawable()) );
w2.setSize(1200, 675);
w2.setPosition(40, 0);
Label lbl2 = new Label("Touch here to Jump !", new LabelStyle(Assets.assetFont.goldBoldNormal, Color.GRAY));
lbl2.setPosition(Settings.width/2, Settings.height/2);
w2.add(lbl2);
groupTouchHere.addActor(w2);
stage.addActor(groupTouchHere);
// sequence of action
SequenceAction sequence = new SequenceAction(
Actions.fadeIn(0.4f, Interpolation.circleOut),
Actions.fadeOut(0.4f, Interpolation.circleIn));
SequenceAction sequenceTotal = new SequenceAction(sequence, Actions.repeat(4, sequence));
groupTouchHere.addAction(sequenceTotal);
}
public void update(float deltaTime){
stage.act();
}