我扩展了 Image 类,以将模糊着色器添加到该actor。对于全屏背景图像来说,它工作正常,但对于小演员而言,效果不佳。
我的图标是从TextureAtlas中提取的。
private ShaderProgram shader;
private float radius = 2f;
public BlurActor(Texture tex){
super(tex);
initShader();
}
public BlurActor(Sprite sprite){
super(sprite);
initShader();
}
private void initShader(){
shader = new ShaderProgram(Gdx.files.internal("shaders/blur.vert"), Gdx.files.internal("shaders/blur.frag"));
shader.begin();
shader.setUniformf("dir", 0.0f, 0.0f);
shader.setUniformf("resolution", getHeight());
shader.setUniformf("radius", radius);
shader.end();
}
@Override
public void draw(Batch batch, float alpha){
batch.setShader(shader);
shader.setUniformf("dir", 1.0f, 1.0f);
shader.setUniformf("radius", radius);
getDrawable().draw(batch, getX(), getY(), getWidth() * getScaleX(), getHeight() * getScaleY());
batch.setShader(null);
}
答案 0 :(得分:0)
根据Tenfour04的请求,以下是着色器文件:
版本:
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;
}
片段:
#ifdef GL_ES
precision mediump float;
precision mediump int;
#else
#define highp;
#endif
varying vec4 v_color;
varying vec2 v_texCoord;
uniform sampler2D u_texture;
uniform float resolution;
uniform float radius;
uniform vec2 dir;
void main() {
vec4 sum = vec4(0.0);
vec2 tc = v_texCoord;
// Number of pixels off the central pixel to sample from
float blur = radius/resolution;
// Blur direction
float hstep = dir.x;
float vstep = dir.y;
// Apply blur using 9 samples and predefined gaussian weights
sum += texture2D(u_texture, vec2(tc.x - 4.0*blur*hstep, tc.y - 4.0*blur*vstep)) * 0.006;
sum += texture2D(u_texture, vec2(tc.x - 3.0*blur*hstep, tc.y - 3.0*blur*vstep)) * 0.044;
sum += texture2D(u_texture, vec2(tc.x - 2.0*blur*hstep, tc.y - 2.0*blur*vstep)) * 0.121;
sum += texture2D(u_texture, vec2(tc.x - 1.0*blur*hstep, tc.y - 1.0*blur*vstep)) * 0.194;
sum += texture2D(u_texture, vec2(tc.x, tc.y)) * 0.27;
sum += texture2D(u_texture, vec2(tc.x + 1.0*blur*hstep, tc.y + 1.0*blur*vstep)) * 0.194;
sum += texture2D(u_texture, vec2(tc.x + 2.0*blur*hstep, tc.y + 2.0*blur*vstep)) * 0.121;
sum += texture2D(u_texture, vec2(tc.x + 3.0*blur*hstep, tc.y + 3.0*blur*vstep)) * 0.044;
sum += texture2D(u_texture, vec2(tc.x + 4.0*blur*hstep, tc.y + 4.0*blur*vstep)) * 0.006;
gl_FragColor = sum;
}