1.shader
v
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
varying vec4 v_color;
uniform mat4 u_projTrans;
varying vec2 v_textCoords;
void main() {
v_color = a_color;
v_color.a = v_color.a * (255.0/254.0);
v_textCoords = a_texCoord0;
gl_Position = u_projTrans * a_position;
}
f
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_color;
varying vec2 v_textCoords;
uniform sampler2D u_texture;
void main() {
float offset = 0.005;
float Gx = -1.0 * (v_color* texture2D(u_texture,v_textCoords + offset * vec2(-1.0, -1.0))).r+
1.0 * (v_color* texture2D(u_texture,v_textCoords + offset * vec2(1.0, -1.0))).r+
-2.0 * (v_color* texture2D(u_texture,v_textCoords + offset * vec2(-1.0, 0.0))).r+
2.0 * (v_color* texture2D(u_texture,v_textCoords + offset * vec2(1.0, 0.0))).r+
-1.0 * (v_color* texture2D(u_texture,v_textCoords + offset * vec2(-1.0, 1.0))).r+
1.0 * (v_color* texture2D(u_texture,v_textCoords + offset * vec2(1.0, 1.0))).r;
float Gy = -1.0 * (v_color* texture2D(u_texture,v_textCoords + offset * vec2(-1.0, -1.0))).r+
-2.0 * (v_color* texture2D(u_texture,v_textCoords + offset * vec2(0.0, -1.0))).r+
-1.0 * (v_color* texture2D(u_texture,v_textCoords + offset * vec2(1.0, -1.0))).r+
1.0 * (v_color* texture2D(u_texture,v_textCoords + offset * vec2(-1.0, 1.0))).r+
2.0 * (v_color* texture2D(u_texture,v_textCoords + offset * vec2(0.0, 1.0))).r+
1.0 * (v_color* texture2D(u_texture,v_textCoords + offset * vec2(1.0, 1.0))).r;
float edgeMagnitude = length(vec2(Gx, Gy));
if(edgeMagnitude>0.9){
gl_FragColor = vec4(0.0,0,0,1.0f);
}else{
discard;
}
}
2.使用
public class LightGroup extends Group {
private ShaderProgram program;
public LightGroup(){
program = new ShaderProgram(
Gdx.files.internal("flight/cir.vert"),
Gdx.files.internal("flight/cir.glsl")
);
Image image = new Image(Asset.getAsset().getTexture("cirtest.png"));
addActor(image);
}
@Override
public void draw(Batch batch, float parentAlpha) {
batch.flush();
batch.setShader(program);
super.draw(batch, parentAlpha);
batch.flush();
batch.setShader(null);
}
}
3.原图
处理后:
4.保存到本地
stage.addAction(Actions.delay(0.4f,Actions.run(()->{
Gdx.app.postRunnable(()->{
Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
try {
PixmapIO.writePNG(Gdx.files.local("screenshot.png"), pixmap);
} catch (Exception e) {
throw new RuntimeException(e);
}
// Image image = new Image(new Texture(pixmap));
// addActor(image);
run();
});
})));
注意:一定是 Gdx.app.postRunnable,不可以在Actions.run中直接执行.具体原因是二者执行时机不同