Skip to content

Instantly share code, notes, and snippets.

@vjeranc
Created December 22, 2020 10:33
Show Gist options
  • Save vjeranc/262bb6498c2ac0259248beed6fb45ee5 to your computer and use it in GitHub Desktop.
Save vjeranc/262bb6498c2ac0259248beed6fb45ee5 to your computer and use it in GitHub Desktop.
// Run in https://thebookofshaders.com/edit.php#11/
// TODO:
// - would be nice to just use the polygon instead of triangle
// + keeping track of the orientation as one descends to lower levels
// - to put the borders
// - to normalize the width/height regardless of zoom level
// + probably find the biggest tile containing the tiles of given size on the Xth level
// - add other tiles
#ifdef GL_ES
precision highp float;
#endif
#define phi 1.61803398874989484820
#define h 1.53884176858762670128
uniform vec2 u_resolution;
vec2 tile(vec2 _st, float _zoom){
_st *= _zoom;
return fract(_st);
}
float cx(in vec2 a, in vec2 b) {
return a.x * b.y - a.y * b.x;
}
float sign(in vec2 p1, in vec2 p2, in vec2 p3) {
return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
}
bool inTri(in vec2 x, in vec2 A, in vec2 B, in vec2 C) {
float d1 = sign(x, A, B);
float d2 = sign(x, B, C);
float d3 = sign(x, C, A);
bool has_neg = d1 < 0. || d2 < 0. || d3 < 0.;
bool has_pos = d1 > 0. || d2 > 0. || d3 > 0.;
return !(has_neg && has_pos);
}
void main() {
vec2 st = (gl_FragCoord.xy / u_resolution * h + 1.);
const int level = 9;
int type = 0; // starting triangle
float X = 4.;
vec2 A = vec2(0.5, h) * X;
vec2 B = vec2(0.) * X;
vec2 C = vec2(1., 0.) * X;
if (cx(B - A, B - st) > 0.) {
gl_FragColor = vec4(0.);
return;
}
if (cx(A - C, A - st) > 0.) {
gl_FragColor = vec4(0.);
return;
}
for (int i = 0; i < level; ++i) {
if (type == 0) { // we are in narrow triangle
vec2 P = A + (B - A) / phi;
// float r = cx(P-C, P-st);
if (inTri(st, P, C, A)) {
B = C;
C = A;
A = P;
type = 1;
} else {
A = C;
C = B;
B = P;
type = 0;
}
} else { // we are in wide triangle
vec2 Q = B + (A - B) / phi;
vec2 R = B + (C - B) / phi;
// float qr = cx(Q - R, Q - st);
// float ar = cx(A - R, A - st);
if (inTri(st, R, C, A)) {
B = C;
C = A;
A = R;
type = 1;
} else if (inTri(st, R, Q, A)) {
C = A;
A = R;
B = Q;
type = 0;
} else {
A = Q;
C = B;
B = R;
type = 1;
}
}
}
float m = 7.1;
vec3 color = type == 1 ? vec3(0.825, 0.294, 0.675) : vec3(0.889, 0.995, 0.337);
color = mix(color, vec3(0.), smoothstep(0.1 * m, 0.8 * m, cx(B - A, st) / length(B - A)));
color = mix(color, vec3(0.), smoothstep(0.1 * m, 0.8 * m, cx(C - A, st) / length(B - A)));
gl_FragColor = vec4(color, 1.);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment