// A density in two dimensions as a field of columns: three populations overlap on a plane, and every column rises as high as
// the density at its place and takes its color from it, from a pale lavender on the floor through violet and magenta to an
// orange peak. White, as a page of a paper is.
// The spread widens or narrows all three, and the height stretches only the columns: an unseen room around the field stands as
// high as the highest column can grow, so the view keeps its size while the columns rise and fall.
sfl 1.12

Style tpl_canvas {
  fill: "#ffffff"
}

Parameters field {
  spread: 1 // 0.5..2 step 0.05, Spread
  lift: 1.6 // 0.6..2.6 step 0.1, Height
  cells: 36
  step: 0.1
  span: cells * step
  top: 2.9
}

Model density {
  Text title {
    text: "Three populations, one field"
    layout {
      position: 48, 736
      size: 700, 30
    }
    style {
      fontSize: 22
      fontWeight: 600
      textColor: "#17142b"
      textAlign: left
    }
  }
  Text subtitle {
    text: "Density of a mixture of three normal distributions on a 36 by 36 grid. Height and color are the density."
    layout {
      position: 48, 768
      size: 900, 18
    }
    style {
      fontSize: 12
      textColor: "#6f6a86"
      textAlign: left
    }
  }

  Scene skyline {
    orbit: 38, 26
    zoom: 1.7
    shadow: true
    environment: "studio"
    exposure: 0.95
    layout {
      position: 0, 0
      size: 1200, 720
    }

    Ground floor {
      color: "#ffffff"
      reflection: 0.06
      roughness: 0.7
    }

    Light key {
      turn: 210
      elevation: 50
      color: "#fff4e8"
      intensity: 1
      ambient: 0.55
      softness: 2.5
    }

    // The base plate the field stands on.
    Solid base {
      chamfer: 0.01
      layout {
        position: (cells - 1) * step / 2, -0.04, (cells - 1) * step / 2
        size: span + 0.16, 0.04, span + 0.16
      }
      style {
        color: "#f1eff7"
        roughness: 0.7
      }
    }

    // An unseen room around the field, as high as the highest column can grow. The camera takes in the whole of it, so the view
    // keeps its size while the columns rise and fall. It is not seen, and casts no shadow.
    Solid room {
      layout {
        position: (cells - 1) * step / 2, 0, (cells - 1) * step / 2
        size: span + 0.16, top, span + 0.16
      }
      style {
        opacity: 0
      }
    }

    Repeat columns {
      columns: cells
      rows: cells
      seed: 11
      u: col / (cells - 1)
      v: row / (cells - 1)
      d: exp(-(pow(u - 0.62, 2) / (0.012 * spread) + pow(v - 0.52, 2) / (0.03 * spread))) + 0.62 * exp(-(pow(u - 0.3, 2) / (0.02 * spread) + pow(v - 0.34, 2) / (0.014 * spread))) + 0.24 * exp(-(pow(u - 0.44, 2) / (0.1 * spread) + pow(v - 0.8, 2) / (0.05 * spread))) + random() * 0.035
      level: min(1, d / 1.05)
      ramp: mix(mix(mix(mix("#ece9f6", "#9d8fd6", level / 0.25), "#6a3fb5", (level - 0.25) / 0.25), "#c2338f", (level - 0.5) / 0.25), "#ff7a2f", (level - 0.75) / 0.25)

      Solid column {
        layout {
          position: col * step, 0, row * step
          size: step * 0.86, max(0.01, d * lift), step * 0.86
        }
        style {
          color: ramp
          roughness: 0.5
        }
      }
    }
  }

  Text spreadLabel {
    text: <- "Spread  " + field.spread
    layout {
      position: 48, 806
      size: 240, 18
    }
    style {
      fontSize: 12
      fontWeight: 600
      textColor: "#17142b"
      textAlign: left
    }
  }
  Component spreadControl {
    type: slider
    value: <-> field.spread
    min: 0.5
    max: 2
    layout {
      position: 48, 830
      size: 260, 32
    }
    style {
      accent: "#6a3fb5"
      track: "#e4e1ee"
      step: 0.05
    }
  }
  Text liftLabel {
    text: <- "Height  " + field.lift
    layout {
      position: 348, 806
      size: 240, 18
    }
    style {
      fontSize: 12
      fontWeight: 600
      textColor: "#17142b"
      textAlign: left
    }
  }
  Component liftControl {
    type: slider
    value: <-> field.lift
    min: 0.6
    max: 2.6
    layout {
      position: 348, 830
      size: 260, 32
    }
    style {
      accent: "#c2338f"
      track: "#e4e1ee"
      step: 0.1
    }
  }
}
