-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.js
319 lines (282 loc) · 9.75 KB
/
utility.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
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
// Vector Utils
export class VectorUtils {
// Returns magnitude of a given vector
static magnitude(v1) {
return Math.sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z);
}
// Normalizes any given vector
static normalize(v) {
return {
x: v.x / this.magnitude(v),
y: v.y / this.magnitude(v),
z: v.z / this.magnitude(v)
}
}
// Adds the components of 2 vectors individually
static add(v1, v2) {
return {
x: v1.x + v2.x,
y: v1.y + v2.y,
z: v1.z + v2.z
};
}
// Subtracts the components of 2 vectors individually
static sub(v1, v2) {
return {
x: v1.x - v2.x,
y: v1.y - v2.y,
z: v1.z - v2.z
};
}
// Multiplies the componenents of 2 vectors individually
static mult(v1, v2) {
return {
x: v1.x * v2.x,
y: v1.y * v2.y,
z: v1.z * v2.z
};
}
// Scales the given vector by a scalar quantity
static scalarMult(v, s) {
return {
x: v.x * s,
y: v.y * s,
z: v.z * s
};
}
// Returns the dot product of 2 vectors
static dotProduct(v1, v2) {
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}
// Rotates an input vector by a rotation using a rotation matrix
static rotateVector(vector, rotation) {
let { x, y, z } = vector;
let { x: alpha, y: beta, z: gamma } = rotation;
let rotatedX = x * Math.cos(gamma) * Math.cos(beta) +
y * (Math.cos(gamma) * Math.sin(beta) * Math.sin(alpha) - Math.sin(gamma) * Math.cos(alpha)) +
z * (Math.cos(gamma) * Math.sin(beta) * Math.cos(alpha) + Math.sin(gamma) * Math.sin(alpha));
let rotatedY = x * Math.sin(gamma) * Math.cos(beta) +
y * (Math.sin(gamma) * Math.sin(beta) * Math.sin(alpha) + Math.cos(gamma) * Math.cos(alpha)) +
z * (Math.sin(gamma) * Math.sin(beta) * Math.cos(alpha) - Math.cos(gamma) * Math.sin(alpha));
let rotatedZ = -x * Math.sin(beta) +
y * Math.cos(beta) * Math.sin(alpha) +
z * Math.cos(beta) * Math.cos(alpha);
return {
x: rotatedX,
y: rotatedY,
z: rotatedZ
};
}
}
// Ray
export class Ray {
constructor(position, direction) {
this.position = position;
this.direction = direction;
}
}
// Scene
export class Scene {
constructor() {
this.objectList = [];
}
add(obj) {
this.objectList.push(obj);
}
getObjects() {
return this.objectList;
}
}
let sphereCount = 0;
// Geometry
export class Sphere {
constructor(params) {
if(!params) params = {};
this.material = params.material? params.material : new Material();
this.radius = params.radius? params.radius : 1;
this.position = params.position? params.position : {x: 0, y: 0, z: 0};
this.id = sphereCount++;
}
calcHit(ray) {
// Vector from ray origin to center of sphere
let vecToCenter = VectorUtils.sub(this.position, ray.position);
// Magnitude of the calculated vector
let distToCenter = VectorUtils.magnitude(vecToCenter);
// Magnitude of projection of origin-center ray on ray's direction
let projected = VectorUtils.dotProduct(vecToCenter, ray.direction);
// Distance from origin of ray to the surface of sphere along the ray direction (Pythagoras)
let distToSurface = projected - Math.sqrt(Math.abs(this.radius ** 2 - distToCenter ** 2 + projected ** 2));
// Consider ray intersected only if intersection is in front of camera and if the hit point is within the sphere
let hasHit = (projected >= 0) && ((distToCenter ** 2 - projected ** 2) <= this.radius ** 2);
// Calculate point of intersection
let hitPoint = VectorUtils.add(ray.position, VectorUtils.scalarMult(ray.direction, distToSurface));
// Calculate normals
let normal = VectorUtils.normalize(VectorUtils.sub(hitPoint, this.position))
return {
intersect: hasHit,
distance: distToSurface,
position: hitPoint,
normal: normal,
obj: this
};
}
}
// Material
export class Material {
constructor(params) {
if(!params) params = {};
this.color = params.color? params.color : {x: 1, y: 1, z: 1};
this.emissiveColor = params.emissiveColor? params.emissiveColor : {x: 0, y: 0, z: 0};
this.emissiveStrength = params.emissiveStrength? params.emissiveStrength : 0;
this.roughness = params.roughness? params.roughness : 1;
}
}
// Camera
export class Camera {
constructor(params) {
this.position = params.position? params.position : {x: 0, y: 0, z: -10};
this.rotation = params.rotation? params.rotation : {x: 0, y: 0, z: 0};
this.fov = params.fov? params.fov : 200;
this.sensitivity = params.sensitivity? params.sensitivity : 0.5;
// Camera movement
this.left = false;
this.right = false;
this.up = false;
this.down = false;
this.w = false;
this.a = false;
this.s = false;
this.d = false;
this.space = false;
this.shift = false;
this.setupCameraListeners();
}
setupCameraListeners() {
document.addEventListener('keydown', (event) => {
if (event.key === 'ArrowLeft') {
this.left = true;
this.right = false;
}
if (event.key === 'ArrowRight') {
this.right = true;
this.left = false;
}
if (event.key === 'ArrowUp') {
this.up = true;
this.down = false;
}
if (event.key === 'ArrowDown') {
this.down = true;
this.up = false;
}
if(event.key === 'w') {
this.w = true;
this.s = false;
}
if(event.key === 'a') {
this.a = true;
this.d = false;
}
if(event.key === 's') {
this.s = true;
this.w = false;
}
if(event.key === 'd') {
this.d = true;
this.a = false;
}
if(event.key === ' ') {
this.space = true;
this.shift = false;
}
if(event.key === 'Shift') {
this.shift = true;
this.space = false;
}
});
document.addEventListener('keyup', (event) => {
if (event.key === 'ArrowLeft') {
this.left = false;
}
if (event.key === 'ArrowRight') {
this.right = false;
}
if (event.key === 'ArrowUp') {
this.up = false;
}
if (event.key === 'ArrowDown') {
this.down = false;
}
if(event.key === 'w') {
this.w = false;
}
if(event.key === 'a') {
this.a = false;
}
if(event.key === 's') {
this.s = false;
}
if(event.key === 'd') {
this.d = false;
}
if(event.key === ' ') {
this.space = false;
}
if(event.key === 'Shift') {
this.shift = false;
}
});
}
move() {
if(this.left) {
this.rotation.y -= 0.1 * this.sensitivity;
}
if(this.right) {
this.rotation.y += 0.1 * this.sensitivity;
}
if(this.up) {
this.rotation.x += 0.1 * this.sensitivity;
}
if(this.down) {
this.rotation.x -= 0.1 * this.sensitivity;
}
if(this.w) {
const direction = {
x: Math.sin(this.rotation.y),
y: Math.cos(this.rotation.y) * Math.sin(this.rotation.z),
z: Math.cos(this.rotation.y) * Math.cos(this.rotation.z)
};
this.position = VectorUtils.add(this.position, VectorUtils.scalarMult(direction, this.sensitivity));
}
if(this.a) {
const direction = {
x: Math.sin(this.rotation.y + Math.PI/2),
y: Math.cos(this.rotation.y + Math.PI/2) * Math.sin(this.rotation.z),
z: Math.cos(this.rotation.y + Math.PI/2) * Math.cos(this.rotation.z)
};
this.position = VectorUtils.sub(this.position, VectorUtils.scalarMult(direction, this.sensitivity));
}
if(this.s) {
const direction = {
x: Math.sin(this.rotation.y),
y: Math.cos(this.rotation.y) * Math.sin(this.rotation.z),
z: Math.cos(this.rotation.y) * Math.cos(this.rotation.z)
};
this.position = VectorUtils.sub(this.position, VectorUtils.scalarMult(direction, this.sensitivity));
}
if(this.d) {
const direction = {
x: Math.sin(this.rotation.y + Math.PI/2),
y: Math.cos(this.rotation.y + Math.PI/2) * Math.sin(this.rotation.z),
z: Math.cos(this.rotation.y + Math.PI/2) * Math.cos(this.rotation.z)
};
this.position = VectorUtils.add(this.position, VectorUtils.scalarMult(direction, this.sensitivity));
}
if(this.space) {
this.position.y -= this.sensitivity;
}
if(this.shift) {
this.position.y += this.sensitivity;
}
}
}