thebookofshaders/09/truchet.frag

77 lines
1.7 KiB
GLSL
Raw Normal View History

2015-06-20 12:39:58 +00:00
// Author @patriciogv ( patriciogonzalezvivo.com ) - 2015
2015-03-15 15:35:14 +00:00
#ifdef GL_ES
precision mediump float;
#endif
#define PI 3.14159265358979323846
uniform vec2 u_resolution;
uniform float u_time;
vec2 rotate2D (vec2 _st, float _angle) {
2015-07-04 12:51:27 +00:00
_st -= 0.5;
_st = mat2(cos(_angle),-sin(_angle),
sin(_angle),cos(_angle)) * _st;
_st += 0.5;
return _st;
2015-03-15 15:35:14 +00:00
}
vec2 tile (vec2 _st, float _zoom) {
2015-07-04 12:51:27 +00:00
_st *= _zoom;
return fract(_st);
2015-03-15 15:35:14 +00:00
}
vec2 rotateTilePattern(vec2 _st){
2015-07-04 12:51:27 +00:00
2015-07-17 22:43:40 +00:00
// Scale the coordinate system by 2x2
2015-03-15 15:35:14 +00:00
_st *= 2.0;
2015-07-17 22:43:40 +00:00
// Give each cell an index number
// according to its position
2015-03-15 15:35:14 +00:00
float index = 0.0;
2015-07-14 14:34:43 +00:00
index += step(1., mod(_st.x,2.0));
index += step(1., mod(_st.y,2.0))*2.0;
2015-11-17 14:36:46 +00:00
2015-07-04 12:51:27 +00:00
// |
2015-11-17 14:36:46 +00:00
// 2 | 3
2015-07-04 12:51:27 +00:00
// |
//--------------
// |
2015-11-17 14:36:46 +00:00
// 0 | 1
2015-07-04 12:51:27 +00:00
// |
// Make each cell between 0.0 - 1.0
2015-03-15 15:35:14 +00:00
_st = fract(_st);
2015-07-17 22:43:40 +00:00
// Rotate each cell according to the index
2015-03-15 15:35:14 +00:00
if(index == 1.0){
2015-07-04 12:51:27 +00:00
// Rotate cell 1 by 90 degrees
2015-03-15 15:35:14 +00:00
_st = rotate2D(_st,PI*0.5);
} else if(index == 2.0){
2015-07-04 12:51:27 +00:00
// Rotate cell 2 by -90 degrees
2015-03-15 15:35:14 +00:00
_st = rotate2D(_st,PI*-0.5);
} else if(index == 3.0){
2015-07-04 12:51:27 +00:00
// Rotate cell 3 by 180 degrees
2015-03-15 15:35:14 +00:00
_st = rotate2D(_st,PI);
}
return _st;
}
void main (void) {
vec2 st = gl_FragCoord.xy/u_resolution.xy;
st = tile(st,3.0);
st = rotateTilePattern(st);
2015-07-04 12:51:27 +00:00
2015-07-17 22:43:40 +00:00
// Make more interesting combinations
2015-07-04 12:51:27 +00:00
// st = tile(st,2.0);
2015-03-15 15:35:14 +00:00
// st = rotate2D(st,-PI*u_time*0.25);
2015-07-04 12:51:27 +00:00
// st = rotateTilePattern(st*2.);
// st = rotate2D(st,PI*u_time*0.25);
2015-03-15 15:35:14 +00:00
// step(st.x,st.y) just makes a b&w triangles
2015-07-17 22:43:40 +00:00
// but you can use whatever design you want.
2015-07-04 12:51:27 +00:00
gl_FragColor = vec4(vec3(step(st.x,st.y)),1.0);
2015-03-15 15:35:14 +00:00
}