Skip to content

Commit d2664cc

Browse files
authored
Merge pull request #4 from processing/Geometries
Geometries
2 parents d60e923 + 8889419 commit d2664cc

2 files changed

Lines changed: 65 additions & 33 deletions

File tree

examples/curated/33_Geometries.js

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,106 @@
1-
/*
1+
/**
22
* @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>.
512
*/
613

14+
// Variable to store NASA model
15+
let astronaut;
16+
17+
function preload() {
18+
astronaut = loadModel('assets/astronaut.obj');
19+
}
20+
721
function setup() {
822
createCanvas(710, 400, WEBGL);
923

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+
1030
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.'
1232
);
1333
}
1434

1535
function draw() {
1636
background(250);
1737

18-
normalMaterial();
38+
// Plane
1939
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();
2442
plane(70);
2543
pop();
2644

45+
// Box
2746
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();
3249
box(70, 70, 70);
3350
pop();
3451

52+
// Cylinder
3553
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();
4056
cylinder(70, 70);
4157
pop();
4258

59+
// Cone
4360
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();
4863
cone(50, 70);
4964
pop();
5065

66+
// Torus
5167
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();
5670
torus(50, 20);
5771
pop();
5872

73+
// Sphere
5974
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);
6480
sphere(50);
6581
pop();
6682

83+
// Ellipsoid
84+
push();
85+
translate(100, 100, 0);
86+
rotateWithFrameCount();
87+
ellipsoid(20, 40, 40);
88+
pop();
89+
90+
// Astronaut
6791
push();
6892
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);
7398
pop();
7499
}
100+
101+
// Rotate 1 degree per frame along all three axes
102+
function rotateWithFrameCount() {
103+
rotateZ(frameCount);
104+
rotateX(frameCount);
105+
rotateY(frameCount);
106+
}

examples/curated/33_Geometries.png

65.3 KB
Loading

0 commit comments

Comments
 (0)