-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplicitTriangle.js
48 lines (38 loc) · 1.15 KB
/
implicitTriangle.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
const { vec3 } = require("gl-matrix");
// based on my implicit rt triangle
function hitTriangle(ray, record) {
const [originX, originY] = ray.origin;
const [dirX, dirY] = ray.dir;
let t0 = (1.0 - originY - originX) / (dirX + dirY);
let t1 = -(1.0 - originY + originX) / (dirX - dirY);
const x = 1.0 - (originY + t0 * dirY);
const y = 1.0 - (originY + t1 * dirY);
if (x <= 0) {
if (t0 > 0) {
// console.log('hit x && t0')
const p = ray.at(t0);
const out = vec3.create();
const normal = vec3.create();
vec3.sub(out, p, vec3.create());
vec3.normalize(normal, out);
record.p = p;
record.normal = normal;
return true;
}
} else if (y <= 0) {
if (t1 > 0) {
// console.log('hit y && t1')
const p = ray.at(t1);
const out = vec3.create();
const normal = vec3.create();
vec3.sub(out, p, vec3.create());
vec3.normalize(normal, out);
record.p = p;
record.normal = normal;
return true;
}
} else {
return false;
}
}
module.exports = {hitTriangle}