added bloom shader

This commit is contained in:
Harrison Deng 2017-09-03 03:29:33 -05:00
parent ba8a2182fb
commit b18cbe2352
12 changed files with 170 additions and 318 deletions

View File

@ -1,174 +0,0 @@
#ifdef GL_ES
precision mediump float;
#endif
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
#define iterations 14
#define formuparam2 0.79
#define volsteps 5
#define stepsize 0.290
#define zoom 0.900
#define tile 0.850
#define speed2 0.10
#define brightness 0.003
#define darkmatter 0.400
#define distfading 0.560
#define saturation 0.800
#define transverseSpeed zoom*2.0
#define cloud 0.11
float triangle(float x, float a) {
float output2 = 2.0*abs( 2.0* ( (x/a) - floor( (x/a) + 0.5) ) ) - 1.0;
return output2;
}
float field(in vec3 p) {
float strength = 7. + .03 * log(1.e-6 + fract(sin(time) * 4373.11));
float accum = 0.;
float prev = 0.;
float tw = 0.;
for (int i = 0; i < 6; ++i) {
float mag = dot(p, p);
p = abs(p) / mag + vec3(-.5, -.8 + 0.1*sin(time*0.7 + 2.0), -1.1+0.3*cos(time*0.3));
float w = exp(-float(i) / 7.);
accum += w * exp(-strength * pow(abs(mag - prev), 2.3));
tw += w;
prev = mag;
}
return max(0., 5. * accum / tw - .7);
}
void main() {
vec2 uv2 = 2. * gl_FragCoord.xy / vec2(512) - 1.;
vec2 uvs = uv2 * vec2(512) / 512.;
float time2 = time;
float speed = speed2;
speed = .01 * cos(time2*0.002 + 3.1415926/4.0);
//speed = 0.0;
float formuparam = formuparam2;
//get coords and direction
vec2 uv = uvs;
//mouse rotation
float a_xz = 0.9;
float a_yz = -.6;
float a_xy = 0.9 + time*0.08;
mat2 rot_xz = mat2(cos(a_xz),sin(a_xz),-sin(a_xz),cos(a_xz));
mat2 rot_yz = mat2(cos(a_yz),sin(a_yz),-sin(a_yz),cos(a_yz));
mat2 rot_xy = mat2(cos(a_xy),sin(a_xy),-sin(a_xy),cos(a_xy));
float v2 =1.0;
vec3 dir=vec3(uv*zoom,1.);
vec3 from=vec3(0.0, 0.0,0.0);
from.x -= 1.;
from.y -= 1.;
vec3 forward = vec3(0.,0.,1.);
from.x += transverseSpeed*(1.0)*cos(0.001*time);
from.y += transverseSpeed*(1.0)*sin(0.001*time);
from.z += 0.003*time;
dir.xy*=rot_xy;
forward.xy *= rot_xy;
dir.xz*=rot_xz;
forward.xz *= rot_xz;
dir.yz*= rot_yz;
forward.yz *= rot_yz;
from.xy*=-rot_xy;
from.xz*=rot_xz;
from.yz*= rot_yz;
//zoom
float zooom = (time2-3311.)*speed;
from += forward* zooom;
float sampleShift = mod( zooom, stepsize );
float zoffset = -sampleShift;
sampleShift /= stepsize; // make from 0 to 1
//volumetric rendering
float s=0.24;
float s3 = s + stepsize/2.0;
vec3 v=vec3(0.);
float t3 = 0.0;
vec3 backCol2 = vec3(0.);
for (int r=0; r<volsteps; r++) {
vec3 p2=from+(s+zoffset)*dir;// + vec3(0.,0.,zoffset);
vec3 p3=from+(s3+zoffset)*dir;// + vec3(0.,0.,zoffset);
p2 = abs(vec3(tile)-mod(p2,vec3(tile*2.))); // tiling fold
p3 = abs(vec3(tile)-mod(p3,vec3(tile*2.))); // tiling fold
#ifdef cloud
t3 = field(p3);
#endif
float pa,a=pa=0.;
for (int i=0; i<iterations; i++) {
p2=abs(p2)/dot(p2,p2)-formuparam; // the magic formula
//p=abs(p)/max(dot(p,p),0.005)-formuparam; // another interesting way to reduce noise
float D = abs(length(p2)-pa); // absolute sum of average change
a += i > 7 ? min( 12., D) : D;
pa=length(p2);
}
//float dm=max(0.,darkmatter-a*a*.001); //dark matter
a*=a*a; // add contrast
//if (r>3) fade*=1.-dm; // dark matter, don't render near
// brightens stuff up a bit
float s1 = s+zoffset;
// need closed form expression for this, now that we shift samples
float fade = pow(distfading,max(0.,float(r)-sampleShift));
//t3 += fade;
v+=fade;
//backCol2 -= fade;
// fade out samples as they approach the camera
if( r == 0 )
fade *= (1. - (sampleShift));
// fade in samples as they approach from the distance
if( r == volsteps-1 )
fade *= sampleShift;
v+=vec3(s1,s1*s1,s1*s1*s1*s1)*a*brightness*fade; // coloring based on distance
backCol2 += mix(.4, 1., v2) * vec3(1.8 * t3 * t3 * t3, 1.4 * t3 * t3, t3) * fade;
s+=stepsize;
s3 += stepsize;
}
v=mix(vec3(length(v)),v,saturation); //color adjust
vec4 forCol2 = vec4(v*.01,1.);
#ifdef cloud
backCol2 *= cloud;
#endif
backCol2.b *= 1.8;
backCol2.r *= 0.05;
backCol2.b = 0.5*mix(backCol2.g, backCol2.b, 0.8);
backCol2.g = 0.0;
backCol2.bg = mix(backCol2.gb, backCol2.bg, 0.5*(cos(time*0.01) + 1.0));
gl_FragColor = forCol2 + vec4(backCol2, 1.0);
}

