forked from processing/p5.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
157 lines (141 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
let angle, px, py;
let img;
const sz = 25;
let stripColors = [];
function preload() {
img = loadImage('../assets/UV_Grid_Sm.jpg');
}
function setup() {
createCanvas(600, 600, WEBGL);
setAttributes('antialias', true);
textureMode(NORMAL);
fill(63, 81, 181);
strokeWeight(2);
for (let i = 0; i < 12; i++) {
stripColors.push([random(255), random(255), random(255)]);
}
}
function draw() {
background(250);
// Reference: TRIANGLE_STRIP
push();
translate(-width / 3, -240);
drawStrip(TRIANGLE_STRIP);
pop();
// Test 1: QUADS
push();
translate(0, -240);
drawStrip(QUADS);
pop();
// Test 2: QUAD_STRIP
push();
translate(width / 3, -240);
drawStrip(QUAD_STRIP);
pop();
line(-width / 2, -160, 0, width / 2, -160, 0);
ngon(5, -200, 0, 120);
ngon(8, 0, 0, 120);
ngon(11, 200, 0, 120);
drawQuads(180);
}
function drawStrip(mode) {
rotate(PI / 2);
scale(0.3);
translate(0, -250);
beginShape(mode);
let vertexIndex = 0;
for (let y = 0; y <= 500; y += 100) {
let sides = [-1, 1];
if (mode === QUADS && y % 200 !== 0) {
// QUAD_STRIP and TRIANGLE_STRIP need the vertices of each shared side
// ordered in the same way:
// 0--2--4--6
// | | | | ⬇️
// 1--3--5--7
//
// ...but QUADS orders vertices in a consisten CCW or CW manner around
// each quad, meaning each side will be in the reverse order of the
// previous:
// 0--3 4--7
// | | | | 🔄
// 1--2 5--6
sides.reverse();
}
for (const side of sides) {
fill(...stripColors[vertexIndex]);
vertex(side * 40, y);
vertexIndex++;
}
}
endShape();
}
function ngon(n, x, y, d) {
beginShape(TESS);
for (let i = 0; i < n + 1; i++) {
angle = TWO_PI / n * i;
px = x + sin(angle) * d / 2;
py = y - cos(angle) * d / 2;
vertex(px, py);
}
for (i = 0; i < n + 1; i++) {
angle = TWO_PI / n * i;
px = x + sin(angle) * d / 4;
py = y - cos(angle) * d / 4;
vertex(px, py);
}
endShape();
}
function drawQuads(y) {
// 2 args
push();
fill(255, 0, 0);
translate(-3 / 8 * width, y);
beginShape(TRIANGLES);
vertex(-sz, -sz);
vertex(sz, -sz);
vertex(sz, sz);
vertex(sz, sz);
vertex(-sz, sz);
vertex(-sz, -sz);
endShape();
pop();
// 3 args
push();
fill(0, 0, 255);
translate(-1 / 8 * width, y);
beginShape(TRIANGLES);
vertex(-sz, -sz, 0);
vertex(sz, -sz, 0);
vertex(sz, sz, 0);
vertex(sz, sz, 0);
vertex(-sz, sz, 0);
vertex(-sz, -sz, 0);
endShape();
pop();
// 4 args
push();
texture(img);
translate(1 / 8 * width, y);
beginShape(TRIANGLES);
vertex(-sz, -sz, 0, 0);
vertex(sz, -sz, 1, 0);
vertex(sz, sz, 1, 1);
vertex(sz, sz, 1, 1);
vertex(-sz, sz, 0, 1);
vertex(-sz, -sz, 0, 0);
endShape();
pop();
// 5 args
push();
texture(img);
translate(3 / 8 * width, y);
beginShape(TRIANGLES);
vertex(-sz, -sz, 0, 0, 0);
vertex(sz, -sz, 0, 1, 0);
vertex(sz, sz, 0, 1, 1);
vertex(sz, sz, 0, 1, 1);
vertex(-sz, sz, 0, 0, 1);
vertex(-sz, -sz, 0, 0, 0);
endShape();
pop();
}