rectangleFormFactor function

double rectangleFormFactor(
  1. List<Vector3> corners,
  2. Vector3 n
)

RectangleFormFactor — gfx-77n.

Lambert's polygon form factor, exact rather than fitted: each edge's subtended angle weighted by how much its plane leans into the normal, summed and halved, over the rectangle clipped to the surface's horizon. See the GLSL of the same name for why no table is shipped, why the clip is four trimmed edges and one along the horizon, and why the sum is negated — the rectangle emits along cross(halfWidth, halfHeight) and this winding is clockwise seen from there.

Implementation

double rectangleFormFactor(List<Vector3> corners, Vector3 n) {
  var total = 0.0;
  var exit = Vector3.zero();
  var entry = Vector3.zero();
  for (var i = 0; i < 4; i++) {
    final a = corners[i];
    final b = corners[i == 3 ? 0 : i + 1];
    final ha = a.dot(n);
    final hb = b.dot(n);
    final d = ha - hb;
    final q = a + (b - a) * (d.abs() > 1e-12 ? ha / d : 0.0);
    final aAbove = ha > 0.0;
    final bAbove = hb > 0.0;
    if (aAbove || bAbove) {
      total += _lambertEdge(aAbove ? a : q, bAbove ? b : q, n);
    }
    if (aAbove && !bAbove) exit = q;
    if (!aAbove && bAbove) entry = q;
  }
  total += _lambertEdge(exit, entry, n);
  return math.max(-total * 0.5, 0.0);
}