lobe function

void lobe(
  1. Painter p,
  2. double x,
  3. double y,
  4. double r,
  5. List<int> fr,
  6. List<int> sm,
  7. double fire,
  8. double glow,
  9. double a, [
  10. double z = 0.0,
  11. int dark = 0,
  12. int style = 0,
])

A lobe of a billowing cloud, shaded like a ball.

Implementation

void lobe(
  Painter p,
  double x,
  double y,
  double r,
  List<int> fr,
  List<int> sm,
  double fire,
  double glow,
  double a, [
  double z = 0.0,
  int dark = 0,
  int style = 0,
]) {
  if (r < 0.7 || a <= 0.01) return;
  final n = fr.length;
  final m = sm.length;
  final back = z < -0.35 ? 1 : 0;
  final rim = 1.0 - math.min(0.35, 1.6 / r);
  final seam = 1.0 - math.min(0.3, 1.3 / r);
  int shadeAt(int i) => sm[math.min(m - 1, i + back + dark)];
  final soft = 0.55 + 0.4 * (1.0 - math.min(1.0, a));

  void px(double lx, double ly, int sx, int sy) {
    final dx = (lx - x) / r;
    final dy = (ly - y) / r;
    final d2 = dx * dx + dy * dy;
    if (d2 > 1.0) return;
    final d = math.sqrt(d2);
    final nz = math.sqrt(1.0 - d2);
    final sky = -0.5 * dx - 0.66 * dy + 0.56 * nz;
    final v = fire * (1.32 - 0.78 * d) + 0.14 * sky * fire;
    int c;
    var k = 0.0;
    var solid = false;
    if (v > 0.44) {
      var i = math.min(n - 3, math.max(0, ((1.3 - v) * (n - 1) * 0.78).floor()));
      if (d > seam) {
        i = math.min(6, math.max(4, i + 2));
      } else if (d > seam - 0.22) {
        i = math.min(n - 3, i + 1);
      }
      c = fr[i];
      k = 0.05 * fire;
      solid = true;
    } else if (style != 0) {
      final lam = -0.42 * dx - 0.78 * dy + 0.46 * nz;
      if (lam > 0.66 && d > 0.4) {
        c = shadeAt(0);
      } else if (lam > 0.16) {
        c = shadeAt(1);
      } else if (lam > -0.3) {
        c = shadeAt(2);
      } else {
        c = shadeAt(lam < -0.64 ? 4 : 3);
        final g = glow * (0.6 + 0.4 * dy);
        if (g > 0.55) {
          c = fr[g > 0.8 ? 5 : 6];
          k = 0.08 * g;
        } else if (g > 0.34) {
          c = mix(c, fr[6], 0.6);
        } else if (g > 0.16) {
          c = mix(c, fr[7], 0.45);
        }
      }
    } else {
      final g = glow * (0.82 * dy + 0.3 * nz);
      if (g > 0.45) {
        c = fr[g > 0.66 ? 4 : 5];
        k = 0.1 * glow;
      } else if (g > 0.3) {
        c = fr[6];
      } else if (g > 0.2) {
        c = fr[7];
      } else {
        final s = sky * 0.5 + 0.5;
        c = shadeAt(s > 0.8 ? 0 : (s > 0.62 ? 1 : (s > 0.42 ? 2 : (s > 0.22 ? 3 : 4))));
      }
    }
    p.blendPx(sx, sy, c, solid || d <= rim ? a : a * soft);
    if (k > 0.0) p.add(sx, sy, c, k);
  }

  if (p.sn != 0.0 || p.cs != 1.0) {
    p.each(x - r, y - r, x + r, y + r, px);
    return;
  }
  final ax = p.tx + (x - r) * p.fx;
  final bx = p.tx + (x + r) * p.fx;
  final sx0 = math.max(p.ox, math.min(ax, bx).floor());
  final sx1 = math.min(p.ox + p.w - 1, math.max(ax, bx).ceil());
  final sy0 = math.max(p.oy, (p.ty + y - r).floor());
  final sy1 = math.min(p.oy + p.h - 1, (p.ty + y + r).ceil());
  for (var sy = sy0; sy <= sy1; sy++) {
    final ly = sy + 0.5 - p.ty;
    final q = (ly - y) / r;
    if (q * q >= 1.0) continue;
    final hw = r * math.sqrt(1.0 - q * q);
    final cx = p.tx + x * p.fx;
    final start = math.max(sx0, (cx - hw - 0.5).floor());
    final end = math.min(sx1, (cx + hw).ceil());
    for (var sx = start; sx <= end; sx++) {
      px((sx + 0.5 - p.tx) * p.fx, ly, sx, sy);
    }
  }
}