๐Ÿ”ฎ :: Grid Perlin Noise

BamgasiJMยท2025๋…„ 9์›” 18์ผ

Nannou <Generative Art>

๋ชฉ๋ก ๋ณด๊ธฐ
21/55
post-thumbnail

๐Ÿ“ Rust Code

use nannou::prelude::*;
use nannou::noise::{NoiseFn, Perlin, Seedable};

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

struct Model {
    time: f32,
    noise: Perlin,  // Perlin ๋…ธ์ด์ฆˆ ์ œ๋„ˆ๋ ˆ์ดํ„ฐ
}

fn model(_app: &App) -> Model {
    _app.new_window().size(800, 800).view(view).build().unwrap();  // 800x800์œผ๋กœ ๋ณ€๊ฒฝ
    Model {
        time: 0.0,
        noise: Perlin::new().set_seed(0),  // ์‹œ๋“œ 0์œผ๋กœ ์„ค์ •
    }
}

fn update(_app: &App, model: &mut Model, update: Update) {
    model.time += update.since_last.as_secs_f32();  // ์‹œ๊ฐ„ ๋ˆ„์ 
}

fn view(app: &App, model: &Model, frame: Frame) {
    let draw = app.draw();
    draw.background().color(BLACK);  // ๋ฐฐ๊ฒฝ ๊ฒ€์ •

    let grid_size = 10.0;  // ๊ทธ๋ฆฌ๋“œ ์…€ ํฌ๊ธฐ
    let win_w = app.window_rect().w();
    let win_h = app.window_rect().h();
    
    // ์ˆ˜์ •๋œ ๊ณ„์‚ฐ: floor ๋Œ€์‹  ceil์„ ์‚ฌ์šฉํ•˜์—ฌ ํ™”๋ฉด์„ ์™„์ „ํžˆ ์ฑ„์›€
    let cols = ((win_w / grid_size).ceil() as usize) + 1;  // ์—ด ์ˆ˜ ๊ณ„์‚ฐ
    let rows = ((win_h / grid_size).ceil() as usize) + 1;  // ํ–‰ ์ˆ˜ ๊ณ„์‚ฐ

    for x in 0..cols {  // ์ค‘์ฒฉ ๋ฃจํ”„: x, y ์ขŒํ‘œ ๋ฐ˜๋ณต
        for y in 0..rows {
            let pos_x = x as f32 * grid_size - win_w / 2.0;  // ์ค‘์•™ ์ •๋ ฌ
            let pos_y = y as f32 * grid_size - win_h / 2.0;

            // Perlin ๋…ธ์ด์ฆˆ ๊ณ„์‚ฐ: ์‹œ๊ฐ„๊ณผ ์œ„์น˜ ์ž…๋ ฅ
            let noise_val = model.noise.get([pos_x as f64 / 200.0, (pos_y as f64 + model.time as f64 * 10.0) / 200.0]) as f32;
            let brightness = map_range(noise_val, -1.0, 1.0, 0.0, 1.0);  // ๊ฐ’ ๋งคํ•‘

            draw.rect()
                .x_y(pos_x, pos_y)
                .w_h(grid_size, grid_size)
                .color(hsla(model.time * 0.1, 1.0, brightness, 1.0));  // ์ƒ‰์ƒ ๋™์  ๋ณ€๊ฒฝ
        }
    }

    draw.to_frame(app, &frame).unwrap();  // ํ”„๋ ˆ์ž„ ๋ Œ๋”๋ง
}

profile
Coding Art with Blender / oF / Processing / p5.js / nannou

0๊ฐœ์˜ ๋Œ“๊ธ€