Advertisement
Guest User

worms

a guest
Aug 18th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.26 KB | None | 0 0
  1. use nannou::color::Gradient;
  2. use nannou::noise::NoiseFn;
  3. use nannou::noise::SuperSimplex;
  4. use nannou::prelude::*;
  5.  
  6. fn main() {
  7.     nannou::sketch(view).size(1024, 1024).run();
  8. }
  9.  
  10. fn view(app: &App, frame: Frame) {
  11.     let noise = SuperSimplex::new();
  12.     let draw = app.draw();
  13.     let draw = draw.scale(0.95).translate(Vec3::new(-475.0, -475.0, 0.0));
  14.     draw.background().color(BLACK);
  15.  
  16.     let gradient = Gradient::new([
  17.         hsl((app.time / 3.0), 1.0, 0.8),
  18.         hsl((app.time / 3.0 + 0.3), 1.0, 0.8),
  19.     ]);
  20.  
  21.     for i in 0..100 {
  22.         let mut base_r = 102.4;
  23.         let n_circles = 50;
  24.         let decay_r = base_r / (n_circles as f32);
  25.         let time = app.time / 2.0;
  26.         let row = i / 10;
  27.         let column = i % 10;
  28.         let row_offset = row as f32 * base_r;
  29.         let column_offset = column as f32 * base_r;
  30.  
  31.         let gradient_id_col = if column > 4 {
  32.             1.0 - ((column as f32 - 5.0) / 4.0)
  33.         } else {
  34.             column as f32 / 4.0
  35.         };
  36.  
  37.         let gradient_id_row = if row > 4 {
  38.             1.0 - ((row as f32 - 5.0) / 4.0)
  39.         } else {
  40.             row as f32 / 4.0
  41.         };
  42.  
  43.         let gradient_id = if gradient_id_row > gradient_id_col {
  44.             gradient_id_col
  45.         } else {
  46.             gradient_id_row
  47.         };
  48.  
  49.         let hue = gradient.get(gradient_id).hue;
  50.  
  51.         for i in 0..n_circles {
  52.             let id_frac = i as f32 / n_circles as f32;
  53.             let frac_sin = ((id_frac.sin() + 1.0) / 2.0);
  54.             let lum_range =
  55.                 (((app.time * 10.0 + id_frac / 0.02).sin() + 1.0) / 2.0).clamp(0.1, 0.9);
  56.             let lum = lum_range;
  57.  
  58.             let sample = i as f64 / (n_circles as f64);
  59.  
  60.             let color = hsl(hue.to_degrees() / 360.0, 0.7, (id_frac + 0.1) * lum);
  61.  
  62.             let offset_x =
  63.                 1.2 * (noise.get([time as f64 + sample + column as f64, 0.0f64]) as f32) * i as f32
  64.                     + column_offset;
  65.             let offset_y =
  66.                 1.2 * (noise.get([0.0f64, time as f64 + sample + row as f64]) as f32) * i as f32
  67.                     + row_offset;
  68.             // let offset_x = app.time.sin() * i as f32 / 2.0;
  69.             // let offset_y = app.time.cos() * i as f32 / 2.0;
  70.  
  71.             draw.ellipse()
  72.                 .w_h(base_r, base_r)
  73.                 .color(color)
  74.                 .x_y(offset_x * decay_r / 2.0, offset_y * decay_r / 2.0)
  75.                 .rotate(sample as f32);
  76.             base_r -= decay_r;
  77.         }
  78.     }
  79.  
  80.     draw.to_frame(app, &frame).unwrap();
  81.     // Capture the frame!
  82.  
  83.     // let file_path = captured_frame_path(app, &frame);
  84.     // app.main_window().capture_frame(file_path);
  85. }
  86.  
  87. fn captured_frame_path(app: &App, frame: &Frame) -> std::path::PathBuf {
  88.     // Create a path that we want to save this frame to.
  89.     app.project_path()
  90.         .expect("failed to locate `project_path`")
  91.         // Capture all frames to a directory called `/<path_to_nannou>/nannou/simple_capture`.
  92.         .join(app.exe_name().unwrap())
  93.         // Name each file after the number of the frame.
  94.         .join(format!("pic{:04}", frame.nth()))
  95.         // The extension will be PNG. We also support tiff, bmp, gif, jpeg, webp and some others.
  96.         .with_extension("png")
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement