我在应用alpha文字后再次遇到麻烦,它来自here的下一个主题stackoverflow
现在首先,我想在每个字符的文本中创建一些动画淡入淡出。我知道使用TemporalAction来创建它。麻烦的是,当标签/文字再次使用着色器时,阿尔法不能应用#39; (当我关闭着色器时,它正在工作)
这里是动作代码,当我创建动画
时 final Label lblR = new Label("READY!", new LabelStyle(Assets.assetFont.goldBoldBig, Color.BLACK));
lblR.setPosition(Settings.width/2 - lblR.getWidth()/2, Settings.height/2 - lblR.getHeight()/2);
DisplayTextAction action = Actions.action(DisplayTextAction.class);
action.setDuration(0.3f);
action.setText(lblR.getText());
lblR.addAction(Actions.sequence(action, Actions.delay(0.4f), Actions.fadeOut(0.4f)));
现在我包含了我的所有文件,
DisplayTextAction
public class DisplayTextAction extends TemporalAction{
public CharSequence completeText;
private Label actor_clone;
private int length_unreveal = 0;
private int length_reveal = 0;
@Override
protected void begin() {
actor_clone = new Label(completeText, new LabelStyle( ((Label)actor).getStyle().font, ((Label)actor).getStyle().fontColor ));
actor_clone.setPosition(((Label)actor).getX(), ((Label)actor).getY());
}
protected void update(float percent) {
if( (length_reveal == 0
|| (int)Math.round(completeText.length()*percent) + 1 != length_reveal )
&& (int)Math.round(completeText.length()*percent) + 1 < completeText.length()) {
length_reveal = (int)Math.round(completeText.length()*percent) + 1;
actor_clone.setText(completeText.subSequence(0, length_reveal));
actor_clone.clearActions();
actor_clone.addAction(Actions.alpha(0f));
actor_clone.addAction(Actions.fadeIn(getDuration()/completeText.length()));
}
((Label)actor).setText(
completeText.subSequence(
0,
(int)Math.round(completeText.length()*percent)));
}
public void setText(CharSequence charSequence){
this.completeText = charSequence.toString();
}
}
垂直字体
uniform mat4 u_projTrans;
attribute vec4 a_position;
attribute vec2 a_texCoord0;
attribute vec4 a_color;
uniform float u_newAlpha;
varying vec4 v_color;
varying vec2 v_texCoord;
varying float v_newAlpha;
void main() {
gl_Position = u_projTrans * a_position;
v_texCoord = a_texCoord0;
v_color = a_color;
v_newAlpha = u_newAlpha;
}
FRAG
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D u_texture;
varying vec4 v_color;
varying vec2 v_texCoord;
varying float v_newAlpha;
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*v_newAlpha);
}
从标签scene2d扩展标签
public class Label extends com.badlogic.gdx.scenes.scene2d.ui.Label {
boolean shaderActive = false;
ShaderProgram shader;
public Label(CharSequence text, LabelStyle style) {
super(text, style);
if(style.font.getScaleX() > 1f) {
shaderActive = true;
shader = Assets.assetFont.fontShader;
}
}
@Override
public void draw(Batch batch, float parentAlpha) {
if(shaderActive) {
batch.setShader(shader);
shader.setUniformf("u_newAlpha", parentAlpha);
super.draw(batch, parentAlpha);
batch.setShader(null);
}else {
super.draw(batch, parentAlpha);
}
}
}
你可以在我的动作中看到,我在Actions中创建了新的我的扩展标签,(我想创建CLONE,但我可以&#39; t,所以我手动创建)。然后我就对它采取行动。第一个标签动画播放时,第二个将创建并播放第一个标签(+1个字符)的每个字符。
我不知道为什么alpha动画没有在它上面工作,(当它适用于着色器时)我尝试使用其他正在运行的动作。我也试图关闭着色器并且它也在工作。之前关于在着色器字体中使用alpha的线程也正常工作。但现在它没有用。
(也许是关于在行动中创造演员,所以阿尔法不再工作了?我不知道。)
THX。
答案 0 :(得分:1)
shader.setUniformf("u_newAlpha", getColor().a * parentAlpha);