Skip to content

Commit

Permalink
fix vertical problems in Mesh triangulation
Browse files Browse the repository at this point in the history
  • Loading branch information
ign-packo committed Oct 21, 2021
1 parent 33f8680 commit 4f0a286
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Converter/Feature2Mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,29 @@ function featureToPolygon(feature, options) {

const geomVertices = vertices.slice(start * 3, end * 3);
const holesOffsets = geometry.indices.map(i => i.offset - start).slice(1);
const normal = new THREE.Vector3(0, 0, 0);
for (let i = 1; i < count; i++) {
normal.x += (geomVertices[3 * (i - 1) + 1] - geomVertices[3 * i + 1]) *
(geomVertices[3 * (i - 1) + 2] + geomVertices[3 * i + 2]);
normal.y += (geomVertices[3 * (i - 1) + 2] - geomVertices[3 * i + 2]) *
(geomVertices[3 * (i - 1) + 0] + geomVertices[3 * i + 0]);
normal.z += (geomVertices[3 * (i - 1) + 0] - geomVertices[3 * i + 0]) *
(geomVertices[3 * (i - 1) + 1] + geomVertices[3 * i + 1]);
}
normal.normalize();
const pt0 = new THREE.Vector3(geomVertices[0], geomVertices[1], geomVertices[2]);
for (let i = 1; i < count; i++) {
const ptIn = new THREE.Vector3(geomVertices[3 * i],
geomVertices[3 * i + 1],
geomVertices[3 * i + 2]);
let v = ptIn - pt0;
v -= normal.dot(v) * normal;
const ptOut = ptIn + v;
geomVertices[3 * i] = ptOut.x;
geomVertices[3 * i + 1] = ptOut.y;
geomVertices[3 * i + 2] = ptOut.z;
}
const triangles = Earcut(geomVertices, holesOffsets, 3);

const startIndice = indices.length;
indices.length += triangles.length;

Expand Down

0 comments on commit 4f0a286

Please sign in to comment.