作品:フローフィールド
この回でできるようになること
何百もの細い線が、見えない流れに乗って、うねり、束になり、分かれていきます。絹の糸のようにも、煙のようにも、川の流れのようにも、見えます。この回では、目に見えない「流れ」を作り、そこに粒子を放して、通ったあとを絵にする、ジェネラティブアートを作ります。
let particles = [];
let zoff = 0;
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
colorMode(HSB);
background(240, 40, 8);
for (let i = 0; i < 600; i++) {
particles[i] = new Particle();
}
}
function draw() {
for (let i = 0; i < particles.length; i++) {
particles[i].follow();
particles[i].update();
particles[i].show();
}
zoff += 0.002;
}
class Particle {
constructor() {
this.pos = createVector(random(width), random(height));
this.prev = createVector(this.pos.x, this.pos.y);
this.vel = createVector(0, 0);
}
// 流れ場から、力を受ける
follow() {
let angle = noise(this.pos.x * 0.005, this.pos.y * 0.005, zoff) * 720;
let force = createVector(cos(angle), sin(angle));
force.mult(0.2);
this.vel.add(force);
this.vel.limit(2);
}
update() {
this.prev.x = this.pos.x;
this.prev.y = this.pos.y;
this.pos.add(this.vel);
// 端から出たら、反対側へ
if (this.pos.x > width) {
this.pos.x = 0;
this.prev.x = 0;
}
if (this.pos.x < 0) {
this.pos.x = width;
this.prev.x = width;
}
if (this.pos.y > height) {
this.pos.y = 0;
this.prev.y = 0;
}
if (this.pos.y < 0) {
this.pos.y = height;
this.prev.y = height;
}
}
show() {
stroke(this.vel.heading() + 180, 60, 100, 0.08);
strokeWeight(1);
line(this.prev.x, this.prev.y, this.pos.x, this.pos.y);
}
}
実行すると、暗い画面に、細い光の筋が、少しずつ、描き込まれていきます。しばらく、眺めていてください。時間がたつほど、線が重なり、流れの形が、浮かび上がってきます。実行し直すたびに、違う絵が、できあがります。
この回が終わると、次のことができるようになります。
- 場所ごとに「向き」を持つ、流れ場を作る
- 粒子が、いまいる場所の向きに従って、進むようにする
- 粒子の通ったあとを、薄い線で重ねて、絵にする
- 進む向きを、色に変える
- 流れ場を、時間とともに、変化させる
流れ場という考え方
川の水面に、葉っぱを落とすと、葉っぱは、流れに乗って、進んでいきます。葉っぱは、自分で向きを決めているのでは、ありません。いまいる場所の、水の流れに、従っているだけです。
場所が変われば、流れの向きも、変わります。まっすぐな場所、曲がる場所、渦を巻く場所。水面のすべての場所に、「ここでは、こちらへ流れる」という向きが、あります。
このように、場所ごとに、向きが決まっているものを、流れ場(フローフィールド)と呼びます。
この作品の仕組みは、次の、たった2つです。
| 仕組み | 内容 |
|---|---|
| 流れ場 | キャンバスの、どの場所にも、向き(角度)がある |
| 粒子 | いまいる場所の向きへ、押されて進む |
粒子は、ただ、流されるだけです。絵を描いているのは、流れ場のほうです。粒子は、見えない流れを、目に見える線に、写し取るための、ペン先です。
ステップ1:流れ場を、目で見る
まず、流れ場そのものを、作って、見てみます。
場所から、角度を決める
「場所 (x, y) での、流れの向き」を、決める方法が、必要です。
random で決めると、隣どうしが、ばらばらの向きになり、流れに、なりません。隣どうしが、少しずつ違う向きになるように、noise を使います。
let angle = noise(x * 0.005, y * 0.005) * 720;
2つの数を渡した noise は、場所ごとに、なめらかに変わる、0 から 1 の値を返します。それに 720 を掛けて、角度(度)に、しています。
なぜ、720 を掛けるのか
1周は、360 度です。360 を掛ければ、十分に、思えます。
しかし、noise の値は、0.5 のあたりに、集まりやすく、0 や 1 に近い値は、あまり、出ません。360 を掛けると、角度は、180 度のあたりに集まり、ほとんどの場所が、左向きに、なってしまいます。
720 を掛けると、角度の範囲は、0 から 720 度、つまり、2周分に、なります。値が、真ん中に集まっていても、角度としては、ぐるりと、あらゆる向きに、ばらけます。
短い線で、向きを描く
マス目状に、場所を回り、その場所の向きを、短い線で、表します。
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
}
function draw() {
background(20, 20, 40);
stroke(255);
strokeWeight(1);
for (let y = 10; y < 400; y += 20) {
for (let x = 10; x < 400; x += 20) {
let angle = noise(x * 0.005, y * 0.005) * 720;
line(x, y, x + cos(angle) * 9, y + sin(angle) * 9);
circle(x, y, 2);
}
}
}
小さな点から、短い線が、出ています。線の向きが、その場所の、流れの向きです。
cos(angle) * 9 と sin(angle) * 9 で、その角度の向きへ、9 だけ進んだ点を、求めています。
隣どうしの線は、ほぼ同じ向きを、向いています。少しずつ、向きが変わっていき、離れた場所では、まったく違う向きに、なっています。全体を眺めると、ゆるやかに、うねる流れが、見えてきます。渦のように、線が回り込んでいる場所も、あります。
試してみよう
0.005 を、2か所とも、0.02 や 0.001 に変えて、流れの細かさが変わることを、確かめてください。数字が大きいと、流れは、細かく、せわしなく変わります。小さいと、キャンバス全体が、ほとんど同じ向きの、大きな流れになります。
ステップ2:粒子を、流れに乗せる
流れ場に、粒子を1つ、放します。粒子を、クラスにします。
粒子が持つもの
class Particle {
constructor() {
this.pos = createVector(random(width), random(height));
this.vel = createVector(0, 0);
}
}
| プロパティ | 意味 |
|---|---|
pos |
位置。最初は、でたらめな場所 |
vel |
速度。最初は、止まっている |
位置も、速度も、ベクトルで、持ちます。
流れから、力を受ける
粒子は、毎フレーム、いまいる場所の、流れの向きを調べて、その向きへ、押されます。
follow() {
let angle = noise(this.pos.x * 0.005, this.pos.y * 0.005) * 720;
let force = createVector(cos(angle), sin(angle));
force.mult(0.2);
this.vel.add(force);
this.vel.limit(2);
}
| 行 | していること |
|---|---|
| 1行目 | 自分の位置での、流れの角度を調べる |
| 2行目 | その角度の向きを向いた、長さ 1 のベクトルを作る |
| 3行目 | 長さを 0.2 にする。これが、流れから受ける力 |
| 4行目 | 力を、速度に足す |
| 5行目 | 速度の大きさが、2 を超えないようにする |
ステップ1 で、マス目の場所から求めていた角度を、今度は、粒子の位置から、求めています。式は、同じです。粒子は、マス目の上に、いる必要は、ありません。noise は、どんな場所でも、値を返します。
力を足す、ということ
流れの向きを、そのまま速度にするのでは、ありません。流れの向きの力を、速度に、足しています。
| 方法 | 動き |
|---|---|
| 速度を、流れの向きに、置き換える | 向きが、かくかくと、急に変わる |
| 流れの向きの力を、速度に足す | いまの勢いを保ちながら、じわりと、向きが変わる |
力を足す方法では、粒子に、勢いが、あります。流れが、急に曲がっていても、粒子は、すぐには曲がれず、少し外へ、ふくらみます。川の流れに乗った、葉っぱの動きです。
limit(2) は、速さの上限です。同じ向きの力を、受け続けると、速度は、どこまでも、大きくなります。それを、2 で、頭打ちにしています。
端から出たら、反対側へ
粒子が、キャンバスの外へ出たら、反対側の端から、入ってくるようにします。右へ出たら、左から。下へ出たら、上から。粒子は、いつまでも、キャンバスの中を、流れ続けます。
let p;
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
p = new Particle();
}
function draw() {
background(20, 20, 40);
p.follow();
p.update();
p.show();
}
class Particle {
constructor() {
this.pos = createVector(random(width), random(height));
this.vel = createVector(0, 0);
}
// 流れ場から、力を受ける
follow() {
let angle = noise(this.pos.x * 0.005, this.pos.y * 0.005) * 720;
let force = createVector(cos(angle), sin(angle));
force.mult(0.2);
this.vel.add(force);
this.vel.limit(2);
}
update() {
this.pos.add(this.vel);
// 端から出たら、反対側へ
if (this.pos.x > width) {
this.pos.x = 0;
}
if (this.pos.x < 0) {
this.pos.x = width;
}
if (this.pos.y > height) {
this.pos.y = 0;
}
if (this.pos.y < 0) {
this.pos.y = height;
}
}
show() {
noStroke();
fill(255);
circle(this.pos.x, this.pos.y, 6);
}
}
白い粒子が、ひとつ、曲がりくねりながら、流れていきます。流れ場は、見えませんが、粒子の動きから、そこに流れがあることが、わかります。
ステップ3:粒子を、たくさん放す
粒子を、600 個に、増やします。配列に入れて、ループで、すべてを動かします。
for (let i = 0; i < 600; i++) {
particles[i] = new Particle();
}
let particles = [];
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
for (let i = 0; i < 600; i++) {
particles[i] = new Particle();
}
}
function draw() {
background(20, 20, 40);
for (let i = 0; i < particles.length; i++) {
particles[i].follow();
particles[i].update();
particles[i].show();
}
}
class Particle {
constructor() {
this.pos = createVector(random(width), random(height));
this.vel = createVector(0, 0);
}
// 流れ場から、力を受ける
follow() {
let angle = noise(this.pos.x * 0.005, this.pos.y * 0.005) * 720;
let force = createVector(cos(angle), sin(angle));
force.mult(0.2);
this.vel.add(force);
this.vel.limit(2);
}
update() {
this.pos.add(this.vel);
// 端から出たら、反対側へ
if (this.pos.x > width) {
this.pos.x = 0;
}
if (this.pos.x < 0) {
this.pos.x = width;
}
if (this.pos.y > height) {
this.pos.y = 0;
}
if (this.pos.y < 0) {
this.pos.y = height;
}
}
show() {
noStroke();
fill(255);
circle(this.pos.x, this.pos.y, 3);
}
}
最初は、キャンバス全体に、ばらまかれていた粒子が、やがて、何本かの筋に、集まっていきます。
流れ場には、流れが集まってくる場所と、散っていく場所が、あります。粒子は、集まる場所へ、吸い寄せられ、そこで、列になって、流れます。川の水が、谷へ集まって、本流になるのと、同じです。
Particle クラスは、1文字も、変えていません。変えたのは、setup と draw で、1個を扱っていたところを、配列とループに、置き換えただけです。
ステップ4:通ったあとを、残す
ここまでは、粒子の、いまの位置を、描いていました。ここからは、粒子の、通ったあとを、描きます。作品が、動く点の集まりから、1枚の絵に、変わります。
背景を、塗り直さない
draw の中の background を、取り除きます。setup の中で、1回だけ、塗ります。こうすると、描いたものが、消えずに、残り続けます。
点ではなく、線で描く
粒子は、1フレームに、最大で 2 だけ、動きます。点を打つと、点と点の間に、すき間が、できます。1フレーム前の位置から、いまの位置まで、線を引きます。
そのために、1フレーム前の位置を、覚えておきます。
this.prev = createVector(this.pos.x, this.pos.y);
update() の最初で、動く前の位置を、prev に、写します。そのあとで、位置を、進めます。
this.prev.x = this.pos.x;
this.prev.y = this.pos.y;
this.pos.add(this.vel);
描くときは、prev から pos へ、線を引きます。
line(this.prev.x, this.prev.y, this.pos.x, this.pos.y);
反対側へ移るときは、前の位置も移す
端から出て、反対側へ移るとき、気を付けることが、あります。
右の端から出た粒子が、左の端へ、移ったとします。prev は、右の端のままです。そのまま、線を引くと、キャンバスを横切る、長い直線が、引かれてしまいます。
そこで、位置を反対側へ移すとき、prev も、同じ場所へ、移します。
if (this.pos.x > width) {
this.pos.x = 0;
this.prev.x = 0;
}
こうすれば、そのフレームに引かれる線は、長さが、ほとんど 0 になり、見えません。
とても薄い線を、重ねる
線の色を、ほとんど透明に、します。
stroke(255, 255, 255, 20);
4つ目の数字が、不透明度です。255 のうちの 20 なので、1本の線は、うっすらとしか、見えません。
しかし、同じ場所を、何度も通ると、線が重なって、だんだん、明るくなります。たくさんの粒子が通る場所は、明るく。たまにしか通らない場所は、暗いままです。流れの太さが、明るさとして、現れます。
let particles = [];
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
background(20, 20, 40);
for (let i = 0; i < 600; i++) {
particles[i] = new Particle();
}
}
function draw() {
for (let i = 0; i < particles.length; i++) {
particles[i].follow();
particles[i].update();
particles[i].show();
}
}
class Particle {
constructor() {
this.pos = createVector(random(width), random(height));
this.prev = createVector(this.pos.x, this.pos.y);
this.vel = createVector(0, 0);
}
// 流れ場から、力を受ける
follow() {
let angle = noise(this.pos.x * 0.005, this.pos.y * 0.005) * 720;
let force = createVector(cos(angle), sin(angle));
force.mult(0.2);
this.vel.add(force);
this.vel.limit(2);
}
update() {
this.prev.x = this.pos.x;
this.prev.y = this.pos.y;
this.pos.add(this.vel);
// 端から出たら、反対側へ
if (this.pos.x > width) {
this.pos.x = 0;
this.prev.x = 0;
}
if (this.pos.x < 0) {
this.pos.x = width;
this.prev.x = width;
}
if (this.pos.y > height) {
this.pos.y = 0;
this.prev.y = 0;
}
if (this.pos.y < 0) {
this.pos.y = height;
this.prev.y = height;
}
}
show() {
stroke(255, 255, 255, 20);
strokeWeight(1);
line(this.prev.x, this.prev.y, this.pos.x, this.pos.y);
}
}
白い線が、少しずつ、描き込まれていきます。粒子が集まる筋は、太く、明るく、浮かび上がります。まばらな場所には、細い線が、ふわりと残ります。
線を、濃く、太くすると、数秒で、画面が、真っ白に、なってしまいます。薄く、細く描くことで、長い時間をかけて、濃淡のある絵が、育っていきます。
クイズ
setup の中の background(20, 20, 40); を、draw の最初にも、書いたとします。画面は、どう見えるでしょう?
▶ 答えを確認する
ほとんど、何も見えなくなります。暗い背景に、ごく短い、とても薄い線が、600 本、ちらちらするだけです。
毎フレーム、背景を塗り直すと、前のフレームまでに描いた線は、すべて、消えます。残るのは、そのフレームに引かれた、長さ 2 以下の、短い線だけです。しかも、不透明度が 20 しかないので、1本では、ほとんど、見えません。
この作品は、消さずに、重ね続けることで、成り立っています。
ステップ5:向きを、色にする
線に、色を付けます。色は、粒子が進んでいる向きから、決めます。
色を、色相・彩度・明るさで、指定します。setup に、colorMode(HSB); を、書きます。色相は、0 から 360 の数で、色合いを表します。角度と、同じ範囲です。
stroke(this.vel.heading() + 180, 60, 100, 0.08);
this.vel.heading() は、速度ベクトルの向きを、角度で返します。−180 度から 180 度の範囲です。180 を足して、0 から 360 にし、色相として、使います。
| 進む向き | heading() |
色相 | 色 |
|---|---|---|---|
| 左 | −180 または 180 | 0 または 360 | 赤 |
| 上 | −90 | 90 | 黄緑 |
| 右 | 0 | 180 | 水色 |
| 下 | 90 | 270 | 紫 |
色相・彩度・明るさで指定するとき、不透明度の範囲は、0 から 1 です。0.08 は、8% です。
let particles = [];
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
colorMode(HSB);
background(240, 40, 8);
for (let i = 0; i < 600; i++) {
particles[i] = new Particle();
}
}
function draw() {
for (let i = 0; i < particles.length; i++) {
particles[i].follow();
particles[i].update();
particles[i].show();
}
}
class Particle {
constructor() {
this.pos = createVector(random(width), random(height));
this.prev = createVector(this.pos.x, this.pos.y);
this.vel = createVector(0, 0);
}
// 流れ場から、力を受ける
follow() {
let angle = noise(this.pos.x * 0.005, this.pos.y * 0.005) * 720;
let force = createVector(cos(angle), sin(angle));
force.mult(0.2);
this.vel.add(force);
this.vel.limit(2);
}
update() {
this.prev.x = this.pos.x;
this.prev.y = this.pos.y;
this.pos.add(this.vel);
// 端から出たら、反対側へ
if (this.pos.x > width) {
this.pos.x = 0;
this.prev.x = 0;
}
if (this.pos.x < 0) {
this.pos.x = width;
this.prev.x = width;
}
if (this.pos.y > height) {
this.pos.y = 0;
this.prev.y = 0;
}
if (this.pos.y < 0) {
this.pos.y = height;
this.prev.y = height;
}
}
show() {
stroke(this.vel.heading() + 180, 60, 100, 0.08);
strokeWeight(1);
line(this.prev.x, this.prev.y, this.pos.x, this.pos.y);
}
}
同じ向きへ流れる線は、同じ色に、なります。流れが、ゆるやかに曲がると、色も、なめらかに、移り変わります。向きの違う流れが、ぶつかるところでは、違う色の線が、交わります。
色を、でたらめに決めたのでは、こうは、なりません。動きそのものから、色を決めているので、形と色が、ひとつに、まとまります。
ステップ6:流れ場を、時間で変える
いまの流れ場は、止まっています。粒子は、やがて、同じ筋ばかりを、通るようになり、絵が、それ以上、変わらなくなります。
流れ場を、時間とともに、ゆっくり変化させます。noise に、3つ目の数を、渡します。
let angle = noise(this.pos.x * 0.005, this.pos.y * 0.005, zoff) * 720;
zoff は、毎フレーム、ほんの少しずつ、増える変数です。
zoff += 0.002;
3つの数を渡した noise は、積み重なった地層の、ある深さの面を、切り出したものだと、考えられます。3つ目の数を、少しずつ変えると、切り出す面が、少しずつ、ずれていきます。隣り合う面は、よく似ているので、流れ場は、形を保ちながら、じわじわと、変わっていきます。
筋の位置が、ゆっくり移動するので、粒子の通り道も、移っていきます。1本の線だった筋が、幅のある帯として、描かれます。リボンや、布のひだのような、面の広がりが、生まれます。
zoff の増やし方 |
絵の印象 |
|---|---|
| 0(増やさない) | くっきりした、細い筋。やがて、変化が止まる |
| 0.002 | なめらかな帯。ゆっくり、育ち続ける |
| 0.02 | 流れが、速く変わりすぎて、筋にならない。もやのようになる |
完成
let particles = [];
let zoff = 0;
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
colorMode(HSB);
background(240, 40, 8);
for (let i = 0; i < 600; i++) {
particles[i] = new Particle();
}
}
function draw() {
for (let i = 0; i < particles.length; i++) {
particles[i].follow();
particles[i].update();
particles[i].show();
}
zoff += 0.002;
}
class Particle {
constructor() {
this.pos = createVector(random(width), random(height));
this.prev = createVector(this.pos.x, this.pos.y);
this.vel = createVector(0, 0);
}
// 流れ場から、力を受ける
follow() {
let angle = noise(this.pos.x * 0.005, this.pos.y * 0.005, zoff) * 720;
let force = createVector(cos(angle), sin(angle));
force.mult(0.2);
this.vel.add(force);
this.vel.limit(2);
}
update() {
this.prev.x = this.pos.x;
this.prev.y = this.pos.y;
this.pos.add(this.vel);
// 端から出たら、反対側へ
if (this.pos.x > width) {
this.pos.x = 0;
this.prev.x = 0;
}
if (this.pos.x < 0) {
this.pos.x = width;
this.prev.x = width;
}
if (this.pos.y > height) {
this.pos.y = 0;
this.prev.y = 0;
}
if (this.pos.y < 0) {
this.pos.y = height;
this.prev.y = height;
}
}
show() {
stroke(this.vel.heading() + 180, 60, 100, 0.08);
strokeWeight(1);
line(this.prev.x, this.prev.y, this.pos.x, this.pos.y);
}
}
プログラムの全体の形を、まとめます。
| 部分 | 役割 |
|---|---|
setup |
背景を、1回だけ塗る。粒子を、600 個作る |
draw |
すべての粒子に、「流れに従う、動く、描く」をさせる。流れ場を、少し進める |
follow() |
いまの位置の、流れの角度を調べ、その向きの力を、速度に足す |
update() |
前の位置を覚え、位置を進める。端から出たら、反対側へ |
show() |
前の位置から、いまの位置へ、薄い線を引く。色は、進む向きから決める |
draw は、とても短く、粒子が何をするかは、すべて、クラスの中に、あります。
パラメータを、変えてみる
この作品は、いくつかの数字を変えるだけで、まったく違う表情に、なります。
| 数字 | 場所 | 小さくすると | 大きくすると |
|---|---|---|---|
600 |
粒子の数 | まばらな、細い線の絵 | 早く、濃く、描き込まれる |
0.005 |
流れの細かさ | 大きな、ゆったりした流れ | 細かく、渦の多い流れ |
720 |
角度の範囲 | 向きが、片寄る | 渦が増え、複雑になる |
0.2 |
力の大きさ | 勢いが勝ち、大きな弧を描く | 流れに、ぴたりと沿う |
2 |
速さの上限 | ゆっくり、なめらかな線 | 速く描けるが、線が粗くなる |
0.08 |
線の不透明度 | 淡く、長く育つ | すぐに、濃くなる |
0.002 |
流れ場の変わる速さ | くっきりした筋 | 幅のある帯。さらに大きいと、もや |
どの数字にも、「正しい値」は、ありません。変えては、眺め、気に入った絵が出るところを、探します。作るというより、育てて、見つける感覚に、近い作品です。
やってみよう
次の3つを作ってください。
- 色を、進む向きではなく、粒子の y 座標から決める。上のほうは青、下のほうは赤紫になるようにする
- 流れの角度を、45 度きざみに、そろえる。
round(angle / 45) * 45で、いちばん近い 45 の倍数に、丸められる - 流れ場に、中心のまわりを回る流れを、足す。粒子の位置から、中心を見た角度を、
atan2で求め、それに 90 度足した向きへ、力を加える
▶ 1. 位置で、色を決める
show() の中の、色を決める1行を、変えます。y 座標が 0 のとき、色相は 200(青)。400 のとき、300(赤紫)になります。
let particles = [];
let zoff = 0;
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
colorMode(HSB);
background(240, 40, 8);
for (let i = 0; i < 600; i++) {
particles[i] = new Particle();
}
}
function draw() {
for (let i = 0; i < particles.length; i++) {
particles[i].follow();
particles[i].update();
particles[i].show();
}
zoff += 0.002;
}
class Particle {
constructor() {
this.pos = createVector(random(width), random(height));
this.prev = createVector(this.pos.x, this.pos.y);
this.vel = createVector(0, 0);
}
// 流れ場から、力を受ける
follow() {
let angle = noise(this.pos.x * 0.005, this.pos.y * 0.005, zoff) * 720;
let force = createVector(cos(angle), sin(angle));
force.mult(0.2);
this.vel.add(force);
this.vel.limit(2);
}
update() {
this.prev.x = this.pos.x;
this.prev.y = this.pos.y;
this.pos.add(this.vel);
// 端から出たら、反対側へ
if (this.pos.x > width) {
this.pos.x = 0;
this.prev.x = 0;
}
if (this.pos.x < 0) {
this.pos.x = width;
this.prev.x = width;
}
if (this.pos.y > height) {
this.pos.y = 0;
this.prev.y = 0;
}
if (this.pos.y < 0) {
this.pos.y = height;
this.prev.y = height;
}
}
show() {
stroke(200 + this.pos.y * 0.25, 70, 100, 0.08);
strokeWeight(1);
line(this.prev.x, this.prev.y, this.pos.x, this.pos.y);
}
}
向きで色を決めたときは、流れに沿って、色が変わりました。位置で決めると、キャンバスの上から下へ、色の層が、できます。同じ流れ場でも、色の決め方で、絵の印象が、大きく変わります。
▶ 2. 角度を、45 度きざみにする
follow() の中で、角度を求めた直後に、1行、足します。
let particles = [];
let zoff = 0;
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
colorMode(HSB);
background(240, 40, 8);
for (let i = 0; i < 600; i++) {
particles[i] = new Particle();
}
}
function draw() {
for (let i = 0; i < particles.length; i++) {
particles[i].follow();
particles[i].update();
particles[i].show();
}
zoff += 0.002;
}
class Particle {
constructor() {
this.pos = createVector(random(width), random(height));
this.prev = createVector(this.pos.x, this.pos.y);
this.vel = createVector(0, 0);
}
// 流れ場から、力を受ける
follow() {
let angle = noise(this.pos.x * 0.005, this.pos.y * 0.005, zoff) * 720;
angle = round(angle / 45) * 45;
let force = createVector(cos(angle), sin(angle));
force.mult(0.2);
this.vel.add(force);
this.vel.limit(2);
}
update() {
this.prev.x = this.pos.x;
this.prev.y = this.pos.y;
this.pos.add(this.vel);
// 端から出たら、反対側へ
if (this.pos.x > width) {
this.pos.x = 0;
this.prev.x = 0;
}
if (this.pos.x < 0) {
this.pos.x = width;
this.prev.x = width;
}
if (this.pos.y > height) {
this.pos.y = 0;
this.prev.y = 0;
}
if (this.pos.y < 0) {
this.pos.y = height;
this.prev.y = height;
}
}
show() {
stroke(this.vel.heading() + 180, 60, 100, 0.08);
strokeWeight(1);
line(this.prev.x, this.prev.y, this.pos.x, this.pos.y);
}
}
流れの向きが、8つの方向だけに、なります。なめらかな曲線ではなく、直線と、角でできた、結晶や、回路のような絵に、なります。
angle / 45 で、「45 度の何個分か」を求め、round で、いちばん近い整数にし、もう一度 45 を掛けています。
▶ 3. 中心のまわりを回る流れを、足す
atan2(y の差, x の差) は、中心から見た、粒子の方角を、角度で返します。その方角に、90 度を足すと、円を描いて回る向きに、なります。
let particles = [];
let zoff = 0;
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
colorMode(HSB);
background(240, 40, 8);
for (let i = 0; i < 600; i++) {
particles[i] = new Particle();
}
}
function draw() {
for (let i = 0; i < particles.length; i++) {
particles[i].follow();
particles[i].update();
particles[i].show();
}
zoff += 0.002;
}
class Particle {
constructor() {
this.pos = createVector(random(width), random(height));
this.prev = createVector(this.pos.x, this.pos.y);
this.vel = createVector(0, 0);
}
// 流れ場から、力を受ける
follow() {
let angle = noise(this.pos.x * 0.005, this.pos.y * 0.005, zoff) * 720;
let force = createVector(cos(angle), sin(angle));
force.mult(0.2);
this.vel.add(force);
// 中心のまわりを回る流れを、足す
let around = atan2(this.pos.y - 200, this.pos.x - 200) + 90;
let swirl = createVector(cos(around), sin(around));
swirl.mult(0.12);
this.vel.add(swirl);
this.vel.limit(2);
}
update() {
this.prev.x = this.pos.x;
this.prev.y = this.pos.y;
this.pos.add(this.vel);
// 端から出たら、反対側へ
if (this.pos.x > width) {
this.pos.x = 0;
this.prev.x = 0;
}
if (this.pos.x < 0) {
this.pos.x = width;
this.prev.x = width;
}
if (this.pos.y > height) {
this.pos.y = 0;
this.prev.y = 0;
}
if (this.pos.y < 0) {
this.pos.y = height;
this.prev.y = height;
}
}
show() {
stroke(this.vel.heading() + 180, 60, 100, 0.08);
strokeWeight(1);
line(this.prev.x, this.prev.y, this.pos.x, this.pos.y);
}
}
ノイズの流れと、渦の流れ。2つの力を、両方とも、速度に足しています。力は、足し合わせることが、できます。粒子は、ノイズに揺さぶられながら、全体としては、中心のまわりを、巡ります。銀河や、台風のような形が、現れます。
まとめ
- 流れ場は、場所ごとに、向きが決まっているもの。
noise(x, y)に数を掛けて、角度にすると、なめらかに変わる流れ場になる noiseの値は、真ん中に集まりやすい。角度にするときは、360 より大きな数を掛けると、向きが、ばらける- 粒子は、いまいる場所の角度を調べ、その向きの力を、速度に足す。速度を置き換えるのではなく、力を足すと、勢いのある、なめらかな動きになる
- 背景を塗り直さず、前の位置から、いまの位置へ、とても薄い線を引くと、通ったあとが、濃淡のある絵になる
- 反対側へ移るときは、前の位置も、いっしょに移す
- 色を、進む向き(
heading())から決めると、形と色が、まとまる noiseの3つ目の数を、少しずつ増やすと、流れ場が、ゆっくり変化する- 絵を描いているのは、流れ場。粒子は、見えない流れを、写し取るペン先