cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
95 lines (94 loc) • 2.86 kB
JavaScript
import Vector3 from "./Vector3.mjs";
import mat4 from "../glmatrix/mat4.mjs";
import vec3 from "../glmatrix/vec3.mjs";
import vec4 from "../glmatrix/vec4.mjs";
var Plane = function(normal, distance) {
this.normal = normal || new Vector3(0, 1, 0);
this.distance = distance || 0;
};
Plane.prototype = {
constructor: Plane,
distanceToPoint: function(point) {
return vec3.dot(point.array, this.normal.array) - this.distance;
},
projectPoint: function(point, out) {
if (!out) {
out = new Vector3();
}
var d = this.distanceToPoint(point);
vec3.scaleAndAdd(out.array, point.array, this.normal.array, -d);
out._dirty = true;
return out;
},
normalize: function() {
var invLen = 1 / vec3.len(this.normal.array);
vec3.scale(this.normal.array, invLen);
this.distance *= invLen;
},
intersectFrustum: function(frustum) {
var coords = frustum.vertices;
var normal = this.normal.array;
var onPlane = vec3.dot(coords[0].array, normal) > this.distance;
for (var i = 1; i < 8; i++) {
if (vec3.dot(coords[i].array, normal) > this.distance != onPlane) {
return true;
}
}
},
intersectLine: function() {
var rd = vec3.create();
return function(start, end, out) {
var d0 = this.distanceToPoint(start);
var d1 = this.distanceToPoint(end);
if (d0 > 0 && d1 > 0 || d0 < 0 && d1 < 0) {
return null;
}
var pn = this.normal.array;
var d = this.distance;
var ro = start.array;
vec3.sub(rd, end.array, start.array);
vec3.normalize(rd, rd);
var divider = vec3.dot(pn, rd);
if (divider === 0) {
return null;
}
if (!out) {
out = new Vector3();
}
var t = (vec3.dot(pn, ro) - d) / divider;
vec3.scaleAndAdd(out.array, ro, rd, -t);
out._dirty = true;
return out;
};
}(),
applyTransform: function() {
var inverseTranspose = mat4.create();
var normalv4 = vec4.create();
var pointv4 = vec4.create();
pointv4[3] = 1;
return function(m4) {
m4 = m4.array;
vec3.scale(pointv4, this.normal.array, this.distance);
vec4.transformMat4(pointv4, pointv4, m4);
this.distance = vec3.dot(pointv4, this.normal.array);
mat4.invert(inverseTranspose, m4);
mat4.transpose(inverseTranspose, inverseTranspose);
normalv4[3] = 0;
vec3.copy(normalv4, this.normal.array);
vec4.transformMat4(normalv4, normalv4, inverseTranspose);
vec3.copy(this.normal.array, normalv4);
};
}(),
copy: function(plane) {
vec3.copy(this.normal.array, plane.normal.array);
this.normal._dirty = true;
this.distance = plane.distance;
},
clone: function() {
var plane = new Plane();
plane.copy(this);
return plane;
}
};
var Plane$1 = Plane;
export { Plane$1 as default };