-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraytracing.html
282 lines (242 loc) · 8.26 KB
/
raytracing.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
<!DOCTYPE html>
<canvas height="600" id="canvas" style="border: 1px black solid" width="600"></canvas>
<script>
"use strict";
// ======================================================================
// canvas access
// ======================================================================
let canvas = document.getElementById("canvas");
let canvas_context = canvas.getContext("2d");
let canvas_buffer = canvas_context.getImageData(0, 0, canvas.width, canvas.height);
let canvas_pitch = canvas_buffer.width * 4;
function PutPixel(x, y, color) {
x = canvas.width/2 + x;
y = canvas.height/2 - y - 1;
if (x < 0 || x >= canvas.width || y < 0 || y >= canvas.height) {
return;
}
let offset = 4*x + canvas_pitch*y;
canvas_buffer.data[offset++] = color[0];
canvas_buffer.data[offset++] = color[1];
canvas_buffer.data[offset++] = color[2];
canvas_buffer.data[offset++] = 255;
}
// Displays the contents of the offscreen buffer into the canvas.
function UpdateCanvas() {
canvas_context.putImageData(canvas_buffer, 0, 0);
}
// ======================================================================
// linear algebra (3D space)
// ======================================================================
function DotProduct(v1, v2) {
return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
}
function Multiply(s, v) {
return [v[0] * s, v[1] * s, v[2] * s];
}
function Add(v1, v2) {
return [v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]];
}
function Subtract(v1, v2) {
return Add(v1, Multiply(-1, v2));
}
function Norm(v) {
return Math.sqrt(DotProduct(v, v));
}
function Normalize(v) {
let norm = Norm(v);
return Multiply(1/norm, v);
}
function MultiplyMV(mat, vec) {
let result = [0, 0, 0];
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
result[i] += vec[j] * mat[i][j];
}
}
return result;
}
// ======================================================================
// Classes for scene elements.
// ======================================================================
// A Sphere.
function Sphere(center, radius, color, specular, reflective) {
this.center = center;
this.radius = radius;
this.color = color;
this.specular = specular;
this.reflective = reflective;
}
// A Light.
function Light(type, intensity, position) {
this.type = type;
this.intensity = intensity;
this.position = position;
}
// Light types.
Light.AMBIENT = 0;
Light.POINT = 1;
Light.DIRECTIONAL = 2;
// A Camera
function Camera(position, rotation) {
this.position = position;
this.rotation = rotation;
}
// ======================================================================
// Graphics functions.
// ======================================================================
// Converts 2D canvas coordinates to 3D viewport coordinates.
function CanvasToViewport(p2d) {
return [
p2d[0] * viewport_size / canvas.width,
p2d[1] * viewport_size / canvas.height,
projection_plane_z];
}
function IntersectRaySphere(origin, direction, sphere) {
// solves the quadratic equation
let r = sphere.radius;
let co = Subtract(origin, sphere.center);
let a = DotProduct(direction, direction);
let b = 2 * DotProduct(co, direction);
let c = DotProduct(co, co) - (r ** 2);
let discriminant = (b ** 2) - 4*a*c;
if (discriminant < 0) {
return [Infinity, Infinity];
}
let t1 = (-b + Math.sqrt(discriminant)) / (2*a);
let t2 = (-b - Math.sqrt(discriminant)) / (2*a);
return [t1, t2];
}
function ReflectRay(ray, normal) {
let dot_p = DotProduct(normal, ray);
return Subtract(
Multiply(
2 * dot_p,
normal),
ray);
}
// Computes the intensity of light upon a given point
function ComputeLighting(point, normal, view, s) {
let intensity = 0.0;
for (let i = 0; i < lights.length; i++) {
let light = lights[i];
if (light.type == Light.AMBIENT) {
intensity += light.intensity;
}
else {
let l_vec, t_max;
if (light.type == Light.POINT) {
l_vec = Subtract(light.position, point);
t_max = 1;
}
else {
l_vec = light.position;
t_max = Infinity;
}
// shadow check
let [shadow_sphere, shadow_t] = ClosestIntersection(point, l_vec, EPSILON, t_max);
if (shadow_sphere != null) {
continue;
}
// diffuse reflection of matte objects
let n_dot_l = DotProduct(normal, l_vec);
if (n_dot_l > 0) {
intensity += light.intensity * n_dot_l / Norm(l_vec);
}
// specular reflection of shiny objects
if (s != -1) {
let reflection_vec = ReflectRay(l_vec, normal);
let r_dot_v = DotProduct(reflection_vec, view);
if (r_dot_v > 0) {
let r_v_len_prod = Norm(reflection_vec) * Norm(view);
intensity += light.intensity * (r_dot_v / r_v_len_prod) ** s;
}
}
}
}
return intensity;
}
function ClosestIntersection(origin, direction, t_min, t_max) {
let closest_t = Infinity;
let closest_sphere = null;
for (let i = 0; i < spheres.length; i++) {
let ts = IntersectRaySphere(origin, direction, spheres[i]);
if (ts[0] < closest_t && t_min < ts[0] && ts[0] < t_max) {
closest_t = ts[0];
closest_sphere = spheres[i];
}
if (ts[1] < closest_t && t_min < ts[1] && ts[1] < t_max) {
closest_t = ts[1];
closest_sphere = spheres[i];
}
}
return [closest_sphere, closest_t];
}
function TraceRay(origin, direction, t_min, t_max, recursion_depth) {
let [closest_sphere, closest_t] = ClosestIntersection(origin, direction, t_min, t_max);
if (closest_sphere == null) {
return background_color;
}
let intersection_point = Add(origin, Multiply(closest_t, direction));
let normal = Normalize(Subtract(intersection_point, closest_sphere.center));
let view = Multiply(-1, direction);
let light_intensity = ComputeLighting(intersection_point, normal, view, closest_sphere.specular);
let local_color = Multiply(light_intensity, closest_sphere.color);
let r = closest_sphere.reflective;
if (recursion_depth <= 0 || r <= 0) {
return local_color;
}
let reflected_ray = ReflectRay(view, normal);
let reflected_color = TraceRay(intersection_point, reflected_ray, EPSILON, Infinity, recursion_depth - 1);
local_color = Multiply(1 - r, local_color);
reflected_color = Multiply(r, reflected_color);
return Add(local_color, reflected_color);
}
// ======================================================================
// Scene setup.
// ======================================================================
let viewport_size = 1;
let projection_plane_z = 1;
let EPSILON = 0.001;
let depth = 3;
let m1 = [
[ 1, 0, 0],
[0, 0.7071, 0.7071],
[0, -0.7071, 0.7071 ]];
let m2 = [
[0.7071, 0, -0.7071],
[ 0, 1, 0],
[0.7071, 0, 0.7071]];
let m3 = [
[0.7071, 0.7071, 0],
[-0.7071, 0.7071, 0],
[0, 0, 1]];
let identity = [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]];
let camera = new Camera([0, 0, -4], identity);
let background_color = [0, 0, 0];
let spheres = [
new Sphere([0, -1, 3], 1, [255, 0, 0], 500, 0.2), // red
new Sphere([2, 0, 4], 1, [0, 0, 255], 500, 0.3), // blue
new Sphere([-2, 0, 4], 1, [0, 255, 0], 10, 0.4), // green
new Sphere([0, -5001, 0], 5000, [255, 255, 0], 1000, 0.5),// yellow Earth
];
let lights = [
new Light(Light.AMBIENT, 0.2),
new Light(Light.POINT, 0.6, [2, 1, 0]),
new Light(Light.DIRECTIONAL, 0.2, [1, 4, 4])
];
// ======================================================================
// main loop
// ======================================================================
for (let x = -canvas.width/2; x < canvas.width/2; x++) {
for (let y = -canvas.height/2; y < canvas.height/2; y++) {
let direction = MultiplyMV(camera.rotation, CanvasToViewport([x, y]));
let color = TraceRay(camera.position, direction, 1, Infinity, depth)
PutPixel(x, y, color);
}
}
UpdateCanvas();
</script>