Code: ShaderExample
package com.shaderexample.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.ScreenUtils;
public class ShaderExample extends ApplicationAdapter {
private SpriteBatch batch;
private Texture img;
private ShaderProgram shaderProgram;
private float time;
@Override
public void create () {
batch = new SpriteBatch();
img = new Texture("example.jpg");
String vertexShader = Gdx.files.internal("shaders/vertex.glsl").readString();
String fragmentShader = Gdx.files.internal("shaders/wavey.glsl").readString();
shaderProgram = new ShaderProgram(vertexShader,fragmentShader);
shaderProgram.pedantic = false;
batch.setShader(shaderProgram);
time = 0;
}
@Override
public void render () {
ScreenUtils.clear(Color.BLACK);
time+=Gdx.graphics.getDeltaTime();
batch.begin();
// pass in the following to the fragment glsl scripts
Vector2 v = new Vector2(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);
v.x = v.x / Gdx.graphics.getWidth();
v.y = v.y / Gdx.graphics.getHeight();
shaderProgram.setUniformf("center", v);
shaderProgram.setUniformf("u_time", time);
shaderProgram.setUniformf("u_resolution", Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.draw(img, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.end();
}
@Override
public void dispose () {
batch.dispose();
img.dispose();
shaderProgram.dispose();
}
}
Shaders
Black and White shader
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
uniform mat4 u_projTrans;
void main() {
vec3 color = texture2D(u_texture, v_texCoords).rgb;
float gray = (color.r + color.g + color.b) / 3.0;
vec3 grayscale = vec3(gray);
gl_FragColor = vec4(grayscale, 1.0);
}
Ripple Effect Shader
#ifdef GL_ES
precision highp float;
#endif
// Passed in values from Java
uniform vec2 center; // Where we start from
uniform float u_time; // effect elapsed time
uniform sampler2D sceneTex; // 0
varying vec2 v_texCoords;
void main()
{
// get pixel coordinates
vec2 l_texCoords = v_texCoords;
vec3 shockParams = vec3(10.0, 0.8, 0.1);
float offset = (u_time- floor(u_time))/u_time;
float CurrentTime = (u_time)*(offset);
//get distance from center
float distance = distance(v_texCoords, center);
if ( (distance <= (CurrentTime + shockParams.z)) && (distance >= (CurrentTime - shockParams.z)) ) {
float diff = (distance - CurrentTime);
float powDiff = 0.0;
if(distance>0.05){
powDiff = 1.0 - pow(abs(diff*shockParams.x), shockParams.y);
}
float diffTime = diff * powDiff;
vec2 diffUV = normalize(v_texCoords-center);
//Perform the distortion and reduce the effect over time
l_texCoords = v_texCoords + ((diffUV * diffTime)/(CurrentTime * distance * 40.0));
}
gl_FragColor = texture2D(sceneTex, l_texCoords);
}
Vignette Shader
#ifdef GL_ES
precision mediump float;
#endif
// Passed in values from Java
uniform vec2 u_resolution;
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
const float outerRadius = 0.65, innerRadius = 0.4, intensity = 0.9;
void main() {
vec4 color = texture2D(u_texture, v_texCoords) * v_color;
vec2 relativePosition = gl_FragCoord.xy / u_resolution - 0.5;
relativePosition.x *= u_resolution.x / u_resolution.y;
float len = length(relativePosition);
float vignette = smoothstep(outerRadius, innerRadius, len);
color.rgb = mix(color.rgb, color.rgb * vignette, intensity);
gl_FragColor = color;
}
Wavey Shader
#version 120
#ifdef GL_ES
precision mediump float;
#endif
// Passed in values from Java
uniform float u_time; // effect elapsed time
uniform vec2 u_resolution;
uniform sampler2D u_texture;
void main()
{
float time = u_time;
vec2 p = gl_FragCoord.xy/u_resolution.xy;
p.x += sin(p.y * 15.0 + u_time * 2.0) / 800.0;
p.y += cos(p.x * 10.0 + u_time * 2.0) / 900.0;
p.x += sin((p.y+p.x) * 15.0 + u_time * 2.0) / (180.0 + (2.0 * sin(u_time)));
p.y += cos((p.y+p.x) * 15.0 + u_time * 2.0) / (200.0 + (2.0 * sin(u_time)));
// Flip back
p.y = 1.0-p.y;
gl_FragColor = texture2D(u_texture, p);
}
Vertex
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
varying vec4 v_color;
varying vec2 v_texCoords;
void main() {
v_color = a_color;
v_texCoords = a_texCoord0;
gl_Position = u_projTrans * a_position;
}