|
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, ellipsoid, and a model of an astronaut. Each shape is rotating in all directions. The surface of the shapes are multicolored.' |
12 | 32 | ); |
13 | 33 | } |
14 | 34 |
|
15 | 35 | function draw() { |
16 | 36 | background(250); |
17 | 37 |
|
18 | | - normalMaterial(); |
| 38 | + // Plane |
19 | 39 | push(); |
20 | | - translate(-240, -100, 0); |
21 | | - rotateZ(frameCount * 0.01); |
22 | | - rotateX(frameCount * 0.01); |
23 | | - rotateY(frameCount * 0.01); |
| 40 | + translate(-250, -100, 0); |
| 41 | + rotateWithFrameCount(); |
24 | 42 | plane(70); |
25 | 43 | pop(); |
26 | 44 |
|
| 45 | + // Box |
27 | 46 | push(); |
28 | | - translate(0, -100, 0); |
29 | | - rotateZ(frameCount * 0.01); |
30 | | - rotateX(frameCount * 0.01); |
31 | | - rotateY(frameCount * 0.01); |
| 47 | + translate(-75, -100, 0); |
| 48 | + rotateWithFrameCount(); |
32 | 49 | box(70, 70, 70); |
33 | 50 | pop(); |
34 | 51 |
|
| 52 | + // Cylinder |
35 | 53 | push(); |
36 | | - translate(240, -100, 0); |
37 | | - rotateZ(frameCount * 0.01); |
38 | | - rotateX(frameCount * 0.01); |
39 | | - rotateY(frameCount * 0.01); |
| 54 | + translate(100, -100, 0); |
| 55 | + rotateWithFrameCount(); |
40 | 56 | cylinder(70, 70); |
41 | 57 | pop(); |
42 | 58 |
|
| 59 | + // Cone |
43 | 60 | push(); |
44 | | - translate(-250, 100, 0); |
45 | | - rotateZ(frameCount * 0.01); |
46 | | - rotateX(frameCount * 0.01); |
47 | | - rotateY(frameCount * 0.01); |
| 61 | + translate(275, -100, 0); |
| 62 | + rotateWithFrameCount(); |
48 | 63 | cone(50, 70); |
49 | 64 | pop(); |
50 | 65 |
|
| 66 | + // Torus |
51 | 67 | push(); |
52 | | - translate(-75, 100, 0); |
53 | | - rotateZ(frameCount * 0.01); |
54 | | - rotateX(frameCount * 0.01); |
55 | | - rotateY(frameCount * 0.01); |
| 68 | + translate(-250, 100, 0); |
| 69 | + rotateWithFrameCount(); |
56 | 70 | torus(50, 20); |
57 | 71 | pop(); |
58 | 72 |
|
| 73 | + // Sphere |
59 | 74 | push(); |
60 | | - translate(100, 100, 0); |
61 | | - rotateZ(frameCount * 0.01); |
62 | | - rotateX(frameCount * 0.01); |
63 | | - rotateY(frameCount * 0.01); |
| 75 | + translate(-75, 100, 0); |
| 76 | + rotateWithFrameCount(); |
| 77 | + |
| 78 | + // Show black stroke to help visualize movement |
| 79 | + stroke(0); |
64 | 80 | sphere(50); |
65 | 81 | pop(); |
66 | 82 |
|
| 83 | + // Ellipsoid |
| 84 | + push(); |
| 85 | + translate(100, 100, 0); |
| 86 | + rotateWithFrameCount(); |
| 87 | + ellipsoid(20, 40, 40); |
| 88 | + pop(); |
| 89 | + |
| 90 | + // Astronaut |
67 | 91 | push(); |
68 | 92 | translate(275, 100, 0); |
69 | | - rotateZ(frameCount * 0.01); |
70 | | - rotateX(frameCount * 0.01); |
71 | | - rotateY(frameCount * 0.01); |
72 | | - ellipsoid(30, 40, 40); |
| 93 | + rotateWithFrameCount(); |
| 94 | + |
| 95 | + // Extra rotation to start model in upright position |
| 96 | + rotateZ(180); |
| 97 | + model(astronaut); |
73 | 98 | pop(); |
74 | 99 | } |
| 100 | + |
| 101 | +// Rotate 1 degree per frame along all three axes |
| 102 | +function rotateWithFrameCount() { |
| 103 | + rotateZ(frameCount); |
| 104 | + rotateX(frameCount); |
| 105 | + rotateY(frameCount); |
| 106 | +} |
0 commit comments