通过git上的ThreeBSP.coffee转换为js代码,有需要的拿走不谢。
// Generated by CoffeeScript 1.12.7
(function() {
var BACK, COPLANAR, EPSILON, FRONT, SPANNING, Timelimit, returning,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
slice = [].slice,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
EPSILON = 1e-5;
COPLANAR = 0;
FRONT = 1;
BACK = 2;
SPANNING = 3;
returning = function(value, fn) {
fn();
return value;
};
Timelimit = (function() {
function Timelimit(timeout, progress) {
this.timeout = timeout;
this.progress = progress;
this.doTask = bind(this.doTask, this);
this.finish = bind(this.finish, this);
this.start = bind(this.start, this);
this.check = bind(this.check, this);
"NOTHING";
}
Timelimit.prototype.check = function() {
var elapsed;
if (this.started == null) {
return;
}
return returning((elapsed = Date.now() - this.started), (function(_this) {
return function() {
var ref, ref1, ref2;
if ((ref = elapsed >= _this.timeout) != null ? ref : 2e308) {
throw new Error("Timeout reached: " + elapsed + "/" + _this.timeout + ", " + ((ref1 = _this.tasks) != null ? ref1 : 0) + " tasks unfinished " + ((ref2 = _this.done) != null ? ref2 : 0) + " finished.");
}
};
})(this));
};
Timelimit.prototype.start = function() {
if (this.started == null) {
this.started = Date.now();
}
if (this.tasks == null) {
this.tasks = 0;
}
if (this.total == null) {
this.total = 0;
}
this.total += 1;
this.tasks += 1;
return this.check();
};
Timelimit.prototype.finish = function() {
var elapsed;
if ((this.tasks != null) && this.tasks < 1) {
throw new Error("Finished more tasks than started");
}
this.tasks -= 1;
elapsed = this.check();
if (this.done == null) {
this.done = 0;
}
this.done += 1;
if (this.progress != null) {
this.progress(this.done, this.total);
}
if (this.tasks === 0) {
"Finished " + this.done + " tasks in " + elapsed + "/" + this.timeout + " ms";
return this.started = this.done = this.total = void 0;
}
};
Timelimit.prototype.doTask = function(block) {
var result;
this.start();
result = block();
this.finish();
return result;
};
return Timelimit;
})();
window.ThreeBSP = (function() {
function ThreeBSP(treeIsh, matrix1, options) {
var base, ref, ref1, ref2, ref3;
this.matrix = matrix1;
this.options = options != null ? options : {};
this.intersect = bind(this.intersect, this);
this.union = bind(this.union, this);
this.subtract = bind(this.subtract, this);
this.toGeometry = bind(this.toGeometry, this);
this.toMesh = bind(this.toMesh, this);
this.toTree = bind(this.toTree, this);
this.withTimer = bind(this.withTimer, this);
if ((this.matrix != null) && !(this.matrix instanceof THREE.Matrix4)) {
this.options = this.matrix;
this.matrix = void 0;
}
if (this.options == null) {
this.options = {};
}
if (this.matrix == null) {
this.matrix = new THREE.Matrix4();
}
if ((base = this.options).timer == null) {
base.timer = new Timelimit((ref = (ref1 = this.options.timer) != null ? ref1.timeout : void 0) != null ? ref : this.options.timeout, (ref2 = (ref3 = this.options.timer) != null ? ref3.progress : void 0) != null ? ref2 : this.options.progress);
}
this.tree = this.toTree(treeIsh);
}
ThreeBSP.prototype.withTimer = function(new_timer, block) {
var old_timer;
old_timer = this.options.timer;
try {
this.options.timer = new_timer;
return block();
} finally {
this.options.timer = old_timer;
}
};
ThreeBSP.prototype.toTree = function(treeIsh) {
var face, fn1, geometry, i, k, len, polygons, ref;
if (treeIsh instanceof ThreeBSP.Node) {
return treeIsh;
}
polygons = [];
geometry = treeIsh instanceof THREE.Geom