-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathraymarching_kado_buildings.html
784 lines (637 loc) · 21.4 KB
/
raymarching_kado_buildings.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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
<!DOCTYPE html>
<!--
// Title: Fusioned Bismuth (TokyoDemoFest 2017 GLSL Graphics Compo 3rd place)
// Copyright (c) 2017 gam0022
// License: Attribution-NonCommercial-ShareAlike (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US)
-->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>three.js webgl - raymarching - carbon</title>
<style type="text/css">
body {
background-color: black;
margin: 0;
padding: 0;
}
a { color: skyblue }
canvas {
display: block;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
#info {
color: white;
font-size: 13px;
position: absolute;
bottom: 10px;
width: 100%;
text-align: center;
z-index: 100;
}
</style>
</head>
<body>
<div id="info">
<a href="http://threejs.org" target="_blank">three.js</a> - webgl raymarching demo - Fusioned Bismuth by <a href="https://github.com/gam0022" target="_blank">gam0022</a><br>
<a href="http://tokyodemofest.jp/2017/" target="_blank">Tokyo Demo Fest 2017</a> GLSL Graphics compo 3rd place (<a href="http://gam0022.net/blog/2017/02/24/tdf2017/" target="_blank">article in Japanese</a>)
</div>
<script id="fragment_shader" type="x-shader/x-fragment">
// Title: Fusioned Bismuth (TokyoDemoFest 2017 GLSL Graphics Compo 3rd place)
// Copyright (c) 2017 gam0022
// License: Attribution-NonCommercial-ShareAlike (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US)
//
// Thanks
// - DE used folding by gaz (https://www.shadertoy.com/view/Mlf3Wj)
// - Sky and Ground by morgan3d (https://www.shadertoy.com/view/4sKGWt)
precision highp float;
#define DEBUG 1
#define SHADER_TOY 0
// uniforms
#if SHADER_TOY == 0
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
#endif
// debug for camera
#if DEBUG
uniform bool debugCamera;
uniform vec3 cameraPos;
uniform vec3 cameraDir;
// uniform float kadoScale;
#else
// float kadoScale;
#endif
float kadoScale;
// consts
const float INF = 1e+10;
const float EPS = 1e-2;
const float EPS_N = 1e-3;
const float OFFSET = EPS * 100.0;
const float PI = 3.14159265359;
const float PI2 = 6.28318530718;
const float PIH = 1.57079632679;
const float PIQ = 0.78539816339;
// globals
const vec3 lightDir = vec3( -0.48666426339228763, 0.8111071056538127, -0.3244428422615251 );
float lTime;
// ray
struct Ray {
vec3 origin;
vec3 direction;
};
// camera
struct Camera {
vec3 eye, target;
vec3 forward, right, up;
float zoom;
};
Ray cameraShootRay(Camera c, vec2 uv) {
c.forward = normalize(c.target - c.eye);
c.right = normalize(cross(c.forward, c.up));
c.up = normalize(cross(c.right, c.forward));
Ray r;
r.origin = c.eye;
r.direction = normalize(uv.x * c.right + uv.y * c.up + c.zoom * c.forward);
return r;
}
// intersection
struct Intersection {
bool hit;
vec3 position;
float distance;
vec3 normal;
vec2 uv;
float count;
int material;
vec3 color;
float reflectance;
};
#define METAL_MATERIAL 0
#define BUILDINGS_MATERIAL 1
#define ROAD_MATERIAL 2
// util
#define saturate(x) clamp(x, 0.0, 1.0)
float hash(vec2 p) { return fract(1e4 * sin(17.0 * p.x + p.y * 0.1) * (0.1 + abs(sin(p.y * 13.0 + p.x)))); }
float noise(vec2 x) {
vec2 i = floor(x), f = fract(x);
float a = hash(i);
float b = hash(i + vec2(1.0, 0.0));
float c = hash(i + vec2(0.0, 1.0));
float d = hash(i + vec2(1.0, 1.0));
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(a, b, u.x) + (c - a) * u.y * (1.0 - u.x) + (d - b) * u.x * u.y;
}
// https://www.shadertoy.com/view/4sKGWt
float fbm(vec2 p) {
const mat2 m2 = mat2(0.8, -0.6, 0.6, 0.8);
p.xy += 0.1 * lTime;
float f = 0.5000 * noise(p); p = m2 * p * 2.02;
f += 0.2500 * noise(p); p = m2 * p * 2.03;
f += 0.1250 * noise(p); p = m2 * p * 2.01;
f += 0.0625 * noise(p);
return f / 0.9375;
}
float easeInCubic( float t ) {
return t * t * t;
}
float easeInOutCubic(float t) {
return t < 0.5 ? 4.0 * t * t * t : (t - 1.0) * (2.0 * t - 2.0) * (2.0 * t - 2.0) + 1.0;
}
float gauss(float x) {
float a = 50.0;
return exp(-x * x / a);
}
// Distance Functions
// operations
//vec3 opRep( vec3 p, float interval ) {
// return mod( p, interval ) - 0.5 * interval;
//}
#define opRep(p, interval) (mod(p, interval) - 0.5 * interval)
//vec2 opRepLimit(vec2 p, float interval, float limit) {
// return mod(clamp(p, -limit, limit), interval) - 0.5 * interval;
//}
#define opRepLimit(p, interval, limit) (mod(clamp(p, -limit, limit), interval) - 0.5 * interval)
// https://www.shadertoy.com/view/Mlf3Wj
mat2 rotate(in float a) {
float s=sin(a),c=cos(a);
return mat2(c,s,-s,c);
}
vec2 fold(in vec2 p, in float s) {
float a = PI / s - atan(p.x, p.y);
float n = PI2 / s;
a = floor(a / n) * n;
p *= rotate(a);
return p;
}
float smin(float d1, float d2, float k) {
float h = exp(-k * d1) + exp(-k * d2);
return -log(h) / k;
}
// Distance Functions
float sdBox( vec3 p, vec3 b ) {
vec3 d = abs(p) - b;
return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
}
#define META_SCENE_END_TIME (32.0)
#define UFO_SCENE_END_TIME (117.0)
#define SNOW_SCENE_END_TIME (182.0)
// http://blog.hvidtfeldts.net/index.php/2011/11/distance-estimated-3d-fractals-vi-the-mandelbox/
float minRadius2 = 0.5;
float fixedRadius2 = 1.0;
float foldingLimit = 1.0;
#define Iterations 8
void sphereFold(inout vec3 z, inout float dz) {
float r2 = dot(z,z);
if (r2 < minRadius2) {
// linear inner scaling
float temp = (fixedRadius2 / minRadius2);
z *= temp;
dz *= temp;
} else if (r2 < fixedRadius2) {
// this is the actual sphere inversion
float temp = fixedRadius2 / r2;
z *= temp;
dz *= temp;
}
}
void boxFold(inout vec3 z, inout float dz) {
z = clamp(z, -foldingLimit, foldingLimit) * 2.0 - z;
}
float dMbox(vec3 z) {
vec3 offset = z;
float dr = 1.0;
for (int n = 0; n < Iterations; n++) {
boxFold(z, dr); // Reflect
sphereFold(z, dr); // Sphere Inversion
z = kadoScale * z + offset; // Scale & Translate
dr = dr * abs(kadoScale) + 1.0;
}
float r = length(z);
return r / abs(dr);
}
float random(in vec2 p) {
return fract(sin(dot(p.xy, vec2(12.34, 56.78))) * 90.0);
}
float sdFloor(in vec3 p) {
return p.y + 7.0;
}
float sdBuilding(in vec3 p, float heightBase) {
float interval = 0.5;
p.y += 7.0;
vec2 seed = p.xz - mod(p.xz, interval);
float height = heightBase + 0.5 * random(seed);
float width = 0.03 + 0.02 * random(seed + 1.0);
float depth = 0.03 + 0.02 * random(seed + 2.0);
vec3 p1 = p;
p1.xz = opRep(p1.xz, interval);
return sdBox(p1, vec3(width, height, depth));
}
float sdBuildings(in vec3 p) {
float d = sdBuilding(p, 1.0);
float m = 0.25;
d = min(d, sdBuilding(p + vec3(m, 0.0, 0.0), 0.5));
d = min(d, sdBuilding(p + vec3(0.0, m, 0.0), 0.75));
d = min(d, sdBuilding(p + vec3(m, m, 0.0), 0.25));
return d;
}
float dScene(vec3 p) {
return min(dMbox(p), min(sdFloor(p), sdBuildings(p)));
}
// color functions
vec3 hsv2rgb(vec3 c) {
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, saturate(p - K.xxx), c.y);
}
#define calcNormal(p, dFunc) normalize(vec2(EPS_N, -EPS_N).xyy * dFunc(p + vec2(EPS_N, -EPS_N).xyy) + vec2(EPS_N, -EPS_N).yyx * dFunc(p + vec2(EPS_N, -EPS_N).yyx ) + vec2(EPS_N, -EPS_N).yxy * dFunc(p + vec2(EPS_N, -EPS_N).yxy) + vec2(EPS_N, -EPS_N).xxx * dFunc(p + vec2(EPS_N, -EPS_N).xxx))
float calcRate(float a, float b) {
return a / (a + b);
}
void intersectScene(inout Intersection intersection, inout Ray ray ) {
float d;
intersection.distance = 0.0;
vec3 p = ray.origin;
for (float i = 0.0; i < 300.0; i++) {
d = dScene(p);
intersection.distance += d;
p = ray.origin + intersection.distance * ray.direction;
intersection.count = i;
if (abs(d) < EPS /*|| intersection.distance > 100.0*/) break;
}
if (abs(d) < EPS) {
intersection.hit = true;
intersection.position = p;
intersection.normal = calcNormal(p, dScene);
if (abs(dMbox(p)) < EPS) {
intersection.material = METAL_MATERIAL;
} else if (abs(sdBuildings(p)) < EPS) {
intersection.material = BUILDINGS_MATERIAL;
} else {
intersection.material = ROAD_MATERIAL;
}
}
}
float calcAo(in vec3 p, in vec3 n){
float sca = 1.0, occ = 0.0;
for(float i=0.; i<5.; i++){
float hr = 0.05 + i * 0.08;
float dd = dScene(n * hr + p);
occ += (hr - dd) * sca;
sca *= 0.5;
}
return saturate(1.0 - occ);
}
float calcShadow(in vec3 p, in vec3 rd) {
float d;
float distance = OFFSET;
float bright = 1.0;
float shadowIntensity = 0.4;
float shadowSharpness = 10.0;
for (int i = 0; i < 30; i++) {
d = dScene(p + rd * distance);
if (d < EPS) return shadowIntensity;
bright = min(bright, shadowSharpness * d / distance);
distance += d;
}
return shadowIntensity + (1.0 - shadowIntensity) * bright;
}
float dBox(vec2 p, vec2 b) {
return max(abs(p.x) - b.x, abs(p.y) - b.y);
}
vec4 buildingsWallTexture(vec2 p) {
vec4 concreteBase = vec4(0.7, 0.7, 1.0, 0.0);
vec4 glassBase = vec4(0.2, 0.2, 0.2, 0.7);
float d = dBox(opRep(p, 0.02), vec2(0.008, 0.005));
return d < 0.0 ? glassBase : concreteBase;
}
vec4 buildingsRoofTexture(vec2 p) {
vec4 concreteBase = vec4(0.8, 0.8, 1.0, 0.0);
return concreteBase;
}
void calcRadiance(inout Intersection intersection, inout Ray ray, int bounce) {
intersection.hit = false;
intersectScene(intersection, ray);
if ( intersection.hit ) {
float diffuse = clamp(dot(lightDir, intersection.normal), 0.2, 1.0) * 0.5 + 0.5;
float specular = pow(saturate(dot(reflect(lightDir, intersection.normal), ray.direction)), 10.0);
float ao = calcAo(intersection.position, intersection.normal);
float shadow = calcShadow(intersection.position, lightDir);
if (intersection.material == METAL_MATERIAL) {
vec3 metalBase = hsv2rgb(vec3(0.1 * intersection.count, 0.7 * sin(lTime), 0.9));
intersection.color = metalBase * diffuse * ao * shadow + 0.1 * specular;
intersection.reflectance = 0.7;
} else if (intersection.material == BUILDINGS_MATERIAL) {
vec4 tex =
buildingsWallTexture(intersection.position.xy) * abs(dot(intersection.normal, vec3(0.0, 0.0, 1.0))) +
buildingsWallTexture(intersection.position.zy) * abs(dot(intersection.normal, vec3(1.0, 0.0, 0.0))) +
buildingsRoofTexture(intersection.position.xz) * abs(dot(intersection.normal, vec3(0.0, 1.0, 0.0)));
intersection.color = tex.rgb * diffuse * ao * shadow;
intersection.reflectance = tex.a;
} else {
intersection.color = vec3(0.5) * diffuse * ao * shadow;
intersection.reflectance = 0.0;
}
// fog
intersection.color = mix(intersection.color, 0.8 * vec3(0.7, 0.75, 0.8), min(1.0, pow(0.02 * intersection.distance, 2.0)));
} else {
vec3 sunnySky = vec3(0.4, 0.55, 0.8);
vec3 cloudySky = vec3(0.7);
vec3 cloud = vec3(1.0, 0.95, 1.0);
vec3 base = mix(sunnySky, cloudySky, step(UFO_SCENE_END_TIME - 20.0, lTime)) * (1.0 - 0.8 * ray.direction.y) * 0.9;
// Sun
float sundot = saturate(dot(ray.direction, lightDir));
base += 0.25 * vec3(1.0, 0.7, 0.4) * pow(sundot, 8.0);
base += 0.75 * vec3(1.0, 0.8, 0.5) * pow(sundot, 64.0);
// Clouds
float rd = ray.direction.y + 0.3;
intersection.color = mix(base, cloud, 0.5 *
smoothstep(0.5, 0.8, fbm((ray.origin.xz + ray.direction.xz * (250000.0 - ray.origin.y) / rd) * 0.000008)));
intersection.color = mix(intersection.color, vec3(0.7, 0.75, 0.8), pow(1.0 - max(rd, 0.0), 4.0));
}
}
void cameraControl(inout Camera camera) {
// time
float t1 = lTime - META_SCENE_END_TIME;
float t2 = t1 - 25.0;
float t3 = t2 - 16.0;
float t4 = t3 - 20.0;
float t5 = lTime - UFO_SCENE_END_TIME;
float t6 = t5 - 7.0;
float t7 = t6 - 10.0;
// blend
// ufo
float b1 = easeInOutCubic(saturate(t1 * 0.2));
float b2 = easeInOutCubic(saturate(t2 * 0.4));
float b3 = easeInOutCubic(saturate(t3 * 0.4));
float b4 = easeInOutCubic(saturate(t4 * 0.2));
// snow
float b5 = easeInOutCubic(saturate(t5 * 0.2));
float b6 = easeInOutCubic(saturate(t6 * 0.5));
float b7 = easeInOutCubic(saturate(t7));
// camera target
camera.target = vec3(0.0);
camera.target.y = clamp(-0.7 * t1, -2.8, 100.0);
camera.target.y = mix(camera.target.y, 1.0 + t2 * 0.3, b2);
camera.target.y = mix(camera.target.y, 0.0, b3);
camera.target.y = mix(camera.target.y, 0.0 + 0.6 * t4, b4);
camera.target.y = mix(camera.target.y, 0.0, b5);
camera.target.x = mix(camera.target.x, -0.7, b6);
camera.target.x = mix(camera.target.x, 0.0, b7);
// camera position
vec3 p0 = camera.target + vec3(0.0, 0.0, -5.0); // down
vec3 p1 = vec3(1.0, 0.3 * abs(t1 - 0.5), 1.0); // look down
vec3 p2 = vec3(2.5 + t2 * 0.9, 0.5 + t2 * 0.1, 0.0); // side
vec3 p3 = vec3(0.01, 4.0 + t3 * 0.2, 0.0); // zoom
vec3 p4 = vec3(1.5, 10.0 + t4 * 0.1, 2.0); // leave
vec3 p5 = vec3(0.0, 0.1, 6.0 - t5 * 0.3);
float v = 0.3 + t5 * 0.07;
vec3 p6 = vec3(1.7 * cos(v) - 1.0, 0.1, (0.5 + t6 * 0.01) * sin(v));
vec3 p7 = vec3(0.0, 0.0, 0.5 + t7 * 0.1);
camera.eye = mix(p0, p1, b1);
camera.eye = mix(camera.eye, p2, b2);
camera.eye = mix(camera.eye, p3, b3);
camera.eye = mix(camera.eye, p4, b4);
camera.eye = mix(camera.eye, p5, b5);
camera.eye = mix(camera.eye, p6, b6);
camera.eye = mix(camera.eye, p7, b7);
}
void main(void) {
// local time
lTime = mod(time, SNOW_SCENE_END_TIME + 1.0);
// fragment position
vec2 uv = ( gl_FragCoord.xy * 2.0 - resolution ) / min( resolution.x, resolution.y );
// camera and ray
Camera camera;
cameraControl(camera);
#if DEBUG
if (debugCamera) {
camera.eye = cameraPos;
camera.target = cameraPos + cameraDir;
}
#endif
camera.up = vec3(0.0, 1.0, 0.0);// y-up
camera.zoom = 2.0;
Ray ray = cameraShootRay(camera, uv);
kadoScale = 2.0 + cos(time * 0.1);
vec3 color = vec3(0.0);
float reflection = 1.0;
Intersection intersection;
for (int bounce = 0; bounce <= 2; bounce++) {
calcRadiance(intersection, ray, bounce);
color += reflection * intersection.color;
if (!intersection.hit) break;
reflection *= intersection.reflectance;
ray.origin = intersection.position + intersection.normal * OFFSET;
ray.direction = normalize(reflect(ray.direction, intersection.normal));
}
gl_FragColor = vec4(color, 1.0);
}
</script>
<script id="vertex_shader" type="x-shader/x-vertex">
attribute vec3 position;
void main(void) {
gl_Position = vec4(position, 1.0);
}
</script>
<script src="js/three.min.js"></script>
<script src="js/controls/FlyControls.js"></script>
<script src="js/controls/OrbitControls.js"></script>
<script src="js/libs/stats.min.js"></script>
<script src="js/libs/dat.gui.min.js"></script>
<script>
var camera, scene, flyControls, orbitControls, renderer;
var prevCameraMatrixWorld;
var geometry, material, plane;
var mouse = new THREE.Vector2( 0.5, 0.5 );
var canvas;
var stats;
var clock = new THREE.Clock();
var config = {
saveImage: function() {
render(true);
window.open( canvas.toDataURL() );
},
soucePlain: function() {
outputShaderToTab( function(text){ return text; });
},
souceRelease: function() {
outputShaderToTab( function(text){
return text.replace(/#define DEBUG 1/, '#define DEBUG 0');
});
},
souceShaderToy: function() {
outputShaderToTab( function(text){
return text.
replace(/#define DEBUG 1/, '#define DEBUG 0').
replace(/#define SHADER_TOY 0/, '#define SHADER_TOY 1').
replace(/void main\(void\)/, 'void mainImage( out vec4 fragColor, in vec2 fragCoord )').
replace(/\b(resolution)\b/g, 'iResolution.xy').
replace(/\b(mouse)\b/g, 'iMouse').
replace(/\b(gl_FragCoord)\b/g, 'fragCoord').
replace(/\b(gl_FragColor)\b/g, 'fragColor').
replace(/\b(time)\b/g, 'iGlobalTime');
});
},
camera: 'Orbit',
resolution: 256,
aspectRatio: 4/3,
pixelRatio: 1,
time: 0,
//time: 105,
//time: 142,
pause: false,
// mandel box
kadoScale: 2.7,
};
init();
animate();
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(35, 800/600);
camera.position.z = 12;
camera.lookAt( new THREE.Vector3( 0.0, 0.0, 0.0 ) );
geometry = new THREE.PlaneBufferGeometry( 2.0, 2.0 );
material = new THREE.RawShaderMaterial( {
uniforms: {
resolution: { type: 'v2', value: new THREE.Vector2( config.resolution, config.resolution ) },
mouse: { type: 'v2', value: mouse },
time: { type: 'f', value: 0.0 },
debugCamera: { type: 'i', value: config.camera !== 'Auto' },
cameraPos: { type: 'v3', value: camera.getWorldPosition() },
cameraDir: { type: 'v3', value: camera.getWorldDirection() },
kadoScale: {type: 'f', value: config.kadoScale},
},
vertexShader: document.getElementById( 'vertex_shader' ).textContent,
fragmentShader: document.getElementById( 'fragment_shader' ).textContent
} );
plane = new THREE.Mesh( geometry, material );
plane.frustumCulled = false;
scene.add( plane );
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( config.pixelRatio );
renderer.setSize( config.resolution * config.aspectRatio, config.resolution );
canvas = renderer.domElement;
canvas.addEventListener( 'mousemove', onMouseMove );
window.addEventListener( 'resize', onWindowResize );
document.body.appendChild( canvas );
//flyControls = new THREE.FlyControls( camera, canvas );
//flyControls.autoForward = true;
//flyControls.dragToLook = false;
//flyControls.rollSpeed = Math.PI / 12;
//flyControls.movementSpeed = 0.1;
orbitControls = new THREE.OrbitControls( camera, canvas );
orbitControls.enablePan = true;
//orbitControls.keyPanSpeed = 0.01;
orbitControls.enableDamping = false;
//orbitControls.dampingFactor = 0.015;
orbitControls.enableZoom = true;
//orbitControls.zoomSpeed = 0.001;
//orbitControls.rotateSpeed = 0.8;
orbitControls.autoRotate = false;
orbitControls.autoRotateSpeed = 0.0;
orbitControls.target = new THREE.Vector3( 0.0, 0.0, 0.0 );
var gui = new dat.GUI();
gui.add( config, 'saveImage' ).name( 'Save as PNG' );
//gui.add( config, 'soucePlain' ).name( 'view Shader' );
gui.add( config, 'souceRelease' ).name( 'GLSL sandbox' );
gui.add( config, 'souceShaderToy' ).name( 'ShaderToy' );
gui.add( config, 'camera', [ 'Auto', 'Orbit' ] ).name( 'Camera' );
gui.add( config, 'resolution', [ 256, 512, 800, 'full' ] ).name( 'Resolution' ).onChange( function( value ) {
onWindowResize();
render(true);
} );
gui.add( config, 'aspectRatio', { '16:9': 16/9, '4:3' : 4/3, '2:1': 2, '1:1': 1.0 } ).name( 'Aspect Ratio' ).onChange( function( value ) {
onWindowResize();
render(true);
} );
gui.add( config, 'pixelRatio', { '1/4x': 0.25, '1/2x' : 0.5, '1x': 1.0, '2x': 2.0 } ).name( 'Pixel Ratio' ).onChange( function( value ) {
onWindowResize();
render(true);
} );
gui.add( config, 'time', 0, 182 ).step( 0.1 ).name( 'time' ).listen();
gui.add( config, 'pause' ).name( 'Pause' );
var kadoFolder = gui.addFolder( "Kado" );
kadoFolder.add(config, 'kadoScale', -5, 5).name('Scale');
kadoFolder.open();
stats = new Stats();
document.body.appendChild( stats.domElement );
}
function animate( timestamp ) {
var delta = clock.getDelta();
if ( !config.pause ) {
config.time += delta;
}
stats.begin();
var needsUpdate = config.time !== material.uniforms.time.value;
switch ( config.camera ) {
case 'Auto':
// none
break;
case 'Orbit':
orbitControls.update();
if (camera && prevCameraMatrixWorld && !camera.matrixWorld.equals(prevCameraMatrixWorld)) {
needsUpdate = true;
}
prevCameraMatrixWorld = camera.matrixWorld.clone();
break;
case 'Fly':
flyControls.update(delta);
break;
}
render(needsUpdate);
stats.end();
requestAnimationFrame( animate );
}
function render(needsUpdate) {
material.uniforms.resolution.value = new THREE.Vector2( canvas.width, canvas.height );
material.uniforms.mouse.value = mouse;
material.uniforms.debugCamera.value = config.camera !== 'Auto';
material.uniforms.cameraPos.value = camera.getWorldPosition();
material.uniforms.cameraDir.value = camera.getWorldDirection();
material.uniforms.kadoScale.value = config.kadoScale;
material.uniforms.time.value = config.time;
if (needsUpdate) {
renderer.render( scene, camera );
}
if (false) {
var pos = camera.getWorldPosition();
var dir = camera.getWorldDirection();
document.getElementById( 'info' ).innerHTML = ''
+ pos.x + ', ' + pos.y + ', ' + pos.z + '<br />'
+ dir.x + ', ' + dir.y + ', ' + dir.z + '<br />'
+ material.uniforms.time.value;
}
}
function onMouseMove( e ) {
mouse.x = e.offsetX / canvas.width;
mouse.y = e.offsetY / canvas.height;
}
function onWindowResize( e ) {
if ( config.resolution === 'full' ) {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
} else {
canvas.width = config.aspectRatio * config.resolution;
canvas.height = config.resolution;
}
renderer.setSize( canvas.width, canvas.height );
renderer.setPixelRatio( config.pixelRatio );
}
function outputTextToTab(text) {
var type = 'text/plain';
window.open('data:' + type + ';base64,' + window.btoa(text));
}
function outputShaderToTab(filter) {
var rawShader = document.getElementById( 'fragment_shader' ).textContent;
var text = filter( rawShader );
outputTextToTab( text );
}
</script>
</body>
</html>