Skip to content

Commit c537ffc

Browse files
committed
add truchet patterns
1 parent 311171c commit c537ffc

3 files changed

+215
-0
lines changed

060-pattern-layers.glsl

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
#endif
4+
5+
#define PI 3.14159265359
6+
7+
uniform vec2 u_resolution;
8+
uniform float u_time;
9+
10+
float box(vec2 p,vec2 b){
11+
vec2 d=abs(p)-b;
12+
return length(max(d,0.))+min(max(d.x,d.y),0.);
13+
}
14+
15+
float fill(float dist,float size){
16+
return smoothstep(-size,+size,dist);
17+
}
18+
19+
mat2 rotate2d(float _angle){
20+
return mat2(cos(_angle),-sin(_angle),
21+
sin(_angle),cos(_angle));
22+
}
23+
24+
mat2 scale(vec2 _scale){
25+
return mat2(_scale.x,0.,
26+
0.,_scale.y);
27+
}
28+
29+
void main(){
30+
vec2 st=gl_FragCoord.xy/u_resolution;
31+
st.x*=u_resolution.x/u_resolution.y;
32+
33+
// Define colors for the backgroun, the layer A and the layer B
34+
vec3 colorBg=vec3(st,0.);
35+
vec3 colorLayerA=vec3(.2824,.4745,1.);
36+
vec3 colorLayerB=vec3(.9451,.0157,.5882);
37+
38+
// Layer A. Each layer has its own coord system
39+
vec2 layerA=st;
40+
layerA*=2.;
41+
layerA=fract(layerA);
42+
43+
//1 draw a box in the middle of the coord system layerA
44+
vec2 boxPosA=layerA-vec2(.5,.5);
45+
float boxA=fill(box(boxPosA,vec2(.2)),.001);
46+
47+
// Add Layer A to the background
48+
colorBg=mix(colorLayerA,colorBg,boxA);
49+
50+
// Layer B
51+
vec2 layerB=st;
52+
layerB*=8.;
53+
layerB=fract(layerB);
54+
55+
//1 draw a box in the middle of the screen?
56+
vec2 boxPosB=layerB-vec2(.5,.5);
57+
float boxB=fill(box(boxPosB,vec2(.2)),.001);
58+
59+
// Add Layer B to the background
60+
colorBg=mix(colorLayerB,colorBg,boxB);
61+
62+
// Exercise!
63+
// 1)Create a multi layer pattern, animate it.
64+
// 2)Search online for parallax. Try to combine the movement of the layers
65+
// in order to give the feeling of depth.
66+
// 3) try to use a custom shape. If you do not have one, spend some time
67+
// creating a function that generates a custom shapes.
68+
69+
gl_FragColor=vec4(colorBg,1.);
70+
}

061-pattern-truchet.glsl

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Author @patriciogv ( patriciogonzalezvivo.com ) - 2015
2+
// via https://thebookofshaders.com/09/
3+
4+
5+
#ifdef GL_ES
6+
precision mediump float;
7+
#endif
8+
9+
#define PI 3.14159265358979323846
10+
11+
uniform vec2 u_resolution;
12+
uniform float u_time;
13+
14+
vec2 rotate2D (vec2 _st, float _angle) {
15+
_st -= 0.5;
16+
_st = mat2(cos(_angle),-sin(_angle),
17+
sin(_angle),cos(_angle)) * _st;
18+
_st += 0.5;
19+
return _st;
20+
}
21+
22+
vec2 tile (vec2 _st, float _zoom) {
23+
_st *= _zoom;
24+
return fract(_st);
25+
}
26+
27+
vec2 rotateTilePattern(vec2 _st){
28+
29+
// Scale the coordinate system by 2x2
30+
_st *= 2.0;
31+
32+
// Give each cell an index number
33+
// according to its position
34+
float index = 0.0;
35+
index += step(1., mod(_st.x,2.0));
36+
index += step(1., mod(_st.y,2.0))*2.0;
37+
38+
// |
39+
// 2 | 3
40+
// |
41+
//--------------
42+
// |
43+
// 0 | 1
44+
// |
45+
46+
// Make each cell between 0.0 - 1.0
47+
_st = fract(_st);
48+
49+
// Rotate each cell according to the index
50+
if(index == 1.0){
51+
// Rotate cell 1 by 90 degrees
52+
_st = rotate2D(_st,PI*0.5);
53+
} else if(index == 2.0){
54+
// Rotate cell 2 by -90 degrees
55+
_st = rotate2D(_st,PI*-0.5);
56+
} else if(index == 3.0){
57+
// Rotate cell 3 by 180 degrees
58+
_st = rotate2D(_st,PI);
59+
}
60+
61+
return _st;
62+
}
63+
64+
void main (void) {
65+
vec2 st = gl_FragCoord.xy/u_resolution.xy;
66+
67+
st = tile(st,3.0);
68+
st = rotateTilePattern(st);
69+
70+
// Make more interesting combinations
71+
// st = tile(st,2.0);
72+
// st = rotate2D(st,-PI*u_time*0.25);
73+
// st = rotateTilePattern(st*2.);
74+
// st = rotate2D(st,PI*u_time*0.25);
75+
76+
// step(st.x,st.y) just makes a b&w triangles
77+
// but you can use whatever design you want.
78+
gl_FragColor = vec4(vec3(step(st.x,st.y)),1.0);
79+
}

062-pattern-truchet-random.glsl

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
#endif
4+
5+
//glslViewer col-invert-sqaures.frag ../textures/pink-necked-green-pigeon-big.jpg
6+
#define PI 3.14159265359
7+
#define SPEED.2
8+
9+
uniform float u_time;
10+
uniform vec2 u_resolution;
11+
uniform sampler2D u_tex0;
12+
13+
// returns a random float value given a vec2
14+
float hash(vec2 p){
15+
p=fract(p*vec2(234.34,435.345));
16+
p+=dot(p,p+34.23);
17+
return fract(p.x*p.y);
18+
}
19+
20+
float myShape(vec2 gv){
21+
// Try to play with the functions that
22+
// you know. Try to came up with your own
23+
// shape.
24+
25+
//return abs(gv.y + gv.x);
26+
return abs(abs(gv.x+gv.y)-.5);
27+
28+
}
29+
30+
void main(void){
31+
vec2 uv=(2.*gl_FragCoord.xy-u_resolution.xy)/u_resolution.y;
32+
vec3 col=vec3(0.);
33+
// 6) Animate
34+
// uv += u_time * 0.3;
35+
uv*=8.;
36+
vec2 gv=fract(uv)-.5;
37+
38+
// 2) each cell has now an id
39+
vec2 id=floor(uv);
40+
41+
// 3) we can use the id to give a random value to each cell.
42+
float n=hash(id);// random between 0 and 1
43+
44+
// 5) random flip the x direction only if a condition is match
45+
if(n<.5){
46+
gv.x*=-1.;
47+
};
48+
49+
// 4) Try to see what happens if you flip the x direction
50+
//gv.x *= -1.;
51+
52+
float width=.25;
53+
// 1) draw something. Possible something that is not simmetric
54+
float d=myShape(gv);
55+
56+
float mask=d-width;
57+
mask=smoothstep(.01,-.01,mask);
58+
59+
// this time we are not uing mix, we are simply adding white to the black bg.
60+
col+=mask;
61+
//col.rg += id;
62+
// outline
63+
//if(gv.x > 0.48 || gv.y > 0.48) col = vec3(1.,0.,0.);
64+
65+
gl_FragColor=vec4(sqrt(col),1.);
66+
}

0 commit comments

Comments
 (0)