thebookofshaders/10/2d-random-truchet.frag

60 lines
1.4 KiB
GLSL
Raw Normal View History

2015-03-15 15:35:14 +00:00
// Author @patriciogv - 2015
2017-08-19 10:11:58 +00:00
// Title: Truchet - 10 print
2015-05-05 14:19:16 +00:00
2015-03-15 15:35:14 +00:00
#ifdef GL_ES
precision mediump float;
#endif
#define PI 3.14159265358979323846
uniform vec2 u_resolution;
2015-05-01 20:05:55 +00:00
uniform vec2 u_mouse;
2015-03-15 15:35:14 +00:00
uniform float u_time;
2017-08-19 10:11:58 +00:00
float random (in vec2 _st) {
2015-03-15 15:35:14 +00:00
return fract(sin(dot(_st.xy,
2017-08-19 10:11:58 +00:00
vec2(12.9898,78.233)))*
2015-03-15 15:35:14 +00:00
43758.5453123);
}
vec2 truchetPattern(in vec2 _st, in float _index){
_index = fract(((_index-0.5)*2.0));
if (_index > 0.75) {
_st = vec2(1.0) - _st;
} else if (_index > 0.5) {
_st = vec2(1.0-_st.x,_st.y);
} else if (_index > 0.25) {
_st = 1.0-vec2(1.0-_st.x,_st.y);
}
return _st;
}
void main() {
vec2 st = gl_FragCoord.xy/u_resolution.xy;
st *= 10.0;
// st = (st-vec2(5.0))*(abs(sin(u_time*0.2))*5.);
// st.x += u_time*3.0;
2015-07-23 14:02:53 +00:00
vec2 ipos = floor(st); // integer
vec2 fpos = fract(st); // fraction
2015-03-15 15:35:14 +00:00
2015-07-23 14:02:53 +00:00
vec2 tile = truchetPattern(fpos, random( ipos ));
2015-03-15 15:35:14 +00:00
float color = 0.0;
// Maze
color = smoothstep(tile.x-0.3,tile.x,tile.y)-
smoothstep(tile.x,tile.x+0.3,tile.y);
// Circles
// color = (step(length(tile),0.6) -
// step(length(tile),0.4) ) +
// (step(length(tile-vec2(1.)),0.6) -
// step(length(tile-vec2(1.)),0.4) );
// Truchet (2 triangles)
// color = step(tile.x,tile.y);
gl_FragColor = vec4(vec3(color),1.0);
2017-08-19 10:11:58 +00:00
}