clusterAt function

({double count, double offset}) clusterAt(
  1. ShaderBindings bindings,
  2. Vector3 world
)

FindCluster: where the entries of world's cell start, and how many.

Implementation

({double offset, double count}) clusterAt(
  ShaderBindings bindings,
  Vector3 world,
) {
  final m = bindings.mat4('LightListInfo', 'cluster_view_projection');
  final grid = bindings.vec4('LightListInfo', 'cluster_grid', Vector4.zero());
  final depth = bindings.vec4('LightListInfo', 'cluster_depth', Vector4.zero());
  final clip = m.transformed(Vector4(world.x, world.y, world.z, 1.0));
  final w = math.max(clip.w, 1e-6);
  double cut(double at, double cells) =>
      math.min(math.max((at * cells).floorToDouble(), 0.0), cells - 1.0);
  final tx = cut(clip.x / w * 0.5 + 0.5, grid.x);
  final ty = cut(clip.y / w * 0.5 + 0.5, grid.y);
  final tz = clip.w <= depth.x
      ? 0.0
      : cut(math.log(clip.w / depth.x) * depth.y / grid.z, grid.z);
  final cell = (tx + ty * grid.x + tz * grid.x * grid.y).round();
  final header = lightListTexel(
    bindings,
    depth.z.round() + cell ~/ 4,
    cell % 4,
  );
  return (offset: header.x, count: header.y);
}