Vala プログラミング

WebGPU プログラミング

おなが@京都先端科学大

nannou: An open-source creative-coding framework for Rust

nannouは、Rust言語で書かれたクリエイティブコーディング環境です。
作品もRust言語でコーディングします。

nannou
Home | Nannou
GitHub - nannou-org/nannou: A Creative Coding Framework for Rust.

nannouを紹介した日本語のページです。
・Rust製クリエイティブコーディング環境 – Qiita
https://qiita.com/applejam/items/322d72decc0b6b067dc5
・モダンな言語のクリエーティブコーディング環境 – Quil, nannou
https://ayumu-nagamatsu.com/archives/931/
・nannou入門 vol.1
https://note.com/daumkuchen/n/n4771af7fb6c3

グラフィックスライブラリとして、前のバージョンv0.12(2019-11-03)まではvulkanoが
使われていましたが、現在のバージョンv0.13(2020-03-05)からwgpuが使われています。
これでかなりの数のissueが解決されています。

nannouを用いた描画には、sketchモードとappモードの2つの方法があります。
sketchモードは、手軽に描画するときに用います。view関数内でコーディングします。
appモードは、手の込んだ作品を描画するときに用います。model関数、update関数、
view関数の3つの関数内でコーディングします。(update関数の他にevent関数を
使うこともある。)

下は、examplesにあるsimple_drawの実行画面です。sketchモードで描画しています。
f:id:onagat12:20200315135003g:plain

プログラム

use nannou::prelude::*;

fn main() {
    nannou::sketch(view).run()
}

fn view(app: &App, frame: Frame) {
    // Begin drawing
    let draw = app.draw();

    // Clear the background to blue.
    draw.background().color(CORNFLOWERBLUE);

    // Draw a purple triangle in the top left half of the window.
    let win = app.window_rect();
    draw.tri()
        .points(win.bottom_left(), win.top_left(), win.top_right())
        .color(VIOLET);

    // Draw an ellipse to follow the mouse.
    let t = app.time;
    draw.ellipse()
        .x_y(app.mouse.x * t.cos(), app.mouse.y)
        .radius(win.w() * 0.125 * t.sin())
        .color(RED);

    // Draw a line!
    draw.line()
        .weight(10.0 + (t.sin() * 0.5 + 0.5) * 90.0)
        .caps_round()
        .color(PALEGOLDENROD)
        .points(win.top_left() * t.sin(), win.bottom_right() * t.cos());

    // Draw a quad that follows the inverse of the ellipse.
    draw.quad()
        .x_y(-app.mouse.x, app.mouse.y)
        .color(DARKGREEN)
        .rotate(t);

    // Draw a rect that follows a different inverse of the ellipse.
    draw.rect()
        .x_y(app.mouse.y, app.mouse.x)
        .w(app.mouse.x * 0.25)
        .hsv(t, 1.0, 1.0);

    // Write the result of our drawing to the window's frame.
    draw.to_frame(app, &frame).unwrap();
}