-
Notifications
You must be signed in to change notification settings - Fork 57
/
webgl-path-tracing.js
1253 lines (1050 loc) · 42.6 KB
/
webgl-path-tracing.js
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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
WebGL Path Tracing (http://madebyevan.com/webgl-path-tracing/)
License: MIT License (see below)
Copyright (c) 2010 Evan Wallace
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
////////////////////////////////////////////////////////////////////////////////
// shader strings
////////////////////////////////////////////////////////////////////////////////
// vertex shader for drawing a textured quad
var renderVertexSource =
' attribute vec3 vertex;' +
' varying vec2 texCoord;' +
' void main() {' +
' texCoord = vertex.xy * 0.5 + 0.5;' +
' gl_Position = vec4(vertex, 1.0);' +
' }';
// fragment shader for drawing a textured quad
var renderFragmentSource =
' precision highp float;' +
' varying vec2 texCoord;' +
' uniform sampler2D texture;' +
' void main() {' +
' gl_FragColor = texture2D(texture, texCoord);' +
' }';
// vertex shader for drawing a line
var lineVertexSource =
' attribute vec3 vertex;' +
' uniform vec3 cubeMin;' +
' uniform vec3 cubeMax;' +
' uniform mat4 modelviewProjection;' +
' void main() {' +
' gl_Position = modelviewProjection * vec4(mix(cubeMin, cubeMax, vertex), 1.0);' +
' }';
// fragment shader for drawing a line
var lineFragmentSource =
' precision highp float;' +
' void main() {' +
' gl_FragColor = vec4(1.0);' +
' }';
// constants for the shaders
var bounces = '5';
var epsilon = '0.0001';
var infinity = '10000.0';
var lightSize = 0.1;
var lightVal = 0.5;
// vertex shader, interpolate ray per-pixel
var tracerVertexSource =
' attribute vec3 vertex;' +
' uniform vec3 eye, ray00, ray01, ray10, ray11;' +
' varying vec3 initialRay;' +
' void main() {' +
' vec2 percent = vertex.xy * 0.5 + 0.5;' +
' initialRay = mix(mix(ray00, ray01, percent.y), mix(ray10, ray11, percent.y), percent.x);' +
' gl_Position = vec4(vertex, 1.0);' +
' }';
// start of fragment shader
var tracerFragmentSourceHeader =
' precision highp float;' +
' uniform vec3 eye;' +
' varying vec3 initialRay;' +
' uniform float textureWeight;' +
' uniform float timeSinceStart;' +
' uniform sampler2D texture;' +
' uniform float glossiness;' +
' vec3 roomCubeMin = vec3(-1.0, -1.0, -1.0);' +
' vec3 roomCubeMax = vec3(1.0, 1.0, 1.0);';
// compute the near and far intersections of the cube (stored in the x and y components) using the slab method
// no intersection means vec.x > vec.y (really tNear > tFar)
var intersectCubeSource =
' vec2 intersectCube(vec3 origin, vec3 ray, vec3 cubeMin, vec3 cubeMax) {' +
' vec3 tMin = (cubeMin - origin) / ray;' +
' vec3 tMax = (cubeMax - origin) / ray;' +
' vec3 t1 = min(tMin, tMax);' +
' vec3 t2 = max(tMin, tMax);' +
' float tNear = max(max(t1.x, t1.y), t1.z);' +
' float tFar = min(min(t2.x, t2.y), t2.z);' +
' return vec2(tNear, tFar);' +
' }';
// given that hit is a point on the cube, what is the surface normal?
// TODO: do this with fewer branches
var normalForCubeSource =
' vec3 normalForCube(vec3 hit, vec3 cubeMin, vec3 cubeMax)' +
' {' +
' if(hit.x < cubeMin.x + ' + epsilon + ') return vec3(-1.0, 0.0, 0.0);' +
' else if(hit.x > cubeMax.x - ' + epsilon + ') return vec3(1.0, 0.0, 0.0);' +
' else if(hit.y < cubeMin.y + ' + epsilon + ') return vec3(0.0, -1.0, 0.0);' +
' else if(hit.y > cubeMax.y - ' + epsilon + ') return vec3(0.0, 1.0, 0.0);' +
' else if(hit.z < cubeMin.z + ' + epsilon + ') return vec3(0.0, 0.0, -1.0);' +
' else return vec3(0.0, 0.0, 1.0);' +
' }';
// compute the near intersection of a sphere
// no intersection returns a value of +infinity
var intersectSphereSource =
' float intersectSphere(vec3 origin, vec3 ray, vec3 sphereCenter, float sphereRadius) {' +
' vec3 toSphere = origin - sphereCenter;' +
' float a = dot(ray, ray);' +
' float b = 2.0 * dot(toSphere, ray);' +
' float c = dot(toSphere, toSphere) - sphereRadius*sphereRadius;' +
' float discriminant = b*b - 4.0*a*c;' +
' if(discriminant > 0.0) {' +
' float t = (-b - sqrt(discriminant)) / (2.0 * a);' +
' if(t > 0.0) return t;' +
' }' +
' return ' + infinity + ';' +
' }';
// given that hit is a point on the sphere, what is the surface normal?
var normalForSphereSource =
' vec3 normalForSphere(vec3 hit, vec3 sphereCenter, float sphereRadius) {' +
' return (hit - sphereCenter) / sphereRadius;' +
' }';
// use the fragment position for randomness
var randomSource =
' float random(vec3 scale, float seed) {' +
' return fract(sin(dot(gl_FragCoord.xyz + seed, scale)) * 43758.5453 + seed);' +
' }';
// random cosine-weighted distributed vector
// from http://www.rorydriscoll.com/2009/01/07/better-sampling/
var cosineWeightedDirectionSource =
' vec3 cosineWeightedDirection(float seed, vec3 normal) {' +
' float u = random(vec3(12.9898, 78.233, 151.7182), seed);' +
' float v = random(vec3(63.7264, 10.873, 623.6736), seed);' +
' float r = sqrt(u);' +
' float angle = 6.283185307179586 * v;' +
// compute basis from normal
' vec3 sdir, tdir;' +
' if (abs(normal.x)<.5) {' +
' sdir = cross(normal, vec3(1,0,0));' +
' } else {' +
' sdir = cross(normal, vec3(0,1,0));' +
' }' +
' tdir = cross(normal, sdir);' +
' return r*cos(angle)*sdir + r*sin(angle)*tdir + sqrt(1.-u)*normal;' +
' }';
// random normalized vector
var uniformlyRandomDirectionSource =
' vec3 uniformlyRandomDirection(float seed) {' +
' float u = random(vec3(12.9898, 78.233, 151.7182), seed);' +
' float v = random(vec3(63.7264, 10.873, 623.6736), seed);' +
' float z = 1.0 - 2.0 * u;' +
' float r = sqrt(1.0 - z * z);' +
' float angle = 6.283185307179586 * v;' +
' return vec3(r * cos(angle), r * sin(angle), z);' +
' }';
// random vector in the unit sphere
// note: this is probably not statistically uniform, saw raising to 1/3 power somewhere but that looks wrong?
var uniformlyRandomVectorSource =
' vec3 uniformlyRandomVector(float seed) {' +
' return uniformlyRandomDirection(seed) * sqrt(random(vec3(36.7539, 50.3658, 306.2759), seed));' +
' }';
// compute specular lighting contribution
var specularReflection =
' vec3 reflectedLight = normalize(reflect(light - hit, normal));' +
' specularHighlight = max(0.0, dot(reflectedLight, normalize(hit - origin)));';
// update ray using normal and bounce according to a diffuse reflection
var newDiffuseRay =
' ray = cosineWeightedDirection(timeSinceStart + float(bounce), normal);';
// update ray using normal according to a specular reflection
var newReflectiveRay =
' ray = reflect(ray, normal);' +
specularReflection +
' specularHighlight = 2.0 * pow(specularHighlight, 20.0);';
// update ray using normal and bounce according to a glossy reflection
var newGlossyRay =
' ray = normalize(reflect(ray, normal)) + uniformlyRandomVector(timeSinceStart + float(bounce)) * glossiness;' +
specularReflection +
' specularHighlight = pow(specularHighlight, 3.0);';
var yellowBlueCornellBox =
' if(hit.x < -0.9999) surfaceColor = vec3(0.1, 0.5, 1.0);' + // blue
' else if(hit.x > 0.9999) surfaceColor = vec3(1.0, 0.9, 0.1);'; // yellow
var redGreenCornellBox =
' if(hit.x < -0.9999) surfaceColor = vec3(1.0, 0.3, 0.1);' + // red
' else if(hit.x > 0.9999) surfaceColor = vec3(0.3, 1.0, 0.1);'; // green
function makeShadow(objects) {
return '' +
' float shadow(vec3 origin, vec3 ray) {' +
concat(objects, function(o){ return o.getShadowTestCode(); }) +
' return 1.0;' +
' }';
}
function makeCalculateColor(objects) {
return '' +
' vec3 calculateColor(vec3 origin, vec3 ray, vec3 light) {' +
' vec3 colorMask = vec3(1.0);' +
' vec3 accumulatedColor = vec3(0.0);' +
// main raytracing loop
' for(int bounce = 0; bounce < ' + bounces + '; bounce++) {' +
// compute the intersection with everything
' vec2 tRoom = intersectCube(origin, ray, roomCubeMin, roomCubeMax);' +
concat(objects, function(o){ return o.getIntersectCode(); }) +
// find the closest intersection
' float t = ' + infinity + ';' +
' if(tRoom.x < tRoom.y) t = tRoom.y;' +
concat(objects, function(o){ return o.getMinimumIntersectCode(); }) +
// info about hit
' vec3 hit = origin + ray * t;' +
' vec3 surfaceColor = vec3(0.75);' +
' float specularHighlight = 0.0;' +
' vec3 normal;' +
// calculate the normal (and change wall color)
' if(t == tRoom.y) {' +
' normal = -normalForCube(hit, roomCubeMin, roomCubeMax);' +
[yellowBlueCornellBox, redGreenCornellBox][environment] +
newDiffuseRay +
' } else if(t == ' + infinity + ') {' +
' break;' +
' } else {' +
' if(false) ;' + // hack to discard the first 'else' in 'else if'
concat(objects, function(o){ return o.getNormalCalculationCode(); }) +
[newDiffuseRay, newReflectiveRay, newGlossyRay][material] +
' }' +
// compute diffuse lighting contribution
' vec3 toLight = light - hit;' +
' float diffuse = max(0.0, dot(normalize(toLight), normal));' +
// trace a shadow ray to the light
' float shadowIntensity = shadow(hit + normal * ' + epsilon + ', toLight);' +
// do light bounce
' colorMask *= surfaceColor;' +
' accumulatedColor += colorMask * (' + lightVal + ' * diffuse * shadowIntensity);' +
' accumulatedColor += colorMask * specularHighlight * shadowIntensity;' +
// calculate next origin
' origin = hit;' +
' }' +
' return accumulatedColor;' +
' }';
}
function makeMain() {
return '' +
' void main() {' +
' vec3 newLight = light + uniformlyRandomVector(timeSinceStart - 53.0) * ' + lightSize + ';' +
' vec3 texture = texture2D(texture, gl_FragCoord.xy / 512.0).rgb;' +
' gl_FragColor = vec4(mix(calculateColor(eye, initialRay, newLight), texture, textureWeight), 1.0);' +
' }';
}
function makeTracerFragmentSource(objects) {
return tracerFragmentSourceHeader +
concat(objects, function(o){ return o.getGlobalCode(); }) +
intersectCubeSource +
normalForCubeSource +
intersectSphereSource +
normalForSphereSource +
randomSource +
cosineWeightedDirectionSource +
uniformlyRandomDirectionSource +
uniformlyRandomVectorSource +
makeShadow(objects) +
makeCalculateColor(objects) +
makeMain();
}
////////////////////////////////////////////////////////////////////////////////
// utility functions
////////////////////////////////////////////////////////////////////////////////
function getEyeRay(matrix, x, y) {
return matrix.multiply(Vector.create([x, y, 0, 1])).divideByW().ensure3().subtract(eye);
}
function setUniforms(program, uniforms) {
for(var name in uniforms) {
var value = uniforms[name];
var location = gl.getUniformLocation(program, name);
if(location == null) continue;
if(value instanceof Vector) {
gl.uniform3fv(location, new Float32Array([value.elements[0], value.elements[1], value.elements[2]]));
} else if(value instanceof Matrix) {
gl.uniformMatrix4fv(location, false, new Float32Array(value.flatten()));
} else {
gl.uniform1f(location, value);
}
}
}
function concat(objects, func) {
var text = '';
for(var i = 0; i < objects.length; i++) {
text += func(objects[i]);
}
return text;
}
Vector.prototype.ensure3 = function() {
return Vector.create([this.elements[0], this.elements[1], this.elements[2]]);
};
Vector.prototype.ensure4 = function(w) {
return Vector.create([this.elements[0], this.elements[1], this.elements[2], w]);
};
Vector.prototype.divideByW = function() {
var w = this.elements[this.elements.length - 1];
var newElements = [];
for(var i = 0; i < this.elements.length; i++) {
newElements.push(this.elements[i] / w);
}
return Vector.create(newElements);
};
Vector.prototype.componentDivide = function(vector) {
if(this.elements.length != vector.elements.length) {
return null;
}
var newElements = [];
for(var i = 0; i < this.elements.length; i++) {
newElements.push(this.elements[i] / vector.elements[i]);
}
return Vector.create(newElements);
};
Vector.min = function(a, b) {
if(a.length != b.length) {
return null;
}
var newElements = [];
for(var i = 0; i < a.elements.length; i++) {
newElements.push(Math.min(a.elements[i], b.elements[i]));
}
return Vector.create(newElements);
};
Vector.max = function(a, b) {
if(a.length != b.length) {
return null;
}
var newElements = [];
for(var i = 0; i < a.elements.length; i++) {
newElements.push(Math.max(a.elements[i], b.elements[i]));
}
return Vector.create(newElements);
};
Vector.prototype.minComponent = function() {
var value = Number.MAX_VALUE;
for(var i = 0; i < this.elements.length; i++) {
value = Math.min(value, this.elements[i]);
}
return value;
};
Vector.prototype.maxComponent = function() {
var value = -Number.MAX_VALUE;
for(var i = 0; i < this.elements.length; i++) {
value = Math.max(value, this.elements[i]);
}
return value;
};
function compileSource(source, type) {
var shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if(!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
throw 'compile error: ' + gl.getShaderInfoLog(shader);
}
return shader;
}
function compileShader(vertexSource, fragmentSource) {
var shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, compileSource(vertexSource, gl.VERTEX_SHADER));
gl.attachShader(shaderProgram, compileSource(fragmentSource, gl.FRAGMENT_SHADER));
gl.linkProgram(shaderProgram);
if(!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
throw 'link error: ' + gl.getProgramInfoLog(shaderProgram);
}
return shaderProgram;
}
////////////////////////////////////////////////////////////////////////////////
// class Sphere
////////////////////////////////////////////////////////////////////////////////
function Sphere(center, radius, id) {
this.center = center;
this.radius = radius;
this.centerStr = 'sphereCenter' + id;
this.radiusStr = 'sphereRadius' + id;
this.intersectStr = 'tSphere' + id;
this.temporaryTranslation = Vector.create([0, 0, 0]);
}
Sphere.prototype.getGlobalCode = function() {
return '' +
' uniform vec3 ' + this.centerStr + ';' +
' uniform float ' + this.radiusStr + ';';
};
Sphere.prototype.getIntersectCode = function() {
return '' +
' float ' + this.intersectStr + ' = intersectSphere(origin, ray, ' + this.centerStr + ', ' + this.radiusStr + ');';
};
Sphere.prototype.getShadowTestCode = function() {
return '' +
this.getIntersectCode() +
' if(' + this.intersectStr + ' < 1.0) return 0.0;';
};
Sphere.prototype.getMinimumIntersectCode = function() {
return '' +
' if(' + this.intersectStr + ' < t) t = ' + this.intersectStr + ';';
};
Sphere.prototype.getNormalCalculationCode = function() {
return '' +
' else if(t == ' + this.intersectStr + ') normal = normalForSphere(hit, ' + this.centerStr + ', ' + this.radiusStr + ');';
};
Sphere.prototype.setUniforms = function(renderer) {
renderer.uniforms[this.centerStr] = this.center.add(this.temporaryTranslation);
renderer.uniforms[this.radiusStr] = this.radius;
};
Sphere.prototype.temporaryTranslate = function(translation) {
this.temporaryTranslation = translation;
};
Sphere.prototype.translate = function(translation) {
this.center = this.center.add(translation);
};
Sphere.prototype.getMinCorner = function() {
return this.center.add(this.temporaryTranslation).subtract(Vector.create([this.radius, this.radius, this.radius]));
};
Sphere.prototype.getMaxCorner = function() {
return this.center.add(this.temporaryTranslation).add(Vector.create([this.radius, this.radius, this.radius]));
};
Sphere.prototype.intersect = function(origin, ray) {
return Sphere.intersect(origin, ray, this.center.add(this.temporaryTranslation), this.radius);
};
Sphere.intersect = function(origin, ray, center, radius) {
var toSphere = origin.subtract(center);
var a = ray.dot(ray);
var b = 2*toSphere.dot(ray);
var c = toSphere.dot(toSphere) - radius*radius;
var discriminant = b*b - 4*a*c;
if(discriminant > 0) {
var t = (-b - Math.sqrt(discriminant)) / (2*a);
if(t > 0) {
return t;
}
}
return Number.MAX_VALUE;
};
////////////////////////////////////////////////////////////////////////////////
// class Cube
////////////////////////////////////////////////////////////////////////////////
function Cube(minCorner, maxCorner, id) {
this.minCorner = minCorner;
this.maxCorner = maxCorner;
this.minStr = 'cubeMin' + id;
this.maxStr = 'cubeMax' + id;
this.intersectStr = 'tCube' + id;
this.temporaryTranslation = Vector.create([0, 0, 0]);
}
Cube.prototype.getGlobalCode = function() {
return '' +
' uniform vec3 ' + this.minStr + ';' +
' uniform vec3 ' + this.maxStr + ';';
};
Cube.prototype.getIntersectCode = function() {
return '' +
' vec2 ' + this.intersectStr + ' = intersectCube(origin, ray, ' + this.minStr + ', ' + this.maxStr + ');';
};
Cube.prototype.getShadowTestCode = function() {
return '' +
this.getIntersectCode() +
' if(' + this.intersectStr + '.x > 0.0 && ' + this.intersectStr + '.x < 1.0 && ' + this.intersectStr + '.x < ' + this.intersectStr + '.y) return 0.0;';
};
Cube.prototype.getMinimumIntersectCode = function() {
return '' +
' if(' + this.intersectStr + '.x > 0.0 && ' + this.intersectStr + '.x < ' + this.intersectStr + '.y && ' + this.intersectStr + '.x < t) t = ' + this.intersectStr + '.x;';
};
Cube.prototype.getNormalCalculationCode = function() {
return '' +
// have to compare intersectStr.x < intersectStr.y otherwise two coplanar
// cubes will look wrong (one cube will "steal" the hit from the other)
' else if(t == ' + this.intersectStr + '.x && ' + this.intersectStr + '.x < ' + this.intersectStr + '.y) normal = normalForCube(hit, ' + this.minStr + ', ' + this.maxStr + ');';
};
Cube.prototype.setUniforms = function(renderer) {
renderer.uniforms[this.minStr] = this.getMinCorner();
renderer.uniforms[this.maxStr] = this.getMaxCorner();
};
Cube.prototype.temporaryTranslate = function(translation) {
this.temporaryTranslation = translation;
};
Cube.prototype.translate = function(translation) {
this.minCorner = this.minCorner.add(translation);
this.maxCorner = this.maxCorner.add(translation);
};
Cube.prototype.getMinCorner = function() {
return this.minCorner.add(this.temporaryTranslation);
};
Cube.prototype.getMaxCorner = function() {
return this.maxCorner.add(this.temporaryTranslation);
};
Cube.prototype.intersect = function(origin, ray) {
return Cube.intersect(origin, ray, this.getMinCorner(), this.getMaxCorner());
};
Cube.intersect = function(origin, ray, cubeMin, cubeMax) {
var tMin = cubeMin.subtract(origin).componentDivide(ray);
var tMax = cubeMax.subtract(origin).componentDivide(ray);
var t1 = Vector.min(tMin, tMax);
var t2 = Vector.max(tMin, tMax);
var tNear = t1.maxComponent();
var tFar = t2.minComponent();
if(tNear > 0 && tNear < tFar) {
return tNear;
}
return Number.MAX_VALUE;
};
////////////////////////////////////////////////////////////////////////////////
// class Light
////////////////////////////////////////////////////////////////////////////////
function Light() {
this.temporaryTranslation = Vector.create([0, 0, 0]);
}
Light.prototype.getGlobalCode = function() {
return 'uniform vec3 light;';
};
Light.prototype.getIntersectCode = function() {
return '';
};
Light.prototype.getShadowTestCode = function() {
return '';
};
Light.prototype.getMinimumIntersectCode = function() {
return '';
};
Light.prototype.getNormalCalculationCode = function() {
return '';
};
Light.prototype.setUniforms = function(renderer) {
renderer.uniforms.light = light.add(this.temporaryTranslation);
};
Light.clampPosition = function(position) {
for(var i = 0; i < position.elements.length; i++) {
position.elements[i] = Math.max(lightSize - 1, Math.min(1 - lightSize, position.elements[i]));
}
};
Light.prototype.temporaryTranslate = function(translation) {
var tempLight = light.add(translation);
Light.clampPosition(tempLight);
this.temporaryTranslation = tempLight.subtract(light);
};
Light.prototype.translate = function(translation) {
light = light.add(translation);
Light.clampPosition(light);
};
Light.prototype.getMinCorner = function() {
return light.add(this.temporaryTranslation).subtract(Vector.create([lightSize, lightSize, lightSize]));
};
Light.prototype.getMaxCorner = function() {
return light.add(this.temporaryTranslation).add(Vector.create([lightSize, lightSize, lightSize]));
};
Light.prototype.intersect = function(origin, ray) {
return Number.MAX_VALUE;
};
////////////////////////////////////////////////////////////////////////////////
// class PathTracer
////////////////////////////////////////////////////////////////////////////////
function PathTracer() {
var vertices = [
-1, -1,
-1, +1,
+1, -1,
+1, +1
];
// create vertex buffer
this.vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
// create framebuffer
this.framebuffer = gl.createFramebuffer();
// create textures
var type = gl.getExtension('OES_texture_float') ? gl.FLOAT : gl.UNSIGNED_BYTE;
this.textures = [];
for(var i = 0; i < 2; i++) {
this.textures.push(gl.createTexture());
gl.bindTexture(gl.TEXTURE_2D, this.textures[i]);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 512, 512, 0, gl.RGB, type, null);
}
gl.bindTexture(gl.TEXTURE_2D, null);
// create render shader
this.renderProgram = compileShader(renderVertexSource, renderFragmentSource);
this.renderVertexAttribute = gl.getAttribLocation(this.renderProgram, 'vertex');
gl.enableVertexAttribArray(this.renderVertexAttribute);
// objects and shader will be filled in when setObjects() is called
this.objects = [];
this.sampleCount = 0;
this.tracerProgram = null;
}
PathTracer.prototype.setObjects = function(objects) {
this.uniforms = {};
this.sampleCount = 0;
this.objects = objects;
// create tracer shader
if(this.tracerProgram != null) {
gl.deleteProgram(this.shaderProgram);
}
this.tracerProgram = compileShader(tracerVertexSource, makeTracerFragmentSource(objects));
this.tracerVertexAttribute = gl.getAttribLocation(this.tracerProgram, 'vertex');
gl.enableVertexAttribArray(this.tracerVertexAttribute);
};
PathTracer.prototype.update = function(matrix, timeSinceStart) {
// calculate uniforms
for(var i = 0; i < this.objects.length; i++) {
this.objects[i].setUniforms(this);
}
this.uniforms.eye = eye;
this.uniforms.glossiness = glossiness;
this.uniforms.ray00 = getEyeRay(matrix, -1, -1);
this.uniforms.ray01 = getEyeRay(matrix, -1, +1);
this.uniforms.ray10 = getEyeRay(matrix, +1, -1);
this.uniforms.ray11 = getEyeRay(matrix, +1, +1);
this.uniforms.timeSinceStart = timeSinceStart;
this.uniforms.textureWeight = this.sampleCount / (this.sampleCount + 1);
// set uniforms
gl.useProgram(this.tracerProgram);
setUniforms(this.tracerProgram, this.uniforms);
// render to texture
gl.useProgram(this.tracerProgram);
gl.bindTexture(gl.TEXTURE_2D, this.textures[0]);
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bindFramebuffer(gl.FRAMEBUFFER, this.framebuffer);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this.textures[1], 0);
gl.vertexAttribPointer(this.tracerVertexAttribute, 2, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
// ping pong textures
this.textures.reverse();
this.sampleCount++;
};
PathTracer.prototype.render = function() {
gl.useProgram(this.renderProgram);
gl.bindTexture(gl.TEXTURE_2D, this.textures[0]);
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.vertexAttribPointer(this.renderVertexAttribute, 2, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
};
////////////////////////////////////////////////////////////////////////////////
// class Renderer
////////////////////////////////////////////////////////////////////////////////
function Renderer() {
var vertices = [
0, 0, 0,
1, 0, 0,
0, 1, 0,
1, 1, 0,
0, 0, 1,
1, 0, 1,
0, 1, 1,
1, 1, 1
];
var indices = [
0, 1, 1, 3, 3, 2, 2, 0,
4, 5, 5, 7, 7, 6, 6, 4,
0, 4, 1, 5, 2, 6, 3, 7
];
// create vertex buffer
this.vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
// create index buffer
this.indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
// create line shader
this.lineProgram = compileShader(lineVertexSource, lineFragmentSource);
this.vertexAttribute = gl.getAttribLocation(this.lineProgram, 'vertex');
gl.enableVertexAttribArray(this.vertexAttribute);
this.objects = [];
this.selectedObject = null;
this.pathTracer = new PathTracer();
}
Renderer.prototype.setObjects = function(objects) {
this.objects = objects;
this.selectedObject = null;
this.pathTracer.setObjects(objects);
};
Renderer.prototype.update = function(modelviewProjection, timeSinceStart) {
var jitter = Matrix.Translation(Vector.create([Math.random() * 2 - 1, Math.random() * 2 - 1, 0]).multiply(1 / 512));
var inverse = jitter.multiply(modelviewProjection).inverse();
this.modelviewProjection = modelviewProjection;
this.pathTracer.update(inverse, timeSinceStart);
};
Renderer.prototype.render = function() {
this.pathTracer.render();
if(this.selectedObject != null) {
gl.useProgram(this.lineProgram);
gl.bindTexture(gl.TEXTURE_2D, null);
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
gl.vertexAttribPointer(this.vertexAttribute, 3, gl.FLOAT, false, 0, 0);
setUniforms(this.lineProgram, {
cubeMin: this.selectedObject.getMinCorner(),
cubeMax: this.selectedObject.getMaxCorner(),
modelviewProjection: this.modelviewProjection
});
gl.drawElements(gl.LINES, 24, gl.UNSIGNED_SHORT, 0);
}
};
////////////////////////////////////////////////////////////////////////////////
// class UI
////////////////////////////////////////////////////////////////////////////////
function UI() {
this.renderer = new Renderer();
this.moving = false;
}
UI.prototype.setObjects = function(objects) {
this.objects = objects;
this.objects.splice(0, 0, new Light());
this.renderer.setObjects(this.objects);
};
UI.prototype.update = function(timeSinceStart) {
this.modelview = makeLookAt(eye.elements[0], eye.elements[1], eye.elements[2], 0, 0, 0, 0, 1, 0);
this.projection = makePerspective(55, 1, 0.1, 100);
this.modelviewProjection = this.projection.multiply(this.modelview);
this.renderer.update(this.modelviewProjection, timeSinceStart);
};
UI.prototype.mouseDown = function(x, y) {
var t;
var origin = eye;
var ray = getEyeRay(this.modelviewProjection.inverse(), (x / 512) * 2 - 1, 1 - (y / 512) * 2);
// test the selection box first
if(this.renderer.selectedObject != null) {
var minBounds = this.renderer.selectedObject.getMinCorner();
var maxBounds = this.renderer.selectedObject.getMaxCorner();
t = Cube.intersect(origin, ray, minBounds, maxBounds);
if(t < Number.MAX_VALUE) {
var hit = origin.add(ray.multiply(t));
if(Math.abs(hit.elements[0] - minBounds.elements[0]) < 0.001) this.movementNormal = Vector.create([-1, 0, 0]);
else if(Math.abs(hit.elements[0] - maxBounds.elements[0]) < 0.001) this.movementNormal = Vector.create([+1, 0, 0]);
else if(Math.abs(hit.elements[1] - minBounds.elements[1]) < 0.001) this.movementNormal = Vector.create([0, -1, 0]);
else if(Math.abs(hit.elements[1] - maxBounds.elements[1]) < 0.001) this.movementNormal = Vector.create([0, +1, 0]);
else if(Math.abs(hit.elements[2] - minBounds.elements[2]) < 0.001) this.movementNormal = Vector.create([0, 0, -1]);
else this.movementNormal = Vector.create([0, 0, +1]);
this.movementDistance = this.movementNormal.dot(hit);
this.originalHit = hit;
this.moving = true;
return true;
}
}
t = Number.MAX_VALUE;
this.renderer.selectedObject = null;
for(var i = 0; i < this.objects.length; i++) {
var objectT = this.objects[i].intersect(origin, ray);
if(objectT < t) {
t = objectT;
this.renderer.selectedObject = this.objects[i];
}
}
return (t < Number.MAX_VALUE);
};
UI.prototype.mouseMove = function(x, y) {
if(this.moving) {
var origin = eye;
var ray = getEyeRay(this.modelviewProjection.inverse(), (x / 512) * 2 - 1, 1 - (y / 512) * 2);
var t = (this.movementDistance - this.movementNormal.dot(origin)) / this.movementNormal.dot(ray);
var hit = origin.add(ray.multiply(t));
this.renderer.selectedObject.temporaryTranslate(hit.subtract(this.originalHit));
// clear the sample buffer
this.renderer.pathTracer.sampleCount = 0;
}
};
UI.prototype.mouseUp = function(x, y) {
if(this.moving) {
var origin = eye;
var ray = getEyeRay(this.modelviewProjection.inverse(), (x / 512) * 2 - 1, 1 - (y / 512) * 2);
var t = (this.movementDistance - this.movementNormal.dot(origin)) / this.movementNormal.dot(ray);
var hit = origin.add(ray.multiply(t));
this.renderer.selectedObject.temporaryTranslate(Vector.create([0, 0, 0]));
this.renderer.selectedObject.translate(hit.subtract(this.originalHit));
this.moving = false;
}
};
UI.prototype.render = function() {
this.renderer.render();
};
UI.prototype.selectLight = function() {
this.renderer.selectedObject = this.objects[0];
};
UI.prototype.addSphere = function() {
this.objects.push(new Sphere(Vector.create([0, 0, 0]), 0.25, nextObjectId++));
this.renderer.setObjects(this.objects);
};
UI.prototype.addCube = function() {
this.objects.push(new Cube(Vector.create([-0.25, -0.25, -0.25]), Vector.create([0.25, 0.25, 0.25]), nextObjectId++));
this.renderer.setObjects(this.objects);
};
UI.prototype.deleteSelection = function() {
for(var i = 0; i < this.objects.length; i++) {
if(this.renderer.selectedObject == this.objects[i]) {
this.objects.splice(i, 1);
this.renderer.selectedObject = null;
this.renderer.setObjects(this.objects);
break;
}
}
};
UI.prototype.updateMaterial = function() {
var newMaterial = parseInt(document.getElementById('material').value, 10);
if(material != newMaterial) {
material = newMaterial;
this.renderer.setObjects(this.objects);
}
};
UI.prototype.updateEnvironment = function() {
var newEnvironment = parseInt(document.getElementById('environment').value, 10);
if(environment != newEnvironment) {
environment = newEnvironment;
this.renderer.setObjects(this.objects);
}
};
UI.prototype.updateGlossiness = function() {
var newGlossiness = parseFloat(document.getElementById('glossiness').value);
if(isNaN(newGlossiness)) newGlossiness = 0;
newGlossiness = Math.max(0, Math.min(1, newGlossiness));
if(material == MATERIAL_GLOSSY && glossiness != newGlossiness) {
this.renderer.pathTracer.sampleCount = 0;
}
glossiness = newGlossiness;
};
////////////////////////////////////////////////////////////////////////////////
// main program
////////////////////////////////////////////////////////////////////////////////
var gl;
var ui;
var error;
var canvas;
var inputFocusCount = 0;
var angleX = 0;
var angleY = 0;
var zoomZ = 2.5;
var eye = Vector.create([0, 0, 0]);
var light = Vector.create([0.4, 0.5, -0.6]);
var nextObjectId = 0;
var MATERIAL_DIFFUSE = 0;
var MATERIAL_MIRROR = 1;
var MATERIAL_GLOSSY = 2;
var material = MATERIAL_DIFFUSE;
var glossiness = 0.6;
var YELLOW_BLUE_CORNELL_BOX = 0;
var RED_GREEN_CORNELL_BOX = 1;
var environment = YELLOW_BLUE_CORNELL_BOX;
function tick(timeSinceStart) {
eye.elements[0] = zoomZ * Math.sin(angleY) * Math.cos(angleX);
eye.elements[1] = zoomZ * Math.sin(angleX);
eye.elements[2] = zoomZ * Math.cos(angleY) * Math.cos(angleX);
document.getElementById('glossiness-factor').style.display = (material == MATERIAL_GLOSSY) ? 'inline' : 'none';
ui.updateMaterial();
ui.updateGlossiness();