|
1 | 1 | /* |
2 | 2 | * @name Geometries |
3 | | - * @arialabel Seven 3D shapes in neon gradient rotating on a white background. Shapes include cube, cylinder, ring, pyramid, sphere, plane, and ellipsoid. |
4 | | - * @description There are seven 3D primitives in p5 now. |
| 3 | + * @description p5's |
| 4 | + * <a href="https://p5js.org/reference/#/p5/WEBGL" target="_blank">WEBGL</a> |
| 5 | + * mode includes 7 primitive shapes. Those shapes are plane, box, |
| 6 | + * cylinder, cone, torus, sphere, and ellipsoid. Additionally, |
| 7 | + * <a href="https://p5js.org/reference/#/p5/model" target="_blank">model()</a> |
| 8 | + * displays a custom geometry loaded via |
| 9 | + * <a href="https://p5js.org/reference/#/p5/loadModel" target="_blank">loadModel()</a>. |
| 10 | + * This example includes each of the primitive shapes. It also includes a model |
| 11 | + * from <a href="https://nasa3d.arc.nasa.gov/models" target="_blank">NASA's collection</a>. |
5 | 12 | */ |
6 | 13 |
|
| 14 | +// Variable to store NASA model |
| 15 | +let astronaut; |
| 16 | + |
| 17 | +function preload() { |
| 18 | + astronaut = loadModel("assets/astronaut.obj"); |
| 19 | +} |
| 20 | + |
7 | 21 | function setup() { |
8 | 22 | createCanvas(710, 400, WEBGL); |
9 | 23 |
|
| 24 | + angleMode(DEGREES); |
| 25 | + |
| 26 | + // Use a normal material, which uses color to illustrate |
| 27 | + // what direction each face of the geometry is pointing |
| 28 | + normalMaterial(); |
| 29 | + |
10 | 30 | describe( |
11 | | - 'a 3d example containing seven primitive objects, a plane, box, cylinder, cone, torus, sphere, and ellipsoid.' |
| 31 | + "Eight 3D shapes: a plane, box, cylinder, cone, torus, sphere, " + |
| 32 | + "ellipsoid, and a model of an astronaut. Each shape is rotating in " + |
| 33 | + "all directions. The surface of the shapes are multicolored." |
12 | 34 | ); |
13 | 35 | } |
14 | 36 |
|
15 | 37 | function draw() { |
16 | 38 | background(250); |
17 | 39 |
|
18 | | - normalMaterial(); |
| 40 | + // Plane |
19 | 41 | push(); |
20 | | - translate(-240, -100, 0); |
21 | | - rotateZ(frameCount * 0.01); |
22 | | - rotateX(frameCount * 0.01); |
23 | | - rotateY(frameCount * 0.01); |
| 42 | + translate(-250, -100, 0); |
| 43 | + rotateZ(frameCount); |
| 44 | + rotateX(frameCount); |
| 45 | + rotateY(frameCount); |
24 | 46 | plane(70); |
25 | 47 | pop(); |
26 | 48 |
|
| 49 | + // Box |
27 | 50 | push(); |
28 | | - translate(0, -100, 0); |
29 | | - rotateZ(frameCount * 0.01); |
30 | | - rotateX(frameCount * 0.01); |
31 | | - rotateY(frameCount * 0.01); |
| 51 | + translate(-75, -100, 0); |
| 52 | + rotateZ(frameCount); |
| 53 | + rotateX(frameCount); |
| 54 | + rotateY(frameCount); |
32 | 55 | box(70, 70, 70); |
33 | 56 | pop(); |
34 | 57 |
|
| 58 | + // Cylinder |
35 | 59 | push(); |
36 | | - translate(240, -100, 0); |
37 | | - rotateZ(frameCount * 0.01); |
38 | | - rotateX(frameCount * 0.01); |
39 | | - rotateY(frameCount * 0.01); |
| 60 | + translate(100, -100, 0); |
| 61 | + rotateZ(frameCount); |
| 62 | + rotateX(frameCount); |
| 63 | + rotateY(frameCount); |
40 | 64 | cylinder(70, 70); |
41 | 65 | pop(); |
42 | 66 |
|
| 67 | + // Cone |
43 | 68 | push(); |
44 | | - translate(-250, 100, 0); |
45 | | - rotateZ(frameCount * 0.01); |
46 | | - rotateX(frameCount * 0.01); |
47 | | - rotateY(frameCount * 0.01); |
| 69 | + translate(275, -100, 0); |
| 70 | + rotateZ(frameCount); |
| 71 | + rotateX(frameCount); |
| 72 | + rotateY(frameCount); |
48 | 73 | cone(50, 70); |
49 | 74 | pop(); |
50 | 75 |
|
| 76 | + // Torus |
51 | 77 | push(); |
52 | | - translate(-75, 100, 0); |
53 | | - rotateZ(frameCount * 0.01); |
54 | | - rotateX(frameCount * 0.01); |
55 | | - rotateY(frameCount * 0.01); |
| 78 | + translate(-250, 100, 0); |
| 79 | + rotateZ(frameCount); |
| 80 | + rotateX(frameCount); |
| 81 | + rotateY(frameCount); |
56 | 82 | torus(50, 20); |
57 | 83 | pop(); |
58 | 84 |
|
| 85 | + // Sphere |
59 | 86 | push(); |
60 | | - translate(100, 100, 0); |
61 | | - rotateZ(frameCount * 0.01); |
62 | | - rotateX(frameCount * 0.01); |
63 | | - rotateY(frameCount * 0.01); |
| 87 | + translate(-75, 100, 0); |
| 88 | + rotateZ(frameCount); |
| 89 | + rotateX(frameCount); |
| 90 | + rotateY(frameCount); |
| 91 | + |
| 92 | + // Show black stroke to help visualize movement |
| 93 | + stroke(0); |
64 | 94 | sphere(50); |
65 | 95 | pop(); |
66 | 96 |
|
| 97 | + // Ellipsoid |
| 98 | + push(); |
| 99 | + translate(100, 100, 0); |
| 100 | + rotateZ(frameCount); |
| 101 | + rotateX(frameCount); |
| 102 | + rotateY(frameCount); |
| 103 | + ellipsoid(20, 40, 40); |
| 104 | + pop(); |
| 105 | + |
| 106 | + // Astronaut |
67 | 107 | push(); |
68 | 108 | translate(275, 100, 0); |
69 | | - rotateZ(frameCount * 0.01); |
70 | | - rotateX(frameCount * 0.01); |
71 | | - rotateY(frameCount * 0.01); |
72 | | - ellipsoid(30, 40, 40); |
| 109 | + rotateZ(180); |
| 110 | + rotateZ(frameCount); |
| 111 | + rotateX(frameCount); |
| 112 | + rotateY(frameCount); |
| 113 | + model(astronaut); |
73 | 114 | pop(); |
74 | 115 | } |
0 commit comments