-
Notifications
You must be signed in to change notification settings - Fork 6
/
SphericalPolyhedra.fs
299 lines (267 loc) · 7.46 KB
/
SphericalPolyhedra.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*{
"CREDIT" : "SphericalPolyhedra by nimitz",
"CATEGORIES" : [
"ci"
],
"DESCRIPTION": "",
"INPUTS": [
{
"NAME": "inputImage",
"TYPE" : "image"
},
{
"NAME": "iZoom",
"TYPE" : "float",
"MIN" : 0.0,
"MAX" : 1.0,
"DEFAULT" : 1.0
},
{
"NAME": "iSteps",
"TYPE" : "float",
"MIN" : 2.0,
"MAX" : 75.0,
"DEFAULT" : 19.0
},
{
"NAME" :"iMouse",
"TYPE" : "point2D",
"DEFAULT" : [0.0, 0.0],
"MAX" : [640.0, 480.0],
"MIN" : [0.0, 0.0]
},
{
"NAME": "iColor",
"TYPE" : "color",
"DEFAULT" : [
0.9,
0.6,
0.0,
1.0
]
}
],
}
*/
// https://www.shadertoy.com/view/4dBXWD
//Spherical polyhedra by nimitz (twitter: @stormoid)
/*
Follow up to my "Sphere mappings" shader (https://www.shadertoy.com/view/4sjXW1)
I was thinking about a cheap way to do icosahedral mapping and realized
I could just project on an axis and rotate the sphere for each projected
"facet".
Here I am showing only tilings of the regular polyhedra but this technique can
be used for any tilings of the sphere, regular or not. (or even arbitrary projections)
I omitted the tetraedron since the small number of projections
results in heavy deformation.
Perhaps there is a way to make that process cheaper? Let me know.
*/
#define time TIME
mat2 mm2(in float a){float c = cos(a), s = sin(a);return mat2(c,-s,s,c);}
vec3 rotx(vec3 p, float a){ float s = sin(a), c = cos(a);
return vec3(p.x, c*p.y - s*p.z, s*p.y + c*p.z); }
vec3 roty(vec3 p, float a){ float s = sin(a), c = cos(a);
return vec3(c*p.x + s*p.z, p.y, -s*p.x + c*p.z); }
vec3 rotz(vec3 p, float a){ float s = sin(a), c = cos(a);
return vec3(c*p.x - s*p.y, s*p.x + c*p.y, p.z); }
//---------------------------Textures--------------------------------
//-------------------------------------------------------------------
vec3 texpent(in vec2 p, in float idx)
{
float siz = RENDERSIZE.x *.007;
vec2 q = abs(p);
float rz = sin(clamp(max(max(q.x*1.176-p.y*0.385, q.x*0.727+p.y),
-p.y*1.237)*33.,0.,25.))*siz+siz;
vec3 col = (sin(vec3(1,1.5,5)*idx)+2.)*(rz+0.25);
col -= sin(dot(p,p)*10.+time*5.)*0.4;
return col;
}
vec3 textri2(in vec2 p, in float idx)
{
float siz = RENDERSIZE.x *.007;
vec2 q = abs(p);
float rz = sin(clamp(max(q.x*1.73205+p.y, -p.y*2.)*32.,0.,25.))*siz+siz;
vec3 col = (sin(vec3(1,1.7,5)*idx)+2.)*(rz+0.25);
col -= sin(p.x*20.+time*5.)*0.2;
return col;
}
vec3 texcub(in vec2 p, in float idx)
{
float siz = RENDERSIZE.x *.007;
float rz = sin(clamp(max(abs(p.x),abs(p.y))*24.,0.,25.))*siz+siz;
vec3 col = (sin(vec3(4,3.,5)*idx*.9)+2.)*(rz+0.25);
float a= atan(p.y,p.x);
col -= sin(a*15.+time*11.)*0.15-0.15;
return col;
}
vec3 textri(in vec2 p, in float idx)
{
float siz = RENDERSIZE.x *.001;
p*=1.31;
vec2 bp = p;
p.x *= 1.732;
vec2 f = fract(p)-0.5;
float d = abs(f.x-f.y);
d = min(abs(f.x+f.y),d);
float f1 = fract((p.y-0.25)*2.);
d = min(d,abs(f1-0.5));
d = 1.-smoothstep(0.,.1/(siz+.7),d);
vec2 q = abs(bp);
p = bp;
d -= smoothstep(1.,1.3,(max(q.x*1.73205+p.y, -p.y*2.)));
vec3 col = (sin(vec3(1.,1.5,5)*idx)+2.)*((1.-d)+0.25);
col -= sin(p.x*10.+time*8.)*0.15-0.1;
return col;
}
//----------------------------------------------------------------------------
//----------------------------------Sphere Tilings----------------------------
//----------------------------------------------------------------------------
//All the rotation matrices can be precomputed for better performance.
//5 mirrored pentagons for the dodecahedron
vec3 dod(in vec3 p)
{
vec3 col = vec3(1);
vec2 uv = vec2(0);
for (float i = 0.;i<=4.;i++)
{
p = roty(p,0.81);
p = rotx(p,0.759);
p = rotz(p,0.3915);
uv = vec2(p.z,p.y)/((p.x));
col = min(texpent(uv,i+1.),col);
}
p = roty(p,0.577);
p = rotx(p,-0.266);
p = rotz(p,-0.848);
uv = vec2(p.z,-p.y)/((p.x));
col = min(texpent(uv,6.),col);
return 1.-col;
}
//10 mirrored triangles for the icosahedron
vec3 ico(in vec3 p)
{
vec3 col = vec3(1);
vec2 uv = vec2(0);
//center band
const float n1 = .7297;
const float n2 = 1.0472;
for (float i = 0.;i<5.;i++)
{
if(mod(i,2.)==0.)
{
p = rotz(p,n1);
p = rotx(p,n2);
}
else
{
p = rotz(p,n1);
p = rotx(p,-n2);
}
uv = vec2(p.z,p.y)/((p.x));
col = min(textri(uv,i+1.),col);
}
p = roty(p,1.048);
p = rotz(p,.8416);
p = rotx(p,.7772);
//top caps
for (float i = 0.;i<5.;i++)
{
p = rotz(p,n1);
p = rotx(p,n2);
uv = vec2(p.z,p.y)/((p.x));
col = min(textri(uv,i+6.),col);
}
return 1.-col;
}
//4 mirrored triangles for octahedron
vec3 octa(in vec3 p)
{
vec3 col = vec3(1);
vec2 uv = vec2(0);
const float n1 = 1.231;
const float n2 = 1.047;
for (float i = 0.;i<4.;i++)
{
p = rotz(p,n1);
p = rotx(p,n2);
uv = vec2(p.z,p.y)/((p.x));
col = min(textri2(uv*.54,i+1.),col);
}
return 1.-col;
}
//cube using the same technique for completeness
vec3 cub(in vec3 p)
{
vec3 col = vec3(1);
vec2 uv = vec2(p.z,p.y)/((p.x));
col = min(texcub(uv*1.01,15.),col);
p = rotz(p,1.5708);
uv = vec2(p.z,p.y)/((p.x));
col = min(texcub(uv*1.01,4.),col);
p = roty(p,1.5708);
uv = vec2(p.z,p.y)/((p.x));
col = min(texcub(uv*1.01,5.),col);
return 1.-col;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
vec2 iSphere2(in vec3 ro, in vec3 rd)
{
vec3 oc = ro;
float b = dot(oc, rd);
float c = dot(oc,oc) - 1.;
float h = b*b - c;
if(h <0.0) return vec2(-1.);
else return vec2((-b - sqrt(h)), (-b + sqrt(h)));
}
void main(void)
{
vec2 p = gl_FragCoord.xy/RENDERSIZE.xy-0.5;
vec2 bp = p+0.5;
p.x*=RENDERSIZE.x/RENDERSIZE.y;
vec2 um = iMouse.xy / RENDERSIZE.xy-.5;
um.x *= RENDERSIZE.x/RENDERSIZE.y;
//camera
vec3 ro = vec3(0.,0.,3.5);
vec3 rd = normalize(vec3(p,-1.4));
mat2 mx = mm2(time*0.25+um.x*6.);
mat2 my = mm2(time*0.27+um.y*6.);
ro.xz *= mx;rd.xz *= mx;
ro.xy *= my;rd.xy *= my;
float sel = mod(floor((time+10.)*0.2),4.);
//sel=0.;
vec2 t = iSphere2(ro,rd);
vec3 col = vec3(0.);
float bg = clamp(dot(-rd,vec3(0.577))*0.3+.6,0.,1.);
if (sel == 0.) col = dod(rd)*1.2;
else if (sel == 1.) col = ico(rd)*1.2;
else if (sel == 2.) col = cub(rd)*1.2;
else if (sel == 3.) col = octa(rd)*1.2;
if (t.x > 0.)
{
vec3 pos = ro+rd*t.x;
vec3 pos2 = ro+rd*t.y;
vec3 rf = reflect(rd,pos);
if (sel == 0.)
{
vec3 col2 = max(dod(pos)*2.,dod(pos2)*.6);
col = mix(max(col,col2),col2,0.6);
}
else if (sel == 1.)
{
vec3 col2 = max(ico(pos2)*0.6,ico(pos)*2.);
col = mix(max(col,col2),col2,0.6);
}
else if (sel == 2.)
{
vec3 col2 = max(cub(pos2)*0.6,cub(pos)*2.);
col = mix(max(col,col2),col2,0.6);
}
else if (sel == 3.)
{
vec3 col2 = max(octa(pos2)*0.6,octa(pos)*2.);
col = mix(max(col,col2),col2,0.6);
}
}
gl_FragColor = vec4(col, 1.0);
}