-
Notifications
You must be signed in to change notification settings - Fork 10
/
index-pattern.js
159 lines (131 loc) · 4.04 KB
/
index-pattern.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import * as dat from 'dat.gui';
import * as tome from 'chromotome';
import SimplexNoise from 'simplex-noise';
import { draw_poly } from './display';
let sketch = function(p) {
let simplex;
let noise_grid;
let opts;
let palette;
let tick;
const grid_dim_x = 1100;
const grid_dim_y = 1100;
const padding = 0;
const canvas_dim_x = grid_dim_x + 2 * padding;
const canvas_dim_y = grid_dim_y + 2 * padding;
const cell_dim = 5;
const nx = grid_dim_x / cell_dim;
const ny = grid_dim_y / cell_dim;
p.setup = function() {
p.createCanvas(canvas_dim_x, canvas_dim_y);
p.frameRate(5);
opts = {
noise_scale: 50,
noise_persistence: 0.5,
apply_sigmoid: 0,
num_shapes: 20,
bottom_size: -0.1,
top_size: 0.5,
gradient: 'radial',
palette: 'delphi'
};
const gui = new dat.GUI();
gui.width = 300;
const f1 = gui.addFolder('Noise field');
f1.add(opts, 'noise_scale', 10, 200, 20).name('Noise scale');
f1.add(opts, 'noise_persistence', 0.1, 1, 0.05).name('Noise persistence');
f1.add(opts, 'num_shapes', 5, 50, 5).name('Layers');
f1.add(opts, 'bottom_size', -1, 1, 0.1).name('Bottom threshold');
f1.add(opts, 'top_size', -1, 1, 0.1).name('Top threshold');
f1.add(opts, 'gradient', ['fill', 'linear', 'radial', 'ring']).name('Gradient');
f1.open();
const f2 = gui.addFolder('Style');
f2.add(opts, 'palette', tome.getNames());
f2.open();
reset();
};
function reset() {
palette = tome.get(opts.palette);
palette.colors = p.shuffle(palette.colors);
tick = 0;
}
p.draw = function() {
p.push();
p.translate(padding, padding);
if (tick === 0) {
p.background(palette.background ? palette.background : '#f5f5f5');
}
if (tick < opts.num_shapes) {
const range = opts.top_size - opts.bottom_size;
const z_val = opts.bottom_size + (range * tick) / opts.num_shapes;
const col = palette.colors[tick % palette.colors.length];
simplex = new SimplexNoise();
noise_grid = build_noise_grid(opts.gradient);
p.fill(col);
process_grid(z_val);
p.pop();
}
tick++;
if (tick === opts.num_shapes + 5) reset();
};
function process_grid(z_val) {
p.push();
for (let y = 0; y < ny; y++) {
p.push();
for (let x = 0; x < nx; x++) {
process_cell(x, y, z_val);
p.translate(cell_dim, 0);
}
p.pop();
p.translate(0, cell_dim);
}
p.pop();
}
function process_cell(x, y, threshold) {
const v1 = get_noise(x, y);
const v2 = get_noise(x + 1, y);
const v3 = get_noise(x + 1, y + 1);
const v4 = get_noise(x, y + 1);
const b1 = v1 > threshold ? 8 : 0;
const b2 = v2 > threshold ? 4 : 0;
const b3 = v3 > threshold ? 2 : 0;
const b4 = v4 > threshold ? 1 : 0;
const id = b1 + b2 + b3 + b4;
if (id === 0) return;
draw_poly(p, id, v1, v2, v3, v4, threshold, cell_dim);
}
function get_noise(x, y) {
return noise_grid[y][x];
}
function build_noise_grid(gradient) {
return [...Array(ny + 1)].map((_, y) =>
[...Array(nx + 1)].map((_, x) => sum_octave(16, x, y) + get_offset(gradient, x, y))
);
}
function get_offset(gradient, x, y) {
if (gradient === 'fill') return 0;
if (gradient === 'linear') return y / nx - 0.5;
if (gradient === 'radial') return 0.2 - distance_from_centre(x, y) / (nx / 2);
if (gradient === 'ring') return -Math.abs(-1 + distance_from_centre(x, y) / (nx / 4));
}
function distance_from_centre(x, y) {
return Math.sqrt(Math.pow(nx / 2 - x, 2) + Math.pow(ny / 2 - y, 2));
}
function sum_octave(num_iterations, x, y) {
let noise = 0;
let maxAmp = 0;
let amp = 1;
let freq = 1 / opts.noise_scale;
for (let i = 0; i < num_iterations; i++) {
noise += simplex.noise3D(x * freq, y * freq, i) * amp;
maxAmp += amp;
amp *= opts.noise_persistence;
freq *= 2;
}
return noise / maxAmp;
}
p.keyPressed = function() {
if (p.keyCode === 80) p.saveCanvas('topollock', 'jpeg');
};
};
new p5(sketch);