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: src/templates/pages/learn/getting-started-in-3d.hbs
+60-17Lines changed: 60 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -75,7 +75,7 @@ slug: learn/
75
75
76
76
<h2>Transformations</h2>
77
77
78
-
<p>p5.js has a few methods, translate(), rotate(), and scale(), that we can use to position and orient objects
78
+
<p>p5.js has a few methods, <aclass="code"href="{{root}}/reference/#/p5/translate">translate()</a>, <aclass="code"href="{{root}}/reference/#/p5/rotate">rotate()</a>, and <aclass="code"href="{{root}}/reference/#/p5/scale">scale()</a>, that we can use to position and orient objects
79
79
within space. Collectively these are known as the <em>transformation</em> of an object. These methods are
80
80
available for
81
81
both 2D and 3D drawing.</p>
@@ -106,6 +106,8 @@ slug: learn/
106
106
href="{{root}}/reference/#/p5/rotateZ">rotateZ(angle)</a> apply an angle to the
107
107
specific axis, but each can be called in combination. </p>
108
108
109
+
{{!-- TODO explain Euler angles --}}
110
+
109
111
<h3>scale()</h3>
110
112
111
113
{{!-- sketch illustrating scaling of geometry --}}
@@ -117,20 +119,61 @@ slug: learn/
117
119
118
120
<h2>Order</h2>
119
121
120
-
<p>One thing that might feel a little different than working in 2d is that our 3d sketch has a camera. The camera
121
-
is a 3d object of its own, with rotation and position that can affect the way that we see the geometry in our
122
-
scene. When we move our object, keep in mind that rotate and translate operate independently from the position
123
-
of your camera. These transformations happen in world space, relative to the world origin.</p>
122
+
<p>Something that can feel unpredictable at first is the order of transformations. Each transformation can affect
123
+
the next one. For example if you call rotate(), followed by translate(), the direction of that translation will
124
+
be affected by the rotation. You can think of it as your entire coordinate system rotating and moving, not just
125
+
the object itself.</p>
126
+
127
+
<p>The order that you use will depend on what you’re trying to do, but a good place to start is to follow the
128
+
order of translate, rotate, and then scale. If you perform these out of order you might find that your
129
+
transformations don’t act the way that you expect them to.</p>
130
+
</p>
131
+
132
+
<p>Another tool we have in p5.js is <aclass="code"href="{{root}}/reference/#/p5/push">push()</a> and <aclass="code"href="{{root}}/reference/#/p5/pop">pop()</a>. Since transformations accumulate and affect each other, you
133
+
might want to control which objects are affected by a certain transformation. push() saves and sets aside our
134
+
current transformations (and certain styling, like stroke() or fill()), which allows you to perform other
135
+
transformations. pop() restores those transformations.</p>
136
+
</p>
137
+
138
+
<p>You might use push() and pop() so that you can move each object individually. If you don’t use push() and
139
+
pop(), you have to keep track of whatever transformations have already taken place, which can get complicated
140
+
and difficult to follow. </p>
141
+
</p>
142
+
143
+
<pre><codeclass="language-javascript">
144
+
// draw a box 100 units to the right
145
+
translate(100,0,0);
146
+
box();
147
+
148
+
// now, to draw another box 100 units to the left
149
+
// since transformations accumulate, we have to
150
+
// subtract 200 to do this
151
+
translate(-200,0,0);
152
+
box();
153
+
</code></pre>
154
+
155
+
<p>Now, the same thing with push() and pop(). Now we can just translate the object where we want it, without
156
+
having to remember where our coordinate system lies.</p>
124
157
125
-
<p>Something that can feel unpredictable at first is the order of transformations. In general, the order that you
126
-
want to perform transformations is scale, rotation, then translation. If you perform these out of order it can
127
-
be a little more difficult to control. Each of these transformations can have an effect on each other, and this
128
-
order makes it much more intuitive. </p>
158
+
<pre><codeclass="language-javascript">
159
+
push(); // detach our coordinate system
160
+
// draw a box 100 units to the right
161
+
translate(100,0,0);
162
+
box();
163
+
pop(); // return to our original coordinate system
164
+
165
+
push(); // detach our coordinate system
166
+
// draw a box 100 units to the left
167
+
translate(-100,0,0);
168
+
box();
169
+
pop(); // return to our original coordinate system
170
+
</code></pre>
171
+
172
+
<p>While this is a more advanced topic, each of these transformations affect what is called the model matrix. The
173
+
transformation matrix is combined with the view matrix and the projection matrix, both of which help simulate
174
+
the view of a camera, and this combination results in our 3D scene! </p>
129
175
130
-
<p>When you call scale, rotate, or translate, each of these calls will affect any objects that are drawn after. If
131
-
you want to move things independently, without having to account for prior transformations, you can use the
132
-
methods push() and pop(). The combination of these two methods allow you to "detach" the current coordinate
133
-
space, which you can then transform, and with pop() you can return to the original coordinate space. </p>
0 commit comments