triangulateShape static method
contour
-- 2D polygon. An array of Vector2
.
holes
-- An array that holds arrays of Vector2
s. Each array
represents a single hole definition.
Used internally by ExtrudeGeometry
and
ShapeGeometry
to calculate faces in shapes with
holes.
Implementation
static List<List<num>> triangulateShape(List<Vector?> contour, List<List<Vector?>> holes) {
final List<double> vertices = []; // flat array of vertices like [ x0,y0, x1,y1, x2,y2, ... ]
List<int> holeIndices = []; // array of hole indices
final List<List<num>> faces = []; // final array of vertex indices like [ [ a,b,d ], [ b,c,d ] ]
removeDupEndPts(contour);
addContour(vertices, contour);
//
int holeIndex = contour.length;
holes.forEach(removeDupEndPts);
for (int i = 0; i < holes.length; i++) {
holeIndices.add(holeIndex);
holeIndex += holes[i].length;
addContour(vertices, holes[i]);
}
final List<num> triangles = Earcut.triangulate(vertices, holeIndices);
for (int i = 0; i < triangles.length; i += 3) {
faces.add(triangles.sublist(i, i + 3));
}
return faces;
}