|
| 1 | +import { visualSuite, visualTest } from '../visualTest'; |
| 2 | + |
| 3 | +visualSuite('Shape drawing', function() { |
| 4 | + for (const mode of ['2D', 'WebGL']) { |
| 5 | + visualSuite(`${mode} mode`, function() { |
| 6 | + const setup = (p5) => { |
| 7 | + p5.createCanvas(50, 50, mode === '2D' ? p5.P2D : p5.WEBGL); |
| 8 | + if (mode !== '2D') { |
| 9 | + p5.translate(-p5.width / 2, -p5.height / 2); |
| 10 | + } |
| 11 | + p5.background(200); |
| 12 | + p5.fill(255); |
| 13 | + p5.stroke(0); |
| 14 | + } |
| 15 | + |
| 16 | + visualTest('Drawing polylines', function(p5, screenshot) { |
| 17 | + setup(p5); |
| 18 | + p5.beginShape(); |
| 19 | + p5.vertex(10, 10); |
| 20 | + p5.vertex(15, 40); |
| 21 | + p5.vertex(40, 35); |
| 22 | + p5.vertex(25, 15); |
| 23 | + p5.vertex(15, 25); |
| 24 | + p5.endShape(); |
| 25 | + screenshot(); |
| 26 | + }); |
| 27 | + |
| 28 | + visualTest('Drawing with contours', function(p5, screenshot) { |
| 29 | + setup(p5); |
| 30 | + |
| 31 | + const vertexCircle = (x, y, r, direction) => { |
| 32 | + for (let i = 0; i <= 12; i++) { |
| 33 | + const angle = p5.map(i, 0, 12, 0, p5.TWO_PI) * direction; |
| 34 | + p5.vertex(x + r * p5.cos(angle), y + r * p5.sin(angle)); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + p5.beginShape(); |
| 39 | + vertexCircle(15, 15, 10, 1); |
| 40 | + |
| 41 | + // Inner cutout |
| 42 | + p5.beginContour(); |
| 43 | + vertexCircle(15, 15, 5, -1); |
| 44 | + p5.endContour(); |
| 45 | + |
| 46 | + // Outer shape |
| 47 | + p5.beginContour(); |
| 48 | + vertexCircle(30, 30, 8, -1); |
| 49 | + p5.endContour(); |
| 50 | + p5.endShape(); |
| 51 | + |
| 52 | + screenshot(); |
| 53 | + }); |
| 54 | + |
| 55 | + visualTest('Drawing triangle fans', function(p5, screenshot) { |
| 56 | + setup(p5); |
| 57 | + p5.beginShape(p5.TRIANGLE_FAN); |
| 58 | + p5.vertex(25, 25); |
| 59 | + for (let i = 0; i <= 12; i++) { |
| 60 | + const angle = p5.map(i, 0, 12, 0, p5.TWO_PI); |
| 61 | + p5.vertex(25 + 10*p5.cos(angle), 25 + 10*p5.sin(angle)); |
| 62 | + } |
| 63 | + p5.endShape(); |
| 64 | + screenshot(); |
| 65 | + }); |
| 66 | + |
| 67 | + visualTest('Drawing triangle strips', function(p5, screenshot) { |
| 68 | + setup(p5); |
| 69 | + p5.beginShape(p5.TRIANGLE_STRIP); |
| 70 | + p5.vertex(10, 10); |
| 71 | + p5.vertex(30, 10); |
| 72 | + p5.vertex(15, 20); |
| 73 | + p5.vertex(35, 20); |
| 74 | + p5.vertex(10, 40); |
| 75 | + p5.vertex(30, 40); |
| 76 | + p5.endShape(); |
| 77 | + screenshot(); |
| 78 | + }); |
| 79 | + |
| 80 | + visualTest('Drawing quad strips', function(p5, screenshot) { |
| 81 | + setup(p5); |
| 82 | + p5.beginShape(p5.QUAD_STRIP); |
| 83 | + p5.vertex(10, 10); |
| 84 | + p5.vertex(30, 10); |
| 85 | + p5.vertex(15, 20); |
| 86 | + p5.vertex(35, 20); |
| 87 | + p5.vertex(10, 40); |
| 88 | + p5.vertex(30, 40); |
| 89 | + p5.endShape(); |
| 90 | + screenshot(); |
| 91 | + }); |
| 92 | + |
| 93 | + visualTest('Drawing closed polylines', function(p5, screenshot) { |
| 94 | + setup(p5); |
| 95 | + p5.beginShape(); |
| 96 | + p5.vertex(10, 10); |
| 97 | + p5.vertex(15, 40); |
| 98 | + p5.vertex(40, 35); |
| 99 | + p5.vertex(25, 15); |
| 100 | + p5.vertex(15, 25); |
| 101 | + p5.endShape(p5.CLOSE); |
| 102 | + screenshot(); |
| 103 | + }); |
| 104 | + |
| 105 | + visualTest('Drawing with curves', function(p5, screenshot) { |
| 106 | + setup(p5); |
| 107 | + p5.beginShape(); |
| 108 | + p5.curveVertex(10, 10); |
| 109 | + p5.curveVertex(10, 10); |
| 110 | + p5.curveVertex(15, 40); |
| 111 | + p5.curveVertex(40, 35); |
| 112 | + p5.curveVertex(25, 15); |
| 113 | + p5.curveVertex(15, 25); |
| 114 | + p5.curveVertex(15, 25); |
| 115 | + p5.endShape(); |
| 116 | + screenshot(); |
| 117 | + }); |
| 118 | + |
| 119 | + visualTest('Drawing with curves with tightness', function(p5, screenshot) { |
| 120 | + setup(p5); |
| 121 | + p5.curveTightness(0.5); |
| 122 | + p5.beginShape(); |
| 123 | + p5.curveVertex(10, 10); |
| 124 | + p5.curveVertex(10, 10); |
| 125 | + p5.curveVertex(15, 40); |
| 126 | + p5.curveVertex(40, 35); |
| 127 | + p5.curveVertex(25, 15); |
| 128 | + p5.curveVertex(15, 25); |
| 129 | + p5.curveVertex(15, 25); |
| 130 | + p5.endShape(); |
| 131 | + screenshot(); |
| 132 | + }); |
| 133 | + |
| 134 | + visualTest('Drawing closed curve loops', function(p5, screenshot) { |
| 135 | + setup(p5); |
| 136 | + p5.beginShape(); |
| 137 | + p5.curveVertex(10, 10); |
| 138 | + p5.curveVertex(15, 40); |
| 139 | + p5.curveVertex(40, 35); |
| 140 | + p5.curveVertex(25, 15); |
| 141 | + p5.curveVertex(15, 25); |
| 142 | + // Repeat first 3 points |
| 143 | + p5.curveVertex(10, 10); |
| 144 | + p5.curveVertex(15, 40); |
| 145 | + p5.curveVertex(40, 35); |
| 146 | + p5.endShape(); |
| 147 | + screenshot(); |
| 148 | + }); |
| 149 | + |
| 150 | + visualTest('Drawing with cubic beziers', function(p5, screenshot) { |
| 151 | + setup(p5); |
| 152 | + p5.beginShape(); |
| 153 | + p5.vertex(10, 10); |
| 154 | + p5.bezierVertex(10, 10, 15, 40, 40, 35); |
| 155 | + p5.bezierVertex(25, 15, 15, 25, 15, 25); |
| 156 | + p5.endShape(); |
| 157 | + screenshot(); |
| 158 | + }); |
| 159 | + |
| 160 | + visualTest('Drawing with quadratic beziers', function(p5, screenshot) { |
| 161 | + setup(p5); |
| 162 | + p5.beginShape(); |
| 163 | + p5.vertex(10, 10); |
| 164 | + p5.quadraticVertex(10, 10, 15, 40); |
| 165 | + p5.quadraticVertex(40, 35, 25, 15); |
| 166 | + p5.quadraticVertex(15, 25, 10, 10); |
| 167 | + p5.endShape(); |
| 168 | + screenshot(); |
| 169 | + }); |
| 170 | + |
| 171 | + if (mode === 'WebGL') { |
| 172 | + visualTest('3D vertex coordinates', function(p5, screenshot) { |
| 173 | + setup(p5); |
| 174 | + |
| 175 | + p5.beginShape(p5.QUAD_STRIP); |
| 176 | + p5.vertex(10, 10, 0); |
| 177 | + p5.vertex(10, 40, -150); |
| 178 | + p5.vertex(40, 10, 150); |
| 179 | + p5.vertex(40, 40, 200); |
| 180 | + p5.endShape(); |
| 181 | + |
| 182 | + screenshot(); |
| 183 | + }); |
| 184 | + |
| 185 | + visualTest('3D quadratic coordinates', function(p5, screenshot) { |
| 186 | + setup(p5); |
| 187 | + |
| 188 | + p5.beginShape(); |
| 189 | + p5.vertex(10, 10, 0); |
| 190 | + p5.vertex(10, 40, -150); |
| 191 | + p5.quadraticVertex(40, 40, 200, 40, 10, 150); |
| 192 | + p5.endShape(p5.CLOSE); |
| 193 | + |
| 194 | + screenshot(); |
| 195 | + }); |
| 196 | + |
| 197 | + visualTest('3D cubic coordinates', function(p5, screenshot) { |
| 198 | + setup(p5); |
| 199 | + |
| 200 | + p5.beginShape(); |
| 201 | + p5.vertex(10, 10, 0); |
| 202 | + p5.vertex(10, 40, -150); |
| 203 | + p5.bezierVertex(40, 40, 200, 40, 10, 150, 10, 10, 0); |
| 204 | + p5.endShape(); |
| 205 | + |
| 206 | + screenshot(); |
| 207 | + }); |
| 208 | + |
| 209 | + visualTest('Texture coordinates', async function(p5, screenshot) { |
| 210 | + setup(p5); |
| 211 | + const tex = await p5.loadImage('/unit/assets/cat.jpg'); |
| 212 | + |
| 213 | + p5.texture(tex); |
| 214 | + p5.beginShape(p5.QUAD_STRIP); |
| 215 | + p5.vertex(10, 10, 0, 0, 0); |
| 216 | + p5.vertex(45, 5, 0, tex.width, 0); |
| 217 | + p5.vertex(15, 35, 0, 0, tex.height); |
| 218 | + p5.vertex(40, 45, 0, tex.width, tex.height); |
| 219 | + p5.endShape(); |
| 220 | + |
| 221 | + screenshot(); |
| 222 | + }); |
| 223 | + |
| 224 | + visualTest('Normalized texture coordinates', async function(p5, screenshot) { |
| 225 | + setup(p5); |
| 226 | + const tex = await p5.loadImage('/unit/assets/cat.jpg'); |
| 227 | + |
| 228 | + p5.texture(tex); |
| 229 | + p5.textureMode(p5.NORMAL); |
| 230 | + p5.beginShape(p5.QUAD_STRIP); |
| 231 | + p5.vertex(10, 10, 0, 0, 0); |
| 232 | + p5.vertex(45, 5, 0, 1, 0); |
| 233 | + p5.vertex(15, 35, 0, 0, 1); |
| 234 | + p5.vertex(40, 45, 0, 1, 1); |
| 235 | + p5.endShape(); |
| 236 | + |
| 237 | + screenshot(); |
| 238 | + }); |
| 239 | + |
| 240 | + visualTest('Per-vertex fills', async function(p5, screenshot) { |
| 241 | + setup(p5); |
| 242 | + p5.beginShape(p5.QUAD_STRIP); |
| 243 | + p5.fill(0); |
| 244 | + p5.vertex(10, 10); |
| 245 | + p5.fill(255, 0, 0); |
| 246 | + p5.vertex(45, 5); |
| 247 | + p5.fill(0, 255, 0); |
| 248 | + p5.vertex(15, 35); |
| 249 | + p5.fill(255, 255, 0); |
| 250 | + p5.vertex(40, 45); |
| 251 | + p5.endShape(); |
| 252 | + |
| 253 | + screenshot(); |
| 254 | + }); |
| 255 | + |
| 256 | + visualTest('Per-vertex strokes', async function(p5, screenshot) { |
| 257 | + setup(p5); |
| 258 | + p5.strokeWeight(5); |
| 259 | + p5.beginShape(p5.QUAD_STRIP); |
| 260 | + p5.stroke(0); |
| 261 | + p5.vertex(10, 10); |
| 262 | + p5.stroke(255, 0, 0); |
| 263 | + p5.vertex(45, 5); |
| 264 | + p5.stroke(0, 255, 0); |
| 265 | + p5.vertex(15, 35); |
| 266 | + p5.stroke(255, 255, 0); |
| 267 | + p5.vertex(40, 45); |
| 268 | + p5.endShape(); |
| 269 | + |
| 270 | + screenshot(); |
| 271 | + }); |
| 272 | + |
| 273 | + visualTest('Per-vertex normals', async function(p5, screenshot) { |
| 274 | + setup(p5); |
| 275 | + p5.normalMaterial(); |
| 276 | + p5.beginShape(p5.QUAD_STRIP); |
| 277 | + p5.normal(-1, -1, 1); |
| 278 | + p5.vertex(10, 10); |
| 279 | + p5.normal(1, -1, 1); |
| 280 | + p5.vertex(45, 5); |
| 281 | + p5.normal(-1, 1, 1); |
| 282 | + p5.vertex(15, 35); |
| 283 | + p5.normal(1, 1, 1); |
| 284 | + p5.vertex(40, 45); |
| 285 | + p5.endShape(); |
| 286 | + |
| 287 | + screenshot(); |
| 288 | + }); |
| 289 | + } |
| 290 | + }); |
| 291 | + } |
| 292 | +}); |
0 commit comments