playcanvas
Version:
PlayCanvas WebGL game engine
56 lines (53 loc) • 1.25 kB
JavaScript
import { Vec3 } from '../math/vec3.js';
var e1 = new Vec3();
var e2 = new Vec3();
var h = new Vec3();
var s = new Vec3();
var q = new Vec3();
var EPSILON = 1e-6;
class Tri {
set(v0, v1, v2) {
this.v0.copy(v0);
this.v1.copy(v1);
this.v2.copy(v2);
return this;
}
intersectsRay(ray, point) {
e1.sub2(this.v1, this.v0);
e2.sub2(this.v2, this.v0);
h.cross(ray.direction, e2);
var a = e1.dot(h);
if (a > -1e-6 && a < EPSILON) {
return false;
}
var f = 1 / a;
s.sub2(ray.origin, this.v0);
var u = f * s.dot(h);
if (u < 0 || u > 1) {
return false;
}
q.cross(s, e1);
var v = f * ray.direction.dot(q);
if (v < 0 || u + v > 1) {
return false;
}
var t = f * e2.dot(q);
if (t > EPSILON) {
if (point instanceof Vec3) {
point.copy(ray.direction).mulScalar(t).add(ray.origin);
}
return true;
}
return false;
}
toString() {
return "[" + this.v0.toString() + ", " + this.v1.toString() + ", " + this.v2.toString() + "]";
}
constructor(v0 = Vec3.ZERO, v1 = Vec3.ZERO, v2 = Vec3.ZERO){
this.v0 = new Vec3();
this.v1 = new Vec3();
this.v2 = new Vec3();
this.set(v0, v1, v2);
}
}
export { Tri };