function buildPolygons(polygons, vertex_data, vertex_template, _ref) {
  var texcoord_index = _ref.texcoord_index,
    texcoord_scale = _ref.texcoord_scale,
    texcoord_normalize = _ref.texcoord_normalize;
  var vertex_elements = vertex_data.vertex_elements,
    num_polygons = polygons.length,
    geom_count = 0,
    min_u,
    min_v,
    max_u,
    max_v,
    min_x,
    min_y,
    max_x,
    max_y,
    span_x,
    span_y,
    scale_u,
    scale_v;
  if (texcoord_index) {
    texcoord_normalize = texcoord_normalize || 1;
    var _ref2 = texcoord_scale || default_uvs;
    var _ref3 = _slicedToArray(_ref2, 4);
    min_u = _ref3[0];
    min_v = _ref3[1];
    max_u = _ref3[2];
    max_v = _ref3[3];
  }
  for (var p = 0; p < num_polygons; p++) {
    var max_rings = 500;
    var polygon = polygons[p];
    if (polygon.length > max_rings) {
      polygon = _toConsumableArray(polygon); // copy to avoid modifying original
      quickselect(polygon, max_rings, 1, polygon.length - 1, function (a, b) {
        return b.area - a.area;
      });
      polygon = polygon.slice(0, max_rings);
    }
    var indices = triangulatePolygon(earcut.flatten(polygon));
    var num_indices = indices.length;
    var element_offset = vertex_data.vertex_count;

    // The vertices and vertex-elements must not be added if earcut returns no indices:
    if (num_indices) {
      // Find polygon extents to calculate UVs, fit them to the axis-aligned bounding box:
      if (texcoord_index) {
        var _Geo$findBoundingBox, _Geo$findBoundingBox2;
        _Geo$findBoundingBox = Geo$1.findBoundingBox(polygon), _Geo$findBoundingBox2 = _slicedToArray(_Geo$findBoundingBox, 4), min_x = _Geo$findBoundingBox2[0], min_y = _Geo$findBoundingBox2[1], max_x = _Geo$findBoundingBox2[2], max_y = _Geo$findBoundingBox2[3], span_x = max_x - min_x, span_y = max_y - min_y, scale_u = (max_u - min_u) / span_x, scale_v = (max_v - min_v) / span_y;
      }
      for (var ring_index = 0; ring_index < polygon.length; ring_index++) {
        // Add vertex data:
        var polygon_ring = polygon[ring_index];
        for (var i = 0; i < polygon_ring.length; i++) {
          var vertex = polygon_ring[i];
          vertex_template[0] = vertex[0];
          vertex_template[1] = vertex[1];

          // Add UVs:
          if (texcoord_index) {
            vertex_template[texcoord_index + 0] = ((vertex[0] - min_x) * scale_u + min_u) * texcoord_normalize;
            vertex_template[texcoord_index + 1] = ((vertex[1] - min_y) * scale_v + min_v) * texcoord_normalize;
          }
          vertex_data.addVertex(vertex_template);
        }
      }

      // Add element indices:
      for (var _i = 0; _i < num_indices; _i++) {
        vertex_elements.push(element_offset + indices[_i]);
      }
      geom_count += num_indices / 3;
    }
  }
  return geom_count;
}
To tesselate flat 2D polygons.
The x & y coordinates will be set as first two elements of provided vertex_template.
@param {Array.<Array.<Array.<Array.<number>>>>} polygons The polygons to tesselate.
@param {VertexData} vertex_data The VertexData were to store the results
@param {Array.<number>} vertex_template The vertex template to use
@param {Object} options The texture coordinate options to apply
@return {number} the number of the resulting geometries (triangles)