|
@@ -15,7 +15,10 @@ var BABYLON = BABYLON || {};
|
|
|
|
|
|
// Convert BABYLON.Mesh to BABYLON.CSG
|
|
|
BABYLON.CSG.FromMesh = function (mesh) {
|
|
|
- var vertex, normal, uv, position, polygon, polygons = [], vertices;
|
|
|
+ var vertex, normal, uv, position,
|
|
|
+ polygon,
|
|
|
+ polygons = [],
|
|
|
+ vertices;
|
|
|
|
|
|
if (mesh instanceof BABYLON.Mesh) {
|
|
|
mesh.computeWorldMatrix(true);
|
|
@@ -49,7 +52,11 @@ var BABYLON = BABYLON || {};
|
|
|
}
|
|
|
|
|
|
polygon = new BABYLON.CSG.Polygon(vertices, { subMeshId: sm, meshId: _currentCSGMeshId, materialIndex: subMeshes[sm].materialIndex });
|
|
|
- polygons.push(polygon);
|
|
|
+
|
|
|
+ // To handle the case of degenerated triangle
|
|
|
+ // polygon.plane == null <=> the polygon does not represent 1 single plane <=> the triangle is degenerated
|
|
|
+ if (polygon.plane)
|
|
|
+ polygons.push(polygon);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -333,6 +340,10 @@ var BABYLON = BABYLON || {};
|
|
|
return Math.sqrt(this.dot(this));
|
|
|
},
|
|
|
|
|
|
+ lengthSq: function () {
|
|
|
+ return this.dot(this);
|
|
|
+ },
|
|
|
+
|
|
|
unit: function () {
|
|
|
return this.dividedBy(this.length());
|
|
|
},
|
|
@@ -400,6 +411,13 @@ var BABYLON = BABYLON || {};
|
|
|
BABYLON.CSG.Plane.EPSILON = 1e-5;
|
|
|
|
|
|
BABYLON.CSG.Plane.fromPoints = function (a, b, c) {
|
|
|
+ var v0 = c.minus(a);
|
|
|
+ var v1 = b.minus(a);
|
|
|
+
|
|
|
+ if (v0.lengthSq() === 0 || v1.lengthSq() === 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
var n = c.minus(a).cross(b.minus(a)).unit();
|
|
|
return new BABYLON.CSG.Plane(n, n.dot(a));
|
|
|
};
|
|
@@ -484,6 +502,7 @@ var BABYLON = BABYLON || {};
|
|
|
this.vertices = vertices;
|
|
|
this.shared = shared;
|
|
|
this.plane = BABYLON.CSG.Plane.fromPoints(vertices[0].pos, vertices[1].pos, vertices[2].pos);
|
|
|
+
|
|
|
};
|
|
|
|
|
|
BABYLON.CSG.Polygon.prototype = {
|
|
@@ -530,8 +549,12 @@ var BABYLON = BABYLON || {};
|
|
|
this.polygons[i].flip();
|
|
|
}
|
|
|
this.plane.flip();
|
|
|
- if (this.front) this.front.invert();
|
|
|
- if (this.back) this.back.invert();
|
|
|
+ if (this.front) {
|
|
|
+ this.front.invert();
|
|
|
+ }
|
|
|
+ if (this.back) {
|
|
|
+ this.back.invert();
|
|
|
+ }
|
|
|
var temp = this.front;
|
|
|
this.front = this.back;
|
|
|
this.back = temp;
|
|
@@ -545,7 +568,9 @@ var BABYLON = BABYLON || {};
|
|
|
for (var i = 0; i < polygons.length; i++) {
|
|
|
this.plane.splitPolygon(polygons[i], front, back, front, back);
|
|
|
}
|
|
|
- if (this.front) front = this.front.clipPolygons(front);
|
|
|
+ if (this.front) {
|
|
|
+ front = this.front.clipPolygons(front);
|
|
|
+ }
|
|
|
if (this.back) {
|
|
|
back = this.back.clipPolygons(back);
|
|
|
} else {
|
|
@@ -591,5 +616,4 @@ var BABYLON = BABYLON || {};
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
-
|
|
|
})();
|