background shader render progress, replacing shape renderers with texture (haven't created said textures yet)

This commit is contained in:
Harrison Deng 2017-06-10 12:34:08 -05:00
parent d036900399
commit cf07c14f86
11 changed files with 106 additions and 75 deletions

View File

@ -3,10 +3,10 @@ attribute vec4 a_color;
attribute vec2 a_texCoord0; attribute vec2 a_texCoord0;
uniform mat4 u_projTrans; uniform mat4 u_projTrans;
uniform vec2 mouse;
varying vec4 vColor; varying vec4 vColor;
varying vec2 vTexCoord; varying vec2 vTexCoord;
void main() { void main() {
vColor = a_color; vColor = a_color;
vTexCoord = a_texCoord0; vTexCoord = a_texCoord0;

View File

@ -1,11 +1,10 @@
#extension GL_OES_standard_derivatives : enable #extension GL_OES_standard_derivatives : enable
uniform float time; varying float vTime;
uniform vec2 mouse; varying vec2 vResolution;
uniform vec2 resolution;
#define M_PI 3.1415926535897932384626433832795 #define M_PI 3.1415926535897932384626433832795
//#define time (time * time * 0.05) //#define vTime (vTime * vTime * 0.05)
vec2 rand2(vec2 p) vec2 rand2(vec2 p)
{ {
@ -44,9 +43,9 @@ float stars(in vec2 x, float numCells, float size, float br)
void main() void main()
{ {
float res = max(resolution.y, resolution.y); float res = max(vResolution.y, vResolution.y);
vec2 coord = gl_FragCoord.xy / resolution; vec2 coord = gl_FragCoord.xy / vResolution;
vec2 tmp = coord; vec2 tmp = coord;
@ -54,13 +53,13 @@ void main()
coord.y = tmp.x; coord.y = tmp.x;
vec3 result = vec3(0.); vec3 result = vec3(0.);
result += stars(vec2(coord.x + time * 2.00,coord.y) , 1., 0.10, 2.) * vec3(.74, .74, .74); result += stars(vec2(coord.x + vTime * 2.00,coord.y) , 1., 0.10, 2.) * vec3(.74, .74, .74);
result += stars(vec2(coord.x + time * 1.10,coord.y) , 1., 0.10, 2.) * vec3(.74, .74, .74); result += stars(vec2(coord.x + vTime * 1.10,coord.y) , 1., 0.10, 2.) * vec3(.74, .74, .74);
result += stars(vec2(coord.x + time * 0.8,coord.y) , 1., 0.10, 2.) * vec3(.74, .74, .74); result += stars(vec2(coord.x + vTime * 0.8,coord.y) , 1., 0.10, 2.) * vec3(.74, .74, .74);
result += stars(vec2(coord.x + time * 0.5,coord.y) , 2., 0.09, 2.) * vec3(.74, .74, .74); result += stars(vec2(coord.x + vTime * 0.5,coord.y) , 2., 0.09, 2.) * vec3(.74, .74, .74);
result += stars(vec2(coord.x + time * 0.2,coord.y) , 4., 0.08, 2.) * vec3(.74, .74, .74); result += stars(vec2(coord.x + vTime * 0.2,coord.y) , 4., 0.08, 2.) * vec3(.74, .74, .74);
result += stars(vec2(coord.x + time * 0.05,coord.y), 8., 0.05, 1.) * vec3(.74, .74, .74); result += stars(vec2(coord.x + vTime * 0.05,coord.y), 8., 0.05, 1.) * vec3(.74, .74, .74);
result += stars(vec2(coord.x + time * 0.025,coord.y), 10., 0.05,0.8) * vec3(.95, .95, .95); result += stars(vec2(coord.x + vTime * 0.025,coord.y), 10., 0.05,0.8) * vec3(.95, .95, .95);
result += stars(coord , 20., 0.025, 0.5) * vec3(.9, .9, .95); result += stars(coord , 20., 0.025, 0.5) * vec3(.9, .9, .95);
@ -70,9 +69,9 @@ void main()
// bar layer open scene ; // bar layer open scene ;
// vec2 bl = gl_FragCoord.xy / resolution.xy; // vec2 bl = gl_FragCoord.xy / vResolution.xy;
// if(bl.y>.50-min(time/20.,0.4) ^^ bl.y<0.50+min(time/20.0,.4)) gl_FragColor = vec4(0.0,sqrt(coord.y+0.1),0.3,1.0); // if(bl.y>.50-min(vTime/20.,0.4) ^^ bl.y<0.50+min(vTime/20.0,.4)) gl_FragColor = vec4(0.0,sqrt(coord.y+0.1),0.3,1.0);

View File

@ -0,0 +1,20 @@
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
uniform float time;
uniform vec2 resolution;
varying vec4 vColor;
varying vec2 vTexCoord;
varying float vTime;
varying vec2 vResolution;
void main() {
vColor = a_color;
vTexCoord = a_texCoord0;
vTime = time;
vResolution = resolution;
gl_Position = u_projTrans * a_position;
}

View File

@ -2,7 +2,6 @@ package zero1hd.polyjet.entity;
import com.badlogic.gdx.assets.AssetManager; import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Pool; import com.badlogic.gdx.utils.Pool;
@ -15,7 +14,6 @@ import zero1hd.polyjet.entity.enemies.VoidCircle;
public class EntityController { public class EntityController {
AssetManager assets; AssetManager assets;
public ShapeRenderer shapes;
public Array<Entity> activeAllies; public Array<Entity> activeAllies;
public Array<Entity> activeEnemies; public Array<Entity> activeEnemies;
@ -30,19 +28,16 @@ public class EntityController {
//Ally pool declaration; //Ally pool declaration;
private Pool<Laser> laserPool; private Pool<Laser> laserPool;
public EntityController(AssetManager assetManager, ShapeRenderer shapeRenderer) { public EntityController(AssetManager assetManager) {
activeAllies = new Array<Entity>(); activeAllies = new Array<Entity>();
activeEnemies = new Array<Entity>(); activeEnemies = new Array<Entity>();
this.assets = assetManager; this.assets = assetManager;
this.shapes = shapeRenderer;
shapes.setAutoShapeType(true);
//Enemy pool initialization; //Enemy pool initialization;
voidCirclePool = new Pool<VoidCircle>() { voidCirclePool = new Pool<VoidCircle>() {
@Override @Override
protected VoidCircle newObject() { protected VoidCircle newObject() {
return new VoidCircle(shapes); return new VoidCircle(assets.get("void_circle.png", Texture.class));
} }
}; };
pelletPool = new Pool<Pellet>() { pelletPool = new Pool<Pellet>() {

View File

@ -1,9 +1,9 @@
package zero1hd.polyjet.entity.enemies; package zero1hd.polyjet.entity.enemies;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Actor;
@ -18,16 +18,16 @@ public class VoidCircle extends Actor implements Entity, Poolable {
private float endRadius; private float endRadius;
private float currentRadius; private float currentRadius;
private Rectangle hitBox; private Rectangle hitBox;
private ShapeRenderer shapeRenderer; private Sprite voidCircleTexture;
private float growthRate; private float growthRate;
private boolean done; private boolean done;
private boolean begin; private boolean begin;
private Vector2 center; private Vector2 center;
public VoidCircle(ShapeRenderer shapeRenderer) { public VoidCircle(Texture voidTexture) {
hitBox = new Rectangle(); hitBox = new Rectangle();
this.shapeRenderer = shapeRenderer;
center = new Vector2(); center = new Vector2();
voidCircleTexture = new Sprite(voidTexture);
} }
public void init(float endRadius, float x, float y, float growthRate, float warningTime) { public void init(float endRadius, float x, float y, float growthRate, float warningTime) {
@ -43,6 +43,7 @@ public VoidCircle(ShapeRenderer shapeRenderer) {
@Override @Override
public void act(float delta) { public void act(float delta) {
toFront(); toFront();
voidCircleTexture.setCenter(getX()+center.x, getY()+center.y);
hitBox.setCenter(getX()+center.x, getY()+center.y); hitBox.setCenter(getX()+center.x, getY()+center.y);
if (timer > 0) { if (timer > 0) {
@ -66,21 +67,16 @@ public VoidCircle(ShapeRenderer shapeRenderer) {
@Override @Override
public void draw(Batch batch, float parentAlpha) { public void draw(Batch batch, float parentAlpha) {
batch.end();
shapeRenderer.begin();
if (begin) { if (begin) {
shapeRenderer.set(ShapeType.Filled); voidCircleTexture.setSize(2*currentRadius, 2*currentRadius);
shapeRenderer.setColor(Color.BLACK); voidCircleTexture.setCenter(getX()+center.x, getY()+center.y);
shapeRenderer.circle(getX()+center.x, getY()+center.y, currentRadius, 48);
} else { } else {
shapeRenderer.set(ShapeType.Line); voidCircleTexture.setSize(2*endRadius, 2*endRadius);
shapeRenderer.setColor(1 - timer/warnTime, 1 - timer/warnTime, 1 - timer/warnTime, 1f); voidCircleTexture.setColor(1f,0f,0f,0.5f);
shapeRenderer.circle(getX()+center.x, getY()+center.y, endRadius, 48); voidCircleTexture.draw(batch);
} }
shapeRenderer.end();
batch.begin();
batch.setColor(Color.WHITE);
super.draw(batch, parentAlpha); super.draw(batch, parentAlpha);
} }
@ -95,6 +91,7 @@ public VoidCircle(ShapeRenderer shapeRenderer) {
begin = false; begin = false;
warnTime = 0; warnTime = 0;
center.set(0, 0); center.set(0, 0);
voidCircleTexture.setPosition(0, 0);
setSize(0, 0); setSize(0, 0);
} }

View File

@ -2,6 +2,7 @@ package zero1hd.polyjet.screens;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer; import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.ScreenAdapter; import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
@ -14,16 +15,21 @@ public class CreativeDebugScreen extends ScreenAdapter {
GamePlayArea gamePlayArea; GamePlayArea gamePlayArea;
InputMultiplexer inputs; InputMultiplexer inputs;
Preferences prefs;
public CreativeDebugScreen(Polyjet core, MainMenu mainMenu) { public CreativeDebugScreen(Polyjet core, MainMenu mainMenu) {
gamePlayArea = new GamePlayArea(core.getAssetManager(), core.getPrefs()); gamePlayArea = new GamePlayArea(core.getAssetManager(), core.getPrefs());
creative = new CreativeStage(core, mainMenu, gamePlayArea); creative = new CreativeStage(core, mainMenu, gamePlayArea);
inputs = new InputMultiplexer(creative, gamePlayArea); inputs = new InputMultiplexer(creative, gamePlayArea);
this.prefs = core.getPrefs();
} }
@Override @Override
public void show() { public void show() {
Gdx.input.setInputProcessor(inputs); Gdx.input.setInputProcessor(inputs);
gamePlayArea.loadShaders(prefs);
super.show(); super.show();
} }
@ -31,7 +37,6 @@ public class CreativeDebugScreen extends ScreenAdapter {
public void render(float delta) { public void render(float delta) {
Gdx.gl.glClearColor(1f, 1f, 1f, 1f); Gdx.gl.glClearColor(1f, 1f, 1f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
Gdx.gl.glLineWidth(1f);
gamePlayArea.getViewport().apply(); gamePlayArea.getViewport().apply();
gamePlayArea.act(); gamePlayArea.act();
@ -50,6 +55,7 @@ public class CreativeDebugScreen extends ScreenAdapter {
@Override @Override
public void dispose() { public void dispose() {
dispose();
super.dispose(); super.dispose();
} }
} }

View File

@ -112,6 +112,7 @@ public class GameScreen extends ScreenAdapter implements InputProcessor {
@Override @Override
public void show() { public void show() {
Gdx.input.setInputProcessor(inputs); Gdx.input.setInputProcessor(inputs);
gameArea.loadShaders(core.getPrefs());
super.show(); super.show();
} }

View File

@ -97,11 +97,16 @@ public class MainMenu extends ScreenAdapter implements TransitionAdapter {
@Override @Override
public void hide() { public void hide() {
saveAll();
super.hide();
}
public void saveAll() {
if (optionsPage != null) { if (optionsPage != null) {
optionsPage.saveOptions(core.getPrefs()); optionsPage.saveOptions(core.getPrefs());
moreOptionsPage.getGraphicsSettings().save(core.getPrefs()); moreOptionsPage.getGraphicsSettings().save(core.getPrefs());
core.getPrefs().flush();
} }
super.hide();
} }
@Override @Override

View File

@ -36,17 +36,17 @@ public class GraphicsTable extends Table {
row(); row();
invertShader = new CheckBox(" Invert Shader", skin, "expandable"); invertShader = new CheckBox(" Invert Shader", skin, "expandable");
invertShader.setChecked(pref.getBoolean("invert_shader", false)); invertShader.setChecked(pref.getBoolean("invert shader", false));
add(invertShader).minHeight(shaders.getHeight()); add(invertShader).minHeight(shaders.getHeight());
row(); row();
glowShader = new CheckBox(" Glow Shader", skin, "expandable"); glowShader = new CheckBox(" Glow Shader", skin, "expandable");
glowShader.setChecked(pref.getBoolean("glow_shader", true)); glowShader.setChecked(pref.getBoolean("glow shader", true));
add(glowShader).minHeight(shaders.getHeight()); add(glowShader).minHeight(shaders.getHeight());
row(); row();
bgShader = new CheckBox(" Background Shader", skin, "expandable"); bgShader = new CheckBox(" Background Shader", skin, "expandable");
bgShader.setChecked(pref.getBoolean("bg_shader", true)); bgShader.setChecked(pref.getBoolean("bg shader", true));
add(bgShader).minHeight(shaders.getHeight()); add(bgShader).minHeight(shaders.getHeight());
row(); row();
@ -104,9 +104,10 @@ public class GraphicsTable extends Table {
} }
public void save(Preferences prefs) { public void save(Preferences prefs) {
prefs.putBoolean("bg_shader", bgShader.isChecked()); Gdx.app.debug("Preferences", "Saved shading values values.");
prefs.putBoolean("glow_shader", glowShader.isChecked());
prefs.putBoolean("invert_shader", invertShader.isChecked()); prefs.putBoolean("bg shader", bgShader.isChecked());
prefs.flush(); prefs.putBoolean("glow shader", glowShader.isChecked());
prefs.putBoolean("invert shader", invertShader.isChecked());
} }
} }

View File

@ -145,7 +145,6 @@ public class OptionsPage extends Page {
prefs.putFloat("music vol", musicVolSlider.getValue()); prefs.putFloat("music vol", musicVolSlider.getValue());
prefs.putFloat("fx vol", fxVolSlider.getValue()); prefs.putFloat("fx vol", fxVolSlider.getValue());
prefs.putString("music dir", directoryField.getText()); prefs.putString("music dir", directoryField.getText());
prefs.flush();
} }
@Override @Override

View File

@ -7,6 +7,7 @@ import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShaderProgram; import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType; import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.FitViewport;
@ -22,7 +23,6 @@ import zero1hd.polyjet.entity.ally.PolyJetEntity;
public class GamePlayArea extends Stage { public class GamePlayArea extends Stage {
public PolyJetEntity polyjet; public PolyJetEntity polyjet;
public ShapeRenderer shapes;
public EntityController ec; public EntityController ec;
private CollisionDetector collisionDetector; private CollisionDetector collisionDetector;
@ -34,15 +34,27 @@ public class GamePlayArea extends Stage {
private ShaderProgram invert; private ShaderProgram invert;
private ShaderProgram bgShader; private ShaderProgram bgShader;
private ShapeRenderer bg; private Vector2 resolution;
private float timePassed;
public GamePlayArea(AssetManager assetManager, Preferences prefs) { public GamePlayArea(AssetManager assetManager, Preferences prefs) {
super(new FitViewport(Polyjet.GAME_AREA_WIDTH, Polyjet.GAME_AREA_HEIGHT)); super(new FitViewport(Polyjet.GAME_AREA_WIDTH, Polyjet.GAME_AREA_HEIGHT));
Gdx.app.debug("Game Area", "new area created");
polyjet = new PolyJetEntity(assetManager, 25f, 25f, "standard"); polyjet = new PolyJetEntity(assetManager, 25f, 25f, "standard");
ec = new EntityController(assetManager);
collisionDetector = new CollisionDetector(ec.activeAllies, ec.activeEnemies);
ec.activeAllies.add(polyjet);
addActor(polyjet); addActor(polyjet);
}
if (prefs.getBoolean("invert_shader")) { /**
* needs to be called right after set (should be called in show method).
* @param prefs
*/
public void loadShaders(Preferences prefs) {
if (prefs.getBoolean("invert shader")) {
Gdx.app.debug("Shader", "using inversion shader");
invert = new ShaderProgram(Gdx.files.internal("shaders/invert_vert.glsl").readString(), Gdx.files.internal("shaders/invert_frag.glsl").readString()); invert = new ShaderProgram(Gdx.files.internal("shaders/invert_vert.glsl").readString(), Gdx.files.internal("shaders/invert_frag.glsl").readString());
ShaderProgram.pedantic = false; ShaderProgram.pedantic = false;
if (!invert.isCompiled()) { if (!invert.isCompiled()) {
@ -56,43 +68,39 @@ public class GamePlayArea extends Stage {
} }
if (prefs.getBoolean("bg_shader")) { if (prefs.getBoolean("bg shader")) {
bgShader = new ShaderProgram(Gdx.files.internal("shaders/invert_vert.glsl").readString(), Gdx.files.internal("shaders/star_frag.glsl").readString()); Gdx.app.debug("Shader", "using background shader");
if (!invert.isCompiled()) {
System.err.println(invert.getLog()); bgShader = new ShaderProgram(Gdx.files.internal("shaders/star_vert.glsl").readString(), Gdx.files.internal("shaders/star_frag.glsl").readString());
if (!bgShader.isCompiled()) {
System.err.println(bgShader.getLog());
System.exit(0); System.exit(0);
} }
if (invert.getLog().length()!=0) { if (bgShader.getLog().length()!=0) {
System.out.println(invert.getLog()); System.out.println(bgShader.getLog());
} }
resolution = new Vector2(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
bg = new ShapeRenderer(8, bgShader);
bg.setColor(Color.BLACK);
} else { } else {
bg = new ShapeRenderer(); //TODO prepare background rendering without shader
bg.setTransformMatrix(getCamera().combined);
bg.setColor(Color.WHITE);
} }
ShaderProgram.pedantic = false; ShaderProgram.pedantic = false;
shapes = new ShapeRenderer(64, getBatch().getShader());
shapes.setProjectionMatrix(getCamera().combined);
ec = new EntityController(assetManager, shapes);
collisionDetector = new CollisionDetector(ec.activeAllies, ec.activeEnemies);
ec.activeAllies.add(polyjet);
} }
@Override @Override
public void draw() { public void draw() {
bg.begin(ShapeType.Filled); //TODO batch draw background
bg.rect(0, 0, Polyjet.GAME_AREA_WIDTH, Polyjet.GAME_AREA_HEIGHT);
bg.end();
super.draw(); super.draw();
} }
@Override @Override
public void act(float delta) { public void act(float delta) {
if (bgShader != null) {
timePassed += delta;
bgShader.setUniformf("time", timePassed);
bgShader.setUniformf("resolution", resolution);
}
collisionDetector.collisionCheck(); collisionDetector.collisionCheck();
ec.deathClean(); ec.deathClean();