You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/static/tutorials/p3d/index.html
+67-38Lines changed: 67 additions & 38 deletions
Original file line number
Diff line number
Diff line change
@@ -64,9 +64,19 @@ <h3>3D Transformations</h3>
64
64
</p>
65
65
66
66
<pre>
67
+
float x,y,z;
68
+
69
+
void setup() {
70
+
size(200,200,P3D);
71
+
x = width/2;
72
+
y = height/2;
73
+
z = 0;
74
+
}
75
+
67
76
void draw() {
68
77
translate(x,y,z);
69
-
rect(0,0,w,h);
78
+
rectMode(CENTER);
79
+
rect(0,0,100,100);
70
80
71
81
z++; // The rectangle moves forward as z increments.
72
82
}
@@ -78,33 +88,39 @@ <h3>3D Transformations</h3>
78
88
79
89
<p><imgsrc="imgs/rotateZ.png"></p>
80
90
<pre>
81
-
translate(x,y,z);
82
-
rotateZ(angle);
91
+
size(200, 200, P3D);
92
+
background(100);
83
93
rectMode(CENTER);
84
-
rect(0,0,w,h);
94
+
fill(51);
95
+
stroke(255);
96
+
97
+
translate(100, 100, 0);
98
+
rotateZ(PI/8);
99
+
rect(0, 0, 100, 100);
100
+
85
101
</pre><br/>
86
102
87
103
<p>We can also rotate around the x and y axes.</p>
88
104
89
105
<p><imgsrc="imgs/rotateX.png"></p>
90
106
<pre>
91
-
rotateX(angle);
107
+
rotateX(PI/8);
92
108
</pre><br/>
93
109
94
110
<p><imgsrc="imgs/rotateY.png"></p>
95
111
<pre>
96
-
rotateY(angle);
112
+
rotateY(PI/8);
97
113
</pre><br/>
98
114
99
115
<p>As well as multiple axes at a time.</p>
100
116
101
117
<p><imgsrc="imgs/rotateXYZ.png"></p>
102
118
<pre>
103
-
translate(x,y,z);
104
-
rotateX(angle1);
105
-
rotateY(angle2);
106
-
rotateZ(angle3);
107
-
rect(0,0,w,h);
119
+
translate(100, 100, 0);
120
+
rotateX(PI/8);
121
+
rotateY(PI/8);
122
+
rotateZ(PI/8);
123
+
rect(0, 0, 100, 100);
108
124
</pre><br/>
109
125
110
126
<h3>3D Shapes</h3>
@@ -119,6 +135,10 @@ <h3>3D Shapes</h3>
119
135
120
136
<p><imgsrc="imgs/primitives3D.png"></p>
121
137
<pre>
138
+
size(640,360,P3D);
139
+
background(0);
140
+
lights();
141
+
122
142
pushMatrix();
123
143
translate(130, height/2, 0);
124
144
rotateY(1.25);
@@ -146,27 +166,33 @@ <h3>3D Shapes</h3>
146
166
147
167
<p><imgsrc="imgs/pyramid3D.png"></p>
148
168
<pre>
149
-
translate(width/2, height/2, 0);
150
-
rotateX(angle1);
151
-
rotateY(angle2);
152
-
scale(100);
153
-
beginShape();
154
-
vertex(-1, -1, -1);
155
-
vertex( 1, -1, -1);
156
-
vertex( 0, 0, 1);
169
+
size(640, 360, P3D);
170
+
background(0);
157
171
158
-
vertex( 1, -1, -1);
159
-
vertex( 1, 1, -1);
160
-
vertex( 0, 0, 1);
172
+
translate(width/2, height/2, 0);
173
+
stroke(255);
174
+
rotateX(PI/2);
175
+
rotateZ(-PI/6);
176
+
noFill();
161
177
162
-
vertex( 1, 1, -1);
163
-
vertex(-1, 1, -1);
164
-
vertex( 0, 0, 1);
178
+
beginShape();
179
+
vertex(-100, -100, -100);
180
+
vertex( 100, -100, -100);
181
+
vertex( 0, 0, 100);
182
+
183
+
vertex( 100, -100, -100);
184
+
vertex( 100, 100, -100);
185
+
vertex( 0, 0, 100);
186
+
187
+
vertex( 100, 100, -100);
188
+
vertex(-100, 100, -100);
189
+
vertex( 0, 0, 100);
190
+
191
+
vertex(-100, 100, -100);
192
+
vertex(-100, -100, -100);
193
+
vertex( 0, 0, 100);
194
+
endShape();
165
195
166
-
vertex(-1, 1, -1);
167
-
vertex(-1, -1, -1);
168
-
vertex( 0, 0, 1);
169
-
endShape();
170
196
</pre><br/>
171
197
172
198
Note above how it's often simpler to specify vertex locations using a standardized unit of measure (i.e. 1 pixel) and relative to a point of origin (0,0,0). The size and position of the shape is then set using matrix transformations: translate(), rotate(), and scale(). For some examples of more sophisticated custom shapes built in 3D, take a look at these examples: <ahref="http://processing.org/learning/topics/rgbcube.html">RGB Cube</a>, <ahref="http://processing.org/learning/topics/vertices.html">Vertices</a>, <ahref="http://processing.org/learning/topics/toroid.html">Toroid</a>, <ahref="http://processing.org/learning/topics/icosahedra.html">Isocahedra</a>.
0 commit comments