var Vec2 = require('./Vec2');
var Vec3 = require('./Vec3');
A plane represented by a point and a normal vector perpendicular to the plane’s surface.
Methematical construct not a 3d geometry mesh.
var plane = new Plane(new Vec3(0, 0, 0), new Vec3(0, 1, 0))
var projectedPoint = plane.project(new Vec3(1,2,3));
var Vec2 = require('./Vec2');
var Vec3 = require('./Vec3');
function Plane(point, normal) {
this.point = point;
this.normal = normal;
this.u = new Vec3(); //?
this.v = new Vec3(); //?
this.updateUV();
}
Plane.prototype.set = function(point, normal) {
this.point = point;
this.normal = normal;
this.updateUV();
}
Plane.prototype.setPoint = function(point) {
this.point = point;
this.updateUV();
}
Plane.prototype.setNormal = function(normal) {
this.normal = normal;
this.updateUV();
}
Plane.prototype.project = function(p) {
var D = Vec3.create().asSub(p, this.point);
var scale = D.dot(this.normal);
var scaled = this.normal.clone().scale(scale);
var projected = p.clone().sub(scaled);
return projected;
}
Test ray plane intersectionray
- { Ray }
Returns array with one element - the intersection point, or empty array if the ray is parallel to the plane
Plane.prototype.intersectRay = function(ray) {
return ray.hitTestPlane(this.point, this.normal)[0];
}
Plane.prototype.rebase = function(p) {
var diff = p.dup().sub(this.point);
var x = this.u.dot(diff);
var y = this.v.dot(diff);
return new Vec2(x, y);
}
Plane.prototype.updateUV = function() {
if (Math.abs(this.normal.x) > Math.abs(this.normal.y)) {
var invLen = 1 / Math.sqrt(this.normal.x * this.normal.x + this.normal.z * this.normal.z);
this.u.set( this.normal.x * invLen, 0, -this.normal.z * invLen);
}
else {
var invLen = 1 / Math.sqrt(this.normal.y * this.normal.y + this.normal.z * this.normal.z);
this.u.set( 0, this.normal.z * invLen, -this.normal.y * invLen);
}
this.v.setVec3(this.normal).cross(this.u);
}
module.exports = Plane;