更新编译输出为es5支持老旧的浏览器

This commit is contained in:
ChenX
2018-06-06 17:23:22 +08:00
parent 256a7ea8a6
commit 29a6dd0fff
38 changed files with 616 additions and 533 deletions

198
dist/ThreeCSG.js vendored
View File

@@ -1,14 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const THREE = require("three");
var THREE = require("three");
/*jshint esversion: 6 */
const EPSILON = 1e-5, COPLANAR = 0, //共面
var EPSILON = 1e-5, COPLANAR = 0, //共面
FRONT = 1, //前
BACK = 2, SPANNING = 3;
class ThreeBSP {
constructor(geometry) {
var ThreeBSP = /** @class */ (function () {
function ThreeBSP(geometry) {
// Convert THREE.Geometry to ThreeBSP
let i, _length_i, face, vertex, faceVertexUvs, uvs, polygon, polygons = [], tree;
var i, _length_i, face, vertex, faceVertexUvs, uvs, polygon, polygons = [], tree;
this.Polygon = Polygon;
this.Vertex = Vertex;
this.Node = Node;
@@ -81,8 +81,8 @@ class ThreeBSP {
this.tree = new Node(polygons);
}
//减
subtract(other_tree) {
let a = this.tree.clone(), b = other_tree.tree.clone();
ThreeBSP.prototype.subtract = function (other_tree) {
var a = this.tree.clone(), b = other_tree.tree.clone();
a.invert();
a.clipTo(b);
b.clipTo(a);
@@ -91,26 +91,26 @@ class ThreeBSP {
b.invert();
a.build(b.allPolygons());
a.invert();
let bsp = new ThreeBSP(a);
var bsp = new ThreeBSP(a);
bsp.matrix = this.matrix;
return bsp;
}
};
//结合
union(other_tree) {
let a = this.tree.clone(), b = other_tree.tree.clone();
ThreeBSP.prototype.union = function (other_tree) {
var a = this.tree.clone(), b = other_tree.tree.clone();
a.clipTo(b);
b.clipTo(a);
b.invert();
b.clipTo(a);
b.invert();
a.build(b.allPolygons());
let bsp = new ThreeBSP(a);
var bsp = new ThreeBSP(a);
bsp.matrix = this.matrix;
return bsp;
}
};
//相交
intersect(other_tree) {
let a = this.tree.clone(), b = other_tree.tree.clone();
ThreeBSP.prototype.intersect = function (other_tree) {
var a = this.tree.clone(), b = other_tree.tree.clone();
a.invert();
b.clipTo(a);
b.invert();
@@ -118,12 +118,12 @@ class ThreeBSP {
b.clipTo(a);
a.build(b.allPolygons());
a.invert();
let bsp = new ThreeBSP(a);
var bsp = new ThreeBSP(a);
bsp.matrix = this.matrix;
return bsp;
}
toGeometry() {
let i, j, matrix = new THREE.Matrix4().getInverse(this.matrix), geometry = new THREE.Geometry(), polygons = this.tree.allPolygons(), polygon_count = polygons.length, polygon, polygon_vertice_count, vertice_dict = {}, vertex_idx_a, vertex_idx_b, vertex_idx_c, vertex, face, verticeUvs;
};
ThreeBSP.prototype.toGeometry = function () {
var i, j, matrix = new THREE.Matrix4().getInverse(this.matrix), geometry = new THREE.Geometry(), polygons = this.tree.allPolygons(), polygon_count = polygons.length, polygon, polygon_vertice_count, vertice_dict = {}, vertex_idx_a, vertex_idx_b, vertex_idx_c, vertex, face, verticeUvs;
for (i = 0; i < polygon_count; i++) {
polygon = polygons[i];
polygon_vertice_count = polygon.vertices.length;
@@ -168,18 +168,19 @@ class ThreeBSP {
}
}
return geometry;
}
toMesh(material) {
let geometry = this.toGeometry(), mesh = new THREE.Mesh(geometry, material);
};
ThreeBSP.prototype.toMesh = function (material) {
var geometry = this.toGeometry(), mesh = new THREE.Mesh(geometry, material);
mesh.position.setFromMatrixPosition(this.matrix);
mesh.rotation.setFromRotationMatrix(this.matrix);
return mesh;
}
}
};
return ThreeBSP;
}());
exports.ThreeBSP = ThreeBSP;
//多边形
class Polygon {
constructor(vertices, normal, w) {
var Polygon = /** @class */ (function () {
function Polygon(vertices, normal, w) {
if (!(vertices instanceof Array)) {
vertices = [];
}
@@ -191,22 +192,22 @@ class Polygon {
this.normal = this.w = undefined;
}
}
calculateProperties() {
let a = this.vertices[0], b = this.vertices[1], c = this.vertices[2];
Polygon.prototype.calculateProperties = function () {
var a = this.vertices[0], b = this.vertices[1], c = this.vertices[2];
this.normal = b.clone().subtract(a).cross(c.clone().subtract(a)).normalize();
this.w = this.normal.clone().dot(a);
return this;
}
clone() {
let i, vertice_count, polygon = new Polygon();
};
Polygon.prototype.clone = function () {
var i, vertice_count, polygon = new Polygon();
for (i = 0, vertice_count = this.vertices.length; i < vertice_count; i++) {
polygon.vertices.push(this.vertices[i].clone());
}
polygon.calculateProperties();
return polygon;
}
flip() {
let i, vertices = [];
};
Polygon.prototype.flip = function () {
var i, vertices = [];
this.normal.multiplyScalar(-1);
this.w *= -1;
for (i = this.vertices.length - 1; i >= 0; i--) {
@@ -214,10 +215,10 @@ class Polygon {
}
this.vertices = vertices;
return this;
}
};
//划分?
classifyVertex(vertex) {
let side_value = this.normal.dot(vertex) - this.w;
Polygon.prototype.classifyVertex = function (vertex) {
var side_value = this.normal.dot(vertex) - this.w;
if (side_value < -EPSILON) {
return BACK;
}
@@ -227,10 +228,10 @@ class Polygon {
else {
return COPLANAR;
}
}
};
//划分边?
classifySide(polygon) {
let i, vertex, classification, num_positive = 0, num_negative = 0, vertice_count = polygon.vertices.length;
Polygon.prototype.classifySide = function (polygon) {
var i, vertex, classification, num_positive = 0, num_negative = 0, vertice_count = polygon.vertices.length;
for (i = 0; i < vertice_count; i++) {
vertex = polygon.vertices[i];
classification = this.classifyVertex(vertex);
@@ -253,10 +254,10 @@ class Polygon {
else {
return SPANNING;
}
}
};
//分解 分离 区域?
splitPolygon(polygon, coplanar_front, coplanar_back, front, back) {
let classification = this.classifySide(polygon);
Polygon.prototype.splitPolygon = function (polygon, coplanar_front, coplanar_back, front, back) {
var classification = this.classifySide(polygon);
if (classification === COPLANAR) {
(this.normal.dot(polygon.normal) > 0 ? coplanar_front : coplanar_back).push(polygon);
}
@@ -267,7 +268,7 @@ class Polygon {
back.push(polygon);
}
else {
let vertice_count, i, j, ti, tj, vi, vj, t, v, f = [], b = [];
var vertice_count = void 0, i = void 0, j = void 0, ti = void 0, tj = void 0, vi = void 0, vj = void 0, t = void 0, v = void 0, f = [], b = [];
for (i = 0, vertice_count = polygon.vertices.length; i < vertice_count; i++) {
j = (i + 1) % vertice_count;
vi = polygon.vertices[i];
@@ -290,82 +291,84 @@ class Polygon {
if (b.length >= 3)
back.push(new Polygon(b).calculateProperties());
}
}
}
};
return Polygon;
}());
exports.Polygon = Polygon;
class Vertex {
constructor(x, y, z, normal, uv) {
var Vertex = /** @class */ (function () {
function Vertex(x, y, z, normal, uv) {
this.x = x;
this.y = y;
this.z = z;
this.normal = normal || new THREE.Vector3();
this.uv = uv || new THREE.Vector2();
}
clone() {
Vertex.prototype.clone = function () {
return new Vertex(this.x, this.y, this.z, this.normal.clone(), this.uv.clone());
}
add(vertex) {
};
Vertex.prototype.add = function (vertex) {
this.x += vertex.x;
this.y += vertex.y;
this.z += vertex.z;
return this;
}
subtract(vertex) {
};
Vertex.prototype.subtract = function (vertex) {
this.x -= vertex.x;
this.y -= vertex.y;
this.z -= vertex.z;
return this;
}
multiplyScalar(scalar) {
};
Vertex.prototype.multiplyScalar = function (scalar) {
this.x *= scalar;
this.y *= scalar;
this.z *= scalar;
return this;
}
};
//×乘
cross(vertex) {
let x = this.x, y = this.y, z = this.z;
Vertex.prototype.cross = function (vertex) {
var x = this.x, y = this.y, z = this.z;
this.x = y * vertex.z - z * vertex.y;
this.y = z * vertex.x - x * vertex.z;
this.z = x * vertex.y - y * vertex.x;
return this;
}
normalize() {
let length = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
};
Vertex.prototype.normalize = function () {
var length = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
this.x /= length;
this.y /= length;
this.z /= length;
return this;
}
};
//点乘
dot(vertex) {
Vertex.prototype.dot = function (vertex) {
return this.x * vertex.x + this.y * vertex.y + this.z * vertex.z;
}
};
//线性插值
lerp(a, t) {
Vertex.prototype.lerp = function (a, t) {
this.add(a.clone().subtract(this).multiplyScalar(t));
this.normal.add(a.normal.clone().sub(this.normal).multiplyScalar(t));
this.uv.add(a.uv.clone().sub(this.uv).multiplyScalar(t));
return this;
}
};
//插值
interpolate(other, t) {
Vertex.prototype.interpolate = function (other, t) {
return this.clone().lerp(other, t);
}
applyMatrix4(m) {
};
Vertex.prototype.applyMatrix4 = function (m) {
// input: THREE.Matrix4 affine matrix
let x = this.x, y = this.y, z = this.z;
let e = m.elements;
var x = this.x, y = this.y, z = this.z;
var 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];
this.z = e[2] * x + e[6] * y + e[10] * z + e[14];
return this;
}
}
};
return Vertex;
}());
exports.Vertex = Vertex;
class Node {
constructor(polygons) {
let i, polygon_count, front = [], back = [];
var Node = /** @class */ (function () {
function Node(polygons) {
var i, polygon_count, front = [], back = [];
this.polygons = [];
this.front = this.back = undefined;
if (!(polygons instanceof Array) || polygons.length === 0)
@@ -382,8 +385,8 @@ class Node {
}
}
//是凸的? 凸包?
isConvex(polygons) {
let i, j;
Node.prototype.isConvex = function (polygons) {
var i, j;
for (i = 0; i < polygons.length; i++) {
for (j = 0; j < polygons.length; j++) {
if (i !== j && polygons[i].classifySide(polygons[j]) !== BACK) {
@@ -392,9 +395,9 @@ class Node {
}
}
return true;
}
build(polygons) {
let i, polygon_count, front = [], back = [];
};
Node.prototype.build = function (polygons) {
var i, polygon_count, front = [], back = [];
if (!this.divider) {
this.divider = polygons[0].clone();
}
@@ -411,17 +414,17 @@ class Node {
this.back = new Node();
this.back.build(back);
}
}
allPolygons() {
let polygons = this.polygons.slice();
};
Node.prototype.allPolygons = function () {
var polygons = this.polygons.slice();
if (this.front)
polygons = polygons.concat(this.front.allPolygons());
if (this.back)
polygons = polygons.concat(this.back.allPolygons());
return polygons;
}
clone() {
let node = new Node();
};
Node.prototype.clone = function () {
var node = new Node();
node.divider = this.divider.clone();
node.polygons = this.polygons.map(function (polygon) {
return polygon.clone();
@@ -429,10 +432,10 @@ class Node {
node.front = this.front && this.front.clone();
node.back = this.back && this.back.clone();
return node;
}
};
//反转
invert() {
let i, polygon_count, temp;
Node.prototype.invert = function () {
var i, polygon_count, temp;
for (i = 0, polygon_count = this.polygons.length; i < polygon_count; i++) {
this.polygons[i].flip();
}
@@ -445,10 +448,10 @@ class Node {
this.front = this.back;
this.back = temp;
return this;
}
};
//
clipPolygons(polygons) {
let i, polygon_count, front, back;
Node.prototype.clipPolygons = function (polygons) {
var i, polygon_count, front, back;
if (!this.divider)
return polygons.slice();
front = [];
@@ -463,14 +466,15 @@ class Node {
else
back = [];
return front.concat(back);
}
clipTo(node) {
};
Node.prototype.clipTo = function (node) {
this.polygons = node.clipPolygons(this.polygons);
if (this.front)
this.front.clipTo(node);
if (this.back)
this.back.clipTo(node);
}
}
};
return Node;
}());
exports.Node = Node;
//# sourceMappingURL=ThreeCSG.js.map