build static method

EnvironmentMapData build(
  1. void radiance(
    1. double x,
    2. double y,
    3. double z,
    4. Float32List out,
    ), {
  2. int size = 32,
  3. int? levels,
})

Builds a map from what radiance says each direction holds, with levels levels (each half the last): down to 2×2 by default, which is log2(size) for a power of two. Level m is blurred for roughness m / (levels - 1).

Implementation

static EnvironmentMapData build(
  void Function(double x, double y, double z, Float32List out) radiance, {
  int size = 32,
  int? levels,
}) {
  final base = List.generate(6, (_) => Float32List(size * size * 4));
  final c = Float32List(3);
  for (var face = 0; face < 6; face++) {
    final out = base[face];
    for (var row = 0; row < size; row++) {
      for (var col = 0; col < size; col++) {
        final (x, y, z) = directionOf(
          face,
          (col + 0.5) / size,
          (row + 0.5) / size,
        );
        final len = math.sqrt(x * x + y * y + z * z);
        radiance(x / len, y / len, z / len, c);
        final o = (row * size + col) * 4;
        out[o] = c[0];
        out[o + 1] = c[1];
        out[o + 2] = c[2];
        out[o + 3] = 1;
      }
    }
  }
  // Down to 1×1 a chain has size.bitLength levels.
  final full = size.bitLength;
  final count = (levels ?? full - 1).clamp(1, full);
  final out = <List<Float32List>>[base];
  for (var m = 1; m < count; m++) {
    out.add(_prefilter(base, size, size >> m, m / (count - 1)));
  }
  return EnvironmentMapData._(size, out, _shOf(base, size));
}