From 8fd8308d477de06b36d30a4772b28cf9ee4528bb Mon Sep 17 00:00:00 2001 From: Recrown Date: Mon, 3 Sep 2018 19:31:16 -0500 Subject: [PATCH] improved bloom system --- android/assets/shaders/bright_filter.fsh | 2 +- android/assets/shaders/gaussian_blur.fsh | 67 +++++++++++-------- android/assets/shaders/gaussian_blur.vsh | 15 +++++ .../audio/visualizer/CircularVisualizer.java | 1 - .../graphics/shaders/BloomShader.java | 58 ++++++++++------ .../desktop/audio/PCMObtainer.java | 2 +- .../desktop/screens/main/MainScreen.java | 4 +- 7 files changed, 96 insertions(+), 53 deletions(-) create mode 100755 android/assets/shaders/gaussian_blur.vsh diff --git a/android/assets/shaders/bright_filter.fsh b/android/assets/shaders/bright_filter.fsh index 3921e9f..8338ba4 100755 --- a/android/assets/shaders/bright_filter.fsh +++ b/android/assets/shaders/bright_filter.fsh @@ -17,6 +17,6 @@ void main() { if (brightness > 0.7) { gl_FragColor = color; } else { - gl_FragColor = vec4(0.0); + gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); } } diff --git a/android/assets/shaders/gaussian_blur.fsh b/android/assets/shaders/gaussian_blur.fsh index 2143387..8f927e9 100755 --- a/android/assets/shaders/gaussian_blur.fsh +++ b/android/assets/shaders/gaussian_blur.fsh @@ -1,34 +1,47 @@ -#version 330 core -#ifdef GL_ES -#define LOWP lowp -precision mediump float; -#else -#define LOWP -#endif - -varying LOWP vec4 vColor; +varying vec4 vColor; varying vec2 vTexCoord; -uniform int horizontal; uniform sampler2D u_texture; -uniform float weight[5] = float[] (0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216); +uniform vec2 resolution; +uniform float radius; +uniform int pass; void main() { - vec2 tex_offset = 1.0 / textureSize(u_texture, 0); - vec4 color = texture(u_texture, vTexCoord); - vec3 result = color.rgb * weight[0]; + //this will be our RGBA sum + vec4 sum = vec4(0.0); + + //our original texcoord for this fragment + vec2 tc = vTexCoord; + + //the amount to blur, i.e. how far off center to sample from + //1.0 -> blur by one pixel + //2.0 -> blur by two pixels, etc. + float blur = radius/resolution.y; + + //the direction of our blur + //(1.0, 0.0) -> x-axis blur + //(0.0, 1.0) -> y-axis blur + float hstep = 1.0; + float vstep = 0.0; + + if (pass == 1) { + hstep = 0.0; + vstep = 1.0; + } + + sum += texture2D(u_texture, vec2(tc.x - 5.0*blur*hstep, tc.y - 5.0*blur*vstep)) * 0.014374; + sum += texture2D(u_texture, vec2(tc.x - 4.0*blur*hstep, tc.y - 4.0*blur*vstep)) * 0.035855; + sum += texture2D(u_texture, vec2(tc.x - 3.0*blur*hstep, tc.y - 3.0*blur*vstep)) * 0.072994; + sum += texture2D(u_texture, vec2(tc.x - 2.0*blur*hstep, tc.y - 2.0*blur*vstep)) * 0.121281; + sum += texture2D(u_texture, vec2(tc.x - 1.0*blur*hstep, tc.y - 1.0*blur*vstep)) * 0.164472; + + sum += texture2D(u_texture, vec2(tc.x, tc.y)) * 0.182049; + + sum += texture2D(u_texture, vec2(tc.x + 1.0*blur*hstep, tc.y + 1.0*blur*vstep)) * 0.164472; + sum += texture2D(u_texture, vec2(tc.x + 2.0*blur*hstep, tc.y + 2.0*blur*vstep)) * 0.121281; + sum += texture2D(u_texture, vec2(tc.x + 3.0*blur*hstep, tc.y + 3.0*blur*vstep)) * 0.072994; + sum += texture2D(u_texture, vec2(tc.x + 4.0*blur*hstep, tc.y + 4.0*blur*vstep)) * 0.035855; + sum += texture2D(u_texture, vec2(tc.x + 5.0*blur*hstep, tc.y + 5.0*blur*vstep)) * 0.014374; - if (horizontal == 1) { - for (int i = 1; i < 5; ++i) { - result += texture(u_texture, vTexCoord + vec2(tex_offset.x * i, 0.0)).rgb * weight[i]; - result += texture(u_texture, vTexCoord - vec2(tex_offset.x * i, 0.0)).rgb * weight[i]; - } - } else { - for (int i = 1; i < 5; ++i) { - result += texture(u_texture, vTexCoord + vec2(0.0, tex_offset.y * i)).rgb * weight[i]; - result += texture(u_texture, vTexCoord - vec2(0.0, tex_offset.y * i)).rgb * weight[i]; - } - } - - gl_FragColor = vec4(result, color.a); + gl_FragColor = vColor * vec4(sum.rgb, 1.0); } \ No newline at end of file diff --git a/android/assets/shaders/gaussian_blur.vsh b/android/assets/shaders/gaussian_blur.vsh new file mode 100755 index 0000000..61d9ea8 --- /dev/null +++ b/android/assets/shaders/gaussian_blur.vsh @@ -0,0 +1,15 @@ +attribute vec4 a_position; +attribute vec4 a_color; +attribute vec2 a_texCoord0; + +uniform mat4 u_projTrans; + +varying vec4 vColor; +varying vec2 vTexCoord; + +void main() { + vColor = a_color; + vTexCoord = a_texCoord0; + + gl_Position = u_projTrans * a_position; +} diff --git a/core/src/zero1hd/rhythmbullet/audio/visualizer/CircularVisualizer.java b/core/src/zero1hd/rhythmbullet/audio/visualizer/CircularVisualizer.java index bba6edb..b42093d 100755 --- a/core/src/zero1hd/rhythmbullet/audio/visualizer/CircularVisualizer.java +++ b/core/src/zero1hd/rhythmbullet/audio/visualizer/CircularVisualizer.java @@ -86,7 +86,6 @@ public class CircularVisualizer implements Disposable { ((OrthographicCamera) camera).setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); shader.begin(); - shader.setUniformMatrix("u_projTrans", camera.combined); mesh.render(shader, GL20.GL_LINE_STRIP, 0, vertexCount); shader.end(); diff --git a/core/src/zero1hd/rhythmbullet/graphics/shaders/BloomShader.java b/core/src/zero1hd/rhythmbullet/graphics/shaders/BloomShader.java index feaa783..a849a37 100755 --- a/core/src/zero1hd/rhythmbullet/graphics/shaders/BloomShader.java +++ b/core/src/zero1hd/rhythmbullet/graphics/shaders/BloomShader.java @@ -34,17 +34,17 @@ public class BloomShader implements Disposable { Gdx.app.debug("Shader", "using glow shader"); brightFilterShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/bright_filter.fsh")); if (!brightFilterShader.isCompiled()) { - Gdx.app.error("Shader failed to compile", brightFilterShader.getLog()); - System.exit(0); + Gdx.app.error("Shader failed to compile bright filter shader", brightFilterShader.getLog()); + System.exit(1); } if (brightFilterShader.getLog().length() != 0) { Gdx.app.error("Shader", brightFilterShader.getLog()); } - gaussianBlurShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/gaussian_blur.fsh")); + gaussianBlurShader = new ShaderProgram(Gdx.files.internal("shaders/gaussian_blur.vsh"), Gdx.files.internal("shaders/gaussian_blur.fsh")); if (!gaussianBlurShader.isCompiled()) { - Gdx.app.error("Shader failed to compile", gaussianBlurShader.getLog()); - System.exit(0); + Gdx.app.error("Shader failed to compile gaussian blur shader", gaussianBlurShader.getLog()); + System.exit(1); } if (gaussianBlurShader.getLog().length() != 0) { Gdx.app.error("Shader", gaussianBlurShader.getLog()); @@ -52,8 +52,8 @@ public class BloomShader implements Disposable { combineShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/combine.fsh")); if (!combineShader.isCompiled()) { - Gdx.app.error("Shader failed to compile", combineShader.getLog()); - System.exit(0); + Gdx.app.error("Shader failed to compile combination shader", combineShader.getLog()); + System.exit(1); } if (combineShader.getLog().length() != 0) { Gdx.app.error("Shader", combineShader.getLog()); @@ -71,10 +71,13 @@ public class BloomShader implements Disposable { combineShader.setUniformi("u_texture1", 1); combineShader.end(); + gaussianBlurShader.begin(); + gaussianBlurShader.setUniformf("resolution", hBlur.getWidth(), vBlur.getHeight()); + gaussianBlurShader.end(); + vBlur.getColorBufferTexture().bind(1); Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0); - ShaderProgram.pedantic = false; } public void begin() { @@ -102,31 +105,46 @@ public class BloomShader implements Disposable { screenBatch.flush(); lightFilterBuffer.end(); - for (int i = 0; i <= bloomLevel; i++) { +// Horizontal gaussian blur + fboRegion.setTexture(lightFilterBuffer.getColorBufferTexture()); + hBlur.begin(); + screenBatch.setShader(gaussianBlurShader); + gaussianBlurShader.setUniformf("radius", 1f + 0.2f*bloomLevel); + gaussianBlurShader.setUniformi("pass", 0); + screenBatch.draw(fboRegion, 0f, 0f, width, height); + screenBatch.flush(); + hBlur.end(); + + +// //Vertical gaussian blur + fboRegion.setTexture(hBlur.getColorBufferTexture()); + vBlur.begin(); + gaussianBlurShader.setUniformi("pass", 1); + screenBatch.draw(fboRegion, 0f, 0f, width, height); + screenBatch.flush(); + vBlur.end(); + + if (bloomLevel > 2) { // Horizontal gaussian blur - if (i > 0) { - fboRegion.setTexture(vBlur.getColorBufferTexture()); - } else { - fboRegion.setTexture(lightFilterBuffer.getColorBufferTexture()); - } + fboRegion.setTexture(vBlur.getColorBufferTexture()); hBlur.begin(); - screenBatch.setShader(gaussianBlurShader); - gaussianBlurShader.setUniformi("horizontal", 1); + gaussianBlurShader.setUniformf("radius", 1.1f); + gaussianBlurShader.setUniformi("pass", 0); screenBatch.draw(fboRegion, 0f, 0f, width, height); screenBatch.flush(); hBlur.end(); + // //Vertical gaussian blur - vBlur.begin(); fboRegion.setTexture(hBlur.getColorBufferTexture()); - screenBatch.setShader(gaussianBlurShader); - gaussianBlurShader.setUniformi("horizontal", 0); + vBlur.begin(); + gaussianBlurShader.setUniformi("pass", 1); screenBatch.draw(fboRegion, 0f, 0f, width, height); screenBatch.flush(); vBlur.end(); } - Gdx.gl.glClearColor(0f, 0f, 0f, 0f); + Gdx.gl.glClearColor(0f, 0f, 0f, 1f); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); screenBatch.setShader(combineShader); fboRegion.setTexture(normalBuffer.getColorBufferTexture()); diff --git a/desktop/src/zero1hd/rhythmbullet/desktop/audio/PCMObtainer.java b/desktop/src/zero1hd/rhythmbullet/desktop/audio/PCMObtainer.java index f0dc035..7d4e826 100755 --- a/desktop/src/zero1hd/rhythmbullet/desktop/audio/PCMObtainer.java +++ b/desktop/src/zero1hd/rhythmbullet/desktop/audio/PCMObtainer.java @@ -110,7 +110,7 @@ public class PCMObtainer implements Observer, PCMSystem { private boolean synchronizeBufferWithPlayback() { int bufferPos = calcBufferPosition(); - if (bufferPos < playingBuffer.limit() || bufferPos >= 0) { + if (bufferPos <= playingBuffer.limit() && bufferPos >= 0) { synchronized (this) { playingBuffer.position(bufferPos); windowsRead = (int) ((mc.getCurrentPosition() * sampleRate) / windowSize); diff --git a/desktop/src/zero1hd/rhythmbullet/desktop/screens/main/MainScreen.java b/desktop/src/zero1hd/rhythmbullet/desktop/screens/main/MainScreen.java index c0e739b..d421660 100755 --- a/desktop/src/zero1hd/rhythmbullet/desktop/screens/main/MainScreen.java +++ b/desktop/src/zero1hd/rhythmbullet/desktop/screens/main/MainScreen.java @@ -43,8 +43,6 @@ public class MainScreen extends ScreenAdapter implements ResizeReadyScreen { private BloomShader bloomShader; - private float lerpAlpha; - private Texture background; private Batch screenBatch; @@ -241,7 +239,7 @@ public class MainScreen extends ScreenAdapter implements ResizeReadyScreen { } if (bloomLevel > 0) { if (bloomShader == null) bloomShader = new BloomShader(screenBatch); - bloomShader.setBloomLevel((bloomLevel-1)*2); + bloomShader.setBloomLevel((bloomLevel-1)); } else if (bloomShader != null) { bloomShader.dispose(); bloomShader = null;