2015-08-25 48 views
2

我一直在尋找一種將模糊效果應用於簡單移動應用程序的屏幕截面的方法,如圖所示:使用libgdx在屏幕的矩形中應用模糊效果使用libgdx的模糊效果

enter image description here

如何使用libgdx實現這種效果?

+1

該問題可以分爲幾個子問題:(1)如何模糊紋理,然後繪製它;(2)如何將屏幕渲染到紋理; (3)如何僅將部分屏幕渲染成紋理這些子問題可以解決嗎,哪些是你面臨的困難? –

+1

你必須使用[着色器](https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson5),[這裏](https://github.com/mattdesl/lwjgl-basics/wiki/OpenGL- ES-Blurs),你可以看到一個很好的介紹模糊效果... –

+0

要麼使用着色器,要麼這裏是一個簡單的方法。採取一個模糊的PNG文件約20 * 20。現在因爲它是一個小文件,所以不會佔用太多內存,並且如果你需要多次使用它,你可以將精靈縮放到任何你想要的大小。 –

回答

0

的proccess是:

  1. 渲染場景的幀緩衝
  2. 渲染的幀緩存與默認着色器
  3. 屏幕呈現的幀緩衝矩形用藍色着色

僞代碼來演示基本算法。

RenderOnTexture rot=new RenderOnTexture(1); 
Shader blurShader=new Shader("shaders/blur.frag","shaders/blur.vert"); 
Shader defaultShader=new Shader("shaders/default.frag","shaders/default.vert"); 

public void render(){ 
    Director.spriteBatch.setShader(defaultShader); 
    //on your render method 
    rot.begin(); 

//render your scene as usual(it will be stored to the framebuffer) 
scene.render(); 

//render the framebuffer to the Screen with the default shader 
TextureRegion regionScene=rot.endAndRender(); 

Director.spriteBatch.setShader(blurShader); 

//Now render the blurred rectange using the regionScene TextureRegion 



    } 


public class RenderOnTexture { 



private float m_fboScaler = 1.5f; 
private boolean m_fboEnabled = true; 
private FrameBuffer m_fbo = null; 
private TextureRegion m_fboRegion = null; 
OrthographicCamera camera; 
public RenderOnTexture(float scale) { 
    int width = (int) (Gdx.graphics.getWidth()*scale); 
    int height = (int) (Gdx.graphics.getHeight()*scale); 
    m_fbo = new FrameBuffer(Format.RGB565, (int)(width * m_fboScaler), (int)(height * m_fboScaler), false); 
    m_fboRegion = new TextureRegion(m_fbo.getColorBufferTexture()); 
    m_fboRegion.flip(false,false); 
    camera=new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
} 
public gColor clearColor=new gColor(0,0,0,1); 
public void begin(){ 
    if(m_fboEnabled) 
    {     
     m_fbo.begin(); 
     Gdx.gl.glClearColor(clearColor.r,clearColor.g,clearColor.b,clearColor.a); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    } 
} 
public TextureRegion end(){ 
    if(m_fbo != null) 
    { 
     m_fbo.end(); 
     return m_fboRegion; 
    } 
    return null; 
} 
public TextureRegion endAndRender(){ 
    if(m_fbo == null)return null; 
    m_fbo.end(); 
    Director.spriteBatch.setProjectionMatrix(camera.combined); 
    Director.spriteBatch.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE); 
    Director.spriteBatch.disableBlending(); 
    boolean draw=Director.spriteBatch.isDrawing(); 
    if(!draw)Director.spriteBatch.begin(); 
    Director.spriteBatch.setColor(1,1,1,1); 
    Director.spriteBatch.draw(m_fboRegion,-Gdx.graphics.getWidth()/2, -Gdx.graphics.getHeight()/2, Gdx.graphics.getWidth(),Gdx.graphics.getHeight()); 
    if(!draw)Director.spriteBatch.end(); 
    return m_fboRegion; 
} 
} 

模糊片段着色器(你需要傳遞的寬度和高度(屏幕)的屬性。

#ifdef GL_ES 
#define LOWP lowp 
precision mediump float; 
#else 
#define LOWP 
#endif 

varying vec4 v_color; 
varying vec2 v_texCoords; 
uniform sampler2D u_texture; 

varying vec4 vertexWorldPos; 
uniform vec4 backColor; 

uniform float width; 
uniform float height; 
void main() { 
    vec2 tex; 
    vec4 color2=vec4(0.0,0.0,0.0,0); 

float stepx=(1.0/width)*4.0; 
float stepy=(1.0/height)*4.0; 
vec4 color; 

tex.x=v_texCoords.x+stepx; 
tex.y=v_texCoords.y+stepy; 
color=v_color * texture2D(u_texture, tex); 
color2.r+=color.r; 
color2.g+=color.g; 
color2.b+=color.b; 
color2.a+=color.a; 

tex.x=v_texCoords.x-stepx; 
tex.y=v_texCoords.y+stepy; 
color=v_color * texture2D(u_texture, tex); 
color2.r+=color.r; 
color2.g+=color.g; 
color2.b+=color.b; 
color2.a+=color.a; 

tex.x=v_texCoords.x-stepx; 
tex.y=v_texCoords.y-stepy; 
color=v_color * texture2D(u_texture, tex); 
color2.r+=color.r; 
color2.g+=color.g; 
color2.b+=color.b; 
color2.a+=color.a; 

tex.x=v_texCoords.x+stepx; 
tex.y=v_texCoords.y-stepy; 
color=v_color * texture2D(u_texture, tex); 
color2.r+=color.r; 
color2.g+=color.g; 
color2.b+=color.b; 
color2.a+=color.a; 

tex.x=v_texCoords.x; 
tex.y=v_texCoords.y; 
color=v_color * texture2D(u_texture, tex); 
color2.r+=color.r; 
color2.g+=color.g; 
color2.b+=color.b; 
color2.a+=color.a; 

color2.r/=5.0; 
color2.g/=5.0; 
color2.b/=5.0; 
color2.a/=5.0; 


gl_FragColor = color2; 

} 

很抱歉的代碼格式,在這裏我是唯一一個誰有代碼編輯器的問題?...

+0

我對來自文件夾「着色器」的文件感到困惑。你包含的第二塊代碼是一個blur.vert或default.vert?或者兩者都是? –