-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
160 lines (144 loc) · 3.06 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
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
160
const boxPos = 0;
const boxes = [];
const boxNum = 1000;
const front = 130;
const size = 6;
const speed = 0.5;
const back = -front;
const lowAlpha = 100;
const highAlpha = 255;
let alpha = lowAlpha;
let lightsOn = true;
const camRotation = {
x: 30,
y: 30,
z: 0,
};
const camCurrent = {
x: 30,
y: 30,
z: 0,
};
const lerpRate = 0.1;
const colours = [
[255, 255, 0],
[0, 255, 255],
[255, 0, 255],
];
const bgCol = [220, 220, 200];
let strokeBox = false;
const fillColours = [colours, [bgCol]];
let currentFillIndex = 1;
function setup() {
const canvas = createCanvas(500, 500, WEBGL);
canvas.parent("canvas-parent");
ortho(-width / 2, width / 2, height / 2, -height / 2, -1000, 1000);
angleMode(DEGREES);
for (let i = 0; i < boxNum; i++) {
boxes.push(randomBox());
}
blendMode(MULTIPLY);
}
function draw() {
background(bgCol);
// background(0);
if (lightsOn) {
directionalLight(255, 255, 255, 100, 100, 100);
ambientLight(255);
}
// orbitControl();
updateCamPos();
for (let i = 0; i < boxNum; i++) {
let b = boxes[i];
push();
translate(b.x, b.y, b.z);
fill(...b.c, b.a);
if (strokeBox) {
stroke(0);
} else {
noStroke();
}
box(b.w, b.h, b.d);
b.z -= speed;
if (b.z < back) {
// b.z= front
boxes[i] = randomBox(front);
}
pop();
}
}
function randomBox(z) {
return {
w: ceil(random(size)) * 10,
h: ceil(random(size)) * 10,
d: ceil(random(size)) * 10,
x: random(-width / 2 + width / 4, width / 2 - width / 4),
y: random(-width / 2 + width / 4, width / 2 - width / 4),
z: z || random(-front, front),
c: random(fillColours[currentFillIndex]),
a: alpha,
};
}
function keyPressed() {
actionSelect(key);
}
function mouseReleased() {
saveSketch();
}
function actionSelect(key) {
if (key === "ArrowUp") {
camRotation.x += 30;
}
if (key === "ArrowDown") {
camRotation.x -= 30;
}
if (key === "ArrowLeft") {
camRotation.y += 30;
}
if (key === "ArrowRight") {
camRotation.y -= 30;
}
if (key === "Enter") {
camRotation.x = 30;
camRotation.y = 30;
}
if (key === "l") {
lightsOn = !lightsOn;
}
if (key === "a") {
if (alpha === lowAlpha) {
alpha = highAlpha;
} else {
alpha = lowAlpha;
}
}
if (key === "c") {
currentFillIndex =
currentFillIndex < fillColours.length - 1 ? currentFillIndex + 1 : 0;
// console.log(currentFillIndex);
}
if (key === "o") {
strokeBox = !strokeBox;
}
if (key === "s") {
saveSketch(true);
}
}
function saveSketch(force = false) {
if (
(mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) ||
force
) {
const time = new Date().getTime();
saveCanvas("ortho amcc - " + time, "jpg");
}
}
function updateCamPos() {
translate(0, 0, 0);
camCurrent.x = lerp(camCurrent.x, camRotation.x, lerpRate);
camCurrent.y = lerp(camCurrent.y, camRotation.y, lerpRate);
camCurrent.z = lerp(camCurrent.z, camRotation.z, lerpRate);
rotateX(camCurrent.x);
rotateY(camCurrent.y);
rotateZ(camCurrent.z);
}