-
Notifications
You must be signed in to change notification settings - Fork 5
/
truchet.yaml
88 lines (85 loc) · 3.63 KB
/
truchet.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import:
- ../geometry/matrices.yaml
styles:
tiling-truchet:
doc:
author:
name: Patricio Gonzalez Vivo
twitter: patriciogv
version: 0.0.1
tangram-version: 0.0.7
licence: MIT
description: |
Repeats a coordinate space (`vec2 st`) in diferent tiles acording to a Truchet patern.
There is two way to do this: by mirroring the spaces (`vec2 truchetMirror (vec2 st)`) or rotating them ('vec2 truchetRotate (vec2 st)')
test:
mirror: { blocks: { color: " color.rg += truchetMirror(v_texcoord.xy);" } }
rotate: { blocks: { color: " color.rg += truchetRotate(v_texcoord.xy);" } }
mix: [geometry-matrices]
shaders:
defines:
PI: 3.14159265358979323846
blocks:
global: |
// Truchet Patern reference
// ================================
//
// |
// 0 | 1
// |
// --------------
// |
// 2 | 3
// |
//
// A- Mirror tiles acording to a Truchet patern
// ================================
vec2 truchetMirror (vec2 st) {
// Shapes mirror pattern
vec2 f_st = fract(st*2.);
vec2 i_st = floor(st*2.);
// non-even row + non-even col
float index = 0.0;
index += mod(i_st.x,2.0);
index += mod(i_st.y,2.0)*2.;
// rotate acording
if(index == 1.0){
f_st.x = 1.-f_st.x;
} else if(index == 2.0){
f_st.y = 1.-f_st.y;
} else if(index == 3.0){
f_st.x = 1.-f_st.x;
f_st.y = 1.-f_st.y;
}
return f_st;
}
//
// B- Rotate tiles acording to a Truchet patern
// ================================
vec2 truchetRotate (vec2 st) {
// Scale the coordinate system by 2x2
st *= 2.0;
//
// Give each cell an index number
// according to its position
float index = 0.0;
index += step(1., mod(st.x,2.0));
index += step(1., mod(st.y,2.0))*2.0;
//
// Make each cell between 0.0 - 1.0
st = fract(st);
//
// Rotate each cell according to the index
if(index == 1.0){
// Rotate cell 1 by 90 degrees
st = rotate2D(st,PI*0.5);
} else if(index == 2.0){
// Rotate cell 2 by -90 degrees
st = rotate2D(st,PI*-0.5);
} else if(index == 3.0){
// Rotate cell 3 by 180 degrees
st = rotate2D(st,PI);
}
//
return st;
}