background shader now functioning

This commit is contained in:
Harrison Deng 2017-06-18 01:25:08 -05:00
parent fe6bd86632
commit 627a631f41
3 changed files with 90 additions and 78 deletions

View File

@ -1,73 +1,89 @@
#extension GL_OES_standard_derivatives : enable // from https://www.shadertoy.com/view/Msf3Wr
varying float vTime; #define GLSLSANDBOX
#define M_PI 3.1415926535897932384626433832795 #ifdef GLSLSANDBOX
varying vec2 vTexCoord; #ifdef GL_ES
precision mediump float;
#endif
uniform float time;
#ifndef GLSLSANDBOXTOYNOTCOMPATIBLE
uniform vec4 iMouse;
uniform int iFrame;
#else /*GLSLSANDBOXTOYNOTCOMPATIBLE*/
uniform vec2 mouse;
#define iMouse mouse
#endif /*GLSLSANDBOXTOYNOTCOMPATIBLE*/
uniform vec2 resolution;
#define iGlobalTime time
#define iResolution (vec3(resolution, 1.))
#endif /*GLSLSANDBOX*/
//#define vTime (vTime * vTime * 0.05)
vec2 rand2(vec2 p) #define iterations 17
#define formuparam 0.53
#define volsteps 20
#define stepsize 0.1
#define zoom 0.800
#define tile 0.850
#define speed 0.010
#define brightness 0.0015
#define darkmatter 0.300
#define distfading 0.730
#define saturation 0.850
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{ {
p = vec2(dot(p, vec2(102.9898,78.233)), dot(p, vec2(26.65125, 83.054543))); //get coords and direction
return fract(sin(p) * 43758.5453); vec2 uv=fragCoord.xy/iResolution.xy-.5;
} uv.y*=iResolution.y/iResolution.x;
vec3 dir=vec3(uv*zoom,1.);
float time=iGlobalTime*speed+.25;
float rand(vec2 p) float a1=.5+time/iResolution.x*2.;
{ float a2=.8+time/iResolution.y*2.;
return fract(sin(dot(p.xy ,vec2(505.90898,18.233))) * 43037.5453); mat2 rot1=mat2(cos(a1),sin(a1),-sin(a1),cos(a1));
} mat2 rot2=mat2(cos(a2),sin(a2),-sin(a2),cos(a2));
dir.xz*=rot1;
dir.xy*=rot2;
vec3 from=vec3(1.,.5,0.5);
from+=vec3(time*2.,time,-2.);
from.xz*=rot1;
from.xy*=rot2;
// Thanks to David Hoskins https://www.shadertoy.com/view/4djGRh //volumetric rendering
float stars(in vec2 x, float numCells, float size, float br) float s=0.1,fade=1.;
{ vec3 v=vec3(0.);
vec2 n = x * numCells; for (int r=0; r<volsteps; r++) {
vec2 f = floor(n); vec3 p=from+s*dir*.5;
p = abs(vec3(tile)-mod(p,vec3(tile*2.))); // tiling fold
float d = 1.0e10; float pa,a=pa=0.;
for (int i = -1; i <= 1; ++i) for (int i=0; i<iterations; i++) {
{ p=abs(p)/dot(p,p)-formuparam; // the magic formula
for (int j = -1; j <= 1; ++j) a+=abs(length(p)-pa); // absolute sum of average change
{ pa=length(p);
vec2 g = f + vec2(float(i), float(j));
g = n - g - rand2(mod(g, numCells)) + rand(g);
// Control size
g *= 1. / (numCells * size);
d = min(d, dot(g, g));
} }
float dm=max(0.,darkmatter-a*a*.001); //dark matter
a*=a*a; // add contrast
if (r>6) fade*=1.-dm; // dark matter, don't render near
//v+=vec3(dm,dm*.5,0.);
v+=fade;
v+=vec3(s,s*s,s*s*s*s)*a*brightness*fade; // coloring based on distance
fade*=distfading; // distance fading
s+=stepsize;
} }
v=mix(vec3(length(v)),v,saturation); //color adjust
fragColor = vec4(v*.01,1.);
return br * (smoothstep(.95, 1., (1. - sqrt(d))));
} }
void main (void)
void main()
{ {
vec2 coord = vTexCoord; vec4 color = vec4 (0.0, 0.0, 0.0, 1.0);
mainImage (color, gl_FragCoord.xy);
vec2 tmp = coord; color.w = 1.0;
gl_FragColor = color;
coord.x = tmp.y;
coord.y = tmp.x;
vec3 result = vec3(0.);
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);
gl_FragColor = vec4(result, 1.0);
// gl_FragColor = vec4(1.0*(vTexCoord.x/vResolution.x), 0.0, 0.0, 1.0);
// bar layer open scene ;
// vec2 bl = gl_FragCoord.xy / vResolution.xy;
// 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

@ -3,15 +3,10 @@ attribute vec4 a_color;
attribute vec2 a_texCoord0; attribute vec2 a_texCoord0;
uniform mat4 u_projTrans; uniform mat4 u_projTrans;
uniform float time;
varying vec4 vColor;
varying vec2 vTexCoord; varying vec2 vTexCoord;
varying float vTime;
void main() { void main() {
vColor = a_color;
vTexCoord = a_texCoord0; vTexCoord = a_texCoord0;
vTime = time;
gl_Position = u_projTrans * a_position; gl_Position = u_projTrans * a_position;
} }

View File

@ -5,8 +5,8 @@ import com.badlogic.gdx.Preferences;
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.ShaderProgram; import com.badlogic.gdx.graphics.glutils.ShaderProgram;
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.TimeUtils;
import com.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.FitViewport;
import zero1hd.polyjet.Polyjet; import zero1hd.polyjet.Polyjet;
@ -32,10 +32,10 @@ public class GamePlayArea extends Stage {
private ShaderProgram invert; private ShaderProgram invert;
private ShaderProgram bgShader; private ShaderProgram bgShader;
private float timePassed;
private Texture background; private Texture background;
private float time;
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"); Gdx.app.debug("Game Area", "new area created");
@ -81,6 +81,7 @@ public class GamePlayArea extends Stage {
if (bgShader.getLog().length()!=0) { if (bgShader.getLog().length()!=0) {
System.out.println(bgShader.getLog()); System.out.println(bgShader.getLog());
} }
} else { } else {
//TODO prepare background rendering without shader //TODO prepare background rendering without shader
} }
@ -91,10 +92,13 @@ public class GamePlayArea extends Stage {
@Override @Override
public void draw() { public void draw() {
//TODO batch draw background //TODO batch draw background
getBatch().begin();
if (bgShader != null) { if (bgShader != null) {
getBatch().setShader(bgShader); getBatch().setShader(bgShader);
bgShader.setUniformf("resolution", background.getWidth(), background.getHeight());
bgShader.setUniformf("time", time);
} }
getBatch().begin();
getBatch().draw(background, 0f, 0f, Polyjet.GAME_AREA_WIDTH, Polyjet.GAME_AREA_HEIGHT); getBatch().draw(background, 0f, 0f, Polyjet.GAME_AREA_WIDTH, Polyjet.GAME_AREA_HEIGHT);
getBatch().end(); getBatch().end();
@ -104,10 +108,7 @@ public class GamePlayArea extends Stage {
@Override @Override
public void act(float delta) { public void act(float delta) {
if (bgShader != null) { time = TimeUtils.millis()/10f;
timePassed += delta;
bgShader.setUniformf("time", timePassed);
}
collisionDetector.collisionCheck(); collisionDetector.collisionCheck();
ec.deathClean(); ec.deathClean();