Skip to content

Instantly share code, notes, and snippets.

@tripzilch
Created July 23, 2015 15:33
Show Gist options
  • Save tripzilch/203e896def2ab28d5afa to your computer and use it in GitHub Desktop.
Save tripzilch/203e896def2ab28d5afa to your computer and use it in GitHub Desktop.
#ifdef GL_ES
precision mediump float;
#endif
#define PROCESSING_TEXTURE_SHADER
uniform sampler2D texture;
uniform vec2 C;
uniform vec2 P;
uniform vec2 M;
uniform float zoom;
varying vec4 vertColor;
varying vec4 vertTexCoord;
const vec3 igamma = vec3(2.2);
const vec3 gamma = 1.0 / igamma;
const float MAXITER = 70.;
const float BAILOUT2 = 4096.0;
const vec4 ones = vec4(1.0);
const vec4 zeros = vec4(0.0);
const float tx = 10.5 / 768.;
vec3 tex(vec2 p) {
return pow(texture2D(texture, .5 + p).xyz, igamma);
}
const float trap_r = .2;
vec3 trap(vec2 Z, float i) {
vec2 X = (Z - vec2(P)) * trap_r;
return vec3(length(X), X);
}
vec3 traptex(vec3 T, float i) {
return tex(normalize(T.yz) * min(.48, T.x * pow(1.0 + i, 0.1)));
}
void main (void) {
vec3 color;
float wsum = 0.0, w;
vec2 Z = zoom * vertTexCoord.st + M;
vec2 Z2 = Z * Z;
float Zmag2 = Z2.x + Z2.y;
vec2 Zprev;
// orbit trap vars
float i = 0.0;
vec3 min_T = trap(Z, i);
color = traptex(min_T, i);
// iteration
for (i = 1.; i < MAXITER; i++) {
if (Zmag2 < BAILOUT2) {
// iterate Z
Zprev = Z;
Z.y *= 2.0 * Z.x;
Z.x = Z2.x - Z2.y;
Z += C;
// calc squared magnitudes
Z2 = Z * Z;
Zmag2 = Z2.x + Z2.y;
// orbit trap
vec3 T = trap(Z, i);
vec3 s = traptex(T, i);
color = mix(s, color, smoothstep(min_T.x, min_T.x * 3.0, T.x));
min_T = (T.x < min_T.x) ? T : min_T;
}
}
color *= step(BAILOUT2, Zmag2);
gl_FragColor = vec4(pow(color, gamma), 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment