-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
669 lines (574 loc) · 21.7 KB
/
index.html
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Planets.js Test</title>
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,600" rel='stylesheet'>
<link rel="stylesheet" href="examples/examples.css">
</head>
<body>
<div class="planet-info">
<div class="planet-heading">Star Planet</div>
<div class="planet-description">A "planet" that looks more like a star.</div>
<br>
<button onclick="generateStarPlanet()">Generate Star Planet</button>
</div>
<canvas id="star-sketch"></canvas>
<script id="shader-vertex-star-halo" type="x-shader/x-vertex">
uniform float sphere_radius;
uniform vec3 sphere_position;
uniform float time;
uniform float time_multiplier;
uniform vec3 color_step_1;
uniform vec3 color_step_2;
uniform vec3 color_step_3;
uniform vec3 color_step_4;
uniform float ratio_step_1;
uniform float ratio_step_2;
varying float v_distance_to_surface;
void main()
{
float camera_distance = distance(sphere_position,cameraPosition);
float angle = asin(sphere_radius / camera_distance);
float visible_radius = tan(angle) * camera_distance;
float distance_to_surface = distance(sphere_position,position) - visible_radius;
v_distance_to_surface = distance_to_surface;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0);
}
</script>
<script id="shader-fragment-star-halo" type="x-shader/x-fragment">
uniform float time;
uniform float time_multiplier;
uniform vec3 color_step_1;
uniform vec3 color_step_2;
uniform vec3 color_step_3;
uniform vec3 color_step_4;
uniform float ratio_step_1;
uniform float ratio_step_2;
varying float v_distance_to_surface;
vec3 get_color_from_gradient(float value)
{
vec3 color;
if(value < ratio_step_1)
{
value = smoothstep(0.0,ratio_step_1,value);
color = mix( color_step_1, color_step_2, vec3(value,value,value));
// color = vec3(1.0,0.0,0.0);
}
else if(value < ratio_step_2)
{
value = smoothstep(ratio_step_1,ratio_step_2,value);
color = mix( color_step_2, color_step_3, vec3(value,value,value));
// color = vec3(0.0,1.0,0.0);
}
else
{
value = smoothstep(ratio_step_2,1.0,value);
color = mix( color_step_3, color_step_4, vec3(value,value,value));
// color = vec3(0.0,0.0,1.0);
}
return color;
}
void main()
{
float halo_1_radius = 0.03;
float strength_1 = (1.0 - v_distance_to_surface / halo_1_radius);
float halo_2_radius = 1.0;
float strength_2 = (1.0 - v_distance_to_surface / halo_2_radius);
vec4 color_1 = vec4(get_color_from_gradient(strength_1),strength_1);
vec4 color_2 = vec4(get_color_from_gradient(strength_2 - 0.7),strength_2);
color_1 = clamp(color_1,vec4(0.0,0.0,0.0,0.0),vec4(1.0,1.0,1.0,1.0));
color_2 = clamp(color_2,vec4(0.0,0.0,0.0,0.0),vec4(1.0,1.0,1.0,1.0));
gl_FragColor = color_1 + color_2 * 0.65;
}
</script>
<script id="shader-vertex-star-sphere" type="x-shader/x-vertex">
//
// Author: Stefan Gustavson ([email protected])
// Version: 2011-08-22
//
// Copyright (c) 2011 Stefan Gustavson. All rights reserved.
// Distributed under the MIT license. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
vec4 mod289(vec4 x)
{
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec4 permute(vec4 x)
{
return mod289(((x*34.0)+1.0)*x);
}
vec4 taylorInvSqrt(vec4 r)
{
return 1.79284291400159 - 0.85373472095314 * r;
}
vec4 fade(vec4 t) {
return t*t*t*(t*(t*6.0-15.0)+10.0);
}
// Classic Perlin noise
float cnoise(vec4 P)
{
vec4 Pi0 = floor(P); // Integer part for indexing
vec4 Pi1 = Pi0 + 1.0; // Integer part + 1
Pi0 = mod289(Pi0);
Pi1 = mod289(Pi1);
vec4 Pf0 = fract(P); // Fractional part for interpolation
vec4 Pf1 = Pf0 - 1.0; // Fractional part - 1.0
vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
vec4 iy = vec4(Pi0.yy, Pi1.yy);
vec4 iz0 = vec4(Pi0.zzzz);
vec4 iz1 = vec4(Pi1.zzzz);
vec4 iw0 = vec4(Pi0.wwww);
vec4 iw1 = vec4(Pi1.wwww);
vec4 ixy = permute(permute(ix) + iy);
vec4 ixy0 = permute(ixy + iz0);
vec4 ixy1 = permute(ixy + iz1);
vec4 ixy00 = permute(ixy0 + iw0);
vec4 ixy01 = permute(ixy0 + iw1);
vec4 ixy10 = permute(ixy1 + iw0);
vec4 ixy11 = permute(ixy1 + iw1);
vec4 gx00 = ixy00 * (1.0 / 7.0);
vec4 gy00 = floor(gx00) * (1.0 / 7.0);
vec4 gz00 = floor(gy00) * (1.0 / 6.0);
gx00 = fract(gx00) - 0.5;
gy00 = fract(gy00) - 0.5;
gz00 = fract(gz00) - 0.5;
vec4 gw00 = vec4(0.75) - abs(gx00) - abs(gy00) - abs(gz00);
vec4 sw00 = step(gw00, vec4(0.0));
gx00 -= sw00 * (step(0.0, gx00) - 0.5);
gy00 -= sw00 * (step(0.0, gy00) - 0.5);
vec4 gx01 = ixy01 * (1.0 / 7.0);
vec4 gy01 = floor(gx01) * (1.0 / 7.0);
vec4 gz01 = floor(gy01) * (1.0 / 6.0);
gx01 = fract(gx01) - 0.5;
gy01 = fract(gy01) - 0.5;
gz01 = fract(gz01) - 0.5;
vec4 gw01 = vec4(0.75) - abs(gx01) - abs(gy01) - abs(gz01);
vec4 sw01 = step(gw01, vec4(0.0));
gx01 -= sw01 * (step(0.0, gx01) - 0.5);
gy01 -= sw01 * (step(0.0, gy01) - 0.5);
vec4 gx10 = ixy10 * (1.0 / 7.0);
vec4 gy10 = floor(gx10) * (1.0 / 7.0);
vec4 gz10 = floor(gy10) * (1.0 / 6.0);
gx10 = fract(gx10) - 0.5;
gy10 = fract(gy10) - 0.5;
gz10 = fract(gz10) - 0.5;
vec4 gw10 = vec4(0.75) - abs(gx10) - abs(gy10) - abs(gz10);
vec4 sw10 = step(gw10, vec4(0.0));
gx10 -= sw10 * (step(0.0, gx10) - 0.5);
gy10 -= sw10 * (step(0.0, gy10) - 0.5);
vec4 gx11 = ixy11 * (1.0 / 7.0);
vec4 gy11 = floor(gx11) * (1.0 / 7.0);
vec4 gz11 = floor(gy11) * (1.0 / 6.0);
gx11 = fract(gx11) - 0.5;
gy11 = fract(gy11) - 0.5;
gz11 = fract(gz11) - 0.5;
vec4 gw11 = vec4(0.75) - abs(gx11) - abs(gy11) - abs(gz11);
vec4 sw11 = step(gw11, vec4(0.0));
gx11 -= sw11 * (step(0.0, gx11) - 0.5);
gy11 -= sw11 * (step(0.0, gy11) - 0.5);
vec4 g0000 = vec4(gx00.x,gy00.x,gz00.x,gw00.x);
vec4 g1000 = vec4(gx00.y,gy00.y,gz00.y,gw00.y);
vec4 g0100 = vec4(gx00.z,gy00.z,gz00.z,gw00.z);
vec4 g1100 = vec4(gx00.w,gy00.w,gz00.w,gw00.w);
vec4 g0010 = vec4(gx10.x,gy10.x,gz10.x,gw10.x);
vec4 g1010 = vec4(gx10.y,gy10.y,gz10.y,gw10.y);
vec4 g0110 = vec4(gx10.z,gy10.z,gz10.z,gw10.z);
vec4 g1110 = vec4(gx10.w,gy10.w,gz10.w,gw10.w);
vec4 g0001 = vec4(gx01.x,gy01.x,gz01.x,gw01.x);
vec4 g1001 = vec4(gx01.y,gy01.y,gz01.y,gw01.y);
vec4 g0101 = vec4(gx01.z,gy01.z,gz01.z,gw01.z);
vec4 g1101 = vec4(gx01.w,gy01.w,gz01.w,gw01.w);
vec4 g0011 = vec4(gx11.x,gy11.x,gz11.x,gw11.x);
vec4 g1011 = vec4(gx11.y,gy11.y,gz11.y,gw11.y);
vec4 g0111 = vec4(gx11.z,gy11.z,gz11.z,gw11.z);
vec4 g1111 = vec4(gx11.w,gy11.w,gz11.w,gw11.w);
vec4 norm00 = taylorInvSqrt(vec4(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100)));
g0000 *= norm00.x;
g0100 *= norm00.y;
g1000 *= norm00.z;
g1100 *= norm00.w;
vec4 norm01 = taylorInvSqrt(vec4(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101)));
g0001 *= norm01.x;
g0101 *= norm01.y;
g1001 *= norm01.z;
g1101 *= norm01.w;
vec4 norm10 = taylorInvSqrt(vec4(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110)));
g0010 *= norm10.x;
g0110 *= norm10.y;
g1010 *= norm10.z;
g1110 *= norm10.w;
vec4 norm11 = taylorInvSqrt(vec4(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111)));
g0011 *= norm11.x;
g0111 *= norm11.y;
g1011 *= norm11.z;
g1111 *= norm11.w;
float n0000 = dot(g0000, Pf0);
float n1000 = dot(g1000, vec4(Pf1.x, Pf0.yzw));
float n0100 = dot(g0100, vec4(Pf0.x, Pf1.y, Pf0.zw));
float n1100 = dot(g1100, vec4(Pf1.xy, Pf0.zw));
float n0010 = dot(g0010, vec4(Pf0.xy, Pf1.z, Pf0.w));
float n1010 = dot(g1010, vec4(Pf1.x, Pf0.y, Pf1.z, Pf0.w));
float n0110 = dot(g0110, vec4(Pf0.x, Pf1.yz, Pf0.w));
float n1110 = dot(g1110, vec4(Pf1.xyz, Pf0.w));
float n0001 = dot(g0001, vec4(Pf0.xyz, Pf1.w));
float n1001 = dot(g1001, vec4(Pf1.x, Pf0.yz, Pf1.w));
float n0101 = dot(g0101, vec4(Pf0.x, Pf1.y, Pf0.z, Pf1.w));
float n1101 = dot(g1101, vec4(Pf1.xy, Pf0.z, Pf1.w));
float n0011 = dot(g0011, vec4(Pf0.xy, Pf1.zw));
float n1011 = dot(g1011, vec4(Pf1.x, Pf0.y, Pf1.zw));
float n0111 = dot(g0111, vec4(Pf0.x, Pf1.yzw));
float n1111 = dot(g1111, Pf1);
vec4 fade_xyzw = fade(Pf0);
vec4 n_0w = mix(vec4(n0000, n1000, n0100, n1100), vec4(n0001, n1001, n0101, n1101), fade_xyzw.w);
vec4 n_1w = mix(vec4(n0010, n1010, n0110, n1110), vec4(n0011, n1011, n0111, n1111), fade_xyzw.w);
vec4 n_zw = mix(n_0w, n_1w, fade_xyzw.z);
vec2 n_yzw = mix(n_zw.xy, n_zw.zw, fade_xyzw.y);
float n_xyzw = mix(n_yzw.x, n_yzw.y, fade_xyzw.x);
return 2.2 * n_xyzw;
}
uniform samplerCube texCube;
uniform float time;
uniform float time_multiplier;
uniform vec3 color_step_1;
uniform vec3 color_step_2;
uniform vec3 color_step_3;
uniform vec3 color_step_4;
uniform float ratio_step_1;
uniform float ratio_step_2;
uniform float displacement;
varying vec3 v_position;
varying float v_intensity;
vec3 get_position(vec3 position)
{
vec3 new_position = position;
// Set up
float value = 0.0;
float frequency_1 = 12.0;
float speed_1 = 0.1;
float frequency_2 = 24.0;
float speed_2 = 0.2;
// First global perlin (from 0 to 1)
float perlin_1 = (cnoise(vec4(frequency_1 * position,time *time_multiplier * 0.6)) + 1.0) / 2.0;
float perlin_2 = (cnoise(vec4(frequency_2 * position,time *time_multiplier * 0.8)) + 1.0) / 2.0;
float value_1 = clamp(perlin_1,0.4,0.5) - 0.35;
float value_2 = perlin_2;
value = value_1 * value_2 * 0.12;
new_position += normal * value;
return new_position;
}
float get_intensity(vec3 position,float angle)
{
// Perlins
float value = 0.0;
float perlin_1 = (cnoise(vec4(2.0 * position,time * time_multiplier * 0.2)) + 1.0) / 2.0;
float perlin_2 = (cnoise(vec4(6.0 * position,time * time_multiplier * 0.3)) + 1.0) / 2.0;
float perlin_3 = (cnoise(vec4(12.0 * position,time * time_multiplier * 0.5)) + 1.0) / 2.0;
float value_1 = clamp(perlin_1,0.4,0.5) - 0.35;
float value_2 = perlin_2;
float value_3 = perlin_3;
value = value_1 * perlin_2 * 8.0 * value_3 * 0.4;
value += clamp(angle - 1.0,0.0,1.0) * 1.2;
return value;
}
void main()
{
vec3 new_position = get_position(position);
vec3 direction = cameraPosition - position;
v_position = position;
float angle = acos(dot(normalize(normal),normalize(direction)));
v_intensity = get_intensity(v_position,angle);
gl_Position = projectionMatrix * modelViewMatrix * vec4(new_position,1.0);
}
</script>
<script id="shader-fragment-star-sphere" type="x-shader/x-fragment">
//
// GLSL textureless classic 4D noise "cnoise",
// with an RSL-style periodic variant "pnoise".
// Author: Stefan Gustavson ([email protected])
// Version: 2011-08-22
//
// Copyright (c) 2011 Stefan Gustavson. All rights reserved.
// Distributed under the MIT license. See LICENSE file.
// https://github.com/ashima/webgl-noise
//
vec4 mod289(vec4 x)
{
return x - floor(x * (1.0 / 289.0)) * 289.0;
}
vec4 permute(vec4 x)
{
return mod289(((x*34.0)+1.0)*x);
}
vec4 taylorInvSqrt(vec4 r)
{
return 1.79284291400159 - 0.85373472095314 * r;
}
vec4 fade(vec4 t) {
return t*t*t*(t*(t*6.0-15.0)+10.0);
}
// Classic Perlin noise
float cnoise(vec4 P)
{
vec4 Pi0 = floor(P); // Integer part for indexing
vec4 Pi1 = Pi0 + 1.0; // Integer part + 1
Pi0 = mod289(Pi0);
Pi1 = mod289(Pi1);
vec4 Pf0 = fract(P); // Fractional part for interpolation
vec4 Pf1 = Pf0 - 1.0; // Fractional part - 1.0
vec4 ix = vec4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
vec4 iy = vec4(Pi0.yy, Pi1.yy);
vec4 iz0 = vec4(Pi0.zzzz);
vec4 iz1 = vec4(Pi1.zzzz);
vec4 iw0 = vec4(Pi0.wwww);
vec4 iw1 = vec4(Pi1.wwww);
vec4 ixy = permute(permute(ix) + iy);
vec4 ixy0 = permute(ixy + iz0);
vec4 ixy1 = permute(ixy + iz1);
vec4 ixy00 = permute(ixy0 + iw0);
vec4 ixy01 = permute(ixy0 + iw1);
vec4 ixy10 = permute(ixy1 + iw0);
vec4 ixy11 = permute(ixy1 + iw1);
vec4 gx00 = ixy00 * (1.0 / 7.0);
vec4 gy00 = floor(gx00) * (1.0 / 7.0);
vec4 gz00 = floor(gy00) * (1.0 / 6.0);
gx00 = fract(gx00) - 0.5;
gy00 = fract(gy00) - 0.5;
gz00 = fract(gz00) - 0.5;
vec4 gw00 = vec4(0.75) - abs(gx00) - abs(gy00) - abs(gz00);
vec4 sw00 = step(gw00, vec4(0.0));
gx00 -= sw00 * (step(0.0, gx00) - 0.5);
gy00 -= sw00 * (step(0.0, gy00) - 0.5);
vec4 gx01 = ixy01 * (1.0 / 7.0);
vec4 gy01 = floor(gx01) * (1.0 / 7.0);
vec4 gz01 = floor(gy01) * (1.0 / 6.0);
gx01 = fract(gx01) - 0.5;
gy01 = fract(gy01) - 0.5;
gz01 = fract(gz01) - 0.5;
vec4 gw01 = vec4(0.75) - abs(gx01) - abs(gy01) - abs(gz01);
vec4 sw01 = step(gw01, vec4(0.0));
gx01 -= sw01 * (step(0.0, gx01) - 0.5);
gy01 -= sw01 * (step(0.0, gy01) - 0.5);
vec4 gx10 = ixy10 * (1.0 / 7.0);
vec4 gy10 = floor(gx10) * (1.0 / 7.0);
vec4 gz10 = floor(gy10) * (1.0 / 6.0);
gx10 = fract(gx10) - 0.5;
gy10 = fract(gy10) - 0.5;
gz10 = fract(gz10) - 0.5;
vec4 gw10 = vec4(0.75) - abs(gx10) - abs(gy10) - abs(gz10);
vec4 sw10 = step(gw10, vec4(0.0));
gx10 -= sw10 * (step(0.0, gx10) - 0.5);
gy10 -= sw10 * (step(0.0, gy10) - 0.5);
vec4 gx11 = ixy11 * (1.0 / 7.0);
vec4 gy11 = floor(gx11) * (1.0 / 7.0);
vec4 gz11 = floor(gy11) * (1.0 / 6.0);
gx11 = fract(gx11) - 0.5;
gy11 = fract(gy11) - 0.5;
gz11 = fract(gz11) - 0.5;
vec4 gw11 = vec4(0.75) - abs(gx11) - abs(gy11) - abs(gz11);
vec4 sw11 = step(gw11, vec4(0.0));
gx11 -= sw11 * (step(0.0, gx11) - 0.5);
gy11 -= sw11 * (step(0.0, gy11) - 0.5);
vec4 g0000 = vec4(gx00.x,gy00.x,gz00.x,gw00.x);
vec4 g1000 = vec4(gx00.y,gy00.y,gz00.y,gw00.y);
vec4 g0100 = vec4(gx00.z,gy00.z,gz00.z,gw00.z);
vec4 g1100 = vec4(gx00.w,gy00.w,gz00.w,gw00.w);
vec4 g0010 = vec4(gx10.x,gy10.x,gz10.x,gw10.x);
vec4 g1010 = vec4(gx10.y,gy10.y,gz10.y,gw10.y);
vec4 g0110 = vec4(gx10.z,gy10.z,gz10.z,gw10.z);
vec4 g1110 = vec4(gx10.w,gy10.w,gz10.w,gw10.w);
vec4 g0001 = vec4(gx01.x,gy01.x,gz01.x,gw01.x);
vec4 g1001 = vec4(gx01.y,gy01.y,gz01.y,gw01.y);
vec4 g0101 = vec4(gx01.z,gy01.z,gz01.z,gw01.z);
vec4 g1101 = vec4(gx01.w,gy01.w,gz01.w,gw01.w);
vec4 g0011 = vec4(gx11.x,gy11.x,gz11.x,gw11.x);
vec4 g1011 = vec4(gx11.y,gy11.y,gz11.y,gw11.y);
vec4 g0111 = vec4(gx11.z,gy11.z,gz11.z,gw11.z);
vec4 g1111 = vec4(gx11.w,gy11.w,gz11.w,gw11.w);
vec4 norm00 = taylorInvSqrt(vec4(dot(g0000, g0000), dot(g0100, g0100), dot(g1000, g1000), dot(g1100, g1100)));
g0000 *= norm00.x;
g0100 *= norm00.y;
g1000 *= norm00.z;
g1100 *= norm00.w;
vec4 norm01 = taylorInvSqrt(vec4(dot(g0001, g0001), dot(g0101, g0101), dot(g1001, g1001), dot(g1101, g1101)));
g0001 *= norm01.x;
g0101 *= norm01.y;
g1001 *= norm01.z;
g1101 *= norm01.w;
vec4 norm10 = taylorInvSqrt(vec4(dot(g0010, g0010), dot(g0110, g0110), dot(g1010, g1010), dot(g1110, g1110)));
g0010 *= norm10.x;
g0110 *= norm10.y;
g1010 *= norm10.z;
g1110 *= norm10.w;
vec4 norm11 = taylorInvSqrt(vec4(dot(g0011, g0011), dot(g0111, g0111), dot(g1011, g1011), dot(g1111, g1111)));
g0011 *= norm11.x;
g0111 *= norm11.y;
g1011 *= norm11.z;
g1111 *= norm11.w;
float n0000 = dot(g0000, Pf0);
float n1000 = dot(g1000, vec4(Pf1.x, Pf0.yzw));
float n0100 = dot(g0100, vec4(Pf0.x, Pf1.y, Pf0.zw));
float n1100 = dot(g1100, vec4(Pf1.xy, Pf0.zw));
float n0010 = dot(g0010, vec4(Pf0.xy, Pf1.z, Pf0.w));
float n1010 = dot(g1010, vec4(Pf1.x, Pf0.y, Pf1.z, Pf0.w));
float n0110 = dot(g0110, vec4(Pf0.x, Pf1.yz, Pf0.w));
float n1110 = dot(g1110, vec4(Pf1.xyz, Pf0.w));
float n0001 = dot(g0001, vec4(Pf0.xyz, Pf1.w));
float n1001 = dot(g1001, vec4(Pf1.x, Pf0.yz, Pf1.w));
float n0101 = dot(g0101, vec4(Pf0.x, Pf1.y, Pf0.z, Pf1.w));
float n1101 = dot(g1101, vec4(Pf1.xy, Pf0.z, Pf1.w));
float n0011 = dot(g0011, vec4(Pf0.xy, Pf1.zw));
float n1011 = dot(g1011, vec4(Pf1.x, Pf0.y, Pf1.zw));
float n0111 = dot(g0111, vec4(Pf0.x, Pf1.yzw));
float n1111 = dot(g1111, Pf1);
vec4 fade_xyzw = fade(Pf0);
vec4 n_0w = mix(vec4(n0000, n1000, n0100, n1100), vec4(n0001, n1001, n0101, n1101), fade_xyzw.w);
vec4 n_1w = mix(vec4(n0010, n1010, n0110, n1110), vec4(n0011, n1011, n0111, n1111), fade_xyzw.w);
vec4 n_zw = mix(n_0w, n_1w, fade_xyzw.z);
vec2 n_yzw = mix(n_zw.xy, n_zw.zw, fade_xyzw.y);
float n_xyzw = mix(n_yzw.x, n_yzw.y, fade_xyzw.x);
return 2.2 * n_xyzw;
}
uniform float time;
uniform float time_multiplier;
uniform vec3 color_step_1;
uniform vec3 color_step_2;
uniform vec3 color_step_3;
uniform vec3 color_step_4;
uniform float ratio_step_1;
uniform float ratio_step_2;
uniform float displacement;
varying float v_angle;
varying vec3 v_position;
varying float v_intensity;
vec3 get_color_from_gradient(float value)
{
vec3 color;
if(value < ratio_step_1)
{
value = smoothstep(0.0,ratio_step_1,value);
color = mix( color_step_1, color_step_2, vec3(value,value,value));
// color = vec3(1.0,0.0,0.0);
}
else if(value < ratio_step_2)
{
value = smoothstep(ratio_step_1,ratio_step_2,value);
color = mix( color_step_2, color_step_3, vec3(value,value,value));
// color = vec3(0.0,1.0,0.0);
}
else
{
value = smoothstep(ratio_step_2,1.0,value);
color = mix( color_step_3, color_step_4, vec3(value,value,value));
// color = vec3(0.0,0.0,1.0);
}
return color;
}
float get_intensity(vec3 position)
{
vec3 new_position = position;
// Perlins
float value = 1.0;
// | frequency | speed
float perlin_4 = (cnoise(vec4(100.0 * new_position,time * time_multiplier * 1.0)) + 1.0) / 2.0;
float perlin_5 = (cnoise(vec4(200.0 * new_position,time * time_multiplier * 1.0)) + 1.0) / 2.0;
float value_4 = 1.0 - clamp(perlin_4,0.0,0.3) * 3.0;
float value_5 = perlin_5;
// Final value
value *= value_4;
value *= value_5;
value *= 14.0;
return value;
}
void main()
{
vec3 new_position = v_position;
float new_displacement = v_intensity * 6.0 * displacement;
// Displacement
if(new_displacement != 0.0)
{
float displacement_1 = (cnoise(vec4(36.0 * new_position,time * 0.0003 * 0.5)) + 1.0) / 2.0;
new_position.x += displacement_1 * new_displacement;
new_position.y += displacement_1 * new_displacement;
new_position.z += displacement_1 * new_displacement;
}
float intensity = get_intensity(new_position) * v_intensity * 3.0;
vec3 color = get_color_from_gradient(intensity);
gl_FragColor = vec4(color,1.0);
}
</script>
<script src="libs/dat.gui.min.js"></script>
<script src='libs/three.js'></script>
<script src="libs/EffectComposer.js"></script>
<script src="libs/RenderPass.js"></script>
<script src="libs/ShaderPass.js"></script>
<script src="libs/MaskPass.js"></script>
<script src="libs/FXAAShader.js"></script>
<script src="libs/CopyShader.js"></script>
<script src="src/planets.js"></script>
</body>
<script>
// HTML Functions (Demo purposes only)
function generateStarPlanet() {
// add planet to the scene if it already hasn't been added
if(!scene.getObjectByName('our-planet')) {
scene.add(planet.starObject);
planet.setupDebugging();
}
}
// Setup Scene, Camera, and Renderer
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(55, window.innerWidth / window.innerHeight, 0.1, 100000);
camera.position.z = 4;
var renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('star-sketch'), alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 1);
// Create StarColor object for the star planet
// Create and name the planet (naming it instead of adding it to the scene for demo purposes)
var colors = new StarColor('#001159', '#0072ff', '#00c7ff', '#90f6ff', 0.4);
var planet = new StarPlanet(1, colors, 0.03, 0.0005);
planet.init(function(shader) {
planet.createStar(shader);
planet.starObject.name = "our-planet";
});
// Resize camera and renderer when the window is resized
var resize = function() {
renderer.setSize( window.innerWidth * window.devicePixelRatio, window.innerHeight * window.devicePixelRatio );
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
};
window.onresize = resize;
resize();
// Mouse movement
var updateFcts = [];
var mouse = {x:0, y:0};
document.addEventListener('mousemove', function(event) {
mouse.x = event.clientX / window.innerWidth;
mouse.y = event.clientY / window.innerHeight;
}, false);
updateFcts.push(function(delta, now) {
camera.position.x += (mouse.x*5 - camera.position.x) * (delta*10);
camera.position.y += (mouse.y*5 - camera.position.y) * (delta*10);
camera.lookAt(scene.position);
});
// Render the scene every frame
updateFcts.push(function() {
planet.updateFrames(camera);
renderer.render(scene, camera);
});
// Loop Runner
var lastTimeMsec = null;
requestAnimationFrame(function animate(nowMsec) {
requestAnimationFrame(animate);
lastTimeMsec = lastTimeMsec || (nowMsec - 1000 / 60);
var deltaMsec = Math.min(200, nowMsec - lastTimeMsec);
lastTimeMsec = nowMsec;
updateFcts.forEach(function(updateFn) {
updateFn(deltaMsec / 1000, nowMsec / 1000);
});
});
</script>
</html>