nannou experiment no2 - perlin noise

Nikolaus Gradwohl2020-12-22T10:07:37+00:00

I made a new nannou experiment - this time using 3D perlin noise to color, displace and resize some circles on a grid. It would propably make more sense to implement this using shaders - but I didn't start looking into the wgpu yet.

If you want to play with it yourself here is the rust code I used to create the animation

use nannou::noise::NoiseFn;
use nannou::prelude::*;

fn main() {
    nannou::app(model)
        .update(update)
        .run();
}

struct Model {
    points: Vec<Vector3>,
    noise: nannou::noise::Perlin,
}

fn model(app: &App) -> Model {
    app.new_window().size(600,600).view(view).build().unwrap();
    let mut p = vec![];
    for x in -15..15 {
        for y in -15..15 {
            p.push(vec3(x as f32, y as f32, 0.0));
        }
    }
    let noise = nannou::noise::Perlin::new();
    Model { points: p, noise: noise, }
}

fn update(app: &App, model: &mut Model, _update: Update) {
    let t = (app.elapsed_frames() as f32) * 0.03;
    let mut pn = vec![];

    for p in &model.points {
        let r = model.noise.get([p.x as f64/10.0, p.y as f64/10.0, t as f64]);
        pn.push(vec3(p.x,p.y,r as f32));
    }
    model.points = pn;
}

fn view(app: &App, model: &Model, frame: Frame) {
    frame.clear(BLACK);
    let draw = app.draw();
    for point in &model.points {
        let d = vec2(point.x, point.y).normalize();
        let r = point.z * 6.0 + 6.0;
        let p = vec2(point.x, point.y)*15.0 + d * point.z * 15.0;
        draw.ellipse()
            .x_y(p.x, p.y)
            .w_h(r,r)
            .color(BLACK)
            .stroke(hsla(1.0 - point.z as f32/2.0+0.5,1.0,0.5,1.0))
            .stroke_weight(1.0);
    }
    draw.to_frame(app, &frame).unwrap();

    if frame.nth() < 2000 {
        let file_path = captured_frame_path(app, &frame);
        app.main_window().capture_frame(file_path);
    }
}

fn captured_frame_path(app: &App, frame: &Frame) -> std::path::PathBuf {
    app.project_path()
        .expect("failed to locate `project_path`")
        .join("frames")
        .join(format!("{:04}", frame.nth()))
        .with_extension("png")
}
Tweet This! submit to reddit Digg! Tags: | no comments | no trackbacks

See also:

nannou experiment - amplitude modulated rainbow sinewave
nannou experiment - particles
nannou experiment

Trackbacks

Comments

Leave a response

Leave a comment