From d4efa20e8f3d98cc24c824d4367d55e44ecc3a6f Mon Sep 17 00:00:00 2001 From: LinJiarui Date: Thu, 5 Mar 2015 21:56:45 +0800 Subject: [PATCH] fix csg build resolve the following issue: when build 2 or more coplanar polygons, the original method maybe split all the polygons into the front node (or the back node), thus leads to stackoverflow. --- csg.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/csg.js b/csg.js index 17b7a2f..0da992a 100644 --- a/csg.js +++ b/csg.js @@ -578,10 +578,17 @@ CSG.Node.prototype = { // (no heuristic is used to pick a good split). build: function(polygons) { if (!polygons.length) return; - if (!this.plane) this.plane = polygons[0].plane.clone(); + var myPolygon; + if (!this.plane) { + myPolygon = polygons[0]; + this.plane = polygons[0].plane.clone(); + this.polygons.push(myPolygon); + } var front = [], back = []; for (var i = 0; i < polygons.length; i++) { - this.plane.splitPolygon(polygons[i], this.polygons, this.polygons, front, back); + if (polygons !== myPolygon){ + this.plane.splitPolygon(polygons[i], this.polygons, this.polygons, front, back); + } } if (front.length) { if (!this.front) this.front = new CSG.Node();