
So I wanted to learn to code shaders, I was at my in law's family for holidays and found ShaderEditor (f-droid link). It was the perfect combination of will and time to hack something.
So I tried the different shader examples and went on to use the Conway's game of life that react to presses. On top of that I changed the colors to use my SO's preferred color and mine too. To avoid the emptyness of a dead game I also added dynamic visualization of the current volume an battery level + charging status.
The ShaderEditor code
Feel free to use it as wallpaper.
I'm not sure about what I'm allowed to set as licence (since I heavily used the examples and don't remember their original one), but all my projects are AGPLv3+
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
/* Cloudy Conway
* http://www.vexorian.com/2015/05/cloudy-conway.html?m=1
*/
uniform vec2 resolution;
uniform int pointerCount;
uniform vec3 pointers[10];
uniform sampler2D backbuffer;
uniform float time;
uniform float battery;
uniform int powerConnected;
uniform float mediaVolume;
//uniform int frame;
const bool debug = false;
//const float time = 0.0;
/*
* Conway Simulation
*/
const vec3 colorDead = vec3(0.2, 0, 0.1); // Deep purple
const vec3 colorAlive = vec3(1, 0.85, 0); // Flashy yellow
float oneIfZero(float value) {
return step(abs(value), 0.1);
}
vec4 get4(float x, float y) {
return texture2D(backbuffer,
(gl_FragCoord.xy + vec2(x, y)) / resolution);
}
vec4 evaluate(float sum) {
vec4 cell = get4(0.0, 0.0);
float wasAlive = cell[3];
float has3 = oneIfZero(sum - 3.0);
float has2 = oneIfZero(sum - 2.0);
// a cell is (or becomes) alive if it has 3 neighbors
// or if it has 2 neighbors *and* was alive already
float isAlive = min(1.0, has3 + has2 * wasAlive);
float justDied = (1.0 - isAlive) * wasAlive;
float afterglowR = cell[0];
float afterglowG = cell[1];
float afterglowB = cell[2];
return vec4(
max(afterglowR * 0.99, colorDead.r * (1.0 - isAlive) + colorAlive.r * isAlive),
colorAlive.g * isAlive,
colorDead.b * (1.0 - isAlive),
isAlive
/*
afterglow * isAlive,
afterglow * (1.0 - isAlive),
max(afterglow * 0.99, justDied),
isAlive
*/
);
}
float get(float x, float y) {
return get4(x, y).a;
}
vec4 conwayStep() {
float sum =
get(-1.0, -1.0) +
get(-1.0, 0.0) +
get(-1.0, 1.0) +
get(0.0, -1.0) +
get(0.0, 1.0) +
get(1.0, -1.0) +
get(1.0, 0.0) +
get(1.0, 1.0);
float tapSize = min(resolution.x, resolution.y) * 0.05;
for (int n = 0; n < pointerCount; ++n) {
if (distance(pointers[n].xy, gl_FragCoord.xy) < tapSize) {
sum = 3.0;
break;
}
}
return evaluate(sum);
}
/*
* Battery and Volume plotting
*/
float plot(float posPercent, float pct, float halfThickness) {
return smoothstep(pct - halfThickness, pct, posPercent) -
smoothstep(pct, pct + halfThickness, posPercent);
return step(pct - halfThickness, posPercent) -
step(pct + halfThickness, posPercent);
}
float wave(float direction, float perpendicular, float movementDirection) {
float movDir = oneIfZero(movementDirection) - movementDirection;
float movement0 = time * 1.0;
//float movement1 = float(frame) * 0.005;
float movement = movement0;
return sin(movDir * perpendicular * 30.0 + movement);
}
float cat(float direction, float perpendicular, float movementDirection) {
return mod(wave(direction, perpendicular, movementDirection), 3.0) - 1.5;
}
float plotBattery(float batDirection, float batPerpendicular) {
float powerC = float(powerConnected);
float charging = 0.15 + powerC;
float batAmplitude = 0.2 * charging;
float batValue =
(cat(batDirection, batPerpendicular, powerC)
* wave(batDirection, batPerpendicular, powerC))
+ 0.5; // Empirically center visible resulting wave
return plot(batDirection, batAmplitude * batValue + battery, resolution.y * 0.000015);
}
float plotVolume(float volDirection, float volPerpendicular) {
float volAmplitude = 0.05 * mediaVolume;
float volValue = wave(volDirection, volPerpendicular, 1.0);
return plot(volDirection, volAmplitude * volValue + mediaVolume, resolution.x * 0.000015);
}
void main() {
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec4 conwaySimulation = conwayStep();
float batValue = plotBattery(uv.x, uv.y);
float volValue = plotVolume(uv.y, uv.x);
conwaySimulation.a = min(1.0, (batValue + volValue) * 2.0 + conwaySimulation.a);
if (debug || time == 0.) {
if (conwaySimulation.a >= 1.0) {
conwaySimulation.rgb = colorAlive;
}
//conwaySimulation.b = step(uv.x, battery);
}
gl_FragColor = conwaySimulation;
}