-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraytracer.js
252 lines (216 loc) · 7.51 KB
/
raytracer.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
import { Camera, Ray, Scene, Sphere, VectorUtils } from "./utility.js";
import { createControls } from "./ui.js";
const canvas = document.getElementById("view");
const context = canvas.getContext("2d");
const width = canvas.width;
const height = canvas.height;
const scene = new Scene();
// Listener to add more spheres when add objects button is clicked
const addBtn = document.getElementById("add");
addBtn.onclick = () => {
const sphere = new Sphere();
createControls(sphere);
scene.add(sphere);
addBtn.blur();
};
// Create camera
const camera = new Camera({ fov: 40 * width / 50 });
// Define number of samples to be rendered
let samples = 100;
const sampleRange = document.getElementById("sample");
const sampleLabel = document.getElementById("sample-label");
sampleLabel.textContent = 100;
sampleRange.onmousemove = () => {
samples = sampleRange.value;
sampleLabel.textContent = samples;
};
// Recursive ray tracing function
function trace(ray, bounce) {
const light = {
x: 0,
y: 0,
z: 0,
};
// Base Case
if (bounce <= 0) {
return light;
} // Recursive Case
else {
// Find closest object that the ray intersected
let collisionInfo = null;
scene.getObjects().forEach((object) => {
const current = object.calcHit(ray);
if (current.intersect) {
if (
collisionInfo == null ||
collisionInfo.distance >= current.distance
) {
collisionInfo = current;
}
}
});
// If collision doesn't occur, return environment light
if (!collisionInfo || !collisionInfo.intersect) {
return light;
}
// Else calculate pixel color based on material properties and further light bounces
const emission = VectorUtils.scalarMult(
collisionInfo.obj.material.emissiveColor,
collisionInfo.obj.material.emissiveStrength,
);
const color = collisionInfo.obj.material.color;
const incomingRay = calculateRayFrom(ray, collisionInfo);
const incomingLight = trace(incomingRay, bounce - 1);
return VectorUtils.add(
emission,
VectorUtils.mult(color, incomingLight),
);
}
}
// Calculate the reflected angle of the ray based on roughness and collision
function calculateRayFrom(ray, collisionInfo) {
// Calculate the exact reflection angle
let reflectDirection = VectorUtils.sub(
ray.direction,
VectorUtils.scalarMult(
collisionInfo.normal,
2 * VectorUtils.dotProduct(ray.direction, collisionInfo.normal),
),
);
// Offset this direction in proportion to roughness to account for glossy and rough surfaces
reflectDirection = {
x: reflectDirection.x +
(Math.random() * 2 - 1) * collisionInfo.obj.material.roughness / 2,
y: reflectDirection.y +
(Math.random() * 2 - 1) * collisionInfo.obj.material.roughness / 2,
z: reflectDirection.z +
(Math.random() * 2 - 1) * collisionInfo.obj.material.roughness / 2,
};
// If angle is greater than 90 with the normal, reverse the direction of the ray
const angleCheck = VectorUtils.dotProduct(
reflectDirection,
collisionInfo.normal,
);
let s = 1;
if (angleCheck < 0) {
s = -1;
}
reflectDirection = VectorUtils.scalarMult(reflectDirection, s);
return new Ray(collisionInfo.position, reflectDirection);
}
// Calculate initial ray from the camera based on camera properties and pixel
function calculateCameraRay(x, y, camera) {
const i = x - width / 2;
const j = y - height / 2;
return new Ray(
camera.position,
VectorUtils.rotateVector(
VectorUtils.normalize({ x: i, y: j, z: camera.fov }),
camera.rotation,
),
);
}
// Render function to use the ray tracer
function rayTraceRender() {
const imageData = context.createImageData(width, height);
const data = imageData.data;
for (let s = 1; s <= samples; s++) {
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
const ray = calculateCameraRay(x, y, camera);
const color = trace(ray, 10);
const index = (y * width + x) * 4;
data[index] += color.x / samples;
data[index + 1] += color.y / samples;
data[index + 2] += color.z / samples;
data[index + 3] = 255;
}
}
console.clear();
console.log(s + " / " + samples);
}
context.putImageData(imageData, 0, 0);
}
// Simpler trace function, used when modifying material properties to have a lower impact on performance
function simpleTrace() {
const imageData = context.createImageData(width, height);
const data = imageData.data;
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
const ray = calculateCameraRay(x, y, camera);
let collisionInfo = null;
scene.getObjects().forEach((object) => {
const current = object.calcHit(ray);
if (current.intersect) {
if (
collisionInfo == null ||
collisionInfo.distance >= current.distance
) {
collisionInfo = current;
}
}
});
if (collisionInfo == null) {
continue;
} else {
// Only process one bounce of ray and color it with the objects color directly
const color = collisionInfo.obj.material.color;
const red = color.x * 255;
const green = color.y * 255;
const blue = color.z * 255;
const index = (y * width + x) * 4;
data[index] = red;
data[index + 1] = green;
data[index + 2] = blue;
data[index + 3] = 255;
}
}
}
context.putImageData(imageData, 0, 0);
}
let lastFrameTime = performance.now();
let currentFps = 0;
let count = 0;
// Calculate and draw fps in viewport mode
function drawFps() {
const currentTime = performance.now();
const deltaTime = currentTime - lastFrameTime;
lastFrameTime = currentTime;
context.font = "14px Arial";
if (count % 5 == 0) {
context.fillStyle = "orange";
currentFps = Math.round(1000 / deltaTime);
context.fillText(currentFps, 10, 20);
count == 0;
} else {
context.fillStyle = "orange";
context.fillText(Math.round(currentFps), 10, 20);
}
count += 1;
}
// Listener to render the image and stop movement triggers from activating during rendering
const renderBtn = document.getElementById("render");
renderBtn.onclick = (e) => {
render = !render;
if (!render) {
requestAnimationFrame(update);
}
renderBtn.blur();
};
let render = false;
// Viewport mode update function to keep moving
function update() {
context.fillStyle = "white";
context.fillRect(0, 0, canvas.width, canvas.height);
// If rendering, render and dont move or update frames
if (render) {
rayTraceRender();
} // Else use viewport mode rendering and move and draw frames and fps
else {
simpleTrace();
camera.move();
drawFps();
requestAnimationFrame(update);
}
}
requestAnimationFrame(update);