|
|
|
@ -18,7 +18,7 @@ export default class ThreeBSP
|
|
|
|
|
constructor(geometry)
|
|
|
|
|
{
|
|
|
|
|
// Convert THREE.Geometry to ThreeBSP
|
|
|
|
|
var i, _length_i,
|
|
|
|
|
let i, _length_i,
|
|
|
|
|
face, vertex, faceVertexUvs, uvs,
|
|
|
|
|
polygon,
|
|
|
|
|
polygons = [],
|
|
|
|
@ -111,7 +111,7 @@ export default class ThreeBSP
|
|
|
|
|
//减
|
|
|
|
|
subtract(other_tree)
|
|
|
|
|
{
|
|
|
|
|
var a = this.tree.clone(),
|
|
|
|
|
let a = this.tree.clone(),
|
|
|
|
|
b = other_tree.tree.clone();
|
|
|
|
|
|
|
|
|
|
a.invert();
|
|
|
|
@ -130,7 +130,7 @@ export default class ThreeBSP
|
|
|
|
|
//结合
|
|
|
|
|
union(other_tree)
|
|
|
|
|
{
|
|
|
|
|
var a = this.tree.clone(),
|
|
|
|
|
let a = this.tree.clone(),
|
|
|
|
|
b = other_tree.tree.clone();
|
|
|
|
|
|
|
|
|
|
a.clipTo(b);
|
|
|
|
@ -147,7 +147,7 @@ export default class ThreeBSP
|
|
|
|
|
//相交
|
|
|
|
|
intersect(other_tree)
|
|
|
|
|
{
|
|
|
|
|
var a = this.tree.clone(),
|
|
|
|
|
let a = this.tree.clone(),
|
|
|
|
|
b = other_tree.tree.clone();
|
|
|
|
|
|
|
|
|
|
a.invert();
|
|
|
|
@ -164,7 +164,7 @@ export default class ThreeBSP
|
|
|
|
|
|
|
|
|
|
toGeometry()
|
|
|
|
|
{
|
|
|
|
|
var i, j,
|
|
|
|
|
let i, j,
|
|
|
|
|
matrix = new THREE.Matrix4().getInverse(this.matrix),
|
|
|
|
|
geometry = new THREE.Geometry(),
|
|
|
|
|
polygons = this.tree.allPolygons(),
|
|
|
|
@ -241,7 +241,7 @@ export default class ThreeBSP
|
|
|
|
|
|
|
|
|
|
toMesh(material)
|
|
|
|
|
{
|
|
|
|
|
var geometry = this.toGeometry(),
|
|
|
|
|
let geometry = this.toGeometry(),
|
|
|
|
|
mesh = new THREE.Mesh(geometry, material);
|
|
|
|
|
|
|
|
|
|
mesh.position.setFromMatrixPosition(this.matrix);
|
|
|
|
@ -276,7 +276,7 @@ class Polygon
|
|
|
|
|
|
|
|
|
|
calculateProperties()
|
|
|
|
|
{
|
|
|
|
|
var a = this.vertices[0],
|
|
|
|
|
let a = this.vertices[0],
|
|
|
|
|
b = this.vertices[1],
|
|
|
|
|
c = this.vertices[2];
|
|
|
|
|
|
|
|
|
@ -291,7 +291,7 @@ class Polygon
|
|
|
|
|
|
|
|
|
|
clone()
|
|
|
|
|
{
|
|
|
|
|
var i, vertice_count,
|
|
|
|
|
let i, vertice_count,
|
|
|
|
|
polygon = new Polygon();
|
|
|
|
|
|
|
|
|
|
for (i = 0, vertice_count = this.vertices.length; i < vertice_count; i++)
|
|
|
|
@ -305,7 +305,7 @@ class Polygon
|
|
|
|
|
|
|
|
|
|
flip()
|
|
|
|
|
{
|
|
|
|
|
var i, vertices = [];
|
|
|
|
|
let i, vertices = [];
|
|
|
|
|
|
|
|
|
|
this.normal.multiplyScalar(-1);
|
|
|
|
|
this.w *= -1;
|
|
|
|
@ -322,7 +322,7 @@ class Polygon
|
|
|
|
|
//划分?
|
|
|
|
|
classifyVertex(vertex)
|
|
|
|
|
{
|
|
|
|
|
var side_value = this.normal.dot(vertex) - this.w;
|
|
|
|
|
let side_value = this.normal.dot(vertex) - this.w;
|
|
|
|
|
|
|
|
|
|
if (side_value < -EPSILON)
|
|
|
|
|
{
|
|
|
|
@ -339,7 +339,7 @@ class Polygon
|
|
|
|
|
//划分边?
|
|
|
|
|
classifySide(polygon)
|
|
|
|
|
{
|
|
|
|
|
var i, vertex, classification,
|
|
|
|
|
let i, vertex, classification,
|
|
|
|
|
num_positive = 0,
|
|
|
|
|
num_negative = 0,
|
|
|
|
|
vertice_count = polygon.vertices.length;
|
|
|
|
@ -375,7 +375,7 @@ class Polygon
|
|
|
|
|
//分解 分离 区域?
|
|
|
|
|
splitPolygon(polygon, coplanar_front, coplanar_back, front, back)
|
|
|
|
|
{
|
|
|
|
|
var classification = this.classifySide(polygon);
|
|
|
|
|
let classification = this.classifySide(polygon);
|
|
|
|
|
|
|
|
|
|
if (classification === COPLANAR)
|
|
|
|
|
{
|
|
|
|
@ -395,7 +395,7 @@ class Polygon
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var vertice_count,
|
|
|
|
|
let vertice_count,
|
|
|
|
|
i, j, ti, tj, vi, vj,
|
|
|
|
|
t, v,
|
|
|
|
|
f = [],
|
|
|
|
@ -477,7 +477,7 @@ class Vertex
|
|
|
|
|
//×乘
|
|
|
|
|
cross(vertex)
|
|
|
|
|
{
|
|
|
|
|
var x = this.x,
|
|
|
|
|
let x = this.x,
|
|
|
|
|
y = this.y,
|
|
|
|
|
z = this.z;
|
|
|
|
|
|
|
|
|
@ -490,7 +490,7 @@ class Vertex
|
|
|
|
|
|
|
|
|
|
normalize()
|
|
|
|
|
{
|
|
|
|
|
var length = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
|
|
|
|
let length = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
|
|
|
|
|
|
|
|
|
|
this.x /= length;
|
|
|
|
|
this.y /= length;
|
|
|
|
@ -534,9 +534,9 @@ class Vertex
|
|
|
|
|
|
|
|
|
|
// input: THREE.Matrix4 affine matrix
|
|
|
|
|
|
|
|
|
|
var x = this.x, y = this.y, z = this.z;
|
|
|
|
|
let x = this.x, y = this.y, z = this.z;
|
|
|
|
|
|
|
|
|
|
var e = m.elements;
|
|
|
|
|
let e = m.elements;
|
|
|
|
|
|
|
|
|
|
this.x = e[0] * x + e[4] * y + e[8] * z + e[12];
|
|
|
|
|
this.y = e[1] * x + e[5] * y + e[9] * z + e[13];
|
|
|
|
@ -554,7 +554,7 @@ class Node
|
|
|
|
|
polygons: any[];
|
|
|
|
|
constructor(polygons?)
|
|
|
|
|
{
|
|
|
|
|
var i, polygon_count,
|
|
|
|
|
let i, polygon_count,
|
|
|
|
|
front = [],
|
|
|
|
|
back = [];
|
|
|
|
|
|
|
|
|
@ -584,7 +584,7 @@ class Node
|
|
|
|
|
//是凸的? 凸包?
|
|
|
|
|
isConvex(polygons)
|
|
|
|
|
{
|
|
|
|
|
var i, j;
|
|
|
|
|
let i, j;
|
|
|
|
|
for (i = 0; i < polygons.length; i++)
|
|
|
|
|
{
|
|
|
|
|
for (j = 0; j < polygons.length; j++)
|
|
|
|
@ -600,7 +600,7 @@ class Node
|
|
|
|
|
|
|
|
|
|
build(polygons)
|
|
|
|
|
{
|
|
|
|
|
var i, polygon_count,
|
|
|
|
|
let i, polygon_count,
|
|
|
|
|
front = [],
|
|
|
|
|
back = [];
|
|
|
|
|
|
|
|
|
@ -629,7 +629,7 @@ class Node
|
|
|
|
|
|
|
|
|
|
allPolygons()
|
|
|
|
|
{
|
|
|
|
|
var polygons = this.polygons.slice();
|
|
|
|
|
let polygons = this.polygons.slice();
|
|
|
|
|
if (this.front) polygons = polygons.concat(this.front.allPolygons());
|
|
|
|
|
if (this.back) polygons = polygons.concat(this.back.allPolygons());
|
|
|
|
|
return polygons;
|
|
|
|
@ -637,7 +637,7 @@ class Node
|
|
|
|
|
|
|
|
|
|
clone()
|
|
|
|
|
{
|
|
|
|
|
var node = new Node();
|
|
|
|
|
let node = new Node();
|
|
|
|
|
|
|
|
|
|
node.divider = this.divider.clone();
|
|
|
|
|
node.polygons = this.polygons.map(function (polygon)
|
|
|
|
@ -653,7 +653,7 @@ class Node
|
|
|
|
|
//反转
|
|
|
|
|
invert()
|
|
|
|
|
{
|
|
|
|
|
var i, polygon_count, temp;
|
|
|
|
|
let i, polygon_count, temp;
|
|
|
|
|
|
|
|
|
|
for (i = 0, polygon_count = this.polygons.length; i < polygon_count; i++)
|
|
|
|
|
{
|
|
|
|
@ -674,7 +674,7 @@ class Node
|
|
|
|
|
//
|
|
|
|
|
clipPolygons(polygons)
|
|
|
|
|
{
|
|
|
|
|
var i, polygon_count,
|
|
|
|
|
let i, polygon_count,
|
|
|
|
|
front, back;
|
|
|
|
|
|
|
|
|
|
if (!this.divider) return polygons.slice();
|
|
|
|
@ -700,4 +700,4 @@ class Node
|
|
|
|
|
if (this.front) this.front.clipTo(node);
|
|
|
|
|
if (this.back) this.back.clipTo(node);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|