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 M_PI 3.1415926535897932384626433832795
varying vec2 vTexCoord;
#define GLSLSANDBOX
#ifdef GLSLSANDBOX
#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)));
return fract(sin(p) * 43758.5453);
//get coords and direction
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 a1=.5+time/iResolution.x*2.;
float a2=.8+time/iResolution.y*2.;
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;
//volumetric rendering
float s=0.1,fade=1.;
vec3 v=vec3(0.);
for (int r=0; r<volsteps; r++) {
vec3 p=from+s*dir*.5;
p = abs(vec3(tile)-mod(p,vec3(tile*2.))); // tiling fold
float pa,a=pa=0.;
for (int i=0; i<iterations; i++) {
p=abs(p)/dot(p,p)-formuparam; // the magic formula
a+=abs(length(p)-pa); // absolute sum of average change
pa=length(p);
}
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.);
}
float rand(vec2 p)
void main (void)
{
return fract(sin(dot(p.xy ,vec2(505.90898,18.233))) * 43037.5453);
}
// Thanks to David Hoskins https://www.shadertoy.com/view/4djGRh
float stars(in vec2 x, float numCells, float size, float br)
{
vec2 n = x * numCells;
vec2 f = floor(n);
float d = 1.0e10;
for (int i = -1; i <= 1; ++i)
{
for (int j = -1; j <= 1; ++j)
{
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));
}
}
return br * (smoothstep(.95, 1., (1. - sqrt(d))));
}
void main()
{
vec2 coord = vTexCoord;
vec2 tmp = coord;
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);
vec4 color = vec4 (0.0, 0.0, 0.0, 1.0);
mainImage (color, gl_FragCoord.xy);
color.w = 1.0;
gl_FragColor = color;
}

View File

@ -3,15 +3,10 @@ attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
uniform float time;
varying vec4 vColor;
varying vec2 vTexCoord;
varying float vTime;
void main() {
vColor = a_color;
vTexCoord = a_texCoord0;
vTime = time;
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.graphics.Texture;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.TimeUtils;
import com.badlogic.gdx.utils.viewport.FitViewport;
import zero1hd.polyjet.Polyjet;
@ -32,10 +32,10 @@ public class GamePlayArea extends Stage {
private ShaderProgram invert;
private ShaderProgram bgShader;
private float timePassed;
private Texture background;
private float time;
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");
@ -81,6 +81,7 @@ public class GamePlayArea extends Stage {
if (bgShader.getLog().length()!=0) {
System.out.println(bgShader.getLog());
}
} else {
//TODO prepare background rendering without shader
}
@ -91,10 +92,13 @@ public class GamePlayArea extends Stage {
@Override
public void draw() {
//TODO batch draw background
getBatch().begin();
if (bgShader != null) {
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().end();
@ -104,10 +108,7 @@ public class GamePlayArea extends Stage {
@Override
public void act(float delta) {
if (bgShader != null) {
timePassed += delta;
bgShader.setUniformf("time", timePassed);
}
time = TimeUtils.millis()/10f;
collisionDetector.collisionCheck();
ec.deathClean();