selectVisibleClusters function

int selectVisibleClusters({
  1. required MeshClusters clusters,
  2. required Matrix4 world,
  3. required Frustum frustum,
  4. required Uint8List visible,
  5. Vector3? eye,
  6. OcclusionTest? occlusion,
  7. Aabb3? scratch,
})

Marks in visible (one byte per cluster) the clusters of clusters a view may see, and returns how many.

Three tests, cheapest first, each only ever removing what the GPU would not have drawn: the cluster's box, carried to the world through world, against frustum; its cone against eye, the view's eye in the mesh's own space (null for a view with no single eye, such as an orthographic one, and for a draw that culls no back faces); and the world box against occlusion, when there is one.

Implementation

int selectVisibleClusters({
  required MeshClusters clusters,
  required Matrix4 world,
  required Frustum frustum,
  required Uint8List visible,
  Vector3? eye,
  OcclusionTest? occlusion,
  Aabb3? scratch,
}) {
  final local = Aabb3();
  final box = scratch ?? Aabb3();
  var count = 0;
  for (var i = 0; i < clusters.length; i++) {
    clusters.boundsInto(i, local);
    box.copyFrom(local);
    box.transform(world);
    final keep =
        frustum.intersectsWithAabb3(box) &&
        (eye == null || !clusters.facesAwayFrom(i, eye.x, eye.y, eye.z)) &&
        (occlusion == null || occlusion.mayBeVisible(box));
    visible[i] = keep ? 1 : 0;
    if (keep) count++;
  }
  return count;
}