-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
122 lines (111 loc) · 2.12 KB
/
sketch.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
let res = 5;
let field = new Array();
let cols, rows;
//noise variables
let noise;
let speedRateX = 0.1;
let speedRateY = 0.1;
let speedRateZ = 0.015;
let zo = 0;
function setup() {
createCanvas(min(600, windowWidth), min(600, windowHeight));
// createCanvas(windowWidth, windowHeight);
noise = new OpenSimplexNoise(Date.now());
cols = floor(width / res) + 1;
rows = floor(height / res) + 1;
for (let i = 0; i < cols; i++) {
field[i] = new Array();
}
}
function draw() {
clear();
let xo = 0;
noStroke();
for (let i = 0; i < cols; i++) {
xo += speedRateX;
let yo = 0;
for (let j = 0; j < rows; j++) {
yo += speedRateY;
let depth = noise.noise3D(xo, yo, zo) + 1;
field[i][j] = depth - 1;
fill(floor(pow(depth, 3) * 50));
rect(i * res, j * res, res, res);
}
}
zo += speedRateZ;
for (let i = 0; i < cols - 1; i++) {
for (let j = 0; j < rows - 1; j++) {
let x = i * res;
let y = j * res;
let state = getState(
ceil(field[i][j]),
ceil(field[i + 1][j]),
ceil(field[i + 1][j + 1]),
ceil(field[i][j + 1])
);
let a, b, c, d;
a = createVector(x + res * 0.5, y);
b = createVector(x + res, y + res * 0.5);
c = createVector(x + res * 0.5, y + res);
d = createVector(x, y + res * 0.5);
strokeWeight(2);
stroke(250);
switch (state) {
case 0:
break;
case 1:
lineV2V(c, d);
break;
case 2:
lineV2V(c, b);
break;
case 3:
lineV2V(b, d);
break;
case 4:
lineV2V(a, b);
break;
case 5:
lineV2V(a, d);
lineV2V(c, b);
break;
case 6:
lineV2V(a, c);
break;
case 7:
lineV2V(a, d);
break;
case 8:
lineV2V(a, d);
break;
case 9:
lineV2V(a, c);
break;
case 10:
lineV2V(a, b);
lineV2V(c, d);
break;
case 11:
lineV2V(a, b);
break;
case 12:
lineV2V(b, d);
break;
case 13:
lineV2V(b, c);
break;
case 14:
lineV2V(c, d);
break;
case 15:
break;
}
}
}
}
function getState(a, b, c, d) {
return a * 8 + b * 4 + c * 2 + d * 1;
}
function lineV2V(a, b) {
line(a.x, a.y, b.x, b.y);
}