41 |
42 |
Examples
43 | {EXAMPLES.map((group:ExampleGroup,i) => {
44 | if(group.type === 'group') {
45 | let items = group.content.map(make_example_button)
46 | return
47 | } else {
48 | return nothing
49 | }
50 | })}
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/examples.md:
--------------------------------------------------------------------------------
1 | # examples
2 |
3 | * What's the shortest possibl
4 | e raytracer using vector math.
5 | * Loop over every pixel
6 | * generate primary ray
7 | * intersect with list of objects
8 | * find normal at closest intersection
9 | * calculate shading using lights.
10 | * project secondary rays and recurse
11 |
12 |
13 | ```javascript
14 | class Color {
15 | constructor(red,green,blue) {
16 | this.red = red,
17 | this.blue = blue
18 | this.green = green
19 | }
20 | }
21 | class Sphere {
22 | center = new Point()
23 | radius = 1
24 | color = new Color(0,255,0)
25 | intersect(ray) {
26 | let l = this.center.sub(ray.origin)
27 | let adj2 = l.dot(ray.direction)
28 | let d2 = l.dot(l).subtract(adj2*adj2)
29 | if(d2 < this.radius*this.radius) {
30 | return true
31 | } else {
32 | return false
33 | }
34 | }
35 | }
36 |
37 | const scene = {
38 | width: 800,
39 | height: 600,
40 | fov: 90.0
41 | sphere: new Sphere(
42 | new Point(0,0,-5),
43 | 1.0,
44 | new Color(0.4,1.0,0.4)
45 | )
46 | }
47 |
48 | class Ray {
49 | origin: new Point(),
50 | direction: new Vec3()
51 | }
52 |
53 | let canvas = new Image(100,100)
54 |
55 | function create_prime(x,y,scene) {
56 | let canvas_x = (x+0.5)/scene.width * 2.0 - 1.0
57 | let canvas_y = 1.0 - (y+0.5)/scene.height * 2.0
58 | return new Ray(
59 | new Point(0,0,0),
60 | new Vec3(canvas_x,canvas_y,-1).normalize(),
61 | )
62 | }
63 |
64 | for let i=0; i