trip.three
Version:
Three.js addon for Trip
37 lines (29 loc) • 1.2 kB
JavaScript
;
var THREE = require('three');
var rayFromWorldAndCamera = require('./rayFromWorldAndCamera');
module.exports = function (worldPos, edge, camera) {
var ray = rayFromWorldAndCamera(worldPos, camera);
var from = edge[0];
var direction = new THREE.Vector3().subVectors(edge[1], edge[0]);
// http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm
var u = direction.clone().normalize();
var v = ray.direction;
var w0 = new THREE.Vector3().subVectors(from, ray.origin);
var a = u.dot(u);
var b = u.dot(v);
var c = v.dot(v);
var d = u.dot(w0);
var e = v.dot(w0);
var sc = (b * e - c * d) / (a * c - b * b);
// const tc = (a*e - b*d)/(a*c - b*b);
var psc = new THREE.Vector3().addVectors(from, u.clone().multiplyScalar(sc));
// const qtc = new THREE.Vector3().addVectors(mouseRay.origin, v.clone().multiplyScalar(tc));
// Check if one the edge or outside by checking the distance to the endpoints
// is less than the length of the edge
var l = direction.length();
if (new THREE.Vector3().subVectors(psc, edge[0]).length() <= l && new THREE.Vector3().subVectors(psc, edge[1]).length() <= l) {
return psc;
} else {
return null;
}
};