-
Notifications
You must be signed in to change notification settings - Fork 6
/
19_TruchetIteration.fs
executable file
·203 lines (175 loc) · 3.63 KB
/
19_TruchetIteration.fs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// SaturdayShader Week 19 : Truchet Iteration
// by Joseph Fiola (http://www.joefiola.com)
// 2015-12-26
// This shader is a mix of Patricio Gonzalez Vivo's "Truchet Tiles" example
// found at http://patriciogonzalezvivo.com/2015/thebookofshaders/09/ @patriciogv ( patriciogonzalezvivo.com ) - 2015
// as well as last week's "Iteration" shader found at http://www.interactiveshaderformat.com/sketches/746
/*{
"CREDIT": "Joseph Fiola",
"DESCRIPTION": "",
"CATEGORIES": [
"Generator"
],
"INPUTS": [
{
"NAME": "invert",
"TYPE": "bool"
},
{
"NAME": "gradient",
"TYPE": "float",
"DEFAULT": 0.2,
"MIN": 0.0,
"MAX": 2.0
},
{
"NAME": "xScale",
"TYPE": "float",
"DEFAULT": 0.49,
"MIN": 1e-4,
"MAX": 0.5
},
{
"NAME": "yScale",
"TYPE": "float",
"DEFAULT": 0.3,
"MIN": 1e-4,
"MAX": 0.5
},
{
"NAME": "tiles",
"TYPE": "float",
"DEFAULT": 2.0,
"MIN": 0.0,
"MAX": 20.0
},
{
"NAME": "rotate",
"TYPE": "float",
"DEFAULT": 0.15,
"MIN": 0.0,
"MAX": 1.0
},
{
"NAME": "pos",
"TYPE": "point2D",
"DEFAULT": [
0.5,
0.5
],
"MIN": [
0.0,
0.0
],
"MAX": [
1.0,
1.0
]
},
{
"NAME": "offset",
"TYPE": "point2D",
"DEFAULT": [
0.05,
0.05
],
"MIN": [
-0.1,
-0.1
],
"MAX": [
0.1,
0.1
]
},
{
"NAME": "scalePos",
"TYPE": "point2D",
"DEFAULT": [
0.5,
0.5
],
"MIN": [
0.0,
0.0
],
"MAX": [
1.0,
1.0
]
}
]
}*/
#define PI 3.14159265358979323846
vec2 rotate2D (vec2 _st, float _angle) {
_st -= 0.5;
_st = mat2(cos(_angle),-sin(_angle),
sin(_angle),cos(_angle)) * _st;
_st += 0.5;
return _st;
}
vec2 tile (vec2 _st, float _zoom) {
_st *= _zoom;
return fract(_st);
}
vec2 rotateTilePattern(vec2 _st){
// Scale the coordinate system by 2x2
_st -=scalePos;
_st *= xScale + yScale * 0.5;
//_st += scalePos;
// 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;
}
float box(vec2 _st, vec2 _size, float _smoothEdges){
//_size = vec2(0.0);
_st += 0.5;
vec2 aa = vec2(_smoothEdges*0.5);
vec2 uv = smoothstep(_size,_size+aa,_st);
uv *= smoothstep(_size,_size+aa,vec2(1.0)-_st);
return uv.x*uv.y;
}
vec3 invertColor(vec3 color) {
return vec3(color *-1.0 + 1.0);
}
void main() {
vec2 st = gl_FragCoord.xy/RENDERSIZE;
st -= vec2(pos);
st.x *= RENDERSIZE.x/RENDERSIZE.y;
st = tile(st,tiles);
st = rotateTilePattern(st);
// Make more interesting combinations
st = rotate2D(st,-PI*rotate*2.);
st = rotateTilePattern(st);
vec3 color = vec3(0.0);
for (float i = 0.0; i<50.0; i++){
vec3 shape = vec3(0.0);
shape = vec3(box(st-=vec2(offset), vec2(xScale, yScale), gradient*0.05));
shape *= invertColor(shape);
color += shape;
}
float finalColor = 0.0;
if (invert) {
color = invertColor(color);
finalColor = color.x + color.y - color.z;
} else {
finalColor = color.x + color.y + color.z;
}
gl_FragColor = vec4(vec3(finalColor),1.0);
}