-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.html
932 lines (634 loc) · 37.4 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
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
<!DOCTYPE html>
<html>
<head>
<title>Raytracing</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta content="width=device-width,minimum-scale=1" name=viewport>
<link rel="stylesheet" media="all" href="custom.css" />
</head>
<body>
<div id="container">
<div class='cred'>
<a href='https://github.com/tmcw/literate-raytracer'>an open source project by tom macwright</a>
</div>
<canvas id='c'></canvas>
<div id='controls'>
<button id='play'>Play</button>
</div>
<div id="background"></div>
<ul id="jump_to">
<li>
<a class="large" href="javascript:void(0);">Jump To …</a>
<a class="small" href="javascript:void(0);">+</a>
<div id="jump_wrapper">
<div id="jump_page">
<a class="source" href="index.html">
index.js
</a>
<a class="source" href="vector.html">
vector.js
</a>
</div>
</li>
</ul>
<div>
<section id="section-1">
<div>
<h1 id="raytracing">Raytracing</h1>
<p><strong>Raytracing</strong> is a relatively simple way to render images of 3D objects.
The core is an elegant idea, that one can simulate the real-world behavior
of photons of light bouncing off of surfaces and colors accumulating from
their paths. It’s not inherently fast, but the simplicity of the core lets
it model interesting things like reflections and depth of field in ways
that mirror natural processes.</p>
<h2 id="cs-301-raytracing">CS 301: Raytracing</h2>
<p>This happens to be a popular subject for education: implementing a raytracer
requires a student to understand vector math, fast code, and even recursion.
The reward is a pretty image - more compelling than the blasé debug output
that students get from most assignments.</p>
<p>But it’s still hard to learn: explanations are written either in the
language of mathematics or programming, and rarely connect all the dots.
Raytracer implementations tend to extremes: one
<a href="http://fabiensanglard.net/rayTracing_back_of_business_card/">fits on a business card</a>,
another <a href="http://www.povray.org/">supports nearly every potential feature</a>,
and most of the rest are <a href="https://github.com/search?q=raytracer+cs&ref=cmdform">homework assignments</a>,
implemented just enough to run, never enough to have comments and documentation.</p>
<h2 id="literate-raytracer">Literate Raytracer</h2>
<p><a href="https://github.com/tmcw/literate-raytracer">Literate raytracer</a> is a
simple implementation of raytracing in Javascript. It’s <a href="http://macwright.org/literate-raytracer/">made to be
read as a narrative</a>, and intends
to <a href="http://macwright.org/literate-raytracer/vector.html">explain vector operations</a> as well.</p>
<h1 id="setup">Setup</h1>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">var</span> c = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"c"</span>),
width = <span class="hljs-number">640</span> * <span class="hljs-number">0.5</span>,
height = <span class="hljs-number">480</span> * <span class="hljs-number">0.5</span>;</pre></div></div>
</section>
<section id="section-2">
<div>
<p>Get a context in order to generate a proper data array. We aren’t going to
use traditional Canvas drawing functions like <code>fillRect</code> - instead this
raytracer will directly compute pixel data and then put it into an image.</p>
</div>
<div class="content"><div class='highlight'><pre>c.width = width;
c.height = height;
<span class="hljs-keyword">var</span> ctx = c.getContext(<span class="hljs-string">"2d"</span>),
data = ctx.getImageData(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, width, height);</pre></div></div>
</section>
<section id="section-3">
<div>
<h1 id="the-scene">The Scene</h1>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">var</span> scene = {};</pre></div></div>
</section>
<section id="section-4">
<div>
<p><img src="http://farm4.staticflickr.com/3781/10524236814_5a9c43487a_b.jpg" alt=""></p>
<p>We need to define three different kinds of things in 3D space:
a <strong>camera</strong> from which we cast rays into the scene, <strong>objects</strong>
that can be hit by those rays and are drawn into the scene, and
<strong>lights</strong> that change the color of rays, by extension coloring objects.</p>
<p>In this case, we define these objects as simple objects with vectors
defined as <code>{x, y, z}</code> objects.</p>
</div>
</section>
<section id="section-5">
<div>
<h2 id="the-camera">The Camera</h2>
<p>Our camera is pretty simple: it’s a point in space, where you can imagine
that the camera ‘sits’, a <code>fieldOfView</code>, which is the angle from the right
to the left side of its frame, and a <code>vector</code> which determines what
angle it points in.</p>
</div>
<div class="content"><div class='highlight'><pre>scene.camera = {
<span class="hljs-attr">point</span>: {
<span class="hljs-attr">x</span>: <span class="hljs-number">0</span>,
<span class="hljs-attr">y</span>: <span class="hljs-number">1.8</span>,
<span class="hljs-attr">z</span>: <span class="hljs-number">10</span>,
},
<span class="hljs-attr">fieldOfView</span>: <span class="hljs-number">45</span>,
<span class="hljs-attr">vector</span>: {
<span class="hljs-attr">x</span>: <span class="hljs-number">0</span>,
<span class="hljs-attr">y</span>: <span class="hljs-number">3</span>,
<span class="hljs-attr">z</span>: <span class="hljs-number">0</span>,
},
};</pre></div></div>
</section>
<section id="section-6">
<div>
<h2 id="lights">Lights</h2>
<p>Lights are defined only as points in space - surfaces that have lambert
shading will be affected by any visible lights.</p>
</div>
<div class="content"><div class='highlight'><pre>scene.lights = [
{
<span class="hljs-attr">x</span>: <span class="hljs-number">-30</span>,
<span class="hljs-attr">y</span>: <span class="hljs-number">-10</span>,
<span class="hljs-attr">z</span>: <span class="hljs-number">20</span>,
},
];</pre></div></div>
</section>
<section id="section-7">
<div>
<h2 id="objects">Objects</h2>
<p>This raytracer handles sphere objects, with any color, position, radius,
and surface properties.</p>
</div>
<div class="content"><div class='highlight'><pre>scene.objects = [
{
<span class="hljs-attr">type</span>: <span class="hljs-string">"sphere"</span>,
<span class="hljs-attr">point</span>: {
<span class="hljs-attr">x</span>: <span class="hljs-number">0</span>,
<span class="hljs-attr">y</span>: <span class="hljs-number">3.5</span>,
<span class="hljs-attr">z</span>: <span class="hljs-number">-3</span>,
},
<span class="hljs-attr">color</span>: {
<span class="hljs-attr">x</span>: <span class="hljs-number">155</span>,
<span class="hljs-attr">y</span>: <span class="hljs-number">200</span>,
<span class="hljs-attr">z</span>: <span class="hljs-number">155</span>,
},
<span class="hljs-attr">specular</span>: <span class="hljs-number">0.2</span>,
<span class="hljs-attr">lambert</span>: <span class="hljs-number">0.7</span>,
<span class="hljs-attr">ambient</span>: <span class="hljs-number">0.1</span>,
<span class="hljs-attr">radius</span>: <span class="hljs-number">3</span>,
},
{
<span class="hljs-attr">type</span>: <span class="hljs-string">"sphere"</span>,
<span class="hljs-attr">point</span>: {
<span class="hljs-attr">x</span>: <span class="hljs-number">-4</span>,
<span class="hljs-attr">y</span>: <span class="hljs-number">2</span>,
<span class="hljs-attr">z</span>: <span class="hljs-number">-1</span>,
},
<span class="hljs-attr">color</span>: {
<span class="hljs-attr">x</span>: <span class="hljs-number">155</span>,
<span class="hljs-attr">y</span>: <span class="hljs-number">155</span>,
<span class="hljs-attr">z</span>: <span class="hljs-number">155</span>,
},
<span class="hljs-attr">specular</span>: <span class="hljs-number">0.1</span>,
<span class="hljs-attr">lambert</span>: <span class="hljs-number">0.9</span>,
<span class="hljs-attr">ambient</span>: <span class="hljs-number">0.0</span>,
<span class="hljs-attr">radius</span>: <span class="hljs-number">0.2</span>,
},
{
<span class="hljs-attr">type</span>: <span class="hljs-string">"sphere"</span>,
<span class="hljs-attr">point</span>: {
<span class="hljs-attr">x</span>: <span class="hljs-number">-4</span>,
<span class="hljs-attr">y</span>: <span class="hljs-number">3</span>,
<span class="hljs-attr">z</span>: <span class="hljs-number">-1</span>,
},
<span class="hljs-attr">color</span>: {
<span class="hljs-attr">x</span>: <span class="hljs-number">255</span>,
<span class="hljs-attr">y</span>: <span class="hljs-number">255</span>,
<span class="hljs-attr">z</span>: <span class="hljs-number">255</span>,
},
<span class="hljs-attr">specular</span>: <span class="hljs-number">0.2</span>,
<span class="hljs-attr">lambert</span>: <span class="hljs-number">0.7</span>,
<span class="hljs-attr">ambient</span>: <span class="hljs-number">0.1</span>,
<span class="hljs-attr">radius</span>: <span class="hljs-number">0.1</span>,
},
];</pre></div></div>
</section>
<section id="section-8">
<div>
<h1 id="throwing-rays">Throwing Rays</h1>
<p>This is one part where we can’t follow nature exactly: technically photons
come out of lights, bounce off of objects, and then some hit the ‘eye’
and many don’t. Simulating this - sending rays in all directions out of
each light and most not having any real effect - would be too inefficient.</p>
<p>Luckily, the reverse is more efficient and has practically the same result -
instead of rays going ‘from’ lights to the eye, we follow rays from the eye
and see if they end up hitting any features and lights on their travels.</p>
<p>For each pixel in the canvas, there needs to be at least one ray of light
that determines its color by bouncing through the scene.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">render</span>(<span class="hljs-params">scene</span>) </span>{</pre></div></div>
</section>
<section id="section-9">
<div>
<p>first ‘unpack’ the scene to make it easier to reference</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">var</span> camera = scene.camera,
objects = scene.objects,
lights = scene.lights;</pre></div></div>
</section>
<section id="section-10">
<div>
<p>This process
is a bit odd, because there’s a disconnect between pixels and vectors:
given the left and right, top and bottom rays, the rays we shoot are just
interpolated between them in little increments.</p>
<p>Starting with the height and width of the scene, the camera’s place,
direction, and field of view, we calculate factors that create
<code>width*height</code> vectors for each ray</p>
</div>
</section>
<section id="section-11">
<div>
<p>Start by creating a simple vector pointing in the direction the camera is
pointing - a unit vector</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">var</span> eyeVector = Vector.unitVector(
Vector.subtract(camera.vector, camera.point)
),</pre></div></div>
</section>
<section id="section-12">
<div>
<p>and then we’ll rotate this by combining it with a version that’s turned
90° right and one that’s turned 90° up. Since the <a href="http://en.wikipedia.org/wiki/Cross_product">cross product</a>
takes two vectors and creates a third that’s perpendicular to both,
we use a pure ‘UP’ vector to turn the camera right, and that ‘right’
vector to turn the camera up.</p>
</div>
<div class="content"><div class='highlight'><pre>vpRight = Vector.unitVector(Vector.crossProduct(eyeVector, Vector.UP)),
vpUp = Vector.unitVector(Vector.crossProduct(vpRight, eyeVector)),</pre></div></div>
</section>
<section id="section-13">
<div>
<p>The actual ending pixel dimensions of the image aren’t important here -
note that <code>width</code> and <code>height</code> are in pixels, but the numbers we compute
here are just based on the ratio between them, <code>height/width</code>, and the
<code>fieldOfView</code> of the camera.</p>
</div>
<div class="content"><div class='highlight'><pre> fovRadians = (<span class="hljs-built_in">Math</span>.PI * (camera.fieldOfView / <span class="hljs-number">2</span>)) / <span class="hljs-number">180</span>,
heightWidthRatio = height / width,
halfWidth = <span class="hljs-built_in">Math</span>.tan(fovRadians),
halfHeight = heightWidthRatio * halfWidth,
camerawidth = halfWidth * <span class="hljs-number">2</span>,
cameraheight = halfHeight * <span class="hljs-number">2</span>,
pixelWidth = camerawidth / (width - <span class="hljs-number">1</span>),
pixelHeight = cameraheight / (height - <span class="hljs-number">1</span>);
<span class="hljs-keyword">var</span> index, color;
<span class="hljs-keyword">var</span> ray = {
<span class="hljs-attr">point</span>: camera.point,
};
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> x = <span class="hljs-number">0</span>; x < width; x++) {
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> y = <span class="hljs-number">0</span>; y < height; y++) {</pre></div></div>
</section>
<section id="section-14">
<div>
<p>turn the raw pixel <code>x</code> and <code>y</code> values into values from -1 to 1
and use these values to scale the facing-right and facing-up
vectors so that we generate versions of the <code>eyeVector</code> that are
skewed in each necessary direction.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">var</span> xcomp = Vector.scale(vpRight, x * pixelWidth - halfWidth),
ycomp = Vector.scale(vpUp, y * pixelHeight - halfHeight);
ray.vector = Vector.unitVector(Vector.add3(eyeVector, xcomp, ycomp));</pre></div></div>
</section>
<section id="section-15">
<div>
<p>use the vector generated to raytrace the scene, returning a color
as a <code>{x, y, z}</code> vector of RGB values</p>
</div>
<div class="content"><div class='highlight'><pre> color = trace(ray, scene, <span class="hljs-number">0</span>);
(index = x * <span class="hljs-number">4</span> + y * width * <span class="hljs-number">4</span>), (data.data[index + <span class="hljs-number">0</span>] = color.x);
data.data[index + <span class="hljs-number">1</span>] = color.y;
data.data[index + <span class="hljs-number">2</span>] = color.z;
data.data[index + <span class="hljs-number">3</span>] = <span class="hljs-number">255</span>;
}
}</pre></div></div>
</section>
<section id="section-16">
<div>
<p>Now that each ray has returned and populated the <code>data</code> array with
correctly lit colors, fill the canvas with the generated data.</p>
</div>
<div class="content"><div class='highlight'><pre> ctx.putImageData(data, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>);
}</pre></div></div>
</section>
<section id="section-17">
<div>
<h1 id="trace">Trace</h1>
<p>Given a ray, shoot it until it hits an object and return that object’s color,
or <code>Vector.WHITE</code> if no object is found. This is the main function that’s
called in order to draw the image, and it recurses into itself if rays
reflect off of objects and acquire more color.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">trace</span>(<span class="hljs-params">ray, scene, depth</span>) </span>{</pre></div></div>
</section>
<section id="section-18">
<div>
<p>This is a recursive method: if we hit something that’s reflective,
then the call to <code>surface()</code> at the bottom will return here and try
to find what the ray reflected into. Since this could easily go
on forever, first check that we haven’t gone more than three bounces
into a reflection.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">if</span> (depth > <span class="hljs-number">3</span>) <span class="hljs-keyword">return</span>;
<span class="hljs-keyword">var</span> distObject = intersectScene(ray, scene);</pre></div></div>
</section>
<section id="section-19">
<div>
<p>If we don’t hit anything, fill this pixel with the background color -
in this case, white.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">if</span> (distObject[<span class="hljs-number">0</span>] === <span class="hljs-literal">Infinity</span>) {
<span class="hljs-keyword">return</span> Vector.WHITE;
}
<span class="hljs-keyword">var</span> dist = distObject[<span class="hljs-number">0</span>],
object = distObject[<span class="hljs-number">1</span>];</pre></div></div>
</section>
<section id="section-20">
<div>
<p>The <code>pointAtTime</code> is another way of saying the ‘intersection point’
of this ray into this object. We compute this by simply taking
the direction of the ray and making it as long as the distance
returned by the intersection check.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> pointAtTime = Vector.add(ray.point, Vector.scale(ray.vector, dist));
<span class="hljs-keyword">return</span> surface(
ray,
scene,
object,
pointAtTime,
sphereNormal(object, pointAtTime),
depth
);
}</pre></div></div>
</section>
<section id="section-21">
<div>
<h1 id="detecting-collisions-against-all-objects">Detecting collisions against all objects</h1>
<p>Given a ray, let’s figure out whether it hits anything, and if so,
what’s the closest thing it hits.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">intersectScene</span>(<span class="hljs-params">ray, scene</span>) </span>{</pre></div></div>
</section>
<section id="section-22">
<div>
<p>The base case is that it hits nothing, and travels for <code>Infinity</code></p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">var</span> closest = [<span class="hljs-literal">Infinity</span>, <span class="hljs-literal">null</span>];</pre></div></div>
</section>
<section id="section-23">
<div>
<p>But for each object, we check whether it has any intersection,
and compare that intersection - is it closer than <code>Infinity</code> at first,
and then is it closer than other objects that have been hit?</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i < scene.objects.length; i++) {
<span class="hljs-keyword">var</span> object = scene.objects[i],
dist = sphereIntersection(object, ray);
<span class="hljs-keyword">if</span> (dist !== <span class="hljs-literal">undefined</span> && dist < closest[<span class="hljs-number">0</span>]) {
closest = [dist, object];
}
}
<span class="hljs-keyword">return</span> closest;
}</pre></div></div>
</section>
<section id="section-24">
<div>
<h2 id="detecting-collisions-against-a-sphere">Detecting collisions against a sphere</h2>
<p><img src="graphics/sphereintersection.png" alt=""></p>
<p>Spheres are one of the simplest objects for rays to interact with, since
the geometrical math for finding intersections and reflections with them
is pretty straightforward.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sphereIntersection</span>(<span class="hljs-params">sphere, ray</span>) </span>{
<span class="hljs-keyword">var</span> eye_to_center = Vector.subtract(sphere.point, ray.point),</pre></div></div>
</section>
<section id="section-25">
<div>
<p>picture a triangle with one side going straight from the camera point
to the center of the sphere, another side being the vector.
the final side is a right angle.</p>
<p>This equation first figures out the length of the vector side</p>
</div>
<div class="content"><div class='highlight'><pre>v = Vector.dotProduct(eye_to_center, ray.vector),</pre></div></div>
</section>
<section id="section-26">
<div>
<p>then the length of the straight from the camera to the center
of the sphere</p>
</div>
<div class="content"><div class='highlight'><pre>eoDot = Vector.dotProduct(eye_to_center, eye_to_center),</pre></div></div>
</section>
<section id="section-27">
<div>
<p>and compute a segment from the right angle of the triangle to a point
on the <code>v</code> line that also intersects the circle</p>
</div>
<div class="content"><div class='highlight'><pre>discriminant = sphere.radius * sphere.radius - eoDot + v * v;</pre></div></div>
</section>
<section id="section-28">
<div>
<p>If the discriminant is negative, that means that the sphere hasn’t
been hit by the ray</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">if</span> (discriminant < <span class="hljs-number">0</span>) {
<span class="hljs-keyword">return</span>;
} <span class="hljs-keyword">else</span> {</pre></div></div>
</section>
<section id="section-29">
<div>
<p>otherwise, we return the distance from the camera point to the sphere
<code>Math.sqrt(dotProduct(a, a))</code> is the length of a vector, so
<code>v - Math.sqrt(discriminant)</code> means the length of the the vector
just from the camera to the intersection point.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> v - <span class="hljs-built_in">Math</span>.sqrt(discriminant);
}
}</pre></div></div>
</section>
<section id="section-30">
<div>
<p>A normal is, at each point on the surface of a sphere or some other object,
a vector that’s perpendicular to the surface and radiates outward. We need
to know this so that we can calculate the way that a ray reflects off of
a sphere.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sphereNormal</span>(<span class="hljs-params">sphere, pos</span>) </span>{
<span class="hljs-keyword">return</span> Vector.unitVector(Vector.subtract(pos, sphere.point));
}</pre></div></div>
</section>
<section id="section-31">
<div>
<h1 id="surface">Surface</h1>
<p><img src="http://farm3.staticflickr.com/2851/10524788334_f2e3903b36_b.jpg" alt=""></p>
<p>If <code>trace()</code> determines that a ray intersected with an object, <code>surface</code>
decides what color it acquires from the interaction.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">surface</span>(<span class="hljs-params">ray, scene, object, pointAtTime, normal, depth</span>) </span>{
<span class="hljs-keyword">var</span> b = object.color,
c = Vector.ZERO,
lambertAmount = <span class="hljs-number">0</span>;</pre></div></div>
</section>
<section id="section-32">
<div>
<p><strong><a href="http://en.wikipedia.org/wiki/Lambertian_reflectance">Lambert shading</a></strong>
is our pretty shading, which shows gradations from the most lit point on
the object to the least.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">if</span> (object.lambert) {
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i < scene.lights.length; i++) {
<span class="hljs-keyword">var</span> lightPoint = scene.lights[i];</pre></div></div>
</section>
<section id="section-33">
<div>
<p>First: can we see the light? If not, this is a shadowy area
and it gets no light from the lambert shading process.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">if</span> (!isLightVisible(pointAtTime, scene, lightPoint)) <span class="hljs-keyword">continue</span>;</pre></div></div>
</section>
<section id="section-34">
<div>
<p>Otherwise, calculate the lambertian reflectance, which
essentially is a ‘diffuse’ lighting system - direct light
is bright, and from there, less direct light is gradually,
beautifully, less light.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">var</span> contribution = Vector.dotProduct(
Vector.unitVector(Vector.subtract(lightPoint, pointAtTime)),
normal
);</pre></div></div>
</section>
<section id="section-35">
<div>
<p>sometimes this formula can return negatives, so we check:
we only want positive values for lighting.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (contribution > <span class="hljs-number">0</span>) lambertAmount += contribution;
}
}</pre></div></div>
</section>
<section id="section-36">
<div>
<p><strong><a href="https://en.wikipedia.org/wiki/Specular_reflection">Specular</a></strong> is a fancy word for ‘reflective’: rays that hit objects
with specular surfaces bounce off and acquire the colors of other objects
they bounce into.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">if</span> (object.specular) {</pre></div></div>
</section>
<section id="section-37">
<div>
<p>This is basically the same thing as what we did in <code>render()</code>, just
instead of looking from the viewpoint of the camera, we’re looking
from a point on the surface of a shiny object, seeing what it sees
and making that part of a reflection.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">var</span> reflectedRay = {
<span class="hljs-attr">point</span>: pointAtTime,
<span class="hljs-attr">vector</span>: Vector.reflectThrough(ray.vector, normal),
};
<span class="hljs-keyword">var</span> reflectedColor = trace(reflectedRay, scene, ++depth);
<span class="hljs-keyword">if</span> (reflectedColor) {
c = Vector.add(c, Vector.scale(reflectedColor, object.specular));
}
}</pre></div></div>
</section>
<section id="section-38">
<div>
<p>lambert should never ‘blow out’ the lighting of an object,
even if the ray bounces between a lot of things and hits lights</p>
</div>
<div class="content"><div class='highlight'><pre>lambertAmount = <span class="hljs-built_in">Math</span>.min(<span class="hljs-number">1</span>, lambertAmount);</pre></div></div>
</section>
<section id="section-39">
<div>
<p><strong>Ambient</strong> colors shine bright regardless of whether there’s a light visible -
a circle with a totally ambient blue color will always just be a flat blue
circle.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> Vector.add3(
c,
Vector.scale(b, lambertAmount * object.lambert),
Vector.scale(b, object.ambient)
);
}</pre></div></div>
</section>
<section id="section-40">
<div>
<p>Check whether a light is visible from some point on the surface of something.
Note that there might be an intersection here, which is tricky - but if it’s
tiny, it’s actually an intersection with the object we’re trying to decide
the surface of. That’s why we check for <code>> -0.005</code> at the end.</p>
<p>This is the part that makes objects cast shadows on each other: from here
we’d check to see if the area in a shadowy spot can ‘see’ a light, and when
this returns <code>false</code>, we make the area shadowy.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isLightVisible</span>(<span class="hljs-params">pt, scene, light</span>) </span>{
<span class="hljs-keyword">var</span> distObject = intersectScene(
{
<span class="hljs-attr">point</span>: pt,
<span class="hljs-attr">vector</span>: Vector.unitVector(Vector.subtract(pt, light)),
},
scene
);
<span class="hljs-keyword">return</span> distObject[<span class="hljs-number">0</span>] > <span class="hljs-number">-0.005</span>;
}</pre></div></div>
</section>
<section id="section-41">
<div>
<p>Here we do a little fun magic, just for the heck of it. We have three spheres
in the scene - <code>scene.objects[0]</code> is the big one, kind of like ‘Earth’.</p>
<p>The other two are little, so let’s make them orbit around the big one
and look cool!</p>
</div>
</section>
<section id="section-42">
<div>
<p>The orbits of the two planets. We use some basic trigonetry to do the orbits:
using <code>Math.sin()</code> and <code>Math.cos()</code>, it’s simple to get a
<a href="http://en.wikipedia.org/wiki/Unit_circle">unit circle</a>
for each planet. Here’s <a href="http://macwright.org/2013/03/05/math-for-pictures.html">an article I wrote</a>
for getting to know <code>sin</code> and <code>cos</code>.</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-keyword">var</span> planet1 = <span class="hljs-number">0</span>,
planet2 = <span class="hljs-number">0</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">tick</span>(<span class="hljs-params"></span>) </span>{</pre></div></div>
</section>
<section id="section-43">
<div>
<p>make one planet spin a little bit faster than the other, just for
effect.</p>
</div>
<div class="content"><div class='highlight'><pre>planet1 += <span class="hljs-number">0.1</span>;
planet2 += <span class="hljs-number">0.2</span>;</pre></div></div>
</section>
<section id="section-44">
<div>
<p>set the position of each moon with some trig.</p>
</div>
<div class="content"><div class='highlight'><pre>scene.objects[<span class="hljs-number">1</span>].point.x = <span class="hljs-built_in">Math</span>.sin(planet1) * <span class="hljs-number">3.5</span>;
scene.objects[<span class="hljs-number">1</span>].point.z = <span class="hljs-number">-3</span> + <span class="hljs-built_in">Math</span>.cos(planet1) * <span class="hljs-number">3.5</span>;
scene.objects[<span class="hljs-number">2</span>].point.x = <span class="hljs-built_in">Math</span>.sin(planet2) * <span class="hljs-number">4</span>;
scene.objects[<span class="hljs-number">2</span>].point.z = <span class="hljs-number">-3</span> + <span class="hljs-built_in">Math</span>.cos(planet2) * <span class="hljs-number">4</span>;</pre></div></div>
</section>
<section id="section-45">
<div>
<p>finally, render the scene!</p>
</div>
<div class="content"><div class='highlight'><pre>render(scene);</pre></div></div>
</section>
<section id="section-46">
<div>
<p>and as soon as we’re finished, render it again and move the planets
again</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">if</span> (playing) <span class="hljs-built_in">setTimeout</span>(tick, <span class="hljs-number">10</span>);
}
<span class="hljs-keyword">var</span> playing = <span class="hljs-literal">false</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">toggle</span>(<span class="hljs-params">e</span>) </span>{
<span class="hljs-keyword">if</span> (playing) {
e.target.innerText = <span class="hljs-string">"Play"</span>;
playing = <span class="hljs-literal">false</span>;
} <span class="hljs-keyword">else</span> {
e.target.innerText = <span class="hljs-string">"Pause"</span>;
playing = <span class="hljs-literal">true</span>;
tick();
}
}
render(scene);</pre></div></div>
</section>
<section id="section-47">
<div>
<p>Then let the user control a cute playing animation!</p>
</div>
<div class="content"><div class='highlight'><pre><span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"play"</span>).onclick = toggle;</pre></div></div>
</section>
</div>
</div>
<script src='src/vector.js'></script>
<script src='src/index.js'></script>
</body>
</html>