-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnti-Aliasing Test Double Looping.html
162 lines (118 loc) · 4.27 KB
/
Anti-Aliasing Test Double Looping.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
<html>
<head>
<title>Raytracing Coffee</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<style>
td {
width: 4px;
height: 4px;
}
</style>
</head>
<body>
<table cellspacing="0" cellpadding="0"></table>
</body>
<script type="text/coffeescript">
len = (u) ->
Math.sqrt(dot(u, u))
dot = (u, v) ->
v[0] * u[0] + v[1] * u[1] + v[2] * u[2]
sub = (u, v) ->
[u[0] - v[0], u[1] - v[1], u[2] - v[2]]
add = (u, v) ->
[v[0] + u[0], v[1] + u[1], v[2] + u[2]]
mul = (s, v) ->
[s * v[0], s * v[1], s * v[2]]
normalize = (v) ->
length = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2])
[v[0] / length, v[1] / length, v[2] / length]
scale = (s, v) ->
[s * v[0], s * v[1], s * v[2]]
class Sphere
constructor: (@center, @radius) ->
intersect: (g, d) ->
# g is the ray origin
# d is the ray direction
c = sub(@center, g)
s = dot(c, d)
discr = (@radius * @radius) - dot(c, c) + s*s
console.log(g, d, c, s, discr) if debug
if discr < 0
return Infinity
return s - Math.sqrt(discr)
normal: (poi) ->
normalize(sub(poi, @center))
class Plane
constructor: (@n, @d) -> #n is the normal vector
@n = normalize(@n)
intersect: (g, d) ->
#@d is referring to the distance from the origin
# distance to the point of intersection on the plane
(dot(g, @n) + @d) / dot(d, @n)
normal: (poi) ->
@n
light = [100, 100, -100]
shapes = []
shapes.push new Plane [0, 1, 0], -1.2
shapes.push new Sphere [0, 0, 5], 1
# g is the origin of the ray (can be the poi or the eye)
# d is the direction
findNearestObject = (g, d) ->
near = t: Infinity
for shape in shapes
# t is the distance to shape
t = shape.intersect(g, d)
#console.log t, eye, dir
if t > 1e-7 && t < near.t
near.t = t
near.shape = shape
near
# What color is pixel x,y?
pixel = (x, y) ->
eye = [0, 0, 0]
dir = normalize([x, y, 3]) #controls field of view
#scale(255, dir)
near = findNearestObject(eye, dir)
if near.t < Infinity
# poi = eye + near.t * dir
poi = add(eye, mul(near.t, dir))
#computes the normal vector for the closest shape given a point of intersection
n = near.shape.normal(poi)
#computes the vector from the point of intersection to the light
# we normalize the light because we want to think of the light infinitely back
l = normalize(sub(light, poi))
shadow = findNearestObject(poi, l)
bright = 0
if shadow.t == Infinity
# dot product gives us the brightness
bright = dot(l, n)
console.log(dir, near, l, n, bright) if debug
# if we hit something with a ray then we return a color
return [255 * bright, 0, 0]
return [0, 0, 255]
r = 2
w = 100 # w is the resolution
n = 3 # n is the number of rays shot through each pixel
debug = false
if debug
console.log pixel 1, .4
else
for v in [-w...w]
tr = "<tr>"
for u in [-w...w]
accum = [0, 0, 0] #The initial value of the accumulator before averaging all the rays
for k in [0...n]
for j in [0...n]
randX = (Math.random() * 2) - 1
randY = (Math.random() * 2) - 1
color = pixel((u + randX / 2) / w, -(v + randY / 2) / w)
accum = add(accum, color)
averageColor = mul(1.0 / n**2, accum)
# We do not use j and k, so it’s the same as sending n^2 rays randomly through each pixel (Just like the case when n = 9 from before)
tr += '<td style = "background:rgb('+Math.floor(averageColor[0]) + ',' + Math.floor(averageColor[1]) + ',' + Math.floor(averageColor[2]) + ')"></td>'
tr += "</tr>"
jQuery('table').append(tr)
</script>
<!-- Coffeescript compiling link -->
<script src="https://cdn.rawgit.com/jashkenas/coffeescript/1.11.1/extras/coffee-script.js"></script>
</html>