View File

@ -0,0 +1,22 @@
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
varying LOWP vec4 vColor;
varying vec2 vTexCoord;
uniform sampler2D u_texture;
void main() {
vec4 color = texture(u_texture, vTexCoord)*vColor;
float brightness = (color.r*0.2126) + (color.g*0.7152) + (color.b * 0.0722);
if (brightness > 0.7) {
gl_FragColor = color;
} else {
gl_FragColor = vec4(0.0);
}
}

View File

@ -0,0 +1,18 @@
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
varying LOWP vec4 vColor;
varying vec2 vTexCoord;
uniform sampler2D u_texture;
uniform sampler2D u_texture1;
void main() {
vec4 origColor = texture(u_texture, vTexCoord);
vec4 blurredColor = texture(u_texture1, vTexCoord);
gl_FragColor = origColor + blurredColor;
}

View File

@ -0,0 +1,33 @@
#version 330 core
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
varying LOWP 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);
void main() {
vec2 tex_offset = 1.0 /textureSize(u_texture, 0);
vec3 result = texture(u_texture, vTexCoord).rgb * weight[0];
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, 1.0);
}

View File

@ -1,38 +0,0 @@
#ifdef GL_ES
#define LOWP lowp
precision mediump float;
#else
#define LOWP
#endif
varying LOWP vec4 vColor;
varying vec2 vTexCoord;
uniform sampler2D u_texture;
void main() {
vec4 sum = vec4(0);
vec2 texcoord = vTexCoord;
int j;
int i;
for (i = -4; i < 4; i++) {
for (j = -3; j < 3; j++) {
sum += texture2D(u_texture, texcoord + vec2(j, i)*0.004) * 0.25;
}
}
if (texture2D(u_texture, texcoord).r * vColor.r < 0.3) {
gl_FragColor = vec4(sum * sum * 0.012 + texture2D(u_texture, texcoord))
* vColor;
} else {
if (texture2D(u_texture, texcoord).r * vColor.r < 0.5) {
gl_FragColor = vec4(
sum * sum * 0.009 + texture2D(u_texture, texcoord))
* vColor;
} else {
gl_FragColor = vec4(
sum * sum * 0.0075 + texture2D(u_texture, texcoord))
* vColor;
}
}
}

View File

@ -1,14 +0,0 @@
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;
}

View File

@ -9,9 +9,9 @@ import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import zero1hd.rhythmbullet.RhythmBullet;
import zero1hd.rhythmbullet.stages.CreativeHUD;
import zero1hd.rhythmbullet.stages.GameHUD;
import zero1hd.rhythmbullet.stages.GamePlayArea;
import zero1hd.rhythmbullet.stages.ui.CreativeHUD;
import zero1hd.rhythmbullet.stages.ui.GameHUD;
public class CreativeScreen extends ScreenAdapter {
CreativeHUD chud;
@ -19,9 +19,6 @@ public class CreativeScreen extends ScreenAdapter {
GamePlayArea gamePlayArea;
InputMultiplexer inputs;
SpriteBatch bgBatch;
private Texture background;
Preferences prefs;
public CreativeScreen(RhythmBullet core, MainMenu mainMenu) {
@ -31,10 +28,6 @@ public class CreativeScreen extends ScreenAdapter {
inputs = new InputMultiplexer(chud, ghud, gamePlayArea);
this.prefs = core.getPrefs();
background = core.getAssetManager().get("star_bg.png");
bgBatch = new SpriteBatch(1);
}
@Override
@ -49,10 +42,6 @@ public class CreativeScreen extends ScreenAdapter {
Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
bgBatch.begin();
bgBatch.draw(background, 0f, 0f);
bgBatch.end();
gamePlayArea.getViewport().apply();
gamePlayArea.act();
gamePlayArea.draw();

View File

@ -5,9 +5,6 @@ import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
@ -15,8 +12,8 @@ import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import zero1hd.rhythmbullet.RhythmBullet;
import zero1hd.rhythmbullet.audio.CoreMusicInfo;
import zero1hd.rhythmbullet.audio.map.GamePlayMap;
import zero1hd.rhythmbullet.stages.GameHUD;
import zero1hd.rhythmbullet.stages.GamePlayArea;
import zero1hd.rhythmbullet.stages.ui.GameHUD;
public class GameScreen extends ScreenAdapter {
private GamePlayArea gameArea;
@ -28,10 +25,6 @@ public class GameScreen extends ScreenAdapter {
private CoreMusicInfo music;
SpriteBatch bgBatch;
private ShaderProgram bgShader;
private Texture background;
public GameScreen(RhythmBullet core, Screen screen) {
this.core = core;
@ -53,25 +46,6 @@ public class GameScreen extends ScreenAdapter {
inputs.addProcessor(gameHUD);
inputs.addProcessor(gameArea);
background = core.getAssetManager().get("star_bg.png");
if (core.getPrefs().getBoolean("bg shader")) {
Gdx.app.debug("Shader", "using background shader");
bgShader = new ShaderProgram(Gdx.files.internal("shaders/bg.vsh"), Gdx.files.internal("shaders/bg.fsh"));
if (!bgShader.isCompiled()) {
System.err.println(bgShader.getLog());
System.exit(0);
}
if (bgShader.getLog().length()!=0) {
System.out.println(bgShader.getLog());
}
bgBatch = new SpriteBatch(2, bgShader);
} else {
bgBatch = new SpriteBatch(2);
}
}
public void setGamePlayMap(GamePlayMap gpm) {
@ -97,20 +71,6 @@ public class GameScreen extends ScreenAdapter {
public void render(float delta) {
Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//Background stuff should literally span the whole screen so no matrice stuff
bgBatch.begin();
if (bgShader != null) {
bgBatch.setShader(bgShader);
bgShader.setUniformf("resolution", Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
if (music != null) {
bgShader.setUniformf("time", music.getPlaybackMusic().getPosition());
}
}
bgBatch.draw(background, 0f, 0f);
bgBatch.end();
//actual game and hud
if (!gameHUD.isPaused()) {
gameArea.act(delta);

View File

@ -1,4 +1,4 @@
package zero1hd.rhythmbullet.stages.ui;
package zero1hd.rhythmbullet.stages;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
@ -18,7 +18,6 @@ import zero1hd.rhythmbullet.audio.AudioAnalyzer;
import zero1hd.rhythmbullet.audio.AudioDataPackage;
import zero1hd.rhythmbullet.audio.map.RhythmMapAlgorithm;
import zero1hd.rhythmbullet.screens.MainMenu;
import zero1hd.rhythmbullet.stages.GamePlayArea;
import zero1hd.rhythmbullet.ui.windows.BassUMGraphWindow;
import zero1hd.rhythmbullet.ui.windows.BeatViewer;
import zero1hd.rhythmbullet.ui.windows.DifficultyWindow;
@ -249,6 +248,8 @@ public class CreativeHUD extends Stage implements MiniListener {
musicPlayBackControls.setAudiofile(null);
analyzer = new AudioAnalyzer(musicSelector.getSelectedMusic());
analyzer.sender.addListener(this);
ghud.setMusic(null);
break;
case SPECTRAL_FLUX_DONE:
analyzer.runThresholdCleaning();
@ -265,7 +266,6 @@ public class CreativeHUD extends Stage implements MiniListener {
musicPlayBackControls.setAudiofile(adp.getMusicInfo());
volumeWindow.setMusic(adp.getMusicInfo());
beatViewer.setMusic(adp.getMusicInfo(), analyzer.getAudioDataPackage());
ghud.setMusic(null);
bassUMgraphWindow.setData(analyzer.getAudioDataPackage().getBassPeaks(), analyzer.getAudioDataPackage().getuMPeaks(), adp.getMusicInfo());
bassUMgraphWindow.getGraph().avgG1 = adp.getBassAvg();

View File

@ -1,4 +1,4 @@
package zero1hd.rhythmbullet.stages.ui;
package zero1hd.rhythmbullet.stages;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
@ -19,7 +19,6 @@ import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import zero1hd.rhythmbullet.RhythmBullet;
import zero1hd.rhythmbullet.stages.GamePlayArea;
import zero1hd.rhythmbullet.ui.builders.HealthBar;
import zero1hd.rhythmbullet.ui.windows.FPSWindow;
import zero1hd.rhythmbullet.ui.windows.PauseMenu;
@ -142,7 +141,6 @@ public class GameHUD extends Stage {
}
}
}
super.act(delta);
}

View File

@ -35,9 +35,13 @@ public class GamePlayArea extends Stage {
public ScoreManager score = new ScoreManager();
private ShaderProgram glowShader;
private FrameBuffer blurTarget;
TextureRegion fboRegion;
private ShaderProgram gaussianBlurShader;
private ShaderProgram brightFilterShader;
private ShaderProgram combineShader;
private FrameBuffer lightFilterBuffer;
private FrameBuffer normalBuffer;
private FrameBuffer hBlur, vBlur;
private TextureRegion fboRegion;
private int fboSize;
public GamePlayArea(AssetManager assetManager, Preferences prefs) {
@ -63,32 +67,62 @@ public class GamePlayArea extends Stage {
/**
* needs to be called right after set as screen (should be called in show method).
* This is due to the saving preference being done once the screen is hidden.
* @param prefs
*/
public void loadShaders(Preferences prefs) {
if (prefs.getBoolean("glow shader")) {
Gdx.app.debug("Shader", "using glow shader");
glowShader = new ShaderProgram(Gdx.files.internal("shaders/glow.vsh"), Gdx.files.internal("shaders/glow.fsh"));
if (!glowShader.isCompiled()) {
System.err.println(glowShader.getLog());
brightFilterShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/bright_filter.fsh"));
if (!brightFilterShader.isCompiled()) {
System.err.println(brightFilterShader.getLog());
System.exit(0);
}
if (glowShader.getLog().length() != 0) {
System.out.println(glowShader.getLog());
if (brightFilterShader.getLog().length() != 0) {
System.out.println(brightFilterShader.getLog());
}
gaussianBlurShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/gaussian_blur.fsh"));
if (!gaussianBlurShader.isCompiled()) {
System.err.println(gaussianBlurShader.getLog());
System.exit(0);
}
if (gaussianBlurShader.getLog().length() != 0) {
System.out.println(gaussianBlurShader.getLog());
}
combineShader = new ShaderProgram(Gdx.files.internal("shaders/basic.vsh"), Gdx.files.internal("shaders/combine.fsh"));
if (!combineShader.isCompiled()) {
System.err.println(combineShader.getLog());
System.exit(0);
}
if (combineShader.getLog().length() != 0) {
System.out.println(combineShader.getLog());
}
if (Gdx.graphics.getWidth() < 1024) {
fboSize = 1024;
} else if (Gdx.graphics.getWidth() < 2048) {
fboSize = 2048;
} else if (Gdx.graphics.getWidth() < 2048) {
fboSize = 4096;
} else {
fboSize = 4096;
}
blurTarget = new FrameBuffer(Format.RGBA8888, fboSize, fboSize, false);
fboRegion = new TextureRegion(blurTarget.getColorBufferTexture());
fboRegion.flip(false, true);
lightFilterBuffer = new FrameBuffer(Format.RGBA8888, fboSize/4, fboSize/4, false);
normalBuffer = new FrameBuffer(Format.RGBA8888, fboSize, fboSize, false);
hBlur = new FrameBuffer(Format.RGBA8888, fboSize, fboSize, false);
vBlur = new FrameBuffer(Format.RGBA8888, fboSize, fboSize, false);
fboRegion = new TextureRegion();
combineShader.begin();
combineShader.setUniformi("u_texture1", 1);
combineShader.end();
vBlur.getColorBufferTexture().bind(1);
Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0);
}
ShaderProgram.pedantic = false;
@ -96,27 +130,52 @@ public class GamePlayArea extends Stage {
@Override
public void draw() {
//TODO batch draw background
if (glowShader != null) {
blurTarget.begin();
if (gaussianBlurShader != null) {
//Begin drawing a normal version of screen
normalBuffer.begin();
Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
getBatch().setBlendFunction(-1, -1);
Gdx.gl20.glBlendFuncSeparate(GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA,GL20.GL_ONE, GL20.GL_DST_ALPHA);
super.draw();
collisionDetector.renderParticles(getBatch(), Gdx.graphics.getDeltaTime(), true);
blurTarget.end(getViewport().getScreenX(), getViewport().getScreenY(), getViewport().getScreenWidth(), getViewport().getScreenHeight());
getBatch().setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
normalBuffer.end(getViewport().getScreenX(), getViewport().getScreenY(), getViewport().getScreenWidth(), getViewport().getScreenHeight());
//END
getBatch().setBlendFunction(GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA);
getBatch().begin();
getBatch().setShader(glowShader);
//Begin light filtering
lightFilterBuffer.begin();
Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
getBatch().setShader(brightFilterShader);
getBatch().begin(); //BATCH STARTS HERE
getRoot().draw(getBatch(), 1f);
getBatch().flush();
lightFilterBuffer.end(getViewport().getScreenX(), getViewport().getScreenY(), getViewport().getScreenWidth(), getViewport().getScreenHeight());
//Horizontal gaussian blur
hBlur.begin();
fboRegion.setTexture(lightFilterBuffer.getColorBufferTexture());
getBatch().setShader(gaussianBlurShader);
gaussianBlurShader.setUniformf("horizontal", 1);
getBatch().draw(fboRegion, 0f, 0f, RhythmBullet.GAME_AREA_WIDTH, RhythmBullet.GAME_AREA_HEIGHT);
getBatch().flush();
hBlur.end(getViewport().getScreenX(), getViewport().getScreenY(), getViewport().getScreenWidth(), getViewport().getScreenHeight());
//Vertical gaussian blur
vBlur.begin();
fboRegion.setTexture(hBlur.getColorBufferTexture());
getBatch().setShader(gaussianBlurShader);
gaussianBlurShader.setUniformf("horizontal", 0);
getBatch().draw(fboRegion, 0f, 0f, RhythmBullet.GAME_AREA_WIDTH, RhythmBullet.GAME_AREA_HEIGHT);
getBatch().flush();
vBlur.end(getViewport().getScreenX(), getViewport().getScreenY(), getViewport().getScreenWidth(), getViewport().getScreenHeight());
//Draw everything to screen
getBatch().setShader(combineShader);
fboRegion.setRegion(normalBuffer.getColorBufferTexture());
fboRegion.flip(false, true);
getBatch().draw(fboRegion, 0f, 0f, RhythmBullet.GAME_AREA_WIDTH, RhythmBullet.GAME_AREA_HEIGHT);
getBatch().setShader(null);
getBatch().end(); //BATCH ENDS HERE
getBatch().end();
} else {
super.draw();
}
@ -162,7 +221,7 @@ public class GamePlayArea extends Stage {
polyjet.moveUp = true;
}
if (keycode == KeyMap.down) {
if (keycode == KeyMap.down) {
polyjet.moveDown = true;
}
@ -205,11 +264,10 @@ public class GamePlayArea extends Stage {
@Override
public void dispose() {
if (glowShader != null) {
blurTarget.dispose();
glowShader.dispose();
if (gaussianBlurShader != null) {
normalBuffer.dispose();
gaussianBlurShader.dispose();
}
super.dispose();
}
}