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;
uniform mat4 u_projTrans;
uniform vec2 mouse;
varying vec4 vColor;
varying vec2 vTexCoord;
void main() {
vColor = a_color;
vTexCoord = a_texCoord0;

View File

@ -1,11 +1,10 @@
#extension GL_OES_standard_derivatives : enable
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
varying float vTime;
varying vec2 vResolution;
#define M_PI 3.1415926535897932384626433832795
//#define time (time * time * 0.05)
//#define vTime (vTime * vTime * 0.05)
vec2 rand2(vec2 p)
{
@ -44,9 +43,9 @@ float stars(in vec2 x, float numCells, float size, float br)
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;
@ -54,13 +53,13 @@ void main()
coord.y = tmp.x;
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 + time * 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 + time * 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 + time * 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 * 2.00,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 + vTime * 0.8,coord.y) , 1., 0.10, 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 + vTime * 0.2,coord.y) , 4., 0.08, 2.) * 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 + 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);
@ -70,9 +69,9 @@ void main()
// 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.graphics.Texture;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Pool;
@ -15,7 +14,6 @@ import zero1hd.polyjet.entity.enemies.VoidCircle;
public class EntityController {
AssetManager assets;
public ShapeRenderer shapes;
public Array<Entity> activeAllies;
public Array<Entity> activeEnemies;
@ -30,19 +28,16 @@ public class EntityController {
//Ally pool declaration;
private Pool<Laser> laserPool;
public EntityController(AssetManager assetManager, ShapeRenderer shapeRenderer) {
public EntityController(AssetManager assetManager) {
activeAllies = new Array<Entity>();
activeEnemies = new Array<Entity>();
this.assets = assetManager;
this.shapes = shapeRenderer;
shapes.setAutoShapeType(true);
//Enemy pool initialization;
voidCirclePool = new Pool<VoidCircle>() {
@Override
protected VoidCircle newObject() {
return new VoidCircle(shapes);
return new VoidCircle(assets.get("void_circle.png", Texture.class));
}
};
pelletPool = new Pool<Pellet>() {

View File

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

View File

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

View File

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

View File

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

View File

@ -36,17 +36,17 @@ public class GraphicsTable extends Table {
row();
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());
row();
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());
row();
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());
row();
@ -104,9 +104,10 @@ public class GraphicsTable extends Table {
}
public void save(Preferences prefs) {
prefs.putBoolean("bg_shader", bgShader.isChecked());
prefs.putBoolean("glow_shader", glowShader.isChecked());
prefs.putBoolean("invert_shader", invertShader.isChecked());
prefs.flush();
Gdx.app.debug("Preferences", "Saved shading values values.");
prefs.putBoolean("bg shader", bgShader.isChecked());
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("fx vol", fxVolSlider.getValue());
prefs.putString("music dir", directoryField.getText());
prefs.flush();
}
@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.ShapeRenderer;
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.utils.viewport.FitViewport;
@ -22,7 +23,6 @@ import zero1hd.polyjet.entity.ally.PolyJetEntity;
public class GamePlayArea extends Stage {
public PolyJetEntity polyjet;
public ShapeRenderer shapes;
public EntityController ec;
private CollisionDetector collisionDetector;
@ -34,15 +34,27 @@ public class GamePlayArea extends Stage {
private ShaderProgram invert;
private ShaderProgram bgShader;
private ShapeRenderer bg;
private Vector2 resolution;
private float timePassed;
public GamePlayArea(AssetManager assetManager, Preferences prefs) {
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");
ec = new EntityController(assetManager);
collisionDetector = new CollisionDetector(ec.activeAllies, ec.activeEnemies);
ec.activeAllies.add(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());
ShaderProgram.pedantic = false;
if (!invert.isCompiled()) {
@ -56,43 +68,39 @@ public class GamePlayArea extends Stage {
}
if (prefs.getBoolean("bg_shader")) {
bgShader = new ShaderProgram(Gdx.files.internal("shaders/invert_vert.glsl").readString(), Gdx.files.internal("shaders/star_frag.glsl").readString());
if (!invert.isCompiled()) {
System.err.println(invert.getLog());
if (prefs.getBoolean("bg shader")) {
Gdx.app.debug("Shader", "using background shader");
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);
}
if (invert.getLog().length()!=0) {
System.out.println(invert.getLog());
if (bgShader.getLog().length()!=0) {
System.out.println(bgShader.getLog());
}
bg = new ShapeRenderer(8, bgShader);
bg.setColor(Color.BLACK);
resolution = new Vector2(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
} else {
bg = new ShapeRenderer();
bg.setTransformMatrix(getCamera().combined);
bg.setColor(Color.WHITE);
//TODO prepare background rendering without shader
}
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
public void draw() {
bg.begin(ShapeType.Filled);
bg.rect(0, 0, Polyjet.GAME_AREA_WIDTH, Polyjet.GAME_AREA_HEIGHT);
bg.end();
//TODO batch draw background
super.draw();
}
@Override
public void act(float delta) {
if (bgShader != null) {
timePassed += delta;
bgShader.setUniformf("time", timePassed);
bgShader.setUniformf("resolution", resolution);
}
collisionDetector.collisionCheck();
ec.deathClean